From 05470bba903493239cec8e53f1f580c004bff0bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Fri, 13 Sep 2013 10:53:59 +0200 Subject: [PATCH] Add a `new_random` method to build a `DMat` or `DVec` filled with random numbers. --- src/dmat.rs | 10 ++++++++++ src/dvec.rs | 12 +++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) 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 {