Update to the last Rust.

This commit is contained in:
Sébastien Crozet 2013-11-27 11:16:16 +01:00
parent 9e7a623d99
commit b244975f93
13 changed files with 76 additions and 88 deletions

View File

@ -8,11 +8,11 @@ macro_rules! bench_mul_mat(
let a: $t = random(); let a: $t = random();
let mut b: $t = random(); let mut b: $t = random();
do $bh.iter { $bh.iter(|| {
do 1000.times { 1000.times(|| {
b = a * b; b = a * b;
} })
} })
} }
} }
) )
@ -48,11 +48,11 @@ 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);
do $bh.iter { $bh.iter(|| {
do 1000.times { 1000.times(|| {
b = a * b; b = a * b;
} })
} })
} }
} }
) )
@ -88,11 +88,11 @@ macro_rules! bench_mul_mat_vec(
let m : $tm = random(); let m : $tm = random();
let mut v : $tv = random(); let mut v : $tv = random();
do $bh.iter { $bh.iter(|| {
do 1000.times { 1000.times(|| {
v = m * v v = m * v
} })
} })
} }
} }
) )
@ -128,11 +128,11 @@ 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);
do $bh.iter { $bh.iter(|| {
do 1000.times { 1000.times(|| {
v = m * v v = m * v
} })
} })
} }
} }
) )

View File

@ -10,11 +10,11 @@ macro_rules! bench_dot_vec(
let b: $t = random(); let b: $t = random();
let mut d = 0.0; let mut d = 0.0;
do $bh.iter { $bh.iter(|| {
do 1000.times { 1000.times(|| {
d = d + na::dot(&a, &b); d = d + na::dot(&a, &b);
} })
} })
} }
} }
) )

View File

@ -477,7 +477,7 @@ pub fn from_homogeneous<M, Res: FromHomogeneous<M>>(m: &M) -> Res {
/// ///
/// The number of sampling point is implementation-specific. It is always uniform. /// The number of sampling point is implementation-specific. It is always uniform.
#[inline(always)] #[inline(always)]
pub fn sample_sphere<V: UniformSphereSample>(f: &fn(V)) { pub fn sample_sphere<V: UniformSphereSample>(f: |V| -> ()) {
UniformSphereSample::sample(f) UniformSphereSample::sample(f)
} }
@ -560,13 +560,13 @@ pub fn mean<N, M: Mean<N>>(observations: &M) -> N {
/// Computes the canonical basis for a given dimension. /// Computes the canonical basis for a given dimension.
#[inline(always)] #[inline(always)]
pub fn canonical_basis<V: Basis>(f: &fn(V) -> bool) { pub fn canonical_basis<V: Basis>(f: |V| -> bool) {
Basis::canonical_basis(f) Basis::canonical_basis(f)
} }
/// Computes the basis of the orthonormal subspace of a given vector. /// Computes the basis of the orthonormal subspace of a given vector.
#[inline(always)] #[inline(always)]
pub fn orthonormal_subspace_basis<V: Basis>(v: &V, f: &fn(V) -> bool) { pub fn orthonormal_subspace_basis<V: Basis>(v: &V, f: |V| -> bool) {
Basis::orthonormal_subspace_basis(v, f) Basis::orthonormal_subspace_basis(v, f)
} }

View File

@ -137,7 +137,7 @@ impl<N: Clone> DMat<N> {
impl<N> DMat<N> { impl<N> DMat<N> {
/// Builds a matrix filled with a given constant. /// Builds a matrix filled with a given constant.
#[inline(always)] #[inline(always)]
pub fn from_fn(nrows: uint, ncols: uint, f: &fn(uint, uint) -> N) -> DMat<N> { pub fn from_fn(nrows: uint, ncols: uint, f: |uint, uint| -> N) -> DMat<N> {
DMat { DMat {
nrows: nrows, nrows: nrows,
ncols: ncols, ncols: ncols,
@ -510,18 +510,14 @@ impl<N: ApproxEq<N>> ApproxEq<N> for DMat<N> {
fn approx_eq(&self, other: &DMat<N>) -> bool { fn approx_eq(&self, other: &DMat<N>) -> bool {
let mut zip = self.mij.iter().zip(other.mij.iter()); let mut zip = self.mij.iter().zip(other.mij.iter());
do zip.all |(a, b)| { zip.all(|(a, b)| a.approx_eq(b))
a.approx_eq(b)
}
} }
#[inline] #[inline]
fn approx_eq_eps(&self, other: &DMat<N>, epsilon: &N) -> bool { fn approx_eq_eps(&self, other: &DMat<N>, epsilon: &N) -> bool {
let mut zip = self.mij.iter().zip(other.mij.iter()); let mut zip = self.mij.iter().zip(other.mij.iter());
do zip.all |(a, b)| { zip.all(|(a, b)| a.approx_eq_eps(b, epsilon))
a.approx_eq_eps(b, epsilon)
}
} }
} }

View File

@ -138,7 +138,7 @@ impl<N: Clone> DVec<N> {
impl<N> DVec<N> { 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(dim: uint, f: &fn(uint) -> N) -> DVec<N> { pub fn from_fn(dim: uint, f: |uint| -> N) -> DVec<N> {
DVec { at: vec::from_fn(dim, |i| f(i)) } DVec { at: vec::from_fn(dim, |i| f(i)) }
} }
} }
@ -329,18 +329,14 @@ impl<N: ApproxEq<N>> ApproxEq<N> for DVec<N> {
fn approx_eq(&self, other: &DVec<N>) -> bool { fn approx_eq(&self, other: &DVec<N>) -> bool {
let mut zip = self.at.iter().zip(other.at.iter()); let mut zip = self.at.iter().zip(other.at.iter());
do zip.all |(a, b)| { zip.all(|(a, b)| a.approx_eq(b))
a.approx_eq(b)
}
} }
#[inline] #[inline]
fn approx_eq_eps(&self, other: &DVec<N>, epsilon: &N) -> bool { fn approx_eq_eps(&self, other: &DVec<N>, epsilon: &N) -> bool {
let mut zip = self.at.iter().zip(other.at.iter()); let mut zip = self.at.iter().zip(other.at.iter());
do zip.all |(a, b)| { zip.all(|(a, b)| a.approx_eq_eps(b, epsilon))
a.approx_eq_eps(b, epsilon)
}
} }
} }

View File

@ -448,18 +448,14 @@ macro_rules! approx_eq_impl(
fn approx_eq(&self, other: &$t<N>) -> bool { fn approx_eq(&self, other: &$t<N>) -> bool {
let mut zip = self.iter().zip(other.iter()); let mut zip = self.iter().zip(other.iter());
do zip.all |(a, b)| { zip.all(|(a, b)| a.approx_eq(b))
a.approx_eq(b)
}
} }
#[inline] #[inline]
fn approx_eq_eps(&self, other: &$t<N>, epsilon: &N) -> bool { fn approx_eq_eps(&self, other: &$t<N>, epsilon: &N) -> bool {
let mut zip = self.iter().zip(other.iter()); let mut zip = self.iter().zip(other.iter());
do zip.all |(a, b)| { zip.all(|(a, b)| a.approx_eq_eps(b, epsilon))
a.approx_eq_eps(b, epsilon)
}
} }
} }
) )

View File

@ -70,37 +70,37 @@ impl<N: Clone> Row<Vec1<N>> for Vec2<N> {
impl<N: One> Basis for Vec1<N> { impl<N: One> Basis for Vec1<N> {
#[inline(always)] #[inline(always)]
fn canonical_basis(f: &fn(Vec1<N>) -> bool) { fn canonical_basis(f: |Vec1<N>| -> bool) {
f(Vec1::new(One::one())); f(Vec1::new(One::one()));
} }
#[inline(always)] #[inline(always)]
fn orthonormal_subspace_basis(_: &Vec1<N>, _: &fn(Vec1<N>) -> bool ) { } fn orthonormal_subspace_basis(_: &Vec1<N>, _: |Vec1<N>| -> bool ) { }
} }
impl<N: Clone + One + Zero + Neg<N>> Basis for Vec2<N> { impl<N: Clone + One + Zero + Neg<N>> Basis for Vec2<N> {
#[inline(always)] #[inline(always)]
fn canonical_basis(f: &fn(Vec2<N>) -> bool) { fn canonical_basis(f: |Vec2<N>| -> bool) {
if !f(Vec2::new(One::one(), Zero::zero())) { return }; if !f(Vec2::new(One::one(), Zero::zero())) { return };
f(Vec2::new(Zero::zero(), One::one())); f(Vec2::new(Zero::zero(), One::one()));
} }
#[inline] #[inline]
fn orthonormal_subspace_basis(n: &Vec2<N>, f: &fn(Vec2<N>) -> bool) { fn orthonormal_subspace_basis(n: &Vec2<N>, f: |Vec2<N>| -> bool) {
f(Vec2::new(-n.y, n.x.clone())); f(Vec2::new(-n.y, n.x.clone()));
} }
} }
impl<N: Clone + Ord + Algebraic + Signed> Basis for Vec3<N> { impl<N: Clone + Ord + Algebraic + Signed> Basis for Vec3<N> {
#[inline(always)] #[inline(always)]
fn canonical_basis(f: &fn(Vec3<N>) -> bool) { fn canonical_basis(f: |Vec3<N>| -> bool) {
if !f(Vec3::new(One::one(), Zero::zero(), Zero::zero())) { return }; if !f(Vec3::new(One::one(), Zero::zero(), Zero::zero())) { return };
if !f(Vec3::new(Zero::zero(), One::one(), Zero::zero())) { return }; if !f(Vec3::new(Zero::zero(), One::one(), Zero::zero())) { return };
f(Vec3::new(Zero::zero(), Zero::zero(), One::one())); f(Vec3::new(Zero::zero(), Zero::zero(), One::one()));
} }
#[inline(always)] #[inline(always)]
fn orthonormal_subspace_basis(n: &Vec3<N>, f: &fn(Vec3<N>) -> bool) { fn orthonormal_subspace_basis(n: &Vec3<N>, f: |Vec3<N>| -> bool) {
let a = let a =
if n.x.clone().abs() > n.y.clone().abs() { if n.x.clone().abs() > n.y.clone().abs() {
Norm::normalize_cpy(&Vec3::new(n.z.clone(), Zero::zero(), -n.x)) Norm::normalize_cpy(&Vec3::new(n.z.clone(), Zero::zero(), -n.x))
@ -187,14 +187,14 @@ static SAMPLES_3_F32: [Vec3<f32>, ..42] = [
impl<N: One + Clone> UniformSphereSample for Vec1<N> { impl<N: One + Clone> UniformSphereSample for Vec1<N> {
#[inline(always)] #[inline(always)]
fn sample(f: &fn(Vec1<N>)) { fn sample(f: |Vec1<N>| -> ()) {
f(One::one()) f(One::one())
} }
} }
impl<N: Cast<f32> + Clone> UniformSphereSample for Vec2<N> { impl<N: Cast<f32> + Clone> UniformSphereSample for Vec2<N> {
#[inline(always)] #[inline(always)]
fn sample(f: &fn(Vec2<N>)) { fn sample(f: |Vec2<N>| -> ()) {
for sample in SAMPLES_2_F32.iter() { for sample in SAMPLES_2_F32.iter() {
f(Cast::from(*sample)) f(Cast::from(*sample))
} }
@ -203,7 +203,7 @@ impl<N: Cast<f32> + Clone> UniformSphereSample for Vec2<N> {
impl<N: Cast<f32> + Clone> UniformSphereSample for Vec3<N> { impl<N: Cast<f32> + Clone> UniformSphereSample for Vec3<N> {
#[inline(always)] #[inline(always)]
fn sample(f: &fn(Vec3<N>)) { fn sample(f: |Vec3<N>| -> ()) {
for sample in SAMPLES_3_F32.iter() { for sample in SAMPLES_3_F32.iter() {
f(Cast::from(*sample)) f(Cast::from(*sample))
} }
@ -212,7 +212,7 @@ impl<N: Cast<f32> + Clone> UniformSphereSample for Vec3<N> {
impl<N: Cast<f32> + Clone> UniformSphereSample for Vec4<N> { impl<N: Cast<f32> + Clone> UniformSphereSample for Vec4<N> {
#[inline(always)] #[inline(always)]
fn sample(_: &fn(Vec4<N>)) { fn sample(_: |Vec4<N>| -> ()) {
fail!("UniformSphereSample::<Vec4<N>>::sample : Not yet implemented.") fail!("UniformSphereSample::<Vec4<N>>::sample : Not yet implemented.")
// for sample in SAMPLES_3_F32.iter() { // for sample in SAMPLES_3_F32.iter() {
// f(Cast::from(*sample)) // f(Cast::from(*sample))

View File

@ -63,10 +63,10 @@ impl<N> Dim for vec::Vec0<N> {
impl<N> Basis for vec::Vec0<N> { impl<N> Basis for vec::Vec0<N> {
#[inline(always)] #[inline(always)]
fn canonical_basis(_: &fn(vec::Vec0<N>) -> bool) { } fn canonical_basis(_: |vec::Vec0<N>| -> bool) { }
#[inline(always)] #[inline(always)]
fn orthonormal_subspace_basis(_: &vec::Vec0<N>, _: &fn(vec::Vec0<N>) -> bool) { } fn orthonormal_subspace_basis(_: &vec::Vec0<N>, _: |vec::Vec0<N>| -> bool) { }
} }
impl<N, T> Add<T, vec::Vec0<N>> for vec::Vec0<N> { impl<N, T> Add<T, vec::Vec0<N>> for vec::Vec0<N> {

View File

@ -215,7 +215,7 @@ macro_rules! basis_impl(
($t: ident, $trhs: ident, $dim: expr) => ( ($t: ident, $trhs: ident, $dim: expr) => (
impl<N: Clone + Num + Algebraic + ApproxEq<N> + $trhs<N, $t<N>>> Basis for $t<N> { impl<N: Clone + Num + Algebraic + ApproxEq<N> + $trhs<N, $t<N>>> Basis for $t<N> {
#[inline] #[inline]
fn canonical_basis(f: &fn($t<N>) -> bool) { fn canonical_basis(f: |$t<N>| -> bool) {
for i in range(0u, $dim) { for i in range(0u, $dim) {
let mut basis_element : $t<N> = Zero::zero(); let mut basis_element : $t<N> = Zero::zero();
@ -228,7 +228,7 @@ macro_rules! basis_impl(
} }
#[inline] #[inline]
fn orthonormal_subspace_basis(n: &$t<N>, f: &fn($t<N>) -> bool) { fn orthonormal_subspace_basis(n: &$t<N>, f: |$t<N>| -> bool) {
// compute the basis of the orthogonal subspace using Gram-Schmidt // compute the basis of the orthogonal subspace using Gram-Schmidt
// orthogonalization algorithm // orthogonalization algorithm
let mut basis: ~[$t<N>] = ~[]; let mut basis: ~[$t<N>] = ~[];

View File

@ -6,21 +6,21 @@ use na;
macro_rules! test_inv_mat_impl( macro_rules! test_inv_mat_impl(
($t: ty) => ( ($t: ty) => (
do 10000.times { 10000.times(|| {
let randmat : $t = random(); let randmat : $t = random();
assert!((na::inv(&randmat).unwrap() * randmat).approx_eq(&na::one())); assert!((na::inv(&randmat).unwrap() * randmat).approx_eq(&na::one()));
} })
); );
) )
macro_rules! test_transpose_mat_impl( macro_rules! test_transpose_mat_impl(
($t: ty) => ( ($t: ty) => (
do 10000.times { 10000.times(|| {
let randmat : $t = random(); let randmat : $t = random();
assert!(na::transpose(&na::transpose(&randmat)) == randmat); assert!(na::transpose(&na::transpose(&randmat)) == randmat);
} })
); );
) )
@ -86,12 +86,12 @@ fn test_inv_mat6() {
#[test] #[test]
fn test_rotation2() { fn test_rotation2() {
do 10000.times { 10000.times(|| {
let randmat: na::Rot2<f64> = na::one(); let randmat: na::Rot2<f64> = na::one();
let ang = Vec1::new(abs::<f64>(random()) % Real::pi()); let ang = Vec1::new(abs::<f64>(random()) % Real::pi());
assert!(na::rotation(&na::append_rotation(&randmat, &ang)).approx_eq(&ang)); assert!(na::rotation(&na::append_rotation(&randmat, &ang)).approx_eq(&ang));
} })
} }
#[test] #[test]
@ -103,14 +103,14 @@ fn test_index_mat2() {
#[test] #[test]
fn test_inv_rotation3() { fn test_inv_rotation3() {
do 10000.times { 10000.times(|| {
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) * (abs::<f64>(random()) % Real::pi()); let ang = na::normalize(&dir) * (abs::<f64>(random()) % Real::pi());
let rot = na::append_rotation(&randmat, &ang); let rot = na::append_rotation(&randmat, &ang);
assert!((na::transpose(&rot) * rot).approx_eq(&na::one())); assert!((na::transpose(&rot) * rot).approx_eq(&na::one()));
} })
} }
#[test] #[test]

View File

@ -6,7 +6,7 @@ use na;
macro_rules! test_iterator_impl( macro_rules! test_iterator_impl(
($t: ty, $n: ty) => ( ($t: ty, $n: ty) => (
do 10000.times { 10000.times(|| {
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();
@ -18,24 +18,24 @@ macro_rules! test_iterator_impl(
} }
assert!(nv == mv && nv == v * n); assert!(nv == mv && nv == v * n);
} })
) )
) )
macro_rules! test_commut_dot_impl( macro_rules! test_commut_dot_impl(
($t: ty) => ( ($t: ty) => (
do 10000.times { 10000.times(|| {
let v1 : $t = random(); let v1 : $t = random();
let v2 : $t = random(); let v2 : $t = random();
assert!(na::dot(&v1, &v2).approx_eq(&na::dot(&v2, &v1))); assert!(na::dot(&v1, &v2).approx_eq(&na::dot(&v2, &v1)));
} })
); );
) )
macro_rules! test_scalar_op_impl( macro_rules! test_scalar_op_impl(
($t: ty, $n: ty) => ( ($t: ty, $n: ty) => (
do 10000.times { 10000.times(|| {
let v1 : $t = random(); let v1 : $t = random();
let n : $n = random(); let n : $n = random();
@ -52,62 +52,62 @@ macro_rules! test_scalar_op_impl(
v1 = v1 / n; v1 = v1 / n;
assert!(v1.approx_eq(&v0)); assert!(v1.approx_eq(&v0));
} })
); );
) )
macro_rules! test_basis_impl( macro_rules! test_basis_impl(
($t: ty) => ( ($t: ty) => (
do 10000.times { 10000.times(|| {
do na::canonical_basis |e1: $t| { na::canonical_basis(|e1: $t| {
do na::canonical_basis |e2: $t| { na::canonical_basis(|e2: $t| {
assert!(e1 == e2 || na::dot(&e1, &e2).approx_eq(&na::zero())); assert!(e1 == e2 || na::dot(&e1, &e2).approx_eq(&na::zero()));
true true
} });
assert!(na::norm(&e1).approx_eq(&na::one())); assert!(na::norm(&e1).approx_eq(&na::one()));
true true
} })
} })
); );
) )
macro_rules! test_subspace_basis_impl( macro_rules! test_subspace_basis_impl(
($t: ty) => ( ($t: ty) => (
do 10000.times { 10000.times(|| {
let v : $t = random(); let v : $t = random();
let v1 = na::normalize(&v); let v1 = na::normalize(&v);
do na::orthonormal_subspace_basis(&v1) |e1| { na::orthonormal_subspace_basis(&v1, |e1| {
// check vectors are orthogonal to v1 // check vectors are orthogonal to v1
assert!(na::dot(&v1, &e1).approx_eq(&na::zero())); assert!(na::dot(&v1, &e1).approx_eq(&na::zero()));
// check vectors form an orthonormal basis // check vectors form an orthonormal basis
assert!(na::norm(&e1).approx_eq(&na::one())); assert!(na::norm(&e1).approx_eq(&na::one()));
// check vectors form an ortogonal basis // check vectors form an ortogonal basis
do na::orthonormal_subspace_basis(&v1) |e2| { na::orthonormal_subspace_basis(&v1, |e2| {
assert!(e1 == e2 || na::dot(&e1, &e2).approx_eq(&na::zero())); assert!(e1 == e2 || na::dot(&e1, &e2).approx_eq(&na::zero()));
true true
} });
true true
} })
} })
); );
) )
#[test] #[test]
fn test_cross_vec3() { fn test_cross_vec3() {
do 10000.times { 10000.times(|| {
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);
assert!(na::dot(&v3, &v2).approx_eq(&na::zero())); assert!(na::dot(&v3, &v2).approx_eq(&na::zero()));
assert!(na::dot(&v3, &v1).approx_eq(&na::zero())); assert!(na::dot(&v3, &v1).approx_eq(&na::zero()));
} })
} }
#[test] #[test]

View File

@ -278,5 +278,5 @@ pub trait FromHomogeneous<U> {
/// function. /// function.
pub trait UniformSphereSample { pub trait UniformSphereSample {
/// Iterate through the samples. /// Iterate through the samples.
fn sample(&fn(Self)); fn sample(|Self| -> ());
} }

View File

@ -57,10 +57,10 @@ impl<N: Algebraic, V: AlgebraicVec<N> + VecExt<N> + Basis + Round> AlgebraicVecE
/// Traits of objects which can form a basis (typically vectors). /// Traits of objects which can form a basis (typically vectors).
pub trait Basis { pub trait Basis {
/// Iterates through the canonical basis of the space in which this object lives. /// Iterates through the canonical basis of the space in which this object lives.
fn canonical_basis(&fn(Self) -> bool); fn canonical_basis(|Self| -> bool);
/// Iterates through a basis of the subspace orthogonal to `self`. /// Iterates through a basis of the subspace orthogonal to `self`.
fn orthonormal_subspace_basis(&Self, &fn(Self) -> bool); fn orthonormal_subspace_basis(&Self, |Self| -> bool);
} }
/// Trait to access rows of a matrix or a vector. /// Trait to access rows of a matrix or a vector.