diff --git a/src/dmat.rs b/src/dmat.rs index 16e700fb..58dfdeb1 100644 --- a/src/dmat.rs +++ b/src/dmat.rs @@ -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 DMat { } } +impl DMat { + /// Builds a matrix filled with random values. + #[inline] + pub fn new_random(nrows: uint, ncols: uint) -> DMat { + DMat::from_fn(nrows, ncols, |_, _| rand::random()) + } +} + impl DMat { /// Builds a matrix filled with a given constant. #[inline] diff --git a/src/dvec.rs b/src/dvec.rs index 50ca0c2f..7d00da44 100644 --- a/src/dvec.rs +++ b/src/dvec.rs @@ -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 DVec { } } +impl DVec { + /// Builds a vector filled with random values. + #[inline] + pub fn new_random(dim: uint) -> DVec { + DVec::from_fn(dim, |_| rand::random()) + } +} + impl DVec { /// Builds a vector filled with a constant. #[inline] @@ -52,7 +62,7 @@ impl DVec { } } -impl DVec { +impl DVec { /// Builds a vector filled with the result of a function. #[inline(always)] pub fn from_fn(dim: uint, f: &fn(uint) -> N) -> DVec {