diff --git a/src/dim1/mat1.rs b/src/dim1/mat1.rs new file mode 100644 index 00000000..bf5584fc --- /dev/null +++ b/src/dim1/mat1.rs @@ -0,0 +1,94 @@ +use core::num::{One, Zero}; +use traits::dim::Dim; +use traits::inv::Inv; +use traits::transpose::Transpose; +use traits::workarounds::rlmul::{RMul, LMul}; +use dim1::vec1::Vec1; + +#[deriving(Eq)] +pub struct Mat1 +{ m11: T } + +pub fn Mat1(m11: T) -> Mat1 +{ + Mat1 + { m11: m11 } +} + +impl Dim for Mat1 +{ + fn dim() -> uint + { 1 } +} + +impl One for Mat1 +{ + fn one() -> Mat1 + { return Mat1(One::one()) } +} + +impl Zero for Mat1 +{ + fn zero() -> Mat1 + { Mat1(Zero::zero()) } + + fn is_zero(&self) -> bool + { self.m11.is_zero() } +} + +impl + Add> Mul, Mat1> for Mat1 +{ + fn mul(&self, other: &Mat1) -> Mat1 + { Mat1 (self.m11 * other.m11) } +} + +impl + Mul> RMul> for Mat1 +{ + fn rmul(&self, other: &Vec1) -> Vec1 + { Vec1(self.m11 * other.x) } +} + +impl + Mul> LMul> for Mat1 +{ + fn lmul(&self, other: &Vec1) -> Vec1 + { Vec1(self.m11 * other.x) } +} + +impl + Quot + Sub + Neg + Zero + One> +Inv for Mat1 +{ + fn inverse(&self) -> Mat1 + { + let mut res : Mat1 = *self; + + res.invert(); + + res + } + + fn invert(&mut self) + { + assert!(!self.m11.is_zero()); + + self.m11 = One::one::() / self.m11 + } +} + +impl Transpose for Mat1 +{ + fn transposed(&self) -> Mat1 + { *self } + + fn transpose(&mut self) + { } +} + +impl ToStr for Mat1 +{ + fn to_str(&self) -> ~str + { + ~"Mat1 {" + + " m11: " + self.m11.to_str() + + " }" + } +} diff --git a/src/dim1/vec1.rs b/src/dim1/vec1.rs new file mode 100644 index 00000000..17f5b4e2 --- /dev/null +++ b/src/dim1/vec1.rs @@ -0,0 +1,64 @@ +use core::num::{Zero, Algebraic}; +use traits::dot::Dot; +use traits::dim::Dim; + +#[deriving(Eq)] +pub struct Vec1 +{ x : T } + +pub fn Vec1(x: T) -> Vec1 +{ Vec1 {x: x} } + +impl Dim for Vec1 +{ + fn dim() -> uint + { 1 } +} + +impl> Add, Vec1> for Vec1 +{ + fn add(&self, other: &Vec1) -> Vec1 + { Vec1(self.x + other.x) } +} + +impl> Sub, Vec1> for Vec1 +{ + fn sub(&self, other: &Vec1) -> Vec1 + { Vec1(self.x - other.x) } +} + +impl + Add + Algebraic> Dot for Vec1 +{ + fn dot(&self, other : &Vec1) -> T + { self.x * other.x } + + fn sqnorm(&self) -> T + { self.dot(self) } + + fn norm(&self) -> T + { self.sqnorm().sqrt() } +} + +impl> Neg> for Vec1 +{ + fn neg(&self) -> Vec1 + { Vec1(-self.x) } +} + +impl Zero for Vec1 +{ + fn zero() -> Vec1 + { + let _0 = Zero::zero(); + Vec1(_0) + } + + fn is_zero(&self) -> bool + { self.x.is_zero() } +} + +impl ToStr for Vec1 +{ + fn to_str(&self) -> ~str + { ~"Vec1 { x : " + self.x.to_str() + " }" } +}