Skip to content

Instantly share code, notes, and snippets.

@Lucas-Developer
Created April 29, 2018 19:54
Show Gist options
  • Select an option

  • Save Lucas-Developer/27cecd506bc374aaebe0f09f77de6984 to your computer and use it in GitHub Desktop.

Select an option

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.
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