#[macro_escape]; macro_rules! clone_impl( // FIXME: use 'Clone' alone. For the moment, we need 'Copy' because the automatic // implementation of Clone for [t, ..n] is badly typed. ($t: ident) => ( impl Clone for $t { #[inline] fn clone(&self) -> $t { $t { mij: copy self.mij } } } ) ) macro_rules! mat_impl( ($t: ident, $dim: expr) => ( impl $t { #[inline] pub fn new(mij: [N, ..$dim * $dim]) -> $t { $t { mij: mij } } #[inline] pub fn offset(&self, i: uint, j: uint) -> uint { i * $dim + j } } ) ) macro_rules! one_impl( ($t: ident, [ $($value: ident)|+ ] ) => ( impl One for $t { #[inline] fn one() -> $t { let (_0, _1) = (Zero::zero::(), One::one::()); return $t::new( [ $( $value.clone(), )+ ] ) } } ) ) macro_rules! zero_impl( ($t: ident, [ $($value: ident)|+ ] ) => ( impl Zero for $t { #[inline] fn zero() -> $t { let _0 = Zero::zero::(); return $t::new( [ $( $value.clone(), )+ ] ) } #[inline] fn is_zero(&self) -> bool { self.mij.iter().all(|e| e.is_zero()) } } ) ) macro_rules! dim_impl( ($t: ident, $dim: expr) => ( impl Dim for $t { #[inline] fn dim() -> uint { $dim } } ) ) macro_rules! mat_indexable_impl( ($t: ident, $dim: expr) => ( impl Indexable<(uint, uint), N> for $t { #[inline] pub fn at(&self, (i, j): (uint, uint)) -> N { self.mij[self.offset(i, j)].clone() } #[inline] pub fn set(&mut self, (i, j): (uint, uint), t: N) { self.mij[self.offset(i, j)] = t } } ) ) macro_rules! column_impl( ($t: ident, $dim: expr) => ( impl + IterableMut> Column for $t { fn set_column(&mut self, col: uint, v: V) { for v.iter().enumerate().advance |(i, e)| { if i == Dim::dim::<$t>() { break } self.set((i, col), e.clone()); } } fn column(&self, col: uint) -> V { let mut res = Zero::zero::(); for res.mut_iter().enumerate().advance |(i, e)| { if i >= Dim::dim::<$t>() { break } *e = self.at((i, col)); } res } } ) ) macro_rules! mul_impl( ($t: ident, $dim: expr) => ( impl Mul<$t, $t> for $t { fn mul(&self, other: &$t) -> $t { let mut res: $t = Zero::zero(); for iterate(0u, $dim) |i| { for iterate(0u, $dim) |j| { let mut acc = Zero::zero::(); for iterate(0u, $dim) |k| { 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 { fn rmul(&self, other: &$v) -> $v { let mut res : $v = Zero::zero(); for iterate(0u, $dim) |i| { for iterate(0u, $dim) |j| { res.at[i] = res.at[i] + other.at[j] * self.at((i, j)); } } res } } ) ) macro_rules! lmul_impl( ($t: ident, $v: ident, $dim: expr) => ( impl LMul<$v> for $t { fn lmul(&self, other: &$v) -> $v { let mut res : $v = Zero::zero(); for iterate(0u, $dim) |i| { for iterate(0u, $dim) |j| { res.at[i] = res.at[i] + other.at[j] * self.at((j, i)); } } res } } ) ) macro_rules! transform_impl( ($t: ident, $v: ident) => ( impl Transform<$v> for $t { #[inline] fn transform_vec(&self, v: &$v) -> $v { self.rmul(v) } #[inline] fn inv_transform(&self, v: &$v) -> $v { match self.inverse() { Some(t) => t.transform_vec(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 iterate(0u, $dim) |k| { // 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 iterate(0u, $dim) |j| { let off_n0_j = self.offset(n0, j); let off_k_j = self.offset(k, j); self.mij.swap(off_n0_j, off_k_j); res.mij.swap(off_n0_j, off_k_j); } } let pivot = self.at((k, k)); for iterate(k, $dim) |j| { let selfval = self.at((k, j)) / pivot; self.set((k, j), selfval); } for iterate(0u, $dim) |j| { let resval = res.at((k, j)) / pivot; res.set((k, j), resval); } for iterate(0u, $dim) |l| { if l != k { let normalizer = self.at((l, k)); for iterate(k, $dim) |j| { let selfval = self.at((l, j)) - self.at((k, j)) * normalizer; self.set((l, j), selfval); } for iterate(0u, $dim) |j| { 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 } fn transpose(&mut self) { for iterate(1u, $dim) |i| { for iterate(0u, $dim - 1) |j| { let off_i_j = self.offset(i, j); let off_j_i = self.offset(j, i); self.mij.swap(off_i_j, off_j_i); } } } } ) ) macro_rules! approx_eq_impl( ($t: ident) => ( impl> ApproxEq for $t { #[inline] fn approx_epsilon() -> N { ApproxEq::approx_epsilon::() } #[inline] fn approx_eq(&self, other: &$t) -> bool { let mut zip = self.mij.iter().zip(other.mij.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.mij.iter().zip(other.mij.iter()); do zip.all |(a, b)| { a.approx_eq_eps(b, epsilon) } } } ) ) macro_rules! rand_impl( ($t: ident, $param: ident, [ $($elem: ident)|+ ]) => ( impl Rand for $t { #[inline] fn rand($param: &mut R) -> $t { $t::new([ $( $elem.gen(), )+ ]) } } ) ) macro_rules! to_homogeneous_impl( ($t: ident, $t2: ident, $dim: expr) => ( impl ToHomogeneous<$t2> for $t { fn to_homogeneous(&self) -> $t2 { let mut res: $t2 = One::one(); for iterate(0, $dim) |i| { for iterate(0, $dim) |j| { res.set((i, j), self.at((i, j))) } } res } } ) ) macro_rules! from_homogeneous_impl( ($t: ident, $t2: ident, $dim2: expr) => ( impl FromHomogeneous<$t2> for $t { fn from_homogeneous(m: &$t2) -> $t { let mut res: $t = One::one(); for iterate(0, $dim2) |i| { for iterate(0, $dim2) |j| { 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 } } ) )