#[macro_escape]; macro_rules! mat_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( impl $t { #[inline] pub fn new($comp0: N $(, $compN: N )*) -> $t { $t { $comp0: $comp0 $(, $compN: $compN )* } } } ) ) macro_rules! mat_cast_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( impl MatCast<$t> for $t { #[inline] fn from(m: $t) -> $t { $t::new(NumCast::from(m.$comp0.clone()) $(, NumCast::from(m.$compN.clone()) )*) } } ) ) macro_rules! add_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( impl> Add<$t, $t> for $t { #[inline] fn add(&self, other: &$t) -> $t { $t::new(self.$comp0 + other.$comp0 $(, self.$compN + other.$compN )*) } } ) ) macro_rules! sub_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( impl> Sub<$t, $t> for $t { #[inline] fn sub(&self, other: &$t) -> $t { $t::new(self.$comp0 - other.$comp0 $(, self.$compN - other.$compN )*) } } ) ) macro_rules! scalar_mul_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( impl> $t { #[inline] /// Scalar multiplication of each component of this matrix by a scalar. pub fn scalar_mul(&self, other: &N) -> $t { $t::new(self.$comp0 * *other $(, self.$compN * *other )*) } } ) ) macro_rules! scalar_div_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( impl> $t { #[inline] /// Scalar division of each component of this matrix by a scalar. pub fn scalar_div(&self, other: &N) -> $t { $t::new(self.$comp0 / *other $(, self.$compN / *other )*) } } ) ) macro_rules! scalar_add_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( impl> ScalarAdd for $t { #[inline] fn scalar_add(&self, other: &N) -> $t { $t::new(self.$comp0 + *other $(, self.$compN + *other )*) } #[inline] fn scalar_add_inplace(&mut self, other: &N) { self.$comp0 = self.$comp0 + *other; $(self.$compN = self.$compN + *other; )* } } ) ) macro_rules! scalar_sub_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( impl> ScalarSub for $t { #[inline] fn scalar_sub(&self, other: &N) -> $t { $t::new(self.$comp0 - *other $(, self.$compN - *other )*) } #[inline] fn scalar_sub_inplace(&mut self, other: &N) { self.$comp0 = self.$comp0 - *other; $(self.$compN = self.$compN - *other; )* } } ) ) macro_rules! iterable_impl( ($t: ident, $dim: expr) => ( impl Iterable for $t { #[inline] fn iter<'l>(&'l self) -> VecIterator<'l, N> { unsafe { cast::transmute::<&'l $t, &'l [N, ..$dim * $dim]>(self).iter() } } } ) ) macro_rules! iterable_mut_impl( ($t: ident, $dim: expr) => ( impl IterableMut for $t { #[inline] fn mut_iter<'l>(&'l mut self) -> VecMutIterator<'l, N> { unsafe { cast::transmute::<&'l mut $t, &'l mut [N, ..$dim * $dim]>(self).mut_iter() } } } ) ) macro_rules! one_impl( ($t: ident, $value0: ident $(, $valueN: ident)* ) => ( impl One for $t { #[inline] fn one() -> $t { let (_0, _1): (N, N) = (Zero::zero(), One::one()); return $t::new($value0.clone() $(, $valueN.clone() )*) } } ) ) macro_rules! dim_impl( ($t: ident, $dim: expr) => ( impl Dim for $t { #[inline] fn dim(_: Option<$t>) -> uint { $dim } } ) ) macro_rules! indexable_impl( ($t: ident, $dim: expr) => ( impl Indexable<(uint, uint), N> for $t { #[inline] fn at(&self, (i, j): (uint, uint)) -> N { unsafe { cast::transmute::<&$t, &[N, ..$dim * $dim]>(self)[i * $dim + j].clone() } } #[inline] fn set(&mut self, (i, j): (uint, uint), val: N) { unsafe { cast::transmute::<&mut $t, &mut [N, ..$dim * $dim]>(self)[i * $dim + j] = val } } #[inline] fn swap(&mut self, (i1, j1): (uint, uint), (i2, j2): (uint, uint)) { unsafe { cast::transmute::<&mut $t, &mut [N, ..$dim * $dim]>(self) .swap(i1 * $dim + j1, i2 * $dim + j2) } } } ) ) macro_rules! column_impl( ($t: ident, $tv: ident, $dim: expr) => ( impl Column<$tv> for $t { #[inline] fn set_column(&mut self, col: uint, v: $tv) { for (i, e) in v.iter().enumerate() { self.set((i, col), e.clone()); } } #[inline] fn column(&self, col: uint) -> $tv { let mut res: $tv = Zero::zero(); for (i, e) in res.mut_iter().enumerate() { *e = self.at((i, col)); } res } } ) ) macro_rules! row_impl( ($t: ident, $tv: ident, $dim: expr) => ( impl Row<$tv> for $t { #[inline] fn set_row(&mut self, row: uint, v: $tv) { for (i, e) in v.iter().enumerate() { self.set((row, i), e.clone()); } } #[inline] fn row(&self, row: uint) -> $tv { let mut res: $tv = Zero::zero(); for (i, e) in res.mut_iter().enumerate() { *e = self.at((row, i)); } res } } ) ) macro_rules! mul_impl( ($t: ident, $dim: expr) => ( impl Mul<$t, $t> for $t { #[inline] fn mul(&self, other: &$t) -> $t { let mut res: $t = Zero::zero(); for i in range(0u, $dim) { for j in range(0u, $dim) { let mut acc: N = Zero::zero(); for k in range(0u, $dim) { acc = acc + self.at((i, k)) * other.at((k, j)); } res.set((i, j), acc); } } res } } ) ) macro_rules! rmul_impl( ($t: ident, $v: ident, $dim: expr) => ( impl RMul<$v> for $t { #[inline] fn rmul(&self, other: &$v) -> $v { let mut res : $v = Zero::zero(); for i in range(0u, $dim) { for j in range(0u, $dim) { let val = res.at(i) + other.at(j) * self.at((i, j)); res.set(i, val) } } res } } ) ) macro_rules! lmul_impl( ($t: ident, $v: ident, $dim: expr) => ( impl LMul<$v> for $t { #[inline] fn lmul(&self, other: &$v) -> $v { let mut res : $v = Zero::zero(); for i in range(0u, $dim) { for j in range(0u, $dim) { let val = res.at(i) + other.at(j) * self.at((j, i)); res.set(i, val) } } res } } ) ) macro_rules! transform_impl( ($t: ident, $v: ident) => ( impl Transform<$v> for $t { #[inline] fn transform(&self, v: &$v) -> $v { self.rmul(v) } #[inline] fn inv_transform(&self, v: &$v) -> $v { match self.inverse() { Some(t) => t.transform(v), None => fail!("Cannot use inv_transform on a non-inversible matrix.") } } } ) ) macro_rules! inv_impl( ($t: ident, $dim: expr) => ( impl Inv for $t { #[inline] fn inverse(&self) -> Option<$t> { let mut res : $t = self.clone(); if res.inplace_inverse() { Some(res) } else { None } } fn inplace_inverse(&mut self) -> bool { let mut res: $t = One::one(); let _0N: N = Zero::zero(); // inversion using Gauss-Jordan elimination for k in range(0u, $dim) { // search a non-zero value on the k-th column // FIXME: would it be worth it to spend some more time searching for the // max instead? let mut n0 = k; // index of a non-zero entry while (n0 != $dim) { if self.at((n0, k)) != _0N { break; } n0 = n0 + 1; } if n0 == $dim { return false } // swap pivot line if n0 != k { for j in range(0u, $dim) { self.swap((n0, j), (k, j)); res.swap((n0, j), (k, j)); } } let pivot = self.at((k, k)); for j in range(k, $dim) { let selfval = self.at((k, j)) / pivot; self.set((k, j), selfval); } for j in range(0u, $dim) { let resval = res.at((k, j)) / pivot; res.set((k, j), resval); } for l in range(0u, $dim) { if l != k { let normalizer = self.at((l, k)); for j in range(k, $dim) { let selfval = self.at((l, j)) - self.at((k, j)) * normalizer; self.set((l, j), selfval); } for j in range(0u, $dim) { let resval = res.at((l, j)) - res.at((k, j)) * normalizer; res.set((l, j), resval); } } } } *self = res; true } } ) ) macro_rules! transpose_impl( ($t: ident, $dim: expr) => ( impl Transpose for $t { #[inline] fn transposed(&self) -> $t { let mut res = self.clone(); res.transpose(); res } #[inline] fn transpose(&mut self) { for i in range(1u, $dim) { for j in range(0u, i) { self.swap((i, j), (j, i)) } } } } ) ) macro_rules! approx_eq_impl( ($t: ident) => ( impl> ApproxEq for $t { #[inline] fn approx_epsilon() -> N { fail!("approx_epsilon is broken since rust revision 8693943676487c01fa09f5f3daf0df6a1f71e24d.") // ApproxEq::::approx_epsilon() } #[inline] fn approx_eq(&self, other: &$t) -> bool { let mut zip = self.iter().zip(other.iter()); do zip.all |(a, b)| { a.approx_eq(b) } } #[inline] fn approx_eq_eps(&self, other: &$t, epsilon: &N) -> bool { let mut zip = self.iter().zip(other.iter()); do zip.all |(a, b)| { a.approx_eq_eps(b, epsilon) } } } ) ) macro_rules! to_homogeneous_impl( ($t: ident, $t2: ident, $dim: expr, $dim2: expr) => ( impl ToHomogeneous<$t2> for $t { #[inline] fn to_homogeneous(&self) -> $t2 { let mut res: $t2 = One::one(); for i in range(0u, $dim) { for j in range(0u, $dim) { res.set((i, j), self.at((i, j))) } } res } } ) ) macro_rules! from_homogeneous_impl( ($t: ident, $t2: ident, $dim: expr, $dim2: expr) => ( impl FromHomogeneous<$t2> for $t { #[inline] fn from(m: &$t2) -> $t { let mut res: $t = One::one(); for i in range(0u, $dim2) { for j in range(0u, $dim2) { res.set((i, j), m.at((i, j))) } } // FIXME: do we have to deal the lost components // (like if the 1 is not a 1… do we have to divide?) res } } ) ) macro_rules! outer_impl( ($t: ident, $m: ident) => ( impl + Zero + Clone> Outer<$t, $m> for $t { #[inline] fn outer(&self, other: &$t) -> $m { let mut res: $m = Zero::zero(); for i in range(0u, Dim::dim(None::<$t>)) { for j in range(0u, Dim::dim(None::<$t>)) { res.set((i, j), self.at(i) * other.at(j)) } } res } } ) )