diff --git a/README.md b/README.md index 9aa08f1..f17c511 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Lmath is generic linear algebra library for Rust. There is still much to do, uni ## Todo: -- Matrix inversion +- ~~Matrix inversion~~ - ~~Matrix rotation~~ - ~~Angle types (degrees/radians)~~ - N x M matrices @@ -23,4 +23,4 @@ Dependant on rust/master: - Make use of static functions for constants and constructors -~B☼ \ No newline at end of file +~B☼ diff --git a/src/matrix.rs b/src/matrix.rs index df533ca..716d36f 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -49,8 +49,6 @@ pub trait Matrix { pure fn col(i: uint) -> ColVec; pure fn row(i: uint) -> RowVec; - - pure fn det() -> T; } pub trait NumericMatrix { @@ -63,6 +61,8 @@ pub trait SquareMatrix { pure fn sub_m(other: &self) -> self; pure fn mul_m(other: &self) -> self; + pure fn det() -> T; + pure fn invert() -> Option; pure fn transpose() -> self; @@ -134,7 +134,7 @@ pub mod Mat2 { } } -pub impl Mat2: Matrix, Vec2> { +pub impl Mat2: Matrix, Vec2> { #[inline(always)] pure fn rows() -> uint { 2 } @@ -155,10 +155,6 @@ pub impl Mat2: Matrix, Vec2> { Vec2::new(self[0][i], self[1][i]) } - - pure fn det() -> T { - self[0][0]*self[1][1] - self[1][0]*self[0][1] - } } pub impl Mat2: NumericMatrix> { @@ -194,6 +190,10 @@ pub impl Mat2: SquareMatrix { self.row(0).dot(&other.col(1)), self.row(1).dot(&other.col(1))) } + pure fn det() -> T { + self[0][0]*self[1][1] - self[1][0]*self[0][1] + } + #[inline(always)] pure fn invert() -> Option> { let _0 = cast(0); @@ -363,7 +363,7 @@ pub mod Mat3 { } } -pub impl Mat3: Matrix, Vec3> { +pub impl Mat3: Matrix, Vec3> { #[inline(always)] pure fn rows() -> uint { 3 } @@ -385,10 +385,6 @@ pub impl Mat3: Matrix, Vec3> { self[1][i], self[2][i]) } - - pure fn det() -> T { - self.col(0).dot(&self.col(1).cross(&self.col(2))) - } } pub impl Mat3: NumericMatrix> { @@ -429,6 +425,10 @@ pub impl Mat3: SquareMatrix { self.row(0).dot(&other.col(2)), self.row(1).dot(&other.col(2)), self.row(2).dot(&other.col(2))) } + pure fn det() -> T { + self.col(0).dot(&self.col(1).cross(&self.col(2))) + } + // #[inline(always)] pure fn invert() -> Option> { let d = self.det(); @@ -677,7 +677,7 @@ pub mod Mat4 { } } -pub impl Mat4: Matrix, Vec4> { +pub impl Mat4: Matrix, Vec4> { #[inline(always)] pure fn rows() -> uint { 4 } @@ -701,20 +701,6 @@ pub impl Mat4: Matrix, Vec4> { self[3][i]) } - pure fn det() -> T { - self[0][0]*Mat3::new(self[1][1], self[2][1], self[3][1], - self[1][2], self[2][2], self[3][2], - self[1][3], self[2][3], self[3][3]).det() - - self[1][0]*Mat3::new(self[0][1], self[2][1], self[3][1], - self[0][2], self[2][2], self[3][2], - self[0][3], self[2][3], self[3][3]).det() + - self[2][0]*Mat3::new(self[0][1], self[1][1], self[3][1], - self[0][2], self[1][2], self[3][2], - self[0][3], self[1][3], self[3][3]).det() - - self[3][0]*Mat3::new(self[0][1], self[1][1], self[2][1], - self[0][2], self[1][2], self[2][2], - self[0][3], self[1][3], self[2][3]).det() - } } pub impl Mat4: NumericMatrix> { @@ -763,6 +749,21 @@ pub impl Mat4: SquareMatrix { self.row(0).dot(&other.col(3)), self.row(1).dot(&other.col(3)), self.row(2).dot(&other.col(3)), self.row(3).dot(&other.col(3))) } + pure fn det() -> T { + self[0][0]*Mat3::new(self[1][1], self[2][1], self[3][1], + self[1][2], self[2][2], self[3][2], + self[1][3], self[2][3], self[3][3]).det() - + self[1][0]*Mat3::new(self[0][1], self[2][1], self[3][1], + self[0][2], self[2][2], self[3][2], + self[0][3], self[2][3], self[3][3]).det() + + self[2][0]*Mat3::new(self[0][1], self[1][1], self[3][1], + self[0][2], self[1][2], self[3][2], + self[0][3], self[1][3], self[3][3]).det() - + self[3][0]*Mat3::new(self[0][1], self[1][1], self[2][1], + self[0][2], self[1][2], self[2][2], + self[0][3], self[1][3], self[2][3]).det() + } + pure fn invert() -> Option> { let d = self.det(); let _0 = cast(0); diff --git a/src/test/test_matrix.rs b/src/test/test_matrix.rs index 0ab73ac..826995f 100644 --- a/src/test/test_matrix.rs +++ b/src/test/test_matrix.rs @@ -50,7 +50,6 @@ fn test_Mat2() { assert a.transpose() == Mat2::new(1f, 2f, 3f, 4f); - io::println(#fmt("%?", option::unwrap(a.invert()))); assert option::unwrap(a.invert()) == Mat2::new(-2f, 1.5f, 1f, -0.5f);