Use slice and range syntax when possible.

This commit is contained in:
Sébastien Crozet 2015-01-09 22:46:26 +01:00
parent 2d4e1bfc95
commit 4b47b1e98a
12 changed files with 97 additions and 98 deletions

View File

@ -11,7 +11,7 @@ macro_rules! bench_mul_dmat(
let a: DMat<f64> = DMat::new_random($nrows, $ncols); let a: DMat<f64> = DMat::new_random($nrows, $ncols);
let mut b: DMat<f64> = DMat::new_random($nrows, $ncols); let mut b: DMat<f64> = DMat::new_random($nrows, $ncols);
for _ in range(0us, 1000) { for _ in (0us .. 1000) {
// XXX: the clone here is highly undesirable! // XXX: the clone here is highly undesirable!
b = a.clone() * b; b = a.clone() * b;
} }
@ -53,7 +53,7 @@ macro_rules! bench_mul_dmat_dvec(
let m : DMat<f64> = DMat::new_random($nrows, $ncols); let m : DMat<f64> = DMat::new_random($nrows, $ncols);
let mut v : DVec<f64> = DVec::new_random($ncols); let mut v : DVec<f64> = DVec::new_random($ncols);
for _ in range(0us, 1000) { for _ in (0us .. 1000) {
// XXX: the clone here is highly undesirable! // XXX: the clone here is highly undesirable!
v = m.clone() * v v = m.clone() * v
} }

View File

@ -81,6 +81,7 @@ Feel free to add your project to this list if you happen to use **nalgebra**!
#![deny(non_upper_case_globals)] #![deny(non_upper_case_globals)]
#![deny(unused_qualifications)] #![deny(unused_qualifications)]
#![deny(unused_results)] #![deny(unused_results)]
// #![allow(unstable)]
#![warn(missing_docs)] #![warn(missing_docs)]
#![feature(old_orphan_check)] #![feature(old_orphan_check)]
#![feature(unboxed_closures)] #![feature(unboxed_closures)]

View File

@ -22,8 +22,8 @@ pub fn householder_matrix<N, V, M>(dim: usize, start: usize, vec: V) -> M
assert!(dim >= stop); assert!(dim >= stop);
for j in range(start, stop) { for j in (start .. stop) {
for i in range(start, stop) { for i in (start .. stop) {
unsafe { unsafe {
let vv = vec.unsafe_at(i - start) * vec.unsafe_at(j - start); let vv = vec.unsafe_at(i - start) * vec.unsafe_at(j - start);
let qkij = qk.unsafe_at((i, j)); let qkij = qk.unsafe_at((i, j));
@ -50,7 +50,7 @@ pub fn qr<N, V, M>(m: &M) -> (M, M)
let iterations = min(rows - 1, cols); let iterations = min(rows - 1, cols);
for ite in range(0us, iterations) { for ite in (0us .. iterations) {
let mut v = r.col_slice(ite, ite, rows); let mut v = r.col_slice(ite, ite, rows);
let alpha = let alpha =
if unsafe { v.unsafe_at(ite) } >= ::zero() { if unsafe { v.unsafe_at(ite) } >= ::zero() {
@ -85,18 +85,18 @@ pub fn eigen_qr<N, V, VS, M>(m: &M, eps: &N, niter: usize) -> (M, V)
// let mut shifter: M = Eye::new_identity(rows); // let mut shifter: M = Eye::new_identity(rows);
let mut iter = 0us; let mut iter = 0us;
for _ in range(0, niter) { for _ in (0 .. niter) {
let mut stop = true; let mut stop = true;
for j in range(0, ::dim::<M>()) { for j in (0 .. ::dim::<M>()) {
for i in range(0, j) { for i in (0 .. j) {
if unsafe { eigenvalues.unsafe_at((i, j)) }.abs() >= *eps { if unsafe { eigenvalues.unsafe_at((i, j)) }.abs() >= *eps {
stop = false; stop = false;
break; break;
} }
} }
for i in range(j + 1, ::dim::<M>()) { for i in (j + 1 .. ::dim::<M>()) {
if unsafe { eigenvalues.unsafe_at((i, j)) }.abs() >= *eps { if unsafe { eigenvalues.unsafe_at((i, j)) }.abs() >= *eps {
stop = false; stop = false;
break; break;

View File

@ -131,7 +131,7 @@ impl<N> DMat<N> {
DMat { DMat {
nrows: nrows, nrows: nrows,
ncols: ncols, ncols: ncols,
mij: range(0, nrows * ncols).map(|i| { let m = i / nrows; f(i - m * nrows, m) }).collect() mij: (0 .. nrows * ncols).map(|i| { let m = i / nrows; f(i - m * nrows, m) }).collect()
} }
} }
@ -157,14 +157,14 @@ impl<N> DMat<N> {
/// Gets a reference to this matrix data. /// Gets a reference to this matrix data.
/// The returned vector contains the matrix data in column-major order. /// The returned vector contains the matrix data in column-major order.
#[inline] #[inline]
pub fn as_vec<'r>(&'r self) -> &'r [N] { pub fn as_vec(&self) -> &[N] {
self.mij.as_slice() &self.mij[]
} }
/// Gets a mutable reference to this matrix data. /// Gets a mutable reference to this matrix data.
/// The returned vector contains the matrix data in column-major order. /// The returned vector contains the matrix data in column-major order.
#[inline] #[inline]
pub fn as_mut_vec<'r>(&'r mut self) -> &'r mut [N] { pub fn as_mut_vec(&mut self) -> &mut [N] {
self.mij.as_mut_slice() self.mij.as_mut_slice()
} }
} }
@ -181,7 +181,7 @@ impl<N: One + Zero + Clone + Copy> Eye for DMat<N> {
fn new_identity(dim: usize) -> DMat<N> { fn new_identity(dim: usize) -> DMat<N> {
let mut res = DMat::new_zeros(dim, dim); let mut res = DMat::new_zeros(dim, dim);
for i in range(0us, dim) { for i in (0us .. dim) {
let _1: N = ::one(); let _1: N = ::one();
res[(i, i)] = _1; res[(i, i)] = _1;
} }
@ -238,7 +238,7 @@ impl<N: Copy> Indexable<(usize, usize), N> for DMat<N> {
unsafe fn unsafe_at(&self, rowcol: (usize, usize)) -> N { unsafe fn unsafe_at(&self, rowcol: (usize, usize)) -> N {
let (row, col) = rowcol; let (row, col) = rowcol;
*self.mij.as_slice().get_unchecked(self.offset(row, col)) *self.mij[].get_unchecked(self.offset(row, col))
} }
#[inline] #[inline]
@ -270,7 +270,7 @@ impl<N> Index<(usize, usize)> for DMat<N> {
assert!(j < self.ncols); assert!(j < self.ncols);
unsafe { unsafe {
self.mij.as_slice().get_unchecked(self.offset(i, j)) self.mij[].get_unchecked(self.offset(i, j))
} }
} }
} }
@ -298,12 +298,12 @@ impl<N: Copy + Mul<N, Output = N> + Add<N, Output = N> + Zero> Mul<DMat<N>> for
let mut res = unsafe { DMat::new_uninitialized(self.nrows, right.ncols) }; let mut res = unsafe { DMat::new_uninitialized(self.nrows, right.ncols) };
for i in range(0us, self.nrows) { for i in (0us .. self.nrows) {
for j in range(0us, right.ncols) { for j in (0us .. right.ncols) {
let mut acc: N = ::zero(); let mut acc: N = ::zero();
unsafe { unsafe {
for k in range(0us, self.ncols) { for k in (0us .. self.ncols) {
acc = acc acc = acc
+ self.unsafe_at((i, k)) * right.unsafe_at((k, j)); + self.unsafe_at((i, k)) * right.unsafe_at((k, j));
} }
@ -325,10 +325,10 @@ impl<N: Copy + Add<N, Output = N> + Mul<N, Output = N> + Zero> Mul<DVec<N>> for
let mut res : DVec<N> = unsafe { DVec::new_uninitialized(self.nrows) }; let mut res : DVec<N> = unsafe { DVec::new_uninitialized(self.nrows) };
for i in range(0us, self.nrows) { for i in (0us .. self.nrows) {
let mut acc: N = ::zero(); let mut acc: N = ::zero();
for j in range(0us, self.ncols) { for j in (0us .. self.ncols) {
unsafe { unsafe {
acc = acc + self.unsafe_at((i, j)) * right.unsafe_at(j); acc = acc + self.unsafe_at((i, j)) * right.unsafe_at(j);
} }
@ -350,10 +350,10 @@ impl<N: Copy + Add<N, Output = N> + Mul<N, Output = N> + Zero> Mul<DMat<N>> for
let mut res : DVec<N> = unsafe { DVec::new_uninitialized(right.ncols) }; let mut res : DVec<N> = unsafe { DVec::new_uninitialized(right.ncols) };
for i in range(0us, right.ncols) { for i in (0us .. right.ncols) {
let mut acc: N = ::zero(); let mut acc: N = ::zero();
for j in range(0us, right.nrows) { for j in (0us .. right.nrows) {
unsafe { unsafe {
acc = acc + self.unsafe_at(j) * right.unsafe_at((j, i)); acc = acc + self.unsafe_at(j) * right.unsafe_at((j, i));
} }
@ -385,7 +385,7 @@ impl<N: BaseNum + Clone> Inv for DMat<N> {
let mut res: DMat<N> = Eye::new_identity(dim); let mut res: DMat<N> = Eye::new_identity(dim);
// inversion using Gauss-Jordan elimination // inversion using Gauss-Jordan elimination
for k in range(0us, dim) { for k in (0us .. dim) {
// search a non-zero value on the k-th column // search a non-zero value on the k-th column
// FIXME: would it be worth it to spend some more time searching for the // FIXME: would it be worth it to spend some more time searching for the
// max instead? // max instead?
@ -406,7 +406,7 @@ impl<N: BaseNum + Clone> Inv for DMat<N> {
// swap pivot line // swap pivot line
if n0 != k { if n0 != k {
for j in range(0us, dim) { for j in (0us .. dim) {
let off_n0_j = self.offset(n0, j); let off_n0_j = self.offset(n0, j);
let off_k_j = self.offset(k, j); let off_k_j = self.offset(k, j);
@ -418,26 +418,26 @@ impl<N: BaseNum + Clone> Inv for DMat<N> {
unsafe { unsafe {
let pivot = self.unsafe_at((k, k)); let pivot = self.unsafe_at((k, k));
for j in range(k, dim) { for j in (k .. dim) {
let selfval = self.unsafe_at((k, j)) / pivot; let selfval = self.unsafe_at((k, j)) / pivot;
self.unsafe_set((k, j), selfval); self.unsafe_set((k, j), selfval);
} }
for j in range(0us, dim) { for j in (0us .. dim) {
let resval = res.unsafe_at((k, j)) / pivot; let resval = res.unsafe_at((k, j)) / pivot;
res.unsafe_set((k, j), resval); res.unsafe_set((k, j), resval);
} }
for l in range(0us, dim) { for l in (0us .. dim) {
if l != k { if l != k {
let normalizer = self.unsafe_at((l, k)); let normalizer = self.unsafe_at((l, k));
for j in range(k, dim) { for j in (k .. dim) {
let selfval = self.unsafe_at((l, j)) - self.unsafe_at((k, j)) * normalizer; let selfval = self.unsafe_at((l, j)) - self.unsafe_at((k, j)) * normalizer;
self.unsafe_set((l, j), selfval); self.unsafe_set((l, j), selfval);
} }
for j in range(0us, dim) { for j in (0us .. dim) {
let resval = res.unsafe_at((l, j)) - res.unsafe_at((k, j)) * normalizer; let resval = res.unsafe_at((l, j)) - res.unsafe_at((k, j)) * normalizer;
res.unsafe_set((l, j), resval); res.unsafe_set((l, j), resval);
} }
@ -465,8 +465,8 @@ impl<N: Clone + Copy> Transpose for DMat<N> {
else { else {
let mut res = unsafe { DMat::new_uninitialized(self.ncols, self.nrows) }; let mut res = unsafe { DMat::new_uninitialized(self.ncols, self.nrows) };
for i in range(0us, self.nrows) { for i in (0us .. self.nrows) {
for j in range(0us, self.ncols) { for j in (0us .. self.ncols) {
unsafe { unsafe {
res.unsafe_set((j, i), self.unsafe_at((i, j))) res.unsafe_set((j, i), self.unsafe_at((i, j)))
} }
@ -480,8 +480,8 @@ impl<N: Clone + Copy> Transpose for DMat<N> {
#[inline] #[inline]
fn transpose(&mut self) { fn transpose(&mut self) {
if self.nrows == self.ncols { if self.nrows == self.ncols {
for i in range(1us, self.nrows) { for i in (1us .. self.nrows) {
for j in range(0us, self.ncols - 1) { for j in (0us .. self.ncols - 1) {
let off_i_j = self.offset(i, j); let off_i_j = self.offset(i, j);
let off_j_i = self.offset(j, i); let off_j_i = self.offset(j, i);
@ -503,8 +503,8 @@ impl<N: BaseNum + Cast<f64> + Clone> Mean<DVec<N>> for DMat<N> {
let mut res: DVec<N> = DVec::new_zeros(self.ncols); let mut res: DVec<N> = DVec::new_zeros(self.ncols);
let normalizer: N = Cast::from(1.0f64 / Cast::from(self.nrows)); let normalizer: N = Cast::from(1.0f64 / Cast::from(self.nrows));
for i in range(0us, self.nrows) { for i in (0us .. self.nrows) {
for j in range(0us, self.ncols) { for j in (0us .. self.ncols) {
unsafe { unsafe {
let acc = res.unsafe_at(j) + self.unsafe_at((i, j)) * normalizer; let acc = res.unsafe_at(j) + self.unsafe_at((i, j)) * normalizer;
res.unsafe_set(j, acc); res.unsafe_set(j, acc);
@ -525,8 +525,8 @@ impl<N: BaseNum + Cast<f64> + Clone> Cov<DMat<N>> for DMat<N> {
let mean = self.mean(); let mean = self.mean();
// FIXME: use the rows iterator when available // FIXME: use the rows iterator when available
for i in range(0us, self.nrows) { for i in (0us .. self.nrows) {
for j in range(0us, self.ncols) { for j in (0us .. self.ncols) {
unsafe { unsafe {
centered.unsafe_set((i, j), self.unsafe_at((i, j)) - mean.unsafe_at(j)); centered.unsafe_set((i, j), self.unsafe_at((i, j)) - mean.unsafe_at(j));
} }
@ -549,8 +549,7 @@ impl<N: Copy + Clone> ColSlice<DVec<N>> for DMat<N> {
// we can init from slice thanks to the matrix being column major // we can init from slice thanks to the matrix being column major
let start= self.offset(row_start, col_id); let start= self.offset(row_start, col_id);
let stop = self.offset(row_end, col_id); let stop = self.offset(row_end, col_id);
let slice = DVec::from_slice( let slice = DVec::from_slice(row_end - row_start, &self.mij[start .. stop]);
row_end - row_start, self.mij.slice(start, stop));
slice slice
} }
} }
@ -564,7 +563,7 @@ impl<N: Copy> RowSlice<DVec<N>> for DMat<N> {
DVec::new_uninitialized(self.nrows) DVec::new_uninitialized(self.nrows)
}; };
let mut slice_idx = 0us; let mut slice_idx = 0us;
for col_id in range(col_start, col_end) { for col_id in (col_start .. col_end) {
unsafe { unsafe {
slice.unsafe_set(slice_idx, self.unsafe_at((row_id, col_id))); slice.unsafe_set(slice_idx, self.unsafe_at((row_id, col_id)));
} }
@ -590,7 +589,7 @@ impl<N: Copy + Clone + Zero> Diag<DVec<N>> for DMat<N> {
assert!(diag.len() == smallest_dim); assert!(diag.len() == smallest_dim);
for i in range(0, smallest_dim) { for i in (0 .. smallest_dim) {
unsafe { self.unsafe_set((i, i), diag.unsafe_at(i)) } unsafe { self.unsafe_set((i, i), diag.unsafe_at(i)) }
} }
} }
@ -601,7 +600,7 @@ impl<N: Copy + Clone + Zero> Diag<DVec<N>> for DMat<N> {
let mut diag: DVec<N> = DVec::new_zeros(smallest_dim); let mut diag: DVec<N> = DVec::new_zeros(smallest_dim);
for i in range(0, smallest_dim) { for i in (0 .. smallest_dim) {
unsafe { diag.unsafe_set(i, self.unsafe_at((i, i))) } unsafe { diag.unsafe_set(i, self.unsafe_at((i, i))) }
} }
@ -635,8 +634,8 @@ impl<N: ApproxEq<N>> ApproxEq<N> for DMat<N> {
impl<N: Show + Copy + String> Show for DMat<N> { impl<N: Show + Copy + String> Show for DMat<N> {
fn fmt(&self, form:&mut Formatter) -> Result { fn fmt(&self, form:&mut Formatter) -> Result {
for i in range(0us, self.nrows()) { for i in (0us .. self.nrows()) {
for j in range(0us, self.ncols()) { for j in (0us .. self.ncols()) {
let _ = write!(form, "{} ", self[(i, j)]); let _ = write!(form, "{} ", self[(i, j)]);
} }
let _ = write!(form, "\n"); let _ = write!(form, "\n");

View File

@ -47,7 +47,7 @@ impl<N: Clone> DVec<N> {
assert!(dim <= vec.len()); assert!(dim <= vec.len());
DVec { DVec {
at: vec.slice_to(dim).to_vec() at: vec[.. dim].to_vec()
} }
} }
} }
@ -56,7 +56,7 @@ impl<N> DVec<N> {
/// Builds a vector filled with the result of a function. /// Builds a vector filled with the result of a function.
#[inline(always)] #[inline(always)]
pub fn from_fn<F: FnMut(usize) -> N>(dim: usize, mut f: F) -> DVec<N> { pub fn from_fn<F: FnMut(usize) -> N>(dim: usize, mut f: F) -> DVec<N> {
DVec { at: range(0, dim).map(|i| f(i)).collect() } DVec { at: (0 .. dim).map(|i| f(i)).collect() }
} }
#[inline] #[inline]

View File

@ -130,7 +130,7 @@ macro_rules! dvec_impl(
fn axpy(&mut self, a: &N, x: &$dvec<N>) { fn axpy(&mut self, a: &N, x: &$dvec<N>) {
assert!(self.len() == x.len()); assert!(self.len() == x.len());
for i in range(0, x.len()) { for i in (0 .. x.len()) {
unsafe { unsafe {
let self_i = self.unsafe_at(i); let self_i = self.unsafe_at(i);
self.unsafe_set(i, self_i + *a * x.unsafe_at(i)) self.unsafe_set(i, self_i + *a * x.unsafe_at(i))
@ -146,7 +146,7 @@ macro_rules! dvec_impl(
pub fn canonical_basis_with_dim(dim: usize) -> Vec<$dvec<N>> { pub fn canonical_basis_with_dim(dim: usize) -> Vec<$dvec<N>> {
let mut res : Vec<$dvec<N>> = Vec::new(); let mut res : Vec<$dvec<N>> = Vec::new();
for i in range(0us, dim) { for i in (0us .. dim) {
let mut basis_element : $dvec<N> = $dvec::new_zeros(dim); let mut basis_element : $dvec<N> = $dvec::new_zeros(dim);
basis_element.set(i, ::one()); basis_element.set(i, ::one());
@ -165,7 +165,7 @@ macro_rules! dvec_impl(
let dim = self.len(); let dim = self.len();
let mut res : Vec<$dvec<N>> = Vec::new(); let mut res : Vec<$dvec<N>> = Vec::new();
for i in range(0us, dim) { for i in (0us .. dim) {
let mut basis_element : $dvec<N> = $dvec::new_zeros(self.len()); let mut basis_element : $dvec<N> = $dvec::new_zeros(self.len());
basis_element.set(i, ::one()); basis_element.set(i, ::one());
@ -276,7 +276,7 @@ macro_rules! dvec_impl(
fn dot(&self, other: &$dvec<N>) -> N { fn dot(&self, other: &$dvec<N>) -> N {
assert!(self.len() == other.len()); assert!(self.len() == other.len());
let mut res: N = ::zero(); let mut res: N = ::zero();
for i in range(0us, self.len()) { for i in (0us .. self.len()) {
res = res + unsafe { self.unsafe_at(i) * other.unsafe_at(i) }; res = res + unsafe { self.unsafe_at(i) * other.unsafe_at(i) };
} }
res res
@ -486,7 +486,7 @@ macro_rules! small_dvec_from_impl (
let mut at: [N; $dim] = [ $( $zeros, )* ]; let mut at: [N; $dim] = [ $( $zeros, )* ];
for i in range(0, dim) { for i in (0 .. dim) {
at[i] = f(i); at[i] = f(i);
} }

View File

@ -419,7 +419,7 @@ macro_rules! diag_impl(
#[inline] #[inline]
fn set_diag(&mut self, diag: &$tv<N>) { fn set_diag(&mut self, diag: &$tv<N>) {
for i in range(0, $dim) { for i in (0 .. $dim) {
unsafe { self.unsafe_set((i, i), diag.unsafe_at(i)) } unsafe { self.unsafe_set((i, i), diag.unsafe_at(i)) }
} }
} }
@ -428,7 +428,7 @@ macro_rules! diag_impl(
fn diag(&self) -> $tv<N> { fn diag(&self) -> $tv<N> {
let mut diag: $tv<N> = ::zero(); let mut diag: $tv<N> = ::zero();
for i in range(0, $dim) { for i in (0 .. $dim) {
unsafe { diag.unsafe_set(i, self.unsafe_at((i, i))) } unsafe { diag.unsafe_set(i, self.unsafe_at((i, i))) }
} }
@ -447,12 +447,12 @@ macro_rules! mat_mul_mat_impl(
// careful! we need to comute other * self here (self is the rhs). // careful! we need to comute other * self here (self is the rhs).
let mut res: $t<N> = ::zero(); let mut res: $t<N> = ::zero();
for i in range(0us, $dim) { for i in (0us .. $dim) {
for j in range(0us, $dim) { for j in (0us .. $dim) {
let mut acc: N = ::zero(); let mut acc: N = ::zero();
unsafe { unsafe {
for k in range(0us, $dim) { for k in (0us .. $dim) {
acc = acc + self.at_fast((i, k)) * right.at_fast((k, j)); acc = acc + self.at_fast((i, k)) * right.at_fast((k, j));
} }
@ -476,8 +476,8 @@ macro_rules! vec_mul_mat_impl(
fn mul(self, right: $t<N>) -> $v<N> { fn mul(self, right: $t<N>) -> $v<N> {
let mut res : $v<N> = $zero(); let mut res : $v<N> = $zero();
for i in range(0us, $dim) { for i in (0us .. $dim) {
for j in range(0us, $dim) { for j in (0us .. $dim) {
unsafe { unsafe {
let val = res.at_fast(i) + self.at_fast(j) * right.at_fast((j, i)); let val = res.at_fast(i) + self.at_fast(j) * right.at_fast((j, i));
res.set_fast(i, val) res.set_fast(i, val)
@ -500,8 +500,8 @@ macro_rules! mat_mul_vec_impl(
fn mul(self, right: $v<N>) -> $v<N> { fn mul(self, right: $v<N>) -> $v<N> {
let mut res : $v<N> = $zero(); let mut res : $v<N> = $zero();
for i in range(0us, $dim) { for i in (0us .. $dim) {
for j in range(0us, $dim) { for j in (0us .. $dim) {
unsafe { unsafe {
let val = res.at_fast(i) + self.at_fast((i, j)) * right.at_fast(j); let val = res.at_fast(i) + self.at_fast((i, j)) * right.at_fast(j);
res.set_fast(i, val) res.set_fast(i, val)
@ -546,7 +546,7 @@ macro_rules! inv_impl(
let mut res: $t<N> = ::one(); let mut res: $t<N> = ::one();
// inversion using Gauss-Jordan elimination // inversion using Gauss-Jordan elimination
for k in range(0us, $dim) { for k in (0us .. $dim) {
// search a non-zero value on the k-th column // search a non-zero value on the k-th column
// FIXME: would it be worth it to spend some more time searching for the // FIXME: would it be worth it to spend some more time searching for the
// max instead? // max instead?
@ -567,7 +567,7 @@ macro_rules! inv_impl(
// swap pivot line // swap pivot line
if n0 != k { if n0 != k {
for j in range(0us, $dim) { for j in (0us .. $dim) {
self.swap((n0, j), (k, j)); self.swap((n0, j), (k, j));
res.swap((n0, j), (k, j)); res.swap((n0, j), (k, j));
} }
@ -575,26 +575,26 @@ macro_rules! inv_impl(
let pivot = self.at((k, k)); let pivot = self.at((k, k));
for j in range(k, $dim) { for j in (k .. $dim) {
let selfval = self.at((k, j)) / pivot; let selfval = self.at((k, j)) / pivot;
self.set((k, j), selfval); self.set((k, j), selfval);
} }
for j in range(0us, $dim) { for j in (0us .. $dim) {
let resval = res.at((k, j)) / pivot; let resval = res.at((k, j)) / pivot;
res.set((k, j), resval); res.set((k, j), resval);
} }
for l in range(0us, $dim) { for l in (0us .. $dim) {
if l != k { if l != k {
let normalizer = self.at((l, k)); let normalizer = self.at((l, k));
for j in range(k, $dim) { for j in (k .. $dim) {
let selfval = self.at((l, j)) - self.at((k, j)) * normalizer; let selfval = self.at((l, j)) - self.at((k, j)) * normalizer;
self.set((l, j), selfval); self.set((l, j), selfval);
} }
for j in range(0us, $dim) { for j in (0us .. $dim) {
let resval = res.at((l, j)) - res.at((k, j)) * normalizer; let resval = res.at((l, j)) - res.at((k, j)) * normalizer;
res.set((l, j), resval); res.set((l, j), resval);
} }
@ -623,8 +623,8 @@ macro_rules! transpose_impl(
#[inline] #[inline]
fn transpose(&mut self) { fn transpose(&mut self) {
for i in range(1us, $dim) { for i in (1us .. $dim) {
for j in range(0us, i) { for j in (0us .. i) {
self.swap((i, j), (j, i)) self.swap((i, j), (j, i))
} }
} }
@ -668,8 +668,8 @@ macro_rules! to_homogeneous_impl(
fn to_homogeneous(&self) -> $t2<N> { fn to_homogeneous(&self) -> $t2<N> {
let mut res: $t2<N> = ::one(); let mut res: $t2<N> = ::one();
for i in range(0us, $dim) { for i in (0us .. $dim) {
for j in range(0us, $dim) { for j in (0us .. $dim) {
res.set((i, j), self.at((i, j))) res.set((i, j), self.at((i, j)))
} }
} }
@ -687,8 +687,8 @@ macro_rules! from_homogeneous_impl(
fn from(m: &$t2<N>) -> $t<N> { fn from(m: &$t2<N>) -> $t<N> {
let mut res: $t<N> = ::one(); let mut res: $t<N> = ::one();
for i in range(0us, $dim2) { for i in (0us .. $dim2) {
for j in range(0us, $dim2) { for j in (0us .. $dim2) {
res.set((i, j), m.at((i, j))) res.set((i, j), m.at((i, j)))
} }
} }
@ -708,8 +708,8 @@ macro_rules! outer_impl(
#[inline] #[inline]
fn outer(&self, other: &$t<N>) -> $m<N> { fn outer(&self, other: &$t<N>) -> $m<N> {
let mut res: $m<N> = ::zero(); let mut res: $m<N> = ::zero();
for i in range(0us, Dim::dim(None::<$t<N>>)) { for i in (0us .. Dim::dim(None::<$t<N>>)) {
for j in range(0us, Dim::dim(None::<$t<N>>)) { for j in (0us .. Dim::dim(None::<$t<N>>)) {
res.set((i, j), self.at(i) * other.at(j)) res.set((i, j), self.at(i) * other.at(j))
} }
} }

View File

@ -270,8 +270,7 @@ Rotation<Vec3<N>> for Rot3<N> {
} }
} }
impl<N: Clone + Rand + BaseFloat> impl<N: Clone + Rand + BaseFloat> Rand for Rot3<N> {
Rand for Rot3<N> {
#[inline] #[inline]
fn rand<R: Rng>(rng: &mut R) -> Rot3<N> { fn rand<R: Rng>(rng: &mut R) -> Rot3<N> {
Rot3::new(rng.gen()) Rot3::new(rng.gen())

View File

@ -310,7 +310,7 @@ macro_rules! basis_impl(
impl<N: Copy + BaseFloat + ApproxEq<N>> Basis for $t<N> { impl<N: Copy + BaseFloat + ApproxEq<N>> Basis for $t<N> {
#[inline] #[inline]
fn canonical_basis<F: FnMut($t<N>) -> bool>(mut f: F) { fn canonical_basis<F: FnMut($t<N>) -> bool>(mut f: F) {
for i in range(0us, $dim) { for i in (0us .. $dim) {
if !f(Basis::canonical_basis_element(i).unwrap()) { return } if !f(Basis::canonical_basis_element(i).unwrap()) { return }
} }
} }
@ -321,7 +321,7 @@ macro_rules! basis_impl(
// orthogonalization algorithm // orthogonalization algorithm
let mut basis: Vec<$t<N>> = Vec::new(); let mut basis: Vec<$t<N>> = Vec::new();
for i in range(0us, $dim) { for i in (0us .. $dim) {
let mut basis_element : $t<N> = ::zero(); let mut basis_element : $t<N> = ::zero();
unsafe { unsafe {

View File

@ -6,7 +6,7 @@ use na::{Vec1, Vec3, Mat1, Mat2, Mat3, Mat4, Mat5, Mat6, Rot3, Persp3, PerspMat3
macro_rules! test_inv_mat_impl( macro_rules! test_inv_mat_impl(
($t: ty) => ( ($t: ty) => (
for _ in range(0us, 10000) { for _ in (0us .. 10000) {
let randmat : $t = random(); let randmat : $t = random();
match na::inv(&randmat) { match na::inv(&randmat) {
@ -19,7 +19,7 @@ macro_rules! test_inv_mat_impl(
macro_rules! test_transpose_mat_impl( macro_rules! test_transpose_mat_impl(
($t: ty) => ( ($t: ty) => (
for _ in range(0us, 10000) { for _ in (0us .. 10000) {
let randmat : $t = random(); let randmat : $t = random();
assert!(na::transpose(&na::transpose(&randmat)) == randmat); assert!(na::transpose(&na::transpose(&randmat)) == randmat);
@ -29,7 +29,7 @@ macro_rules! test_transpose_mat_impl(
macro_rules! test_qr_impl( macro_rules! test_qr_impl(
($t: ty) => ( ($t: ty) => (
for _ in range(0us, 10000) { for _ in (0us .. 10000) {
let randmat : $t = random(); let randmat : $t = random();
let (q, r) = na::qr(&randmat); let (q, r) = na::qr(&randmat);
@ -43,7 +43,7 @@ macro_rules! test_qr_impl(
// NOTE: deactivated untile we get a better convergence rate. // NOTE: deactivated untile we get a better convergence rate.
// macro_rules! test_eigen_qr_impl( // macro_rules! test_eigen_qr_impl(
// ($t: ty) => { // ($t: ty) => {
// for _ in range(0us, 10000) { // for _ in (0us .. 10000) {
// let randmat : $t = random(); // let randmat : $t = random();
// // Make it symetric so that we can recompose the matrix to test at the end. // // Make it symetric so that we can recompose the matrix to test at the end.
// let randmat = na::transpose(&randmat) * randmat; // let randmat = na::transpose(&randmat) * randmat;
@ -125,7 +125,7 @@ fn test_inv_mat6() {
#[test] #[test]
fn test_rotation2() { fn test_rotation2() {
for _ in range(0us, 10000) { for _ in (0us .. 10000) {
let randmat: na::Rot2<f64> = na::one(); let randmat: na::Rot2<f64> = na::one();
let ang = Vec1::new(na::abs(&random::<f64>()) % BaseFloat::pi()); let ang = Vec1::new(na::abs(&random::<f64>()) % BaseFloat::pi());
@ -142,7 +142,7 @@ fn test_index_mat2() {
#[test] #[test]
fn test_inv_rotation3() { fn test_inv_rotation3() {
for _ in range(0us, 10000) { for _ in (0us .. 10000) {
let randmat: Rot3<f64> = na::one(); let randmat: Rot3<f64> = na::one();
let dir: Vec3<f64> = random(); let dir: Vec3<f64> = random();
let ang = na::normalize(&dir) * (na::abs(&random::<f64>()) % BaseFloat::pi()); let ang = na::normalize(&dir) * (na::abs(&random::<f64>()) % BaseFloat::pi());
@ -250,7 +250,7 @@ fn test_dmat_from_vec() {
/* FIXME: review qr decomposition to make it work with DMat. /* FIXME: review qr decomposition to make it work with DMat.
#[test] #[test]
fn test_qr() { fn test_qr() {
for _ in range(0us, 10) { for _ in (0us .. 10) {
let dim1: usize = random(); let dim1: usize = random();
let dim2: usize = random(); let dim2: usize = random();
let rows = min(40, max(dim1, dim2)); let rows = min(40, max(dim1, dim2));

View File

@ -5,7 +5,7 @@ use std::rand::random;
#[test] #[test]
fn test_quat_as_mat() { fn test_quat_as_mat() {
for _ in range(0us, 10000) { for _ in (0us .. 10000) {
let axis_angle: Vec3<f64> = random(); let axis_angle: Vec3<f64> = random();
assert!(na::approx_eq(&UnitQuat::new(axis_angle).to_rot(), &Rot3::new(axis_angle))) assert!(na::approx_eq(&UnitQuat::new(axis_angle).to_rot(), &Rot3::new(axis_angle)))
@ -14,7 +14,7 @@ fn test_quat_as_mat() {
#[test] #[test]
fn test_quat_mul_vec_or_pnt_as_mat() { fn test_quat_mul_vec_or_pnt_as_mat() {
for _ in range(0us, 10000) { for _ in (0us .. 10000) {
let axis_angle: Vec3<f64> = random(); let axis_angle: Vec3<f64> = random();
let vec: Vec3<f64> = random(); let vec: Vec3<f64> = random();
let pnt: Pnt3<f64> = random(); let pnt: Pnt3<f64> = random();
@ -31,7 +31,7 @@ fn test_quat_mul_vec_or_pnt_as_mat() {
#[test] #[test]
fn test_quat_div_quat() { fn test_quat_div_quat() {
for _ in range(0us, 10000) { for _ in (0us .. 10000) {
let axis_angle1: Vec3<f64> = random(); let axis_angle1: Vec3<f64> = random();
let axis_angle2: Vec3<f64> = random(); let axis_angle2: Vec3<f64> = random();
@ -47,7 +47,7 @@ fn test_quat_div_quat() {
#[test] #[test]
fn test_quat_to_axis_angle() { fn test_quat_to_axis_angle() {
for _ in range(0us, 10000) { for _ in (0us .. 10000) {
let axis_angle: Vec3<f64> = random(); let axis_angle: Vec3<f64> = random();
let q = UnitQuat::new(axis_angle); let q = UnitQuat::new(axis_angle);
@ -59,7 +59,7 @@ fn test_quat_to_axis_angle() {
#[test] #[test]
fn test_quat_euler_angles() { fn test_quat_euler_angles() {
for _ in range(0us, 10000) { for _ in (0us .. 10000) {
let angles: Vec3<f64> = random(); let angles: Vec3<f64> = random();
let q = UnitQuat::new_with_euler_angles(angles.x, angles.y, angles.z); let q = UnitQuat::new_with_euler_angles(angles.x, angles.y, angles.z);

View File

@ -5,7 +5,7 @@ use na::{Vec0, Vec1, Vec2, Vec3, Vec4, Vec5, Vec6, Mat3, Iterable, IterableMut};
macro_rules! test_iterator_impl( macro_rules! test_iterator_impl(
($t: ty, $n: ty) => ( ($t: ty, $n: ty) => (
for _ in range(0us, 10000) { for _ in (0us .. 10000) {
let v: $t = random(); let v: $t = random();
let mut mv: $t = v.clone(); let mut mv: $t = v.clone();
let n: $n = random(); let n: $n = random();
@ -23,7 +23,7 @@ macro_rules! test_iterator_impl(
macro_rules! test_commut_dot_impl( macro_rules! test_commut_dot_impl(
($t: ty) => ( ($t: ty) => (
for _ in range(0us, 10000) { for _ in (0us .. 10000) {
let v1 : $t = random(); let v1 : $t = random();
let v2 : $t = random(); let v2 : $t = random();
@ -34,7 +34,7 @@ macro_rules! test_commut_dot_impl(
macro_rules! test_scalar_op_impl( macro_rules! test_scalar_op_impl(
($t: ty, $n: ty) => ( ($t: ty, $n: ty) => (
for _ in range(0us, 10000) { for _ in (0us .. 10000) {
let v1 : $t = random(); let v1 : $t = random();
let n : $n = random(); let n : $n = random();
@ -57,7 +57,7 @@ macro_rules! test_scalar_op_impl(
macro_rules! test_basis_impl( macro_rules! test_basis_impl(
($t: ty) => ( ($t: ty) => (
for _ in range(0us, 10000) { for _ in (0us .. 10000) {
na::canonical_basis(|e1: $t| { na::canonical_basis(|e1: $t| {
na::canonical_basis(|e2: $t| { na::canonical_basis(|e2: $t| {
assert!(e1 == e2 || na::approx_eq(&na::dot(&e1, &e2), &na::zero())); assert!(e1 == e2 || na::approx_eq(&na::dot(&e1, &e2), &na::zero()));
@ -75,7 +75,7 @@ macro_rules! test_basis_impl(
macro_rules! test_subspace_basis_impl( macro_rules! test_subspace_basis_impl(
($t: ty) => ( ($t: ty) => (
for _ in range(0us, 10000) { for _ in (0us .. 10000) {
let v : $t = random(); let v : $t = random();
let v1 = na::normalize(&v); let v1 = na::normalize(&v);
@ -99,7 +99,7 @@ macro_rules! test_subspace_basis_impl(
#[test] #[test]
fn test_cross_vec3() { fn test_cross_vec3() {
for _ in range(0us, 10000) { for _ in (0us .. 10000) {
let v1 : Vec3<f64> = random(); let v1 : Vec3<f64> = random();
let v2 : Vec3<f64> = random(); let v2 : Vec3<f64> = random();
let v3 : Vec3<f64> = na::cross(&v1, &v2); let v3 : Vec3<f64> = na::cross(&v1, &v2);