From 965601d4e0e1a0afa119eea6244b6a968af68eae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sun, 9 Jun 2013 12:09:22 +0000 Subject: [PATCH] Adapted to new vec iterator api. --- src/ndim/dmat.rs | 21 +++++++++++++++------ src/ndim/dvec.rs | 41 +++++++++++++++++++++++++---------------- src/tests/vec.rs | 30 +++++++++++++++++++++--------- 3 files changed, 61 insertions(+), 31 deletions(-) diff --git a/src/ndim/dmat.rs b/src/ndim/dmat.rs index af06bbeb..71262076 100644 --- a/src/ndim/dmat.rs +++ b/src/ndim/dmat.rs @@ -1,7 +1,8 @@ use std::uint::iterate; use std::num::{One, Zero}; -use std::vec::{from_elem, swap, all, all2, len}; +use std::vec::{from_elem, swap}; use std::cmp::ApproxEq; +use std::iterator::IteratorUtil; use traits::inv::Inv; use traits::division_ring::DivisionRing; use traits::transpose::Transpose; @@ -19,7 +20,7 @@ pub fn zero_mat_with_dim(dim: uint) -> DMat { DMat { dim: dim, mij: from_elem(dim * dim, Zero::zero()) } } pub fn is_zero_mat(mat: &DMat) -> bool -{ all(mat.mij, |e| e.is_zero()) } +{ mat.mij.all(|e| e.is_zero()) } pub fn one_mat_with_dim(dim: uint) -> DMat { @@ -90,7 +91,7 @@ RMul> for DMat { fn rmul(&self, other: &DVec) -> DVec { - assert!(self.dim == len(other.at)); + assert!(self.dim == other.at.len()); let dim = self.dim; let mut res : DVec = zero_vec_with_dim(dim); @@ -110,7 +111,7 @@ LMul> for DMat { fn lmul(&self, other: &DVec) -> DVec { - assert!(self.dim == len(other.at)); + assert!(self.dim == other.at.len()); let dim = self.dim; let mut res : DVec = zero_vec_with_dim(dim); @@ -248,8 +249,16 @@ impl> ApproxEq for DMat { ApproxEq::approx_epsilon::() } fn approx_eq(&self, other: &DMat) -> bool - { all2(self.mij, other.mij, |a, b| a.approx_eq(b)) } + { + let mut zip = self.mij.iter().zip(other.mij.iter()); + + do zip.all |(a, b)| { a.approx_eq(b) } + } fn approx_eq_eps(&self, other: &DMat, epsilon: &T) -> bool - { all2(self.mij, other.mij, |a, b| a.approx_eq_eps(b, epsilon)) } + { + let mut zip = self.mij.iter().zip(other.mij.iter()); + + do zip.all |(a, b)| { a.approx_eq_eps(b, epsilon) } + } } diff --git a/src/ndim/dvec.rs b/src/ndim/dvec.rs index 26ff815a..5654fd49 100644 --- a/src/ndim/dvec.rs +++ b/src/ndim/dvec.rs @@ -1,7 +1,8 @@ use std::uint::iterate; use std::num::{Zero, One, Algebraic}; -use std::vec::{map_zip, map, all2, len, from_elem, all}; +use std::vec::{map_zip, map, from_elem}; use std::cmp::ApproxEq; +use std::iterator::IteratorUtil; use traits::ring::Ring; use traits::division_ring::DivisionRing; use traits::dot::Dot; @@ -20,7 +21,7 @@ pub fn zero_vec_with_dim(dim: uint) -> DVec { DVec { at: from_elem(dim, Zero::zero::()) } } pub fn is_zero_vec(vec: &DVec) -> bool -{ all(vec.at, |e| e.is_zero()) } +{ vec.at.all(|e| e.is_zero()) } // FIXME: is Clone needed? impl> DVec @@ -45,12 +46,12 @@ impl> DVec { // compute the basis of the orthogonal subspace using Gram-Schmidt // orthogonalization algorithm - let dim = len(self.at); + let dim = self.at.len(); let mut res : ~[DVec] = ~[]; for iterate(0u, dim) |i| { - let mut basis_element : DVec = zero_vec_with_dim(len(self.at)); + let mut basis_element : DVec = zero_vec_with_dim(self.at.len()); basis_element.at[i] = One::one(); @@ -78,7 +79,7 @@ impl> Add, DVec> for DVec { fn add(&self, other: &DVec) -> DVec { - assert!(len(self.at) == len(other.at)); + assert!(self.at.len() == other.at.len()); DVec { at: map_zip(self.at, other.at, | a, b | { *a + *b }) } } } @@ -87,7 +88,7 @@ impl> Sub, DVec> for DVec { fn sub(&self, other: &DVec) -> DVec { - assert!(len(self.at) == len(other.at)); + assert!(self.at.len() == other.at.len()); DVec { at: map_zip(self.at, other.at, | a, b | *a - *b) } } } @@ -103,11 +104,11 @@ Dot for DVec { fn dot(&self, other: &DVec) -> T { - assert!(len(self.at) == len(other.at)); + assert!(self.at.len() == other.at.len()); let mut res = Zero::zero::(); - for iterate(0u, len(self.at)) |i| + for iterate(0u, self.at.len()) |i| { res += self.at[i] * other.at[i]; } res @@ -120,7 +121,7 @@ impl SubDot for DVec { let mut res = Zero::zero::(); - for iterate(0u, len(self.at)) |i| + for iterate(0u, self.at.len()) |i| { res += (self.at[i] - a.at[i]) * b.at[i]; } res @@ -135,7 +136,7 @@ ScalarMul for DVec fn scalar_mul_inplace(&mut self, s: &T) { - for iterate(0u, len(self.at)) |i| + for iterate(0u, self.at.len()) |i| { self.at[i] *= *s; } } } @@ -149,7 +150,7 @@ ScalarDiv for DVec fn scalar_div_inplace(&mut self, s: &T) { - for iterate(0u, len(self.at)) |i| + for iterate(0u, self.at.len()) |i| { self.at[i] /= *s; } } } @@ -162,7 +163,7 @@ ScalarAdd for DVec fn scalar_add_inplace(&mut self, s: &T) { - for iterate(0u, len(self.at)) |i| + for iterate(0u, self.at.len()) |i| { self.at[i] += *s; } } } @@ -175,7 +176,7 @@ ScalarSub for DVec fn scalar_sub_inplace(&mut self, s: &T) { - for iterate(0u, len(self.at)) |i| + for iterate(0u, self.at.len()) |i| { self.at[i] -= *s; } } } @@ -214,7 +215,7 @@ Norm for DVec { let l = self.norm(); - for iterate(0u, len(self.at)) |i| + for iterate(0u, self.at.len()) |i| { self.at[i] /= l; } l @@ -227,8 +228,16 @@ impl> ApproxEq for DVec { ApproxEq::approx_epsilon::() } fn approx_eq(&self, other: &DVec) -> bool - { all2(self.at, other.at, |a, b| a.approx_eq(b)) } + { + let mut zip = self.at.iter().zip(other.at.iter()); + + do zip.all |(a, b)| { a.approx_eq(b) } + } fn approx_eq_eps(&self, other: &DVec, epsilon: &T) -> bool - { all2(self.at, other.at, |a, b| a.approx_eq_eps(b, epsilon)) } + { + let mut zip = self.at.iter().zip(other.at.iter()); + + do zip.all |(a, b)| { a.approx_eq_eps(b, epsilon) } + } } diff --git a/src/tests/vec.rs b/src/tests/vec.rs index 75f06131..05321de8 100644 --- a/src/tests/vec.rs +++ b/src/tests/vec.rs @@ -1,10 +1,10 @@ #[test] +use std::iterator::IteratorUtil; +#[test] use std::num::{Zero, One}; #[test] use std::rand::{random}; #[test] -use std::vec::{all, all2}; -#[test] use std::cmp::ApproxEq; #[test] use dim3::vec3::Vec3; @@ -44,9 +44,14 @@ macro_rules! test_basis_impl( let basis = Basis::canonical_basis::<$t>(); // check vectors form an ortogonal basis - assert!(all2(basis, basis, |e1, e2| e1 == e2 || e1.dot(e2).approx_eq(&Zero::zero()))); + assert!( + do basis.iter().zip(basis.iter()).all + |(e1, e2)| { e1 == e2 || e1.dot(e2).approx_eq(&Zero::zero()) } + ); // check vectors form an orthonormal basis - assert!(all(basis, |e| e.norm().approx_eq(&One::one()))); + assert!( + do basis.iter().all |e| { e.norm().approx_eq(&One::one()) } + ); } ); ) @@ -56,15 +61,22 @@ macro_rules! test_subspace_basis_impl( for 10000.times { let v : Vec3 = random(); - let v1 = v.normalized(); - let subbasis = v1.orthogonal_subspace_basis(); + let v1 = v.normalized(); + let subbasis = v1.orthogonal_subspace_basis(); // check vectors are orthogonal to v1 - assert!(all(subbasis, |e| v1.dot(e).approx_eq(&Zero::zero()))); + assert!( + do subbasis.iter().all |e| { v1.dot(e).approx_eq(&Zero::zero()) } + ); // check vectors form an ortogonal basis - assert!(all2(subbasis, subbasis, |e1, e2| e1 == e2 || e1.dot(e2).approx_eq(&Zero::zero()))); + assert!( + do subbasis.iter().zip(subbasis.iter()).all + |(e1, e2)| { e1 == e2 || e1.dot(e2).approx_eq(&Zero::zero()) } + ); // check vectors form an orthonormal basis - assert!(all(subbasis, |e| e.norm().approx_eq(&One::one()))); + assert!( + do subbasis.iter().all |e| { e.norm().approx_eq(&One::one()) } + ); } ); )