#![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! at_fast_impl( ($t: ident, $dim: expr) => ( impl $t { #[inline] pub unsafe fn at_fast(&self, (i, j): (uint, uint)) -> N { (*mem::transmute::<&$t, &[N, ..$dim * $dim]>(self) .unsafe_ref(i + j * $dim)).clone() } #[inline] pub unsafe fn set_fast(&mut self, (i, j): (uint, uint), val: N) { (*mem::transmute::<&mut $t, &mut [N, ..$dim * $dim]>(self) .unsafe_mut_ref(i + j * $dim)) = val } } ) ) macro_rules! mat_cast_impl( ($t: ident, $tcast: ident, $comp0: ident $(,$compN: ident)*) => ( impl> $tcast for $t { #[inline] fn to(v: $t) -> $t { $t::new(Cast::from(v.$comp0.clone()) $(, Cast::from(v.$compN.clone()))*) } } ) ) macro_rules! add_impl( ($t: ident, $trhs: ident, $comp0: ident $(,$compN: ident)*) => ( impl> $trhs> for $t { #[inline] fn binop(left: &$t, right: &$t) -> $t { $t::new(left.$comp0 + right.$comp0 $(, left.$compN + right.$compN)*) } } ) ) macro_rules! sub_impl( ($t: ident, $trhs: ident, $comp0: ident $(,$compN: ident)*) => ( impl> $trhs> for $t { #[inline] fn binop(left: &$t, right: &$t) -> $t { $t::new(left.$comp0 - right.$comp0 $(, left.$compN - right.$compN)*) } } ) ) macro_rules! mat_mul_scalar_impl( ($t: ident, $n: ident, $trhs: ident, $comp0: ident $(,$compN: ident)*) => ( impl $trhs<$n, $t<$n>> for $n { #[inline] fn binop(left: &$t<$n>, right: &$n) -> $t<$n> { $t::new(left.$comp0 * *right $(, left.$compN * *right)*) } } ) ) macro_rules! mat_div_scalar_impl( ($t: ident, $n: ident, $trhs: ident, $comp0: ident $(,$compN: ident)*) => ( impl $trhs<$n, $t<$n>> for $n { #[inline] fn binop(left: &$t<$n>, right: &$n) -> $t<$n> { $t::new(left.$comp0 / *right $(, left.$compN / *right)*) } } ) ) macro_rules! mat_add_scalar_impl( ($t: ident, $n: ident, $trhs: ident, $comp0: ident $(,$compN: ident)*) => ( impl $trhs<$n, $t<$n>> for $n { #[inline] fn binop(left: &$t<$n>, right: &$n) -> $t<$n> { $t::new(left.$comp0 + *right $(, left.$compN + *right)*) } } ) ) macro_rules! eye_impl( ($t: ident, $ndim: expr, $($comp_diagN: ident),+) => ( impl Eye for $t { fn new_identity(dim: uint) -> $t { assert!(dim == $ndim); let mut eye: $t = Zero::zero(); $(eye.$comp_diagN = One::one();)+ eye } } ) ) macro_rules! mat_sub_scalar_impl( ($t: ident, $n: ident, $trhs: ident, $comp0: ident $(,$compN: ident)*) => ( impl $trhs<$n, $t<$n>> for $n { #[inline] fn binop(left: &$t<$n>, right: &$n) -> $t<$n> { $t::new(left.$comp0 - *right $(, left.$compN - *right)*) } } ) ) macro_rules! absolute_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( impl Absolute<$t> for $t { #[inline] fn abs(m: &$t) -> $t { $t::new(m.$comp0.abs() $(, m.$compN.abs() )*) } } ) ) macro_rules! iterable_impl( ($t: ident, $dim: expr) => ( impl Iterable for $t { #[inline] fn iter<'l>(&'l self) -> Items<'l, N> { unsafe { mem::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) -> MutItems<'l, N> { unsafe { mem::transmute::<&'l mut $t, &'l mut [N, ..$dim * $dim]>(self).mut_iter() } } } ) ) macro_rules! one_impl( ($t: ident, $value0: expr $(, $valueN: expr)* ) => ( impl One for $t { #[inline] fn one() -> $t { $t::new($value0() $(, $valueN() )*) } } ) ) 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 { mem::transmute::<&$t, &[N, ..$dim * $dim]>(self)[i + j * $dim].clone() } } #[inline] fn set(&mut self, (i, j): (uint, uint), val: N) { unsafe { mem::transmute::<&mut $t, &mut [N, ..$dim * $dim]>(self)[i + j * $dim] = val } } #[inline] fn swap(&mut self, (i1, j1): (uint, uint), (i2, j2): (uint, uint)) { unsafe { mem::transmute::<&mut $t, &mut [N, ..$dim * $dim]>(self) .swap(i1 + j1 * $dim, i2 + j2 * $dim) } } #[inline] fn shape(&self) -> (uint, uint) { ($dim, $dim) } #[inline] unsafe fn unsafe_at(&self, (i, j): (uint, uint)) -> N { (*mem::transmute::<&$t, &[N, ..$dim * $dim]>(self).unsafe_ref(i + j * $dim)).clone() } #[inline] unsafe fn unsafe_set(&mut self, (i, j): (uint, uint), val: N) { (*mem::transmute::<&mut $t, &mut [N, ..$dim * $dim]>(self).unsafe_mut_ref(i + j * $dim)) = val } } ) ) macro_rules! col_slice_impl( ($t: ident, $tv: ident, $dim: expr) => ( impl ColSlice> for $t { fn col_slice(& self, cid: uint, rstart: uint, rend: uint) -> DVec { assert!(cid < $dim); assert!(rstart < rend); assert!(rend <= $dim); let col = self.col(cid); let res = DVec::from_vec( rend - rstart, unsafe { mem::transmute::<&$tv, & [N, ..$dim]> (&col).slice(rstart, rend) }); res } } ) ) macro_rules! row_impl( ($t: ident, $tv: ident, $dim: expr) => ( impl Row<$tv> for $t { #[inline] fn nrows(&self) -> uint { Dim::dim(None::<$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! row_slice_impl( ($t: ident, $tv: ident, $dim: expr) => ( impl RowSlice> for $t { fn row_slice(& self, rid: uint, cstart: uint, cend: uint) -> DVec { assert!(rid < $dim); assert!(cstart < cend); assert!(cend <= $dim); let row = self.row(rid); let res = DVec::from_vec( cend - cstart, unsafe { mem::transmute::<&$tv, & [N, ..$dim]> (&row).slice(cstart, cend) }); res } } ) ) macro_rules! col_impl( ($t: ident, $tv: ident, $dim: expr) => ( impl Col<$tv> for $t { #[inline] fn ncols(&self) -> uint { Dim::dim(None::<$t>) } #[inline] fn set_col(&mut self, col: uint, v: $tv) { for (i, e) in v.iter().enumerate() { self.set((i, col), e.clone()); } } #[inline] fn col(&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! mat_mul_mat_impl( ($t: ident, $trhs: ident, $dim: expr) => ( impl $trhs> for $t { #[inline] fn binop(left: &$t, right: &$t) -> $t { // careful! we need to comute other * self here (self is the rhs). let mut res: $t = Zero::zero(); for i in range(0u, $dim) { for j in range(0u, $dim) { let mut acc: N = Zero::zero(); unsafe { for k in range(0u, $dim) { acc = acc + left.at_fast((i, k)) * right.at_fast((k, j)); } res.set_fast((i, j), acc); } } } res } } ) ) macro_rules! vec_mul_mat_impl( ($t: ident, $v: ident, $trhs: ident, $dim: expr) => ( impl $trhs> for $t { #[inline] fn binop(left: &$v, right: &$t) -> $v { let mut res : $v = Zero::zero(); for i in range(0u, $dim) { for j in range(0u, $dim) { unsafe { let val = res.at_fast(i) + left.at_fast(j) * right.at_fast((j, i)); res.set_fast(i, val) } } } res } } ) ) macro_rules! mat_mul_vec_impl( ($t: ident, $v: ident, $trhs: ident, $dim: expr) => ( impl $trhs> for $v { #[inline] fn binop(left: &$t, right: &$v) -> $v { let mut res : $v = Zero::zero(); for i in range(0u, $dim) { for j in range(0u, $dim) { unsafe { let val = res.at_fast(i) + left.at_fast((i, j)) * right.at_fast(j); res.set_fast(i, val) } } } res } } ) ) macro_rules! inv_impl( ($t: ident, $dim: expr) => ( impl Inv for $t { #[inline] fn inv_cpy(m: &$t) -> Option<$t> { let mut res : $t = m.clone(); if res.inv() { Some(res) } else { None } } fn inv(&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 transpose_cpy(m: &$t) -> $t { let mut res = m.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(_: Option<$t>) -> N { ApproxEq::approx_epsilon(None::) } #[inline] fn approx_eq(a: &$t, b: &$t) -> bool { let mut zip = a.iter().zip(b.iter()); zip.all(|(a, b)| ApproxEq::approx_eq(a, b)) } #[inline] fn approx_eq_eps(a: &$t, b: &$t, epsilon: &N) -> bool { let mut zip = a.iter().zip(b.iter()); zip.all(|(a, b)| ApproxEq::approx_eq_eps(a, b, epsilon)) } } ) ) macro_rules! to_homogeneous_impl( ($t: ident, $t2: ident, $dim: expr, $dim2: expr) => ( impl ToHomogeneous<$t2> for $t { #[inline] fn to_homogeneous(m: &$t) -> $t2 { let mut res: $t2 = One::one(); for i in range(0u, $dim) { for j in range(0u, $dim) { res.set((i, j), m.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> Outer<$m> for $t { #[inline] fn outer(a: &$t, b: &$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), a.at(i) * b.at(j)) } } res } } ) )