Compare commits

...

3 Commits

Author SHA1 Message Date
Jon Janzen
9c20d13833 allow rectangular matrixes 2021-03-31 15:38:00 -06:00
Jon Janzen
82360600fe Fixed transpose by moving to an iterative method 2021-03-31 15:30:08 -06:00
Jon Janzen
588acf36a3 added determinant 2021-03-31 15:19:48 -06:00

View File

@@ -6,24 +6,24 @@ use structs::Tuple;
use std::ops::Index;
#[derive(Debug)]
pub struct Matrix<const COUNT: usize> {
matrix: [[f32; COUNT]; COUNT],
pub struct Matrix<const H: usize, const W: usize> {
matrix: [[f32; W]; H],
}
impl<const COUNT: usize> Matrix<COUNT> {
pub fn new() -> Matrix<COUNT> {
impl<const H: usize, const W: usize> Matrix<H, W> {
pub fn new() -> Matrix<H, W> {
Matrix {
matrix: [[0f32; COUNT]; COUNT],
matrix: [[0f32; W]; H],
}
}
pub fn from_array(matrix: [[f32; COUNT]; COUNT]) -> Matrix<COUNT> {
pub fn from_array(matrix: [[f32; W]; H]) -> Matrix<H, W> {
Matrix {
matrix: matrix,
}
}
pub fn identity() -> Matrix<COUNT> {
pub fn identity() -> Matrix<H, W> {
let mut m = Matrix::new();
for i in 0..m.matrix.len() {
m.matrix[i][i] = 1.0;
@@ -32,24 +32,28 @@ impl<const COUNT: usize> Matrix<COUNT> {
}
pub fn transpose(&mut self) {
let tmp = [
[self.matrix[0][0], self.matrix[1][0], self.matrix[2][0], self.matrix[3][0], ],
[self.matrix[0][1], self.matrix[1][1], self.matrix[2][1], self.matrix[3][1], ],
[self.matrix[0][2], self.matrix[1][2], self.matrix[2][2], self.matrix[3][2], ],
[self.matrix[0][3], self.matrix[1][3], self.matrix[2][3], self.matrix[3][3], ],
];
self.matrix = tmp;
for i in 0..self.matrix.len() {
for j in i..self.matrix[0].len() {
let v = self.matrix[i][j];
self.matrix[i][j] = self.matrix[j][i];
self.matrix[j][i] = v;
}
}
}
pub fn determinant(&self) -> f32 {
self.matrix[0][0] * self.matrix[1][1] - self.matrix[0][1] * self.matrix[1][0]
}
}
impl<const COUNT: usize> Index<usize> for Matrix<COUNT> {
type Output = [f32; COUNT];
impl<const H: usize, const W: usize> Index<usize> for Matrix<H, W> {
type Output = [f32; W];
fn index(&self, index: usize) -> &Self::Output {
&self.matrix[index]
}
}
impl<const COUNT: usize> PartialEq for Matrix<COUNT> {
impl<const H: usize, const W: usize> PartialEq for Matrix<H, W> {
fn eq(&self, _rhs: &Self) -> bool {
if self.matrix.len() != _rhs.matrix.len() {
return false;
@@ -69,10 +73,10 @@ impl<const COUNT: usize> PartialEq for Matrix<COUNT> {
}
}
impl<const COUNT: usize> Matrix<COUNT> {
fn calc_val_for_mul(&self, row: usize, rhs: &Matrix<COUNT>, col: usize) -> f32 {
impl<const H: usize, const W: usize> Matrix<H, W> {
fn calc_val_for_mul(&self, row: usize, rhs: &Matrix<H, W>, col: usize) -> f32 {
let mut sum = 0.0;
for i in 0..self.matrix.len() {
for i in 0..W {
sum += self.matrix[row][i] * rhs.matrix[i][col];
}
sum
@@ -86,13 +90,13 @@ impl<const COUNT: usize> Matrix<COUNT> {
}
}
impl<const COUNT: usize> std::ops::Mul<Matrix<COUNT>> for Matrix<COUNT> {
type Output = Matrix<COUNT>;
impl<const H: usize, const W: usize> std::ops::Mul<Matrix<H, W>> for Matrix<H, W> {
type Output = Matrix<H, W>;
fn mul(self, _rhs: Matrix<COUNT>) -> Matrix<COUNT> {
let mut result = [[0f32; COUNT]; COUNT];
for row in 0..COUNT {
for col in 0..COUNT {
fn mul(self, _rhs: Matrix<H, W>) -> Matrix<H, W> {
let mut result = [[0f32; W]; H];
for row in 0..H {
for col in 0..W {
result[row][col] = self.calc_val_for_mul(row, &_rhs, col);
}
}
@@ -101,7 +105,7 @@ impl<const COUNT: usize> std::ops::Mul<Matrix<COUNT>> for Matrix<COUNT> {
}
impl<const COUNT: usize> std::ops::Mul<Tuple> for Matrix<COUNT> {
impl<const H: usize, const W: usize> std::ops::Mul<Tuple> for Matrix<H, W> {
type Output = Tuple;
fn mul(self, _rhs: Tuple) -> Tuple {
@@ -292,7 +296,7 @@ mod tests {
let t = Tuple::new(1.0, 2.0, 3.0, 4.0);
let expected = Tuple::new(1.0, 2.0, 3.0, 4.0);
assert_eq!(Matrix::<4>::identity() * t, expected);
assert_eq!(Matrix::<4, 4>::identity() * t, expected);
}
#[test]
@@ -318,6 +322,16 @@ mod tests {
fn transpose_identity() {
let mut m = Matrix::identity();
m.transpose();
assert_eq!(m, Matrix::<4>::identity());
assert_eq!(m, Matrix::<4, 4>::identity());
}
#[test]
fn determinant_2x2() {
let m = Matrix::from_array([
[1.0, 5.0],
[-3.0, 2.0],
]);
assert_eq!(17.0, m.determinant());
}
}