Add Rand for nmat.

This commit is contained in:
Sébastien Crozet 2013-05-18 11:38:56 +00:00
parent 98b484c311
commit 622435f7ad
1 changed files with 26 additions and 8 deletions

View File

@ -1,4 +1,5 @@
use core::num::{One, Zero}; use core::num::{One, Zero};
use core::rand::{Rand, Rng, RngUtil};
use core::vec::{from_elem, swap, all, all2}; use core::vec::{from_elem, swap, all, all2};
use std::cmp::FuzzyEq; use std::cmp::FuzzyEq;
use traits::dim::Dim; use traits::dim::Dim;
@ -250,6 +251,23 @@ impl<D, T:FuzzyEq<T>> FuzzyEq<T> for NMat<D, T>
{ all2(self.mij, other.mij, |a, b| a.fuzzy_eq_eps(b, epsilon)) } { all2(self.mij, other.mij, |a, b| a.fuzzy_eq_eps(b, epsilon)) }
} }
impl<D: Dim, T: Rand + Zero + Copy> Rand for NMat<D, T>
{
fn rand<R: Rng>(rng: &R) -> NMat<D, T>
{
let dim = Dim::dim::<D>();
let mut res : NMat<D, T> = Zero::zero();
for uint::range(0u, dim) |i|
{
for uint::range(0u, dim) |j|
{ res.set(i, j, &rng.gen()); }
}
res
}
}
impl<D: Dim, T: ToStr> ToStr for NMat<D, T> impl<D: Dim, T: ToStr> ToStr for NMat<D, T>
{ {
fn to_str(&self) -> ~str fn to_str(&self) -> ~str