1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
use rand::random; use std::ops; #[derive(Clone)] pub struct Matrix { nrows: i16, ncols: i16, data: Vec<Vec<f64>> } /// A Matrix is represented here by 3 fields named: nrows: i16, ncols: i16, data: Vec<Vec<f64>> impl Matrix { /// Returns the number of rows as i16. /// # Examples /// use nn::Matrix; /// /// let matrix: Matrix = Matrix::create_random_matrix(6, 6); /// /// println!("The matrix has {} rows and {} cols", matrix.nrows(), matrix.ncols()); /// #[allow(dead_code)] pub fn nrows(&self) -> i16 { self.nrows } /// Returns the number of cols as i16. /// # Examples /// use nn::Matrix; /// /// let matrix: Matrix = Matrix::create_random_matrix(6, 6); /// /// println!("The matrix has {} rows and {} cols", matrix.nrows(), matrix.ncols()); /// #[allow(dead_code)] pub fn ncols(&self) -> i16 { self.ncols } /// Sets one value in the Vector. /// # Arguments /// /// * `x` - A i16 Integer that holds the row, in which the variable is set. /// * `y` - A i16 Integer that holds the col, in which the variable is set. /// * `v` - A f64 Float that holds the new variable value. /// /// # Examples /// use nn::Matrix; /// /// let matrix: Matrix = Matrix::create_random_matrix(6, 6); /// /// matrix.print(); /// /// matrix.set(0, 0, 5.3); /// /// matrix.print(); /// #[allow(dead_code)] pub fn set(&mut self, x: i16, y: i16, v: f64) { assert!(x >= 0 && x <= (self.data.len() as i16 -1), "{}", format!("Row Index out of bounds! {} but got Index:{}", self.get_shape_string(), x)); assert!(y >= 0 && y <= (self.data[0].len() as i16 -1), "{}", format!("Column Index out of bounds! {} but got Index:{}", self.get_shape_string(), y)); self.data[x as usize][y as usize] = v; } /// Returns one value in the Vector by a given row and col as f64. /// # Arguments /// /// * `x` - A i16 Integer that holds the row, in which the variable is set. /// * `y` - A i16 Integer that holds the col, in which the variable is set. /// /// # Examples /// use nn::Matrix; /// /// let matrix: Matrix = Matrix::create_random_matrix(6, 6); /// /// println!("Value at row 0 and col 0 has the value {}", matrix.get(0, 0)); /// #[allow(dead_code)] pub fn get(&self, x: i16, y: i16) -> f64 { assert!(x >= 0 && x <= (self.data.len() as i16 -1), "{}", format!("Row Index out of bounds! {} but got Index:{}", self.get_shape_string(), x)); assert!(y >= 0 && y <= (self.data[0].len() as i16 -1), "{}", format!("Column Index out of bounds! {} but got Index:{}", self.get_shape_string(), y)); return self.data[x as usize][y as usize]; } /// Returns two matrices added together one by one as a Matrix. /// # Arguments /// /// * `a` - A &Matrix. /// * `b` - A &Matrix. /// /// # Examples /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrixb: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrix_sum: Matrix = Matrix::add(&matrixa, &matrixb); /// #[allow(dead_code)] pub fn add(a: &Matrix, b: &Matrix) -> Matrix { assert!(a.ncols() == b.nrows(), "{}", format!("Dimensional mismatch! A: [{}, {}] | B: [{}, {}]", a.nrows(), a.ncols(), b.nrows(), b.ncols())); let mut data = Vec::<Vec<f64>>::new(); for i in 0..(a.nrows) { let mut row = Vec::<f64>::new(); for j in 0..(a.ncols) { row.push(a.data[i as usize][j as usize] + b.data[i as usize][j as usize]) } data.push(row); } Matrix { nrows: a.nrows, ncols: a.ncols, data: data } } /// Returns two matrices subtracted one by one as a Matrix. /// # Arguments /// /// * `a` - A &Matrix. /// * `b` - A &Matrix. /// /// # Examples /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrixb: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrix_sub: Matrix = Matrix::sub(&matrixa, &matrixb); /// #[allow(dead_code)] pub fn sub(a: &Matrix, b: &Matrix) -> Matrix { assert!(a.ncols() == b.nrows(), "{}", format!("Dimensional mismatch! A: [{}, {}] | B: [{}, {}]", a.nrows(), a.ncols(), b.nrows(), b.ncols())); let mut data = Vec::<Vec<f64>>::new(); for i in 0..(a.nrows) { let mut row = Vec::<f64>::new(); for j in 0..(a.ncols) { row.push(a.data[i as usize][j as usize] - b.data[i as usize][j as usize]) } data.push(row); } Matrix { nrows: a.nrows, ncols: a.ncols, data: data } } /// Returns two matrices multiplied one by one as a Matrix. /// # Arguments /// /// * `a` - A &Matrix. /// * `b` - A &Matrix. /// /// # Examples /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrixb: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrix_mul: Matrix = Matrix::mul(&matrixa, &matrixb); /// #[allow(dead_code)] pub fn mul(a: &Matrix, b: &Matrix) -> Matrix { assert!(a.ncols() == b.nrows(), "{}", format!("Dimensional mismatch! A: [{}, {}] | B: [{}, {}]", a.nrows(), a.ncols(), b.nrows(), b.ncols())); let mut data = Vec::<Vec<f64>>::new(); for i in 0..(a.nrows) { let mut row = Vec::<f64>::new(); for j in 0..(a.ncols) { row.push(a.data[i as usize][j as usize] * b.data[i as usize][j as usize]) } data.push(row); } Matrix { nrows: a.nrows, ncols: a.ncols, data: data } } /// Returns two matrices divided one by one as a Matrix. /// # Arguments /// /// * `a` - A &Matrix. /// * `b` - A &Matrix. /// /// # Examples /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrixb: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrix_div: Matrix = Matrix::div(&matrixa, &matrixb); /// #[allow(dead_code)] pub fn div(a: &Matrix, b: &Matrix) -> Matrix { assert!(a.ncols() == b.nrows(), "{}", format!("Dimensional mismatch! A: [{}, {}] | B: [{}, {}]", a.nrows(), a.ncols(), b.nrows(), b.ncols())); let mut data = Vec::<Vec<f64>>::new(); for i in 0..(a.nrows) { let mut row = Vec::<f64>::new(); for j in 0..(a.ncols) { row.push(a.data[i as usize][j as usize] / b.data[i as usize][j as usize]) } data.push(row); } Matrix { nrows: a.nrows, ncols: a.ncols, data: data } } /// Returns the dot product of two matrices as a Matrix. /// # Arguments /// /// * `a` - A &Matrix. /// * `b` - A &Matrix. /// /// # Examples /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(1, 2); /// /// let matrixb: Matrix = Matrix::create_random_matrix(2, 1); /// /// let matrix_dot: Matrix = Matrix::dot(&matrixa, &matrixb); /// #[allow(dead_code)] pub fn dot(a: &Matrix, b: &Matrix) -> Matrix { assert!(a.ncols() == b.nrows(), "{}", format!("Dimensional mismatch! A: [{}, {}] | B: [{}, {}]", a.nrows(), a.ncols(), b.nrows(), b.ncols())); let mut mat = Matrix::create_matrix(a.nrows(), b.ncols()); for i in 0..(mat.nrows()) { for k in 0..(mat.ncols()) { for j in 0..(a.ncols()) { mat.set(i, k, mat.get(i, k) + a.get(i, j) * b.get(j, k)) } } } mat } /// Returns the transposed Matrix as a Matrix. /// # Arguments /// /// * `a` - A &Matrix. /// /// # Examples /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrix_t: Matrix = Matrix::transpose(&matrixa); /// #[allow(dead_code)] pub fn transpose(a: &Matrix) -> Matrix { let mut data = Matrix::create_matrix(a.ncols, a.nrows); for i in 0..(a.nrows) { for j in 0..(a.ncols) { data.set(j, i, a.get(i, j)); } } data } /// Applies a given function fn(f64) -> f64 to every value in the Matrix and returns the new Matrix as a Matrix. /// # Arguments /// /// * `a` - A &Matrix. /// * `f` - A fn(f64) -> f64. /// /// # Examples /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// let closure = |i: f64| -> f64 { i+10.0 }; /// let matrixb = matrixa.map(closure); #[allow(dead_code)] pub fn map(&self, f: fn(f64) -> f64) -> Matrix { let mut data = Matrix::create_matrix(self.nrows, self.ncols); for i in 0..(self.nrows) { for j in 0..(self.ncols) { data.set(i, j, f(self.data[i as usize][j as usize])); } } data } /// Adds a single f64 float to every value in a given Matrix and returns it as a Matrix. /// # Arguments /// /// * `a` - A &Matrix. /// * `v` - A f64. /// /// # Examples /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let scalar_add: Matrix = Matrix::scalar_add(&matrixa, 10.0); /// #[allow(dead_code)] pub fn scalar_add(a: &Matrix, v: f64) -> Matrix { let mut data = Matrix::create_matrix(a.nrows, a.ncols); for i in 0..(a.nrows) { for j in 0..(a.ncols) { data.set(i, j, a.get(i, j) + v); } } data } /// Subtracts a single f64 float from every value in a given Matrix and returns it as a Matrix. /// # Arguments /// /// * `a` - A &Matrix. /// * `v` - A f64. /// /// # Examples /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let scalar_sub: Matrix = Matrix::scalar_sub(&matrixa, 10.0); /// #[allow(dead_code)] pub fn scalar_sub(a: &Matrix, v: f64) -> Matrix { let mut data = Matrix::create_matrix(a.nrows, a.ncols); for i in 0..(a.nrows) { for j in 0..(a.ncols) { data.set(i, j, a.get(i, j) - v); } } data } /// Subtracts every value of the Matrix from a given float f64 and returns it as a Matrix. /// # Arguments /// /// * `a` - A &Matrix. /// * `v` - A f64. /// /// # Examples /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let scalar_sub: Matrix = Matrix::scalar_sub_first(&matrixa, 10.0); /// #[allow(dead_code)] pub fn scalar_sub_first(a: &Matrix, v: f64) -> Matrix { let mut data = Matrix::create_matrix(a.nrows, a.ncols); for i in 0..(a.nrows) { for j in 0..(a.ncols) { data.set(i, j, v - a.get(i, j)); } } data } /// Multiplies every value of the Matrix by a given float f64 and returns it as a Matrix. /// # Arguments /// /// * `a` - A &Matrix. /// * `v` - A f64. /// /// # Examples /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let scalar_sub: Matrix = Matrix::scalar_mult(&matrixa, 10.0); /// #[allow(dead_code)] pub fn scalar_mult(a: &Matrix, v: f64) -> Matrix { let mut data = Matrix::create_matrix(a.nrows, a.ncols); for i in 0..(a.nrows) { for j in 0..(a.ncols) { data.set(i, j, a.get(i, j) * v); } } data } /// Divides every value of the Matrix by a given float f64 and returns it as a Matrix. /// # Arguments /// /// * `a` - A &Matrix. /// * `v` - A f64. /// /// # Examples /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let scalar_sub: Matrix = Matrix::scalar_div(&matrixa, 10.0); /// #[allow(dead_code)] pub fn scalar_div(a: &Matrix, v: f64) -> Matrix { let mut data = Matrix::create_matrix(a.nrows, a.ncols); for i in 0..(a.nrows) { for j in 0..(a.ncols) { data.set(i, j, a.get(i, j) / v); } } data } /// Returns a Matrix where every value has the opposite sign as a Matrix. /// # Arguments /// /// * `a` - A &Matrix. /// /// # Examples /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrixa_inverse: Matrix = Matrix::inverse(&matrixa); /// #[allow(dead_code)] pub fn inverse(a: &Matrix) -> Matrix { let mut data = Matrix::create_matrix(a.nrows, a.ncols); for i in 0..(a.nrows) { for j in 0..(a.ncols) { data.set(i, j, -a.get(i, j)); } } data } /// Sums every row together and returns a Matrix with the same number of rows and 1 column. /// # Arguments /// /// * `a` - A &Matrix. /// /// # Examples /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrixa_rows_sum: = Matrix::sum_rows(&matrixa); /// #[allow(dead_code)] pub fn sum_rows(a: &Matrix) -> Matrix { let mut data = Matrix::create_matrix(a.nrows(), 1); for i in 0..(a.nrows()) { let mut sum: f64 = 0.0; for j in 0..(a.ncols()) { sum += a.get(i, j); } data.set(i, 0, sum); } return data; } /// Every value in the Matrix is raised to a given power and returned as a Matrix. /// # Arguments /// /// * `a` - A &Matrix. /// * `v` - A f64. /// /// # Examples /// /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrixa_pow_2 = Matrix::pow(&matrixa, 2.0); /// #[allow(dead_code)] pub fn pow(a: &Matrix, v: f64) -> Matrix { let mut data = Matrix::create_matrix(a.nrows, a.ncols); for i in 0..(a.nrows) { for j in 0..(a.ncols) { data.set(i, j, a.get(i, j).powf(v)); } } data } /// Filters every value in the Matrix by a given function fn(f64) -> bool. If the output of the function is true, the value stays the same. If the output of the function is false, the value is replaced by a given float f64. It returns a Matrix. /// # Arguments /// /// * `a` - A &Matrix. /// * `f` - A fn(f64) -> bool. /// * `v` - A f64. /// /// # Examples /// /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let closure = |v: f64| -> bool { v < 10.0 }; /// /// let matrixb = Matrix::filter_by_function(&matrixa, closure, 0.0); /// #[allow(dead_code)] pub fn filter_by_function(a: &Matrix, f: fn(f64) -> bool, v: f64) -> Matrix { let mut data = Matrix::create_matrix(a.nrows, a.ncols); for i in 0..(a.nrows) { for j in 0..(a.ncols) { let temp = a.get(i, j); match f(temp) { false => data.set(i, j, v), true => data.set(i, j, temp) } } } data } #[allow(dead_code)] fn get_digits(&self, i: &f64) -> i32 { i.to_string().chars().count() as i32 } /// Returns the shape of a Matrix as a string formatted with Shape:({}, {}) as a String. /// /// # Examples /// /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// println!("Matrix has {}", matrixa.get_shape_string()); /// #[allow(dead_code)] pub fn get_shape_string(&self) -> String { format!("Shape:({}, {})", self.nrows(), self.ncols()) } #[allow(dead_code)] fn get_most_digits(&self) -> i32 { let mut cnt: i32 = 0; for i in 0..(self.data.len()) { for j in 0..(self.data[i].len()) { let digits = self.get_digits(&self.data[i][j]); if digits > cnt { cnt = digits; } } } cnt } #[allow(dead_code)] fn to_append(&self, i: &f64, cnt: i32) -> String { let mut appendix = String::new(); for _ in 0..(cnt - self.get_digits(i)) { appendix = format!("{}{}", appendix, " "); } appendix } #[allow(dead_code)] fn before_print(&self, cnt: i32) -> String { let mut appendix = String::new(); for _ in 0..(cnt * (self.ncols() as i32) + 3 + (self.ncols() as i32) ) { appendix = format!("{}{}", appendix, "-"); } appendix } /// Pretty prints the Matrix to the console using print!(). /// /// # Examples /// /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// matrixa.print(); /// #[allow(dead_code)] pub fn print(&self) { let most_digits: i32 = self.get_most_digits(); println!("\t{}", self.before_print(most_digits)); for i in 0..(self.data.len()) { for j in 0..(self.data[i].len()) { if j == 0 && j == self.data[i].len() - 1 { let current = self.get(i as i16, j as i16); print!("\t| {}{} |\n", current, self.to_append(¤t, most_digits)); } else if j == 0 { let current = self.get(i as i16, j as i16); print!("\t| {}{}", current, self.to_append(¤t, most_digits)); } else if j == self.data[i].len() - 1 { let current = self.get(i as i16, j as i16); print!(" {}{} |\n", current, self.to_append(¤t, most_digits)); } else { let current = self.get(i as i16, j as i16); print!(" {}{}", current, self.to_append(¤t, most_digits)); } } } println!("\t{}", self.before_print(most_digits)); println!("\t{}", self.get_shape_string()); } /// Creates a Matrix by a given number of rows as i16 and cols as i16 where every value is zero and returns it as a Matrix. /// # Arguments /// /// * `nrows` - A i16. /// * `ncols` - A i16. /// /// # Examples /// /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_matrix(6, 6); /// #[allow(dead_code)] pub fn create_matrix(nrows: i16, ncols: i16) -> Matrix { let mut data = Vec::<Vec<f64>>::new(); for _ in 0..nrows { let mut row = Vec::<f64>::new(); for _ in 0..ncols { row.push(0.0); } data.push(row); } Matrix { nrows: nrows, ncols: ncols, data: data } } /// Creates a Matrix by a given number of rows as i16 and cols as i16 where every value is random between 0 and 1 and returns it as a Matrix. /// # Arguments /// /// * `nrows` - A i16. /// * `ncols` - A i16. /// /// # Examples /// /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_matrix(6, 6); /// #[allow(dead_code)] pub fn create_random_matrix(nrows: i16, ncols: i16) -> Matrix { let mut data = Vec::<Vec<f64>>::new(); for _ in 0..nrows { let mut row = Vec::<f64>::new(); for _ in 0..ncols { row.push(random()); } data.push(row); } Matrix { nrows: nrows, ncols: ncols, data: data } } /// Creates a Matrix by a given number of rows as i16 and cols as i16 where every value is a given float and returns it as a Matrix. /// # Arguments /// /// * `nrows` - A i16. /// * `ncols` - A i16. /// * `v` - A f64. /// /// # Examples /// /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_matrix_by_float(6, 6, 10.0); /// #[allow(dead_code)] pub fn create_matrix_by_float(nrows: i16, ncols: i16, v: f64) -> Matrix { let mut data = Vec::<Vec<f64>>::new(); for _ in 0..nrows { let mut row = Vec::<f64>::new(); for _ in 0..ncols { row.push(v); } data.push(row); } Matrix { nrows: nrows, ncols: ncols, data: data } } /// Creates a Matrix by nested float array &[&[f64]] and returns it as a Matrix. /// # Arguments /// /// * `v` - A &[&[f64]]. /// /// # Examples /// /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_matrix_from_nested_float_array(&[ /// &[10.0, 1.0, 1.2, 3.4], /// &[2.3, 4.3, 2.1, 5.4] /// ]); /// /// matrixa.print(); /// #[allow(dead_code)] pub fn create_matrix_from_nested_float_array(v: &[&[f64]]) -> Matrix { assert!(v.len() != 0, "Please provide filled Arrays"); let mut mat: Matrix = Matrix::create_matrix(v.len() as i16, v[0].len() as i16); for i in 0..(v.len()) { for j in 0..(v[i].len()) { mat.set(i as i16, j as i16, v[i][j]); } } mat } } /// Creates a Matrix by nested float array &[&[f64]] and returns it as a Matrix. /// /// # Examples /// /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(2, 2); /// /// println!("The value at Row 0 and Col 0 has the value {}", matrixa[0..0]); /// impl ops::Index<ops::Range<usize>> for Matrix { type Output = f64; fn index(&self, l: ops::Range<usize>) -> &f64 { let i = &l.start; let j = &l.end; &self.data[ *i as usize][ *j as usize] } } /// Returns a Matrix where every value has the opposite sign as a Matrix. /// /// # Examples /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrixa_inverse: Matrix = Matrix::inverse(&matrixa); /// impl ops::Not for Matrix { type Output = Matrix; fn not(self) -> Self::Output { Matrix::inverse(&self) } } /// Returns two matrices added together one by one as a Matrix. /// /// # Examples /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrixb: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrix_sum: Matrix = matrixa.clone() + matrixb.clone(); /// impl ops::Add<Matrix> for Matrix { type Output = Matrix; fn add(self, _rhs: Matrix) -> Matrix { Matrix::add(&self, &_rhs) } } /// Returns two matrices multiplied one by one as a Matrix. /// /// # Examples /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrixb: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrix_mul: Matrix = matrixa.clone() * matrixb.clone(); /// impl ops::Mul<Matrix> for Matrix { type Output = Matrix; fn mul(self, _rhs: Matrix) -> Matrix { Matrix::mul(&self, &_rhs) } } /// Returns two matrices subtracted one by one as a Matrix. /// /// # Examples /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrixb: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrix_sub: Matrix = matrixa.clone() - matrixb.clone(); /// impl ops::Sub<Matrix> for Matrix { type Output = Matrix; fn sub(self, _rhs: Matrix) -> Matrix { Matrix::sub(&self, &_rhs) } } /// Returns two matrices divided one by one as a Matrix. /// /// # Examples /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrixb: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrix_div: Matrix = matrixa.clone() / matrixb.clone(); /// impl ops::Div<Matrix> for Matrix { type Output = Matrix; fn div(self, _rhs: Matrix) -> Matrix { Matrix::div(&self, &_rhs) } } /// Returns the dot product of two matrices as a Matrix. /// /// # Examples /// use nn::Matrix; /// /// let matrixa: Matrix = Matrix::create_random_matrix(1, 2); /// /// let matrixb: Matrix = Matrix::create_random_matrix(2, 1); /// /// let matrix_dot: Matrix = matrixa.clone() | matrixb.clone(); /// impl ops::BitOr<Matrix> for Matrix { type Output = Matrix; fn bitor(self, rhs: Matrix) -> Matrix { Matrix::dot(&self, &rhs) } } /// Adds a Matrix to a given Matrix one by one. /// /// # Examples /// use nn::Matrix; /// /// let mut matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrixb: Matrix = Matrix::create_random_matrix(6, 6); /// /// matrixa += matrixb; /// impl ops::AddAssign<Matrix> for Matrix { fn add_assign(&mut self, _rhs: Matrix) { *self = Matrix::add(self, &_rhs); } } /// Multiplies a Matrix with a given Matrix one by one. /// /// # Examples /// use nn::Matrix; /// /// let mut matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrixb: Matrix = Matrix::create_random_matrix(6, 6); /// /// matrixa *= matrixb; /// impl ops::MulAssign<Matrix> for Matrix { fn mul_assign(&mut self, _rhs: Matrix) { *self = Matrix::mul(self, &_rhs); } } /// Divides a Matrix by a given Matrix one by one. /// /// # Examples /// use nn::Matrix; /// /// let mut matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrixb: Matrix = Matrix::create_random_matrix(6, 6); /// /// matrixa /= matrixb; /// impl ops::DivAssign<Matrix> for Matrix { fn div_assign(&mut self, _rhs: Matrix) { *self = Matrix::div(self, &_rhs); } } /// Subtracts a Matrix with a given Matrix one by one. /// /// # Examples /// use nn::Matrix; /// /// let mut matrixa: Matrix = Matrix::create_random_matrix(6, 6); /// /// let matrixb: Matrix = Matrix::create_random_matrix(6, 6); /// /// matrixa -= matrixb; /// impl ops::SubAssign<Matrix> for Matrix { fn sub_assign(&mut self, _rhs: Matrix) { *self = Matrix::sub(self, &_rhs); } }