Created
April 29, 2018 19:54
-
-
Save Lucas-Developer/27cecd506bc374aaebe0f09f77de6984 to your computer and use it in GitHub Desktop.
Multiply two matrices together. They can be of any dimensions, so long as the number of columns of the first matrix is equal to the number of rows of the second matrix.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Matrix { | |
| dat: [[f32; 3]; 3] | |
| } | |
| impl Matrix { | |
| pub fn mult_m(a: Matrix, b: Matrix) -> Matrix | |
| { | |
| let mut out = Matrix { | |
| dat: [[0., 0., 0.], | |
| [0., 0., 0.], | |
| [0., 0., 0.] | |
| ] | |
| }; | |
| for i in 0..3{ | |
| for j in 0..3 { | |
| for k in 0..3 { | |
| out.dat[i][j] += a.dat[i][k] * b.dat[k][j]; | |
| } | |
| } | |
| } | |
| out | |
| } | |
| pub fn print(self) | |
| { | |
| for i in 0..3 { | |
| for j in 0..3 { | |
| print!("{} ", self.dat[i][j]); | |
| } | |
| print!("\n"); | |
| } | |
| } | |
| } | |
| fn main() | |
| { | |
| let a = Matrix { | |
| dat: [[1., 2., 3.], | |
| [4., 5., 6.], | |
| [7., 8., 9.] | |
| ] | |
| }; | |
| let b = Matrix { | |
| dat: [[1., 0., 0.], | |
| [0., 1., 0.], | |
| [0., 0., 1.]] | |
| }; | |
| let c = Matrix::mult_m(a, b); | |
| c.print(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment