Add a `new_random` method to build a `DMat` or `DVec` filled with random numbers.

This commit is contained in:
Sébastien Crozet 2013-09-13 10:53:59 +02:00
parent 1cf3506e06
commit 05470bba90
2 changed files with 21 additions and 1 deletions

View File

@ -1,3 +1,5 @@
use std::rand::Rand;
use std::rand;
use std::num::{One, Zero};
use std::vec;
use std::cmp::ApproxEq;
@ -33,6 +35,14 @@ impl<N: Zero + Clone> DMat<N> {
}
}
impl<N: Rand> DMat<N> {
/// Builds a matrix filled with random values.
#[inline]
pub fn new_random(nrows: uint, ncols: uint) -> DMat<N> {
DMat::from_fn(nrows, ncols, |_, _| rand::random())
}
}
impl<N: One + Clone> DMat<N> {
/// Builds a matrix filled with a given constant.
#[inline]

View File

@ -1,4 +1,6 @@
use std::num::{Zero, One, Algebraic};
use std::rand::Rand;
use std::rand;
use std::vec;
use std::vec::{VecIterator, VecMutIterator};
use std::cmp::ApproxEq;
@ -44,6 +46,14 @@ impl<N: One + Clone> DVec<N> {
}
}
impl<N: Rand> DVec<N> {
/// Builds a vector filled with random values.
#[inline]
pub fn new_random(dim: uint) -> DVec<N> {
DVec::from_fn(dim, |_| rand::random())
}
}
impl<N: Clone> DVec<N> {
/// Builds a vector filled with a constant.
#[inline]
@ -52,7 +62,7 @@ impl<N: Clone> DVec<N> {
}
}
impl<N: Clone> DVec<N> {
impl<N> DVec<N> {
/// Builds a vector filled with the result of a function.
#[inline(always)]
pub fn from_fn(dim: uint, f: &fn(uint) -> N) -> DVec<N> {