From 622435f7ad1bbb5e26f9625f654a35cf6ab464f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sat, 18 May 2013 11:38:56 +0000 Subject: [PATCH] Add Rand for nmat. --- src/ndim/nmat.rs | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/ndim/nmat.rs b/src/ndim/nmat.rs index d7981c5e..9dec7038 100644 --- a/src/ndim/nmat.rs +++ b/src/ndim/nmat.rs @@ -1,4 +1,5 @@ use core::num::{One, Zero}; +use core::rand::{Rand, Rng, RngUtil}; use core::vec::{from_elem, swap, all, all2}; use std::cmp::FuzzyEq; use traits::dim::Dim; @@ -38,13 +39,13 @@ impl Dim for NMat { Dim::dim::() } } -impl Index<(uint, uint), T> for NMat +impl Index<(uint, uint), T> for NMat { fn index(&self, &(i, j): &(uint, uint)) -> T { self.mij[NMat::offset::(i, j)] } } -impl One for NMat +impl One for NMat { fn one() -> NMat { @@ -59,7 +60,7 @@ impl One for NMat } } -impl Zero for NMat +impl Zero for NMat { fn zero() -> NMat { @@ -72,7 +73,7 @@ impl Zero for NMat { all(self.mij, |e| e.is_zero()) } } -impl + Add + Zero> +impl + Add + Zero> Mul, NMat> for NMat { fn mul(&self, other: &NMat) -> NMat @@ -97,7 +98,7 @@ Mul, NMat> for NMat } } -impl + Mul + Zero> +impl + Mul + Zero> RMul> for NMat { fn rmul(&self, other: &NVec) -> NVec @@ -115,7 +116,7 @@ RMul> for NMat } } -impl + Mul + Zero> +impl + Mul + Zero> LMul> for NMat { fn lmul(&self, other: &NVec) -> NVec @@ -241,7 +242,7 @@ impl Transpose for NMat } } -impl> FuzzyEq for NMat +impl> FuzzyEq for NMat { fn fuzzy_eq(&self, other: &NMat) -> bool { all2(self.mij, other.mij, |a, b| a.fuzzy_eq(b)) } @@ -250,7 +251,24 @@ impl> FuzzyEq for NMat { all2(self.mij, other.mij, |a, b| a.fuzzy_eq_eps(b, epsilon)) } } -impl ToStr for NMat +impl Rand for NMat +{ + fn rand(rng: &R) -> NMat + { + let dim = Dim::dim::(); + let mut res : NMat = Zero::zero(); + + for uint::range(0u, dim) |i| + { + for uint::range(0u, dim) |j| + { res.set(i, j, &rng.gen()); } + } + + res + } +} + +impl ToStr for NMat { fn to_str(&self) -> ~str { ~"Mat" + Dim::dim::().to_str() + " {" + self.mij.to_str() + " }" }