2013-09-22 16:58:21 +08:00
|
|
|
//! Matrix with dimensions unknown at compile-time.
|
|
|
|
|
2013-09-13 16:53:59 +08:00
|
|
|
use std::rand::Rand;
|
|
|
|
use std::rand;
|
2013-06-02 02:50:00 +08:00
|
|
|
use std::num::{One, Zero};
|
2013-09-07 14:43:17 +08:00
|
|
|
use std::vec;
|
2013-06-02 02:50:00 +08:00
|
|
|
use std::cmp::ApproxEq;
|
2013-09-05 06:01:10 +08:00
|
|
|
use std::util;
|
2013-09-15 16:48:18 +08:00
|
|
|
use dvec::{DVec, DVecMulRhs};
|
2013-09-22 16:58:21 +08:00
|
|
|
use traits::operations::{Inv, Transpose};
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-09-05 06:01:10 +08:00
|
|
|
/// Matrix with dimensions unknown at compile-time.
|
2013-05-31 17:28:42 +08:00
|
|
|
#[deriving(Eq, ToStr, Clone)]
|
2013-08-05 16:13:44 +08:00
|
|
|
pub struct DMat<N> {
|
2013-09-05 06:01:10 +08:00
|
|
|
priv nrows: uint,
|
|
|
|
priv ncols: uint,
|
2013-08-05 15:44:56 +08:00
|
|
|
priv mij: ~[N]
|
2013-05-31 17:28:42 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 16:48:18 +08:00
|
|
|
/// Trait of object `o` which can be multiplied by a `DMat` `d`: `d * o`.
|
|
|
|
pub trait DMatMulRhs<N, Res> {
|
|
|
|
/// Multiplies a `DMat` by `Self`.
|
|
|
|
fn binop(left: &DMat<N>, right: &Self) -> Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<N, Rhs: DMatMulRhs<N, Res>, Res> Mul<Rhs, Res> for DMat<N> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn mul(&self, other: &Rhs) -> Res {
|
|
|
|
DMatMulRhs::binop(self, other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-13 17:11:04 +08:00
|
|
|
impl<N> DMat<N> {
|
|
|
|
/// Creates an uninitialized matrix.
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn new_uninitialized(nrows: uint, ncols: uint) -> DMat<N> {
|
|
|
|
let mut vec = vec::with_capacity(nrows * ncols);
|
|
|
|
vec::raw::set_len(&mut vec, nrows * ncols);
|
|
|
|
|
|
|
|
DMat {
|
|
|
|
nrows: nrows,
|
|
|
|
ncols: ncols,
|
|
|
|
mij: vec
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-05 06:01:10 +08:00
|
|
|
impl<N: Zero + Clone> DMat<N> {
|
|
|
|
/// Builds a matrix filled with zeros.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
/// * `dim` - The dimension of the matrix. A `dim`-dimensional matrix contains `dim * dim`
|
|
|
|
/// components.
|
|
|
|
#[inline]
|
|
|
|
pub fn new_zeros(nrows: uint, ncols: uint) -> DMat<N> {
|
2013-09-07 14:43:17 +08:00
|
|
|
DMat::from_elem(nrows, ncols, Zero::zero())
|
2013-09-05 06:01:10 +08:00
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-09-05 06:01:10 +08:00
|
|
|
/// Tests if all components of the matrix are zeroes.
|
|
|
|
#[inline]
|
|
|
|
pub fn is_zero(&self) -> bool {
|
|
|
|
self.mij.iter().all(|e| e.is_zero())
|
|
|
|
}
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-09-13 16:53:59 +08:00
|
|
|
impl<N: Rand> DMat<N> {
|
|
|
|
/// Builds a matrix filled with random values.
|
|
|
|
#[inline]
|
|
|
|
pub fn new_random(nrows: uint, ncols: uint) -> DMat<N> {
|
|
|
|
DMat::from_fn(nrows, ncols, |_, _| rand::random())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-07 14:43:17 +08:00
|
|
|
impl<N: One + Clone> DMat<N> {
|
|
|
|
/// Builds a matrix filled with a given constant.
|
|
|
|
#[inline]
|
|
|
|
pub fn new_ones(nrows: uint, ncols: uint) -> DMat<N> {
|
|
|
|
DMat::from_elem(nrows, ncols, One::one())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<N: Clone> DMat<N> {
|
|
|
|
/// Builds a matrix filled with a given constant.
|
|
|
|
#[inline]
|
|
|
|
pub fn from_elem(nrows: uint, ncols: uint, val: N) -> DMat<N> {
|
|
|
|
DMat {
|
|
|
|
nrows: nrows,
|
|
|
|
ncols: ncols,
|
|
|
|
mij: vec::from_elem(nrows * ncols, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<N> DMat<N> {
|
|
|
|
/// Builds a matrix filled with a given constant.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn from_fn(nrows: uint, ncols: uint, f: &fn(uint, uint) -> N) -> DMat<N> {
|
|
|
|
DMat {
|
|
|
|
nrows: nrows,
|
|
|
|
ncols: ncols,
|
|
|
|
mij: vec::from_fn(nrows * ncols, |i| { let m = i % ncols; f(m, m - i * ncols) })
|
|
|
|
}
|
|
|
|
}
|
2013-09-08 23:19:50 +08:00
|
|
|
|
|
|
|
/// The number of row on the matrix.
|
2013-09-18 20:22:29 +08:00
|
|
|
#[inline]
|
2013-09-08 23:19:50 +08:00
|
|
|
pub fn nrows(&self) -> uint {
|
|
|
|
self.nrows
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The number of columns on the matrix.
|
2013-09-18 20:22:29 +08:00
|
|
|
#[inline]
|
2013-09-08 23:19:50 +08:00
|
|
|
pub fn ncols(&self) -> uint {
|
|
|
|
self.ncols
|
|
|
|
}
|
2013-09-18 20:22:29 +08:00
|
|
|
|
|
|
|
/// Transforms this matrix into an array. This consumes the matrix and is O(1).
|
|
|
|
#[inline]
|
|
|
|
pub fn to_array(self) -> ~[N] {
|
|
|
|
self.mij
|
|
|
|
}
|
2013-09-07 14:43:17 +08:00
|
|
|
}
|
|
|
|
|
2013-09-05 06:01:10 +08:00
|
|
|
// FIXME: add a function to modify the dimension (to avoid useless allocations)?
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-09-05 06:01:10 +08:00
|
|
|
impl<N: One + Zero + Clone> DMat<N> {
|
|
|
|
/// Builds an identity matrix.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
/// * `dim` - The dimension of the matrix. A `dim`-dimensional matrix contains `dim * dim`
|
|
|
|
/// components.
|
|
|
|
#[inline]
|
|
|
|
pub fn new_identity(dim: uint) -> DMat<N> {
|
2013-09-13 17:11:04 +08:00
|
|
|
let mut res = DMat::new_zeros(dim, dim);
|
2013-09-05 06:01:10 +08:00
|
|
|
|
|
|
|
for i in range(0u, dim) {
|
|
|
|
let _1: N = One::one();
|
|
|
|
res.set(i, i, _1);
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
}
|
|
|
|
|
2013-09-05 06:01:10 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: Clone> DMat<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn offset(&self, i: uint, j: uint) -> uint {
|
2013-09-05 06:01:10 +08:00
|
|
|
i * self.ncols + j
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
|
|
|
|
/// Changes the value of a component of the matrix.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
2013-09-05 06:01:10 +08:00
|
|
|
/// * `row` - 0-based index of the line to be changed
|
|
|
|
/// * `col` - 0-based index of the column to be changed
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-09-05 06:01:10 +08:00
|
|
|
pub fn set(&mut self, row: uint, col: uint, val: N) {
|
|
|
|
assert!(row < self.nrows);
|
|
|
|
assert!(col < self.ncols);
|
|
|
|
self.mij[self.offset(row, col)] = val
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
|
|
|
|
2013-09-18 20:22:29 +08:00
|
|
|
/// Just like `set` without bounds checking.
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn set_fast(&mut self, row: uint, col: uint, val: N) {
|
|
|
|
let off = self.offset(row, col);
|
|
|
|
*self.mij.unsafe_mut_ref(off) = val
|
|
|
|
}
|
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
/// Reads the value of a component of the matrix.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
2013-09-05 06:01:10 +08:00
|
|
|
/// * `row` - 0-based index of the line to be read
|
|
|
|
/// * `col` - 0-based index of the column to be read
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-09-05 06:01:10 +08:00
|
|
|
pub fn at(&self, row: uint, col: uint) -> N {
|
|
|
|
assert!(row < self.nrows);
|
|
|
|
assert!(col < self.ncols);
|
2013-09-18 20:22:29 +08:00
|
|
|
unsafe { self.at_fast(row, col) }
|
2013-09-13 17:32:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Just like `at` without bounds checking.
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn at_fast(&self, row: uint, col: uint) -> N {
|
|
|
|
vec::raw::get(self.mij, self.offset(row, col))
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 16:48:18 +08:00
|
|
|
impl<N: Clone + Mul<N, N> + Add<N, N> + Zero> DMatMulRhs<N, DMat<N>> for DMat<N> {
|
|
|
|
fn binop(left: &DMat<N>, right: &DMat<N>) -> DMat<N> {
|
|
|
|
assert!(left.ncols == right.nrows);
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-09-15 16:48:18 +08:00
|
|
|
let mut res = unsafe { DMat::new_uninitialized(left.nrows, right.ncols) };
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-09-15 16:48:18 +08:00
|
|
|
for i in range(0u, left.nrows) {
|
|
|
|
for j in range(0u, right.ncols) {
|
2013-08-28 20:22:12 +08:00
|
|
|
let mut acc: N = Zero::zero();
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-09-18 20:22:29 +08:00
|
|
|
unsafe {
|
|
|
|
for k in range(0u, left.ncols) {
|
2013-09-15 16:48:18 +08:00
|
|
|
acc = acc + left.at_fast(i, k) * right.at_fast(k, j);
|
2013-09-13 17:32:30 +08:00
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-09-18 20:22:29 +08:00
|
|
|
res.set_fast(i, j, acc);
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
res
|
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
}
|
|
|
|
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
impl<N: Clone + Add<N, N> + Mul<N, N> + Zero>
|
2013-09-15 16:48:18 +08:00
|
|
|
DMatMulRhs<N, DVec<N>> for DVec<N> {
|
|
|
|
fn binop(left: &DMat<N>, right: &DVec<N>) -> DVec<N> {
|
|
|
|
assert!(left.ncols == right.at.len());
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-09-15 16:48:18 +08:00
|
|
|
let mut res : DVec<N> = unsafe { DVec::new_uninitialized(left.nrows) };
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-09-15 16:48:18 +08:00
|
|
|
for i in range(0u, left.nrows) {
|
2013-09-05 06:01:10 +08:00
|
|
|
let mut acc: N = Zero::zero();
|
|
|
|
|
2013-09-15 16:48:18 +08:00
|
|
|
for j in range(0u, left.ncols) {
|
2013-09-13 17:32:30 +08:00
|
|
|
unsafe {
|
2013-09-15 16:48:18 +08:00
|
|
|
acc = acc + left.at_fast(i, j) * right.at_fast(j);
|
2013-09-13 17:32:30 +08:00
|
|
|
}
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-09-05 06:01:10 +08:00
|
|
|
|
|
|
|
res.at[i] = acc;
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
res
|
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 16:48:18 +08:00
|
|
|
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
impl<N: Clone + Add<N, N> + Mul<N, N> + Zero>
|
2013-09-15 16:48:18 +08:00
|
|
|
DVecMulRhs<N, DVec<N>> for DMat<N> {
|
|
|
|
fn binop(left: &DVec<N>, right: &DMat<N>) -> DVec<N> {
|
|
|
|
assert!(right.nrows == left.at.len());
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-09-15 16:48:18 +08:00
|
|
|
let mut res : DVec<N> = unsafe { DVec::new_uninitialized(right.ncols) };
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-09-15 16:48:18 +08:00
|
|
|
for i in range(0u, right.ncols) {
|
2013-09-05 06:01:10 +08:00
|
|
|
let mut acc: N = Zero::zero();
|
|
|
|
|
2013-09-15 16:48:18 +08:00
|
|
|
for j in range(0u, right.nrows) {
|
2013-09-13 17:32:30 +08:00
|
|
|
unsafe {
|
2013-09-15 16:48:18 +08:00
|
|
|
acc = acc + left.at_fast(j) * right.at_fast(j, i);
|
2013-09-13 17:32:30 +08:00
|
|
|
}
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-09-05 06:01:10 +08:00
|
|
|
|
|
|
|
res.at[i] = acc;
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
res
|
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
}
|
|
|
|
|
Rework of the traits for Vectors.
The goal is to make traits less fine-grained for vectors, and reduce the amount of `use`.
- Scalar{Mul, Div} are removed, replaced by Mul<N, V> and Div<N, V>,
- Ring and DivisionRing are removed. Use Num instead.
- VectorSpace, Dot, and Norm are removed, replaced by the new, higher-level traits.
Add four traits:
- Vec: common operations on vectors. Replaces VectorSpace and Dot.
- AlgebraicVec: Vec + the old Norm trait.
- VecExt: Vec + every other traits vectors implement.
- AlgebraicVecExt: AlgebraicVec + VecExt.
2013-08-19 00:33:25 +08:00
|
|
|
impl<N: Clone + Num>
|
2013-08-05 16:13:44 +08:00
|
|
|
Inv for DMat<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn inverse(&self) -> Option<DMat<N>> {
|
2013-08-05 15:44:56 +08:00
|
|
|
let mut res : DMat<N> = self.clone();
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
if res.inplace_inverse() {
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
None
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
fn inplace_inverse(&mut self) -> bool {
|
2013-09-05 06:01:10 +08:00
|
|
|
assert!(self.nrows == self.ncols);
|
|
|
|
|
|
|
|
let dim = self.nrows;
|
|
|
|
let mut res: DMat<N> = DMat::new_identity(dim);
|
|
|
|
let _0T: N = Zero::zero();
|
2013-05-31 17:28:42 +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
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
while (n0 != dim) {
|
2013-09-18 20:22:29 +08:00
|
|
|
if unsafe { self.at_fast(n0, k) } != _0T {
|
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
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-18 20:22:29 +08:00
|
|
|
unsafe {
|
|
|
|
let pivot = self.at_fast(k, k);
|
2013-08-05 15:44:56 +08:00
|
|
|
|
2013-09-18 20:22:29 +08:00
|
|
|
for j in range(k, dim) {
|
|
|
|
let selfval = self.at_fast(k, j) / pivot;
|
|
|
|
self.set_fast(k, j, selfval);
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
|
2013-09-18 20:22:29 +08:00
|
|
|
for j in range(0u, dim) {
|
|
|
|
let resval = res.at_fast(k, j) / pivot;
|
|
|
|
res.set_fast(k, j, resval);
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
|
2013-09-18 20:22:29 +08:00
|
|
|
for l in range(0u, dim) {
|
|
|
|
if l != k {
|
|
|
|
let normalizer = self.at_fast(l, k);
|
2013-08-05 15:44:56 +08:00
|
|
|
|
2013-09-18 20:22:29 +08:00
|
|
|
for j in range(k, dim) {
|
|
|
|
let selfval = self.at_fast(l, j) - self.at_fast(k, j) * normalizer;
|
|
|
|
self.set_fast(l, j, selfval);
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
|
2013-09-18 20:22:29 +08:00
|
|
|
for j in range(0u, dim) {
|
|
|
|
let resval = res.at_fast(l, j) - res.at_fast(k, j) * normalizer;
|
|
|
|
res.set_fast(l, j, resval);
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
}
|
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
*self = res;
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
true
|
2013-05-31 17:28:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: Clone> Transpose for DMat<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn transposed(&self) -> DMat<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
let mut res = self.clone();
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
res.transpose();
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
res
|
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
fn transpose(&mut self) {
|
2013-09-05 06:01:10 +08:00
|
|
|
for i in range(1u, self.nrows) {
|
|
|
|
for j in range(0u, self.ncols - 1) {
|
2013-08-05 15:44:56 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2013-09-05 06:01:10 +08:00
|
|
|
|
|
|
|
util::swap(&mut self.nrows, &mut self.ncols);
|
2013-05-31 17:28:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: ApproxEq<N>> ApproxEq<N> for DMat<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn approx_epsilon() -> N {
|
2013-09-05 06:01:10 +08:00
|
|
|
fail!("This function cannot work due to a compiler bug.")
|
2013-08-28 20:22:12 +08:00
|
|
|
// let res: N = ApproxEq::<N>::approx_epsilon();
|
|
|
|
|
|
|
|
// res
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn approx_eq(&self, other: &DMat<N>) -> bool {
|
2013-08-05 15:44:56 +08:00
|
|
|
let mut zip = self.mij.iter().zip(other.mij.iter());
|
2013-06-09 20:09:22 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
do zip.all |(a, b)| {
|
|
|
|
a.approx_eq(b)
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn approx_eq_eps(&self, other: &DMat<N>, epsilon: &N) -> bool {
|
2013-08-05 15:44:56 +08:00
|
|
|
let mut zip = self.mij.iter().zip(other.mij.iter());
|
2013-06-09 20:09:22 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
do zip.all |(a, b)| {
|
|
|
|
a.approx_eq_eps(b, epsilon)
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-05-31 17:28:42 +08:00
|
|
|
}
|