2014-04-02 04:58:06 +08:00
|
|
|
#![macro_escape]
|
2013-07-22 16:26:20 +08:00
|
|
|
|
|
|
|
macro_rules! mat_impl(
|
2013-07-23 05:42:35 +08:00
|
|
|
($t: ident, $comp0: ident $(,$compN: ident)*) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N> $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
pub fn new($comp0: N $(, $compN: N )*) -> $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
$t {
|
|
|
|
$comp0: $comp0
|
|
|
|
$(, $compN: $compN )*
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-09-13 19:21:42 +08:00
|
|
|
macro_rules! at_fast_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
|
|
|
impl<N: Clone> $t<N> {
|
|
|
|
#[inline]
|
2013-12-11 23:30:50 +08:00
|
|
|
pub unsafe fn at_fast(&self, (i, j): (uint, uint)) -> N {
|
2014-05-12 07:31:22 +08:00
|
|
|
(*mem::transmute::<&$t<N>, &[N, ..$dim * $dim]>(self)
|
2013-10-18 04:40:44 +08:00
|
|
|
.unsafe_ref(i + j * $dim)).clone()
|
2013-09-13 19:21:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2013-12-11 23:30:50 +08:00
|
|
|
pub unsafe fn set_fast(&mut self, (i, j): (uint, uint), val: N) {
|
2014-05-12 07:31:22 +08:00
|
|
|
(*mem::transmute::<&mut $t<N>, &mut [N, ..$dim * $dim]>(self)
|
2013-10-18 04:40:44 +08:00
|
|
|
.unsafe_mut_ref(i + j * $dim)) = val
|
2013-09-13 19:21:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-07-23 05:42:35 +08:00
|
|
|
macro_rules! mat_cast_impl(
|
2013-10-10 04:59:44 +08:00
|
|
|
($t: ident, $tcast: ident, $comp0: ident $(,$compN: ident)*) => (
|
|
|
|
impl<Nin: Clone, Nout: Clone + Cast<Nin>> $tcast<Nout> for $t<Nin> {
|
|
|
|
#[inline]
|
|
|
|
fn to(v: $t<Nin>) -> $t<Nout> {
|
|
|
|
$t::new(Cast::from(v.$comp0.clone()) $(, Cast::from(v.$compN.clone()))*)
|
|
|
|
}
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-10-10 04:59:44 +08:00
|
|
|
)
|
2013-07-23 05:42:35 +08:00
|
|
|
)
|
|
|
|
|
2013-08-12 22:50:50 +08:00
|
|
|
macro_rules! add_impl(
|
2013-09-15 03:11:43 +08:00
|
|
|
($t: ident, $trhs: ident, $comp0: ident $(,$compN: ident)*) => (
|
2014-01-21 06:40:32 +08:00
|
|
|
impl<N: Add<N, N>> $trhs<N, $t<N>> for $t<N> {
|
2013-09-15 03:11:43 +08:00
|
|
|
#[inline]
|
2013-09-15 16:48:18 +08:00
|
|
|
fn binop(left: &$t<N>, right: &$t<N>) -> $t<N> {
|
|
|
|
$t::new(left.$comp0 + right.$comp0 $(, left.$compN + right.$compN)*)
|
2013-09-15 03:11:43 +08:00
|
|
|
}
|
2013-08-12 22:50:50 +08:00
|
|
|
}
|
2013-09-15 03:11:43 +08:00
|
|
|
)
|
2013-08-12 22:50:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! sub_impl(
|
2013-09-15 03:11:43 +08:00
|
|
|
($t: ident, $trhs: ident, $comp0: ident $(,$compN: ident)*) => (
|
2014-01-21 06:40:32 +08:00
|
|
|
impl<N: Sub<N, N>> $trhs<N, $t<N>> for $t<N> {
|
2013-09-15 03:11:43 +08:00
|
|
|
#[inline]
|
2013-09-15 16:48:18 +08:00
|
|
|
fn binop(left: &$t<N>, right: &$t<N>) -> $t<N> {
|
|
|
|
$t::new(left.$comp0 - right.$comp0 $(, left.$compN - right.$compN)*)
|
2013-09-15 03:11:43 +08:00
|
|
|
}
|
2013-08-12 22:50:50 +08:00
|
|
|
}
|
2013-09-15 03:11:43 +08:00
|
|
|
)
|
2013-08-12 22:50:50 +08:00
|
|
|
)
|
|
|
|
|
2014-05-23 01:54:54 +08:00
|
|
|
macro_rules! mat_mul_scalar_impl(
|
2013-09-15 03:11:43 +08:00
|
|
|
($t: ident, $n: ident, $trhs: ident, $comp0: ident $(,$compN: ident)*) => (
|
|
|
|
impl $trhs<$n, $t<$n>> for $n {
|
|
|
|
#[inline]
|
2013-09-15 16:48:18 +08:00
|
|
|
fn binop(left: &$t<$n>, right: &$n) -> $t<$n> {
|
|
|
|
$t::new(left.$comp0 * *right $(, left.$compN * *right)*)
|
2013-09-15 03:11:43 +08:00
|
|
|
}
|
2013-08-12 23:04:53 +08:00
|
|
|
}
|
2013-09-15 03:11:43 +08:00
|
|
|
)
|
2013-08-12 23:04:53 +08:00
|
|
|
)
|
|
|
|
|
2014-05-23 01:54:54 +08:00
|
|
|
macro_rules! mat_div_scalar_impl(
|
2013-09-15 03:11:43 +08:00
|
|
|
($t: ident, $n: ident, $trhs: ident, $comp0: ident $(,$compN: ident)*) => (
|
|
|
|
impl $trhs<$n, $t<$n>> for $n {
|
|
|
|
#[inline]
|
2013-09-15 16:48:18 +08:00
|
|
|
fn binop(left: &$t<$n>, right: &$n) -> $t<$n> {
|
|
|
|
$t::new(left.$comp0 / *right $(, left.$compN / *right)*)
|
2013-09-15 03:11:43 +08:00
|
|
|
}
|
2013-08-12 23:04:53 +08:00
|
|
|
}
|
2013-09-15 03:11:43 +08:00
|
|
|
)
|
2013-08-12 23:04:53 +08:00
|
|
|
)
|
|
|
|
|
2014-05-23 01:54:54 +08:00
|
|
|
macro_rules! mat_add_scalar_impl(
|
2013-09-15 03:11:43 +08:00
|
|
|
($t: ident, $n: ident, $trhs: ident, $comp0: ident $(,$compN: ident)*) => (
|
|
|
|
impl $trhs<$n, $t<$n>> for $n {
|
|
|
|
#[inline]
|
2013-09-15 16:48:18 +08:00
|
|
|
fn binop(left: &$t<$n>, right: &$n) -> $t<$n> {
|
|
|
|
$t::new(left.$comp0 + *right $(, left.$compN + *right)*)
|
2013-09-15 03:11:43 +08:00
|
|
|
}
|
2013-08-12 23:04:53 +08:00
|
|
|
}
|
2013-09-15 03:11:43 +08:00
|
|
|
)
|
2013-08-12 23:04:53 +08:00
|
|
|
)
|
|
|
|
|
2014-05-12 01:46:04 +08:00
|
|
|
|
|
|
|
macro_rules! eye_impl(
|
|
|
|
($t: ident, $ndim: expr, $($comp_diagN: ident),+) => (
|
|
|
|
impl<N: Zero + One> Eye for $t<N> {
|
|
|
|
fn new_identity(dim: uint) -> $t<N> {
|
|
|
|
assert!(dim == $ndim);
|
|
|
|
let mut eye: $t<N> = Zero::zero();
|
|
|
|
$(eye.$comp_diagN = One::one();)+
|
|
|
|
eye
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-05-23 01:54:54 +08:00
|
|
|
macro_rules! mat_sub_scalar_impl(
|
2013-09-15 03:11:43 +08:00
|
|
|
($t: ident, $n: ident, $trhs: ident, $comp0: ident $(,$compN: ident)*) => (
|
|
|
|
impl $trhs<$n, $t<$n>> for $n {
|
|
|
|
#[inline]
|
2013-09-15 16:48:18 +08:00
|
|
|
fn binop(left: &$t<$n>, right: &$n) -> $t<$n> {
|
|
|
|
$t::new(left.$comp0 - *right $(, left.$compN - *right)*)
|
2013-09-15 03:11:43 +08:00
|
|
|
}
|
2013-08-12 23:04:53 +08:00
|
|
|
}
|
2013-09-15 03:11:43 +08:00
|
|
|
)
|
2013-08-12 23:04:53 +08:00
|
|
|
)
|
|
|
|
|
2013-09-13 16:26:19 +08:00
|
|
|
macro_rules! absolute_impl(
|
|
|
|
($t: ident, $comp0: ident $(,$compN: ident)*) => (
|
|
|
|
impl<N: Signed> Absolute<$t<N>> for $t<N> {
|
|
|
|
#[inline]
|
2013-10-17 03:44:33 +08:00
|
|
|
fn abs(m: &$t<N>) -> $t<N> {
|
|
|
|
$t::new(m.$comp0.abs() $(, m.$compN.abs() )*)
|
2013-09-13 16:26:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-07-22 16:26:20 +08:00
|
|
|
macro_rules! iterable_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N> Iterable<N> for $t<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2014-01-18 17:49:47 +08:00
|
|
|
fn iter<'l>(&'l self) -> Items<'l, N> {
|
2013-08-05 16:13:44 +08:00
|
|
|
unsafe {
|
2014-05-12 07:31:22 +08:00
|
|
|
mem::transmute::<&'l $t<N>, &'l [N, ..$dim * $dim]>(self).iter()
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! iterable_mut_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N> IterableMut<N> for $t<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2014-01-18 17:49:47 +08:00
|
|
|
fn mut_iter<'l>(&'l mut self) -> MutItems<'l, N> {
|
2013-08-05 16:13:44 +08:00
|
|
|
unsafe {
|
2014-05-12 07:31:22 +08:00
|
|
|
mem::transmute::<&'l mut $t<N>, &'l mut [N, ..$dim * $dim]>(self).mut_iter()
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! one_impl(
|
2013-09-07 23:26:05 +08:00
|
|
|
($t: ident, $value0: expr $(, $valueN: expr)* ) => (
|
2014-01-21 06:40:32 +08:00
|
|
|
impl<N: Clone + Num> One for $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn one() -> $t<N> {
|
2013-09-07 23:26:05 +08:00
|
|
|
$t::new($value0() $(, $valueN() )*)
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! dim_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N> Dim for $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-28 20:22:12 +08:00
|
|
|
fn dim(_: Option<$t<N>>) -> uint {
|
2013-08-05 16:13:44 +08:00
|
|
|
$dim
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! indexable_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: Clone> Indexable<(uint, uint), N> for $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-14 15:43:02 +08:00
|
|
|
fn at(&self, (i, j): (uint, uint)) -> N {
|
2013-08-05 16:13:44 +08:00
|
|
|
unsafe {
|
2014-05-12 07:31:22 +08:00
|
|
|
mem::transmute::<&$t<N>, &[N, ..$dim * $dim]>(self)[i + j * $dim].clone()
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
|
|
|
|
#[inline]
|
2013-08-14 15:43:02 +08:00
|
|
|
fn set(&mut self, (i, j): (uint, uint), val: N) {
|
2013-08-05 16:13:44 +08:00
|
|
|
unsafe {
|
2014-05-12 07:31:22 +08:00
|
|
|
mem::transmute::<&mut $t<N>, &mut [N, ..$dim * $dim]>(self)[i + j * $dim] = val
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
|
|
|
|
#[inline]
|
2013-08-14 15:43:02 +08:00
|
|
|
fn swap(&mut self, (i1, j1): (uint, uint), (i2, j2): (uint, uint)) {
|
2013-08-05 15:44:56 +08:00
|
|
|
unsafe {
|
2014-05-12 07:31:22 +08:00
|
|
|
mem::transmute::<&mut $t<N>, &mut [N, ..$dim * $dim]>(self)
|
2013-10-18 04:40:44 +08:00
|
|
|
.swap(i1 + j1 * $dim, i2 + j2 * $dim)
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
2013-12-02 03:17:18 +08:00
|
|
|
|
2014-05-12 01:46:04 +08:00
|
|
|
#[inline]
|
|
|
|
fn shape(&self) -> (uint, uint) {
|
|
|
|
($dim, $dim)
|
|
|
|
}
|
|
|
|
|
2013-12-02 03:17:18 +08:00
|
|
|
#[inline]
|
|
|
|
unsafe fn unsafe_at(&self, (i, j): (uint, uint)) -> N {
|
2014-05-12 07:31:22 +08:00
|
|
|
(*mem::transmute::<&$t<N>, &[N, ..$dim * $dim]>(self).unsafe_ref(i + j * $dim)).clone()
|
2013-12-02 03:17:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
unsafe fn unsafe_set(&mut self, (i, j): (uint, uint), val: N) {
|
2014-05-12 07:31:22 +08:00
|
|
|
(*mem::transmute::<&mut $t<N>, &mut [N, ..$dim * $dim]>(self).unsafe_mut_ref(i + j * $dim)) = val
|
2013-12-02 03:17:18 +08:00
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-09-18 10:20:36 +08:00
|
|
|
macro_rules! index_impl(
|
|
|
|
($t: ident, $tv: ident, $dim: expr) => (
|
|
|
|
impl<N> Index<uint, $tv<N>> for $t<N> {
|
|
|
|
fn index(&self, i: &uint) -> &$tv<N> {
|
|
|
|
unsafe {
|
|
|
|
&mem::transmute::<&$t<N>, &[$tv<N>, ..$dim]>(self)[*i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<N> IndexMut<uint, $tv<N>> for $t<N> {
|
|
|
|
fn index_mut(&mut self, i: &uint) -> &mut $tv<N> {
|
|
|
|
unsafe {
|
|
|
|
&mut mem::transmute::<&mut $t<N>, &mut [$tv<N>, ..$dim]>(self)[*i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-05-12 20:06:25 +08:00
|
|
|
macro_rules! col_slice_impl(
|
2014-08-16 18:16:26 +08:00
|
|
|
($t: ident, $tv: ident, $slice: ident, $dim: expr) => (
|
|
|
|
impl<N: Clone + Zero> ColSlice<$slice<N>> for $t<N> {
|
|
|
|
fn col_slice(&self, cid: uint, rstart: uint, rend: uint) -> $slice<N> {
|
2014-05-12 20:06:25 +08:00
|
|
|
let col = self.col(cid);
|
2014-08-16 18:16:26 +08:00
|
|
|
|
|
|
|
$slice::from_slice(rend - rstart, col.as_slice().slice(rstart, rend))
|
2014-05-12 20:06:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-08-26 05:01:44 +08:00
|
|
|
macro_rules! row_impl(
|
|
|
|
($t: ident, $tv: ident, $dim: expr) => (
|
2014-09-20 05:51:27 +08:00
|
|
|
impl<N: Clone + Zero> Row<$tv<N>> for $t<N> {
|
2013-09-13 16:26:19 +08:00
|
|
|
#[inline]
|
2013-10-06 22:54:09 +08:00
|
|
|
fn nrows(&self) -> uint {
|
2013-09-13 16:26:19 +08:00
|
|
|
Dim::dim(None::<$t<N>>)
|
|
|
|
}
|
|
|
|
|
2013-08-26 05:01:44 +08:00
|
|
|
#[inline]
|
|
|
|
fn set_row(&mut self, row: uint, v: $tv<N>) {
|
2014-09-20 05:51:27 +08:00
|
|
|
for (i, e) in v.iter().enumerate() {
|
|
|
|
self.set((row, i), e.clone());
|
|
|
|
}
|
2013-08-26 05:01:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn row(&self, row: uint) -> $tv<N> {
|
2014-09-20 05:51:27 +08:00
|
|
|
let mut res: $tv<N> = Zero::zero();
|
|
|
|
|
|
|
|
for (i, e) in res.mut_iter().enumerate() {
|
|
|
|
*e = self.at((row, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
2013-08-26 05:01:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-05-12 20:06:25 +08:00
|
|
|
macro_rules! row_slice_impl(
|
2014-08-16 18:16:26 +08:00
|
|
|
($t: ident, $tv: ident, $slice: ident, $dim: expr) => (
|
|
|
|
impl<N: Clone + Zero> RowSlice<$slice<N>> for $t<N> {
|
|
|
|
fn row_slice(&self, rid: uint, cstart: uint, cend: uint) -> $slice<N> {
|
2014-05-12 20:06:25 +08:00
|
|
|
let row = self.row(rid);
|
2014-08-16 18:16:26 +08:00
|
|
|
|
|
|
|
$slice::from_slice(cend - cstart, row.as_slice().slice(cstart, cend))
|
2014-05-12 20:06:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-09-13 16:26:19 +08:00
|
|
|
macro_rules! col_impl(
|
|
|
|
($t: ident, $tv: ident, $dim: expr) => (
|
2014-09-20 05:51:27 +08:00
|
|
|
impl<N: Clone> Col<$tv<N>> for $t<N> {
|
2013-09-13 16:26:19 +08:00
|
|
|
#[inline]
|
2013-10-06 22:54:09 +08:00
|
|
|
fn ncols(&self) -> uint {
|
2013-09-13 16:26:19 +08:00
|
|
|
Dim::dim(None::<$t<N>>)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn set_col(&mut self, col: uint, v: $tv<N>) {
|
2014-09-20 05:51:27 +08:00
|
|
|
self[col] = v;
|
2013-09-13 16:26:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn col(&self, col: uint) -> $tv<N> {
|
2014-09-20 05:51:27 +08:00
|
|
|
self[col].clone()
|
2013-09-13 16:26:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-08-16 19:12:08 +08:00
|
|
|
macro_rules! diag_impl(
|
|
|
|
($t: ident, $tv: ident, $dim: expr) => (
|
|
|
|
impl<N: Clone + Zero> Diag<$tv<N>> for $t<N> {
|
|
|
|
#[inline]
|
|
|
|
fn from_diag(diag: &$tv<N>) -> $t<N> {
|
|
|
|
let mut res: $t<N> = Zero::zero();
|
|
|
|
|
|
|
|
res.set_diag(diag);
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn set_diag(&mut self, diag: &$tv<N>) {
|
|
|
|
for i in range(0, $dim) {
|
|
|
|
unsafe { self.unsafe_set((i, i), diag.unsafe_at(i)) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn diag(&self) -> $tv<N> {
|
|
|
|
let mut diag: $tv<N> = Zero::zero();
|
|
|
|
|
|
|
|
for i in range(0, $dim) {
|
|
|
|
unsafe { diag.unsafe_set(i, self.unsafe_at((i, i))) }
|
|
|
|
}
|
|
|
|
|
|
|
|
diag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-09-14 23:08:48 +08:00
|
|
|
macro_rules! mat_mul_mat_impl(
|
|
|
|
($t: ident, $trhs: ident, $dim: expr) => (
|
|
|
|
impl<N: Clone + Num> $trhs<N, $t<N>> for $t<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2013-09-15 16:48:18 +08:00
|
|
|
fn binop(left: &$t<N>, right: &$t<N>) -> $t<N> {
|
2013-09-14 23:08:48 +08:00
|
|
|
// careful! we need to comute other * self here (self is the rhs).
|
2013-08-05 15:44:56 +08:00
|
|
|
let mut res: $t<N> = Zero::zero();
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for i in range(0u, $dim) {
|
|
|
|
for j in range(0u, $dim) {
|
2013-08-28 20:22:12 +08:00
|
|
|
let mut acc: N = Zero::zero();
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-09-13 19:21:42 +08:00
|
|
|
unsafe {
|
|
|
|
for k in range(0u, $dim) {
|
2013-09-15 16:48:18 +08:00
|
|
|
acc = acc + left.at_fast((i, k)) * right.at_fast((k, j));
|
2013-09-13 19:21:42 +08:00
|
|
|
}
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-09-13 19:21:42 +08:00
|
|
|
res.set_fast((i, j), acc);
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
|
|
|
}
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
res
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-09-15 16:48:18 +08:00
|
|
|
macro_rules! vec_mul_mat_impl(
|
2014-10-10 17:23:52 +08:00
|
|
|
($t: ident, $v: ident, $trhs: ident, $dim: expr, $zero: expr) => (
|
2013-09-15 16:48:18 +08:00
|
|
|
impl<N: Clone + Num> $trhs<N, $v<N>> for $t<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2013-09-15 16:48:18 +08:00
|
|
|
fn binop(left: &$v<N>, right: &$t<N>) -> $v<N> {
|
2014-10-10 17:23:52 +08:00
|
|
|
let mut res : $v<N> = $zero();
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for i in range(0u, $dim) {
|
|
|
|
for j in range(0u, $dim) {
|
2013-09-13 19:21:42 +08:00
|
|
|
unsafe {
|
2013-09-15 16:48:18 +08:00
|
|
|
let val = res.at_fast(i) + left.at_fast(j) * right.at_fast((j, i));
|
2013-09-13 19:21:42 +08:00
|
|
|
res.set_fast(i, val)
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
|
|
|
}
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
res
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-09-15 16:48:18 +08:00
|
|
|
macro_rules! mat_mul_vec_impl(
|
2014-10-10 17:23:52 +08:00
|
|
|
($t: ident, $v: ident, $trhs: ident, $dim: expr, $zero: expr) => (
|
2013-09-15 03:11:43 +08:00
|
|
|
impl<N: Clone + Num> $trhs<N, $v<N>> for $v<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2013-09-15 16:48:18 +08:00
|
|
|
fn binop(left: &$t<N>, right: &$v<N>) -> $v<N> {
|
2014-10-10 17:23:52 +08:00
|
|
|
let mut res : $v<N> = $zero();
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for i in range(0u, $dim) {
|
|
|
|
for j in range(0u, $dim) {
|
2013-09-13 19:21:42 +08:00
|
|
|
unsafe {
|
2013-09-15 16:48:18 +08:00
|
|
|
let val = res.at_fast(i) + left.at_fast((i, j)) * right.at_fast(j);
|
2013-09-13 19:21:42 +08:00
|
|
|
res.set_fast(i, val)
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
|
|
|
}
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
res
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-10-10 17:23:52 +08:00
|
|
|
macro_rules! pnt_mul_mat_impl(
|
|
|
|
($t: ident, $v: ident, $trhs: ident, $dim: expr, $zero: expr) => (
|
|
|
|
vec_mul_mat_impl!($t, $v, $trhs, $dim, $zero)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! mat_mul_pnt_impl(
|
|
|
|
($t: ident, $v: ident, $trhs: ident, $dim: expr, $zero: expr) => (
|
|
|
|
mat_mul_vec_impl!($t, $v, $trhs, $dim, $zero)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-07-22 16:26:20 +08:00
|
|
|
macro_rules! inv_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
2014-06-01 21:21:45 +08:00
|
|
|
impl<N: Clone + Num>
|
2013-08-05 16:13:44 +08:00
|
|
|
Inv for $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-10-14 16:22:32 +08:00
|
|
|
fn inv_cpy(m: &$t<N>) -> Option<$t<N>> {
|
|
|
|
let mut res : $t<N> = m.clone();
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-10-14 16:22:32 +08:00
|
|
|
if res.inv() {
|
2013-08-05 16:13:44 +08:00
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
None
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-10-14 16:22:32 +08:00
|
|
|
fn inv(&mut self) -> bool {
|
2013-08-05 15:44:56 +08:00
|
|
|
let mut res: $t<N> = One::one();
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
// inversion using Gauss-Jordan elimination
|
2013-08-05 16:13:44 +08:00
|
|
|
for k in range(0u, $dim) {
|
2013-08-05 15:44:56 +08:00
|
|
|
// 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
|
|
|
|
|
2014-01-23 08:22:23 +08:00
|
|
|
while n0 != $dim {
|
2014-09-18 10:47:28 +08:00
|
|
|
if self.at((n0, k)) != Zero::zero() {
|
2013-08-05 16:13:44 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
|
|
|
|
n0 = n0 + 1;
|
|
|
|
}
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
if n0 == $dim {
|
|
|
|
return false
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
|
|
|
|
// swap pivot line
|
2013-08-05 16:13:44 +08:00
|
|
|
if n0 != k {
|
|
|
|
for j in range(0u, $dim) {
|
2013-08-05 15:44:56 +08:00
|
|
|
self.swap((n0, j), (k, j));
|
|
|
|
res.swap((n0, j), (k, j));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let pivot = self.at((k, k));
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for j in range(k, $dim) {
|
2013-08-05 15:44:56 +08:00
|
|
|
let selfval = self.at((k, j)) / pivot;
|
|
|
|
self.set((k, j), selfval);
|
|
|
|
}
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for j in range(0u, $dim) {
|
2013-08-05 15:44:56 +08:00
|
|
|
let resval = res.at((k, j)) / pivot;
|
|
|
|
res.set((k, j), resval);
|
|
|
|
}
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for l in range(0u, $dim) {
|
|
|
|
if l != k {
|
2013-08-05 15:44:56 +08:00
|
|
|
let normalizer = self.at((l, k));
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for j in range(k, $dim) {
|
2013-08-05 15:44:56 +08:00
|
|
|
let selfval = self.at((l, j)) - self.at((k, j)) * normalizer;
|
|
|
|
self.set((l, j), selfval);
|
|
|
|
}
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for j in range(0u, $dim) {
|
2013-08-05 15:44:56 +08:00
|
|
|
let resval = res.at((l, j)) - res.at((k, j)) * normalizer;
|
|
|
|
res.set((l, j), resval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
*self = res;
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
true
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! transpose_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: Clone> Transpose for $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-10-14 16:22:32 +08:00
|
|
|
fn transpose_cpy(m: &$t<N>) -> $t<N> {
|
|
|
|
let mut res = m.clone();
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
res.transpose();
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
res
|
|
|
|
}
|
2013-08-08 02:53:51 +08:00
|
|
|
|
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn transpose(&mut self) {
|
|
|
|
for i in range(1u, $dim) {
|
|
|
|
for j in range(0u, i) {
|
|
|
|
self.swap((i, j), (j, i))
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! approx_eq_impl(
|
|
|
|
($t: ident) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: ApproxEq<N>> ApproxEq<N> for $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2014-01-10 03:48:30 +08:00
|
|
|
fn approx_epsilon(_: Option<$t<N>>) -> N {
|
|
|
|
ApproxEq::approx_epsilon(None::<N>)
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2014-01-10 03:48:30 +08:00
|
|
|
fn approx_eq(a: &$t<N>, b: &$t<N>) -> bool {
|
|
|
|
let mut zip = a.iter().zip(b.iter());
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2014-01-10 03:48:30 +08:00
|
|
|
zip.all(|(a, b)| ApproxEq::approx_eq(a, b))
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2014-01-10 03:48:30 +08:00
|
|
|
fn approx_eq_eps(a: &$t<N>, b: &$t<N>, epsilon: &N) -> bool {
|
|
|
|
let mut zip = a.iter().zip(b.iter());
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2014-01-10 03:48:30 +08:00
|
|
|
zip.all(|(a, b)| ApproxEq::approx_eq_eps(a, b, epsilon))
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! to_homogeneous_impl(
|
|
|
|
($t: ident, $t2: ident, $dim: expr, $dim2: expr) => (
|
2014-01-21 06:40:32 +08:00
|
|
|
impl<N: Num + Clone> ToHomogeneous<$t2<N>> for $t<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2013-10-17 03:44:33 +08:00
|
|
|
fn to_homogeneous(m: &$t<N>) -> $t2<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
let mut res: $t2<N> = One::one();
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for i in range(0u, $dim) {
|
|
|
|
for j in range(0u, $dim) {
|
2013-10-17 03:44:33 +08:00
|
|
|
res.set((i, j), m.at((i, j)))
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! from_homogeneous_impl(
|
|
|
|
($t: ident, $t2: ident, $dim: expr, $dim2: expr) => (
|
2014-01-21 06:40:32 +08:00
|
|
|
impl<N: Num + Clone> FromHomogeneous<$t2<N>> for $t<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn from(m: &$t2<N>) -> $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
let mut res: $t<N> = One::one();
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for i in range(0u, $dim2) {
|
|
|
|
for j in range(0u, $dim2) {
|
|
|
|
res.set((i, j), m.at((i, j)))
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
// FIXME: do we have to deal the lost components
|
|
|
|
// (like if the 1 is not a 1… do we have to divide?)
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2013-08-12 22:45:31 +08:00
|
|
|
|
|
|
|
macro_rules! outer_impl(
|
|
|
|
($t: ident, $m: ident) => (
|
2014-01-21 06:40:32 +08:00
|
|
|
impl<N: Clone + Mul<N, N> + Zero> Outer<$m<N>> for $t<N> {
|
2013-08-12 22:45:31 +08:00
|
|
|
#[inline]
|
2013-10-17 03:44:33 +08:00
|
|
|
fn outer(a: &$t<N>, b: &$t<N>) -> $m<N> {
|
2013-08-28 20:22:12 +08:00
|
|
|
let mut res: $m<N> = Zero::zero();
|
2013-08-12 22:45:31 +08:00
|
|
|
|
2013-08-30 15:53:17 +08:00
|
|
|
for i in range(0u, Dim::dim(None::<$t<N>>)) {
|
|
|
|
for j in range(0u, Dim::dim(None::<$t<N>>)) {
|
2013-10-17 03:44:33 +08:00
|
|
|
res.set((i, j), a.at(i) * b.at(j))
|
2013-08-12 22:45:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|