2013-06-02 02:50:00 +08:00
|
|
|
use std::uint::iterate;
|
|
|
|
use std::num::{One, Zero};
|
2013-06-09 20:09:22 +08:00
|
|
|
use std::vec::{from_elem, swap};
|
2013-06-02 02:50:00 +08:00
|
|
|
use std::cmp::ApproxEq;
|
2013-06-09 20:09:22 +08:00
|
|
|
use std::iterator::IteratorUtil;
|
2013-05-31 17:28:42 +08:00
|
|
|
use traits::inv::Inv;
|
2013-06-01 00:35:48 +08:00
|
|
|
use traits::division_ring::DivisionRing;
|
2013-05-31 17:28:42 +08:00
|
|
|
use traits::transpose::Transpose;
|
|
|
|
use traits::workarounds::rlmul::{RMul, LMul};
|
|
|
|
use ndim::dvec::{DVec, zero_vec_with_dim};
|
|
|
|
|
|
|
|
#[deriving(Eq, ToStr, Clone)]
|
2013-06-10 07:36:47 +08:00
|
|
|
pub struct DMat<N>
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
|
|
|
dim: uint, // FIXME: handle more than just square matrices
|
2013-06-10 07:36:47 +08:00
|
|
|
mij: ~[N]
|
2013-05-31 17:28:42 +08:00
|
|
|
}
|
|
|
|
|
2013-06-10 07:36:47 +08:00
|
|
|
pub fn zero_mat_with_dim<N: Zero + Copy>(dim: uint) -> DMat<N>
|
2013-05-31 17:28:42 +08:00
|
|
|
{ DMat { dim: dim, mij: from_elem(dim * dim, Zero::zero()) } }
|
|
|
|
|
2013-06-10 07:36:47 +08:00
|
|
|
pub fn is_zero_mat<N: Zero>(mat: &DMat<N>) -> bool
|
2013-06-09 20:09:22 +08:00
|
|
|
{ mat.mij.all(|e| e.is_zero()) }
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-06-10 07:36:47 +08:00
|
|
|
pub fn one_mat_with_dim<N: Copy + One + Zero>(dim: uint) -> DMat<N>
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
|
|
|
let mut res = zero_mat_with_dim(dim);
|
2013-06-10 07:36:47 +08:00
|
|
|
let _1 = One::one::<N>();
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-06-02 02:50:00 +08:00
|
|
|
for iterate(0u, dim) |i|
|
2013-05-31 17:28:42 +08:00
|
|
|
{ res.set(i, i, &_1); }
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2013-06-10 07:36:47 +08:00
|
|
|
impl<N: Copy> DMat<N>
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
|
|
|
pub fn offset(&self, i: uint, j: uint) -> uint
|
|
|
|
{ i * self.dim + j }
|
|
|
|
|
2013-06-10 07:36:47 +08:00
|
|
|
pub fn set(&mut self, i: uint, j: uint, t: &N)
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
|
|
|
assert!(i < self.dim);
|
|
|
|
assert!(j < self.dim);
|
|
|
|
self.mij[self.offset(i, j)] = *t
|
|
|
|
}
|
2013-06-01 00:35:48 +08:00
|
|
|
|
2013-06-10 07:36:47 +08:00
|
|
|
pub fn at(&self, i: uint, j: uint) -> N
|
2013-06-01 00:35:48 +08:00
|
|
|
{
|
|
|
|
assert!(i < self.dim);
|
|
|
|
assert!(j < self.dim);
|
|
|
|
self.mij[self.offset(i, j)]
|
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
}
|
|
|
|
|
2013-06-10 07:36:47 +08:00
|
|
|
impl<N: Copy> Index<(uint, uint), N> for DMat<N>
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
2013-06-10 07:36:47 +08:00
|
|
|
fn index(&self, &(i, j): &(uint, uint)) -> N
|
2013-06-01 00:35:48 +08:00
|
|
|
{ self.at(i, j) }
|
2013-05-31 17:28:42 +08:00
|
|
|
}
|
|
|
|
|
2013-06-10 07:36:47 +08:00
|
|
|
impl<N: Copy + Mul<N, N> + Add<N, N> + Zero>
|
|
|
|
Mul<DMat<N>, DMat<N>> for DMat<N>
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
2013-06-10 07:36:47 +08:00
|
|
|
fn mul(&self, other: &DMat<N>) -> DMat<N>
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
|
|
|
assert!(self.dim == other.dim);
|
|
|
|
|
|
|
|
let dim = self.dim;
|
|
|
|
let mut res = zero_mat_with_dim(dim);
|
|
|
|
|
2013-06-02 02:50:00 +08:00
|
|
|
for iterate(0u, dim) |i|
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
2013-06-02 02:50:00 +08:00
|
|
|
for iterate(0u, dim) |j|
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
2013-06-10 07:36:47 +08:00
|
|
|
let mut acc = Zero::zero::<N>();
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-06-02 02:50:00 +08:00
|
|
|
for iterate(0u, dim) |k|
|
2013-06-01 00:35:48 +08:00
|
|
|
{ acc += self.at(i, k) * other.at(k, j); }
|
2013-05-31 17:28:42 +08:00
|
|
|
|
|
|
|
res.set(i, j, &acc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-10 07:36:47 +08:00
|
|
|
impl<N: Copy + Add<N, N> + Mul<N, N> + Zero>
|
|
|
|
RMul<DVec<N>> for DMat<N>
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
2013-06-10 07:36:47 +08:00
|
|
|
fn rmul(&self, other: &DVec<N>) -> DVec<N>
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
2013-06-09 20:09:22 +08:00
|
|
|
assert!(self.dim == other.at.len());
|
2013-05-31 17:28:42 +08:00
|
|
|
|
|
|
|
let dim = self.dim;
|
2013-06-10 07:36:47 +08:00
|
|
|
let mut res : DVec<N> = zero_vec_with_dim(dim);
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-06-02 02:50:00 +08:00
|
|
|
for iterate(0u, dim) |i|
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
2013-06-02 02:50:00 +08:00
|
|
|
for iterate(0u, dim) |j|
|
2013-06-01 00:35:48 +08:00
|
|
|
{ res.at[i] = res.at[i] + other.at[j] * self.at(i, j); }
|
2013-05-31 17:28:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-10 07:36:47 +08:00
|
|
|
impl<N: Copy + Add<N, N> + Mul<N, N> + Zero>
|
|
|
|
LMul<DVec<N>> for DMat<N>
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
2013-06-10 07:36:47 +08:00
|
|
|
fn lmul(&self, other: &DVec<N>) -> DVec<N>
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
2013-06-09 20:09:22 +08:00
|
|
|
assert!(self.dim == other.at.len());
|
2013-05-31 17:28:42 +08:00
|
|
|
|
|
|
|
let dim = self.dim;
|
2013-06-10 07:36:47 +08:00
|
|
|
let mut res : DVec<N> = zero_vec_with_dim(dim);
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-06-02 02:50:00 +08:00
|
|
|
for iterate(0u, dim) |i|
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
2013-06-02 02:50:00 +08:00
|
|
|
for iterate(0u, dim) |j|
|
2013-06-01 00:35:48 +08:00
|
|
|
{ res.at[i] = res.at[i] + other.at[j] * self.at(j, i); }
|
2013-05-31 17:28:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-10 07:36:47 +08:00
|
|
|
impl<N: Clone + Copy + Eq + DivisionRing>
|
|
|
|
Inv for DMat<N>
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
2013-06-10 07:36:47 +08:00
|
|
|
fn inverse(&self) -> DMat<N>
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
2013-06-10 07:36:47 +08:00
|
|
|
let mut res : DMat<N> = self.clone();
|
2013-05-31 17:28:42 +08:00
|
|
|
|
|
|
|
res.invert();
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn invert(&mut self)
|
|
|
|
{
|
|
|
|
let dim = self.dim;
|
2013-06-10 07:36:47 +08:00
|
|
|
let mut res = one_mat_with_dim::<N>(dim);
|
|
|
|
let _0T = Zero::zero::<N>();
|
2013-05-31 17:28:42 +08:00
|
|
|
|
|
|
|
// inversion using Gauss-Jordan elimination
|
2013-06-02 02:50:00 +08:00
|
|
|
for iterate(0u, dim) |k|
|
2013-05-31 17:28:42 +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?
|
|
|
|
|
2013-06-01 00:35:48 +08:00
|
|
|
let mut n0 = k; // index of a non-zero entry
|
2013-05-31 17:28:42 +08:00
|
|
|
|
|
|
|
while (n0 != dim)
|
|
|
|
{
|
2013-06-01 00:35:48 +08:00
|
|
|
if (self.at(n0, k) != _0T)
|
2013-05-31 17:28:42 +08:00
|
|
|
{ break; }
|
|
|
|
|
|
|
|
n0 += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(n0 != dim); // non inversible matrix
|
|
|
|
|
|
|
|
// swap pivot line
|
|
|
|
if (n0 != k)
|
|
|
|
{
|
2013-06-02 02:50:00 +08:00
|
|
|
for iterate(0u, dim) |j|
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
|
|
|
let off_n0_j = self.offset(n0, j);
|
|
|
|
let off_k_j = self.offset(k, j);
|
|
|
|
|
|
|
|
swap(self.mij, off_n0_j, off_k_j);
|
|
|
|
swap(res.mij, off_n0_j, off_k_j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-01 00:35:48 +08:00
|
|
|
let pivot = self.at(k, k);
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-06-02 02:50:00 +08:00
|
|
|
for iterate(k, dim) |j|
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
2013-06-01 00:35:48 +08:00
|
|
|
let selfval = &(self.at(k, j) / pivot);
|
2013-05-31 17:28:42 +08:00
|
|
|
self.set(k, j, selfval);
|
2013-06-01 00:35:48 +08:00
|
|
|
}
|
|
|
|
|
2013-06-02 02:50:00 +08:00
|
|
|
for iterate(0u, dim) |j|
|
2013-06-01 00:35:48 +08:00
|
|
|
{
|
|
|
|
let resval = &(res.at(k, j) / pivot);
|
2013-05-31 17:28:42 +08:00
|
|
|
res.set(k, j, resval);
|
|
|
|
}
|
|
|
|
|
2013-06-02 02:50:00 +08:00
|
|
|
for iterate(0u, dim) |l|
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
|
|
|
if (l != k)
|
|
|
|
{
|
2013-06-01 00:35:48 +08:00
|
|
|
let normalizer = self.at(l, k);
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-06-02 02:50:00 +08:00
|
|
|
for iterate(k, dim) |j|
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
2013-06-01 00:35:48 +08:00
|
|
|
let selfval = &(self.at(l, j) - self.at(k, j) * normalizer);
|
|
|
|
self.set(l, j, selfval);
|
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-06-02 02:50:00 +08:00
|
|
|
for iterate(0u, dim) |j|
|
2013-06-01 00:35:48 +08:00
|
|
|
{
|
|
|
|
let resval = &(res.at(l, j) - res.at(k, j) * normalizer);
|
|
|
|
res.set(l, j, resval);
|
2013-05-31 17:28:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-01 00:35:48 +08:00
|
|
|
|
|
|
|
*self = res;
|
2013-05-31 17:28:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-10 07:36:47 +08:00
|
|
|
impl<N:Copy> Transpose for DMat<N>
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
2013-06-10 07:36:47 +08:00
|
|
|
fn transposed(&self) -> DMat<N>
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
|
|
|
let mut res = copy *self;
|
|
|
|
|
|
|
|
res.transpose();
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn transpose(&mut self)
|
|
|
|
{
|
|
|
|
let dim = self.dim;
|
|
|
|
|
2013-06-02 02:50:00 +08:00
|
|
|
for iterate(1u, dim) |i|
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
2013-06-02 02:50:00 +08:00
|
|
|
for iterate(0u, dim - 1) |j|
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
|
|
|
let off_i_j = self.offset(i, j);
|
|
|
|
let off_j_i = self.offset(j, i);
|
|
|
|
|
|
|
|
swap(self.mij, off_i_j, off_j_i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-10 07:36:47 +08:00
|
|
|
impl<N: ApproxEq<N>> ApproxEq<N> for DMat<N>
|
2013-05-31 17:28:42 +08:00
|
|
|
{
|
2013-06-10 07:36:47 +08:00
|
|
|
fn approx_epsilon() -> N
|
|
|
|
{ ApproxEq::approx_epsilon::<N, N>() }
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-06-10 07:36:47 +08:00
|
|
|
fn approx_eq(&self, other: &DMat<N>) -> bool
|
2013-06-09 20:09:22 +08:00
|
|
|
{
|
|
|
|
let mut zip = self.mij.iter().zip(other.mij.iter());
|
|
|
|
|
|
|
|
do zip.all |(a, b)| { a.approx_eq(b) }
|
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-06-10 07:36:47 +08:00
|
|
|
fn approx_eq_eps(&self, other: &DMat<N>, epsilon: &N) -> bool
|
2013-06-09 20:09:22 +08:00
|
|
|
{
|
|
|
|
let mut zip = self.mij.iter().zip(other.mij.iter());
|
|
|
|
|
|
|
|
do zip.all |(a, b)| { a.approx_eq_eps(b, epsilon) }
|
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
}
|