From 663e2a5fd5cf12b01d10048d0c0848c7ddf1ac99 Mon Sep 17 00:00:00 2001 From: Daniel Vainsencher Date: Sat, 12 Mar 2016 15:07:08 -0500 Subject: [PATCH 1/3] Add randn to create an array of standard gaussian numbers. --- Cargo.toml | 2 +- src/impl_constructors.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 34150ca00..fc04f58e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,7 @@ optional = true [dependencies] blas-sys = { version = "0.5", optional = true, default-features = false } - +rand = "0.3" # deprecated! use ndarray-rblas instead rblas = { version = "0.0.13", optional = true } diff --git a/src/impl_constructors.rs b/src/impl_constructors.rs index feb608013..978563ccd 100644 --- a/src/impl_constructors.rs +++ b/src/impl_constructors.rs @@ -8,6 +8,9 @@ //! Constructor methods for ndarray //! +extern crate rand; +use self::rand::distributions::{Normal, IndependentSample}; +use self::rand::{Rng, XorShiftRng, SeedableRng}; use libnum; use imp_prelude::*; @@ -252,3 +255,28 @@ impl ArrayBase } } + +use super::NdFloat; + +impl ArrayBase + where S: DataOwned, + A: NdFloat, + D: Dimension, +{ + pub fn randn_rng(dim: D, rng: &mut R) -> ArrayBase, D> + { + let size = dim.size_checked().expect("Shape too large: overflow in size"); + let normal = Normal::new(1.0, 0.0); + let iter = (0..size).map(|_| A::from(normal.ind_sample(rng)).unwrap()); + let arr = OwnedArray::from_iter(iter); + arr.into_shape(dim).unwrap() + } + + pub fn randn(dim: D) -> ArrayBase, D> + { + // Use the thread_rng once to seed a fast, non-crypto grade rng. + // To use a crypto quality RNG, use randn_rng directly. + let mut rng : XorShiftRng = SeedableRng::from_seed(rand::thread_rng().gen::<[u32;4]>()); + ArrayBase::, D>::randn_rng(dim, &mut rng) + } +} From 589df30e0f387e26efe564515a9e33fb17a9b502 Mon Sep 17 00:00:00 2001 From: Daniel Vainsencher Date: Sun, 13 Mar 2016 00:07:52 -0500 Subject: [PATCH 2/3] Use asprim crate for a conversion, idimatic weak_rng for a seeded XorShift rng. --- Cargo.toml | 1 + src/impl_constructors.rs | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fc04f58e3..c03725d7e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ optional = true [dependencies] blas-sys = { version = "0.5", optional = true, default-features = false } +asprim = "0.1" rand = "0.3" # deprecated! use ndarray-rblas instead rblas = { version = "0.0.13", optional = true } diff --git a/src/impl_constructors.rs b/src/impl_constructors.rs index 978563ccd..e58d048e0 100644 --- a/src/impl_constructors.rs +++ b/src/impl_constructors.rs @@ -10,7 +10,10 @@ //! extern crate rand; use self::rand::distributions::{Normal, IndependentSample}; -use self::rand::{Rng, XorShiftRng, SeedableRng}; +use self::rand::{Rng}; + +extern crate asprim; +use self::asprim::AsPrim; use libnum; use imp_prelude::*; @@ -260,14 +263,14 @@ use super::NdFloat; impl ArrayBase where S: DataOwned, - A: NdFloat, + A: NdFloat+AsPrim, D: Dimension, { pub fn randn_rng(dim: D, rng: &mut R) -> ArrayBase, D> { let size = dim.size_checked().expect("Shape too large: overflow in size"); let normal = Normal::new(1.0, 0.0); - let iter = (0..size).map(|_| A::from(normal.ind_sample(rng)).unwrap()); + let iter = (0..size).map(|_| normal.ind_sample(rng).as_()); let arr = OwnedArray::from_iter(iter); arr.into_shape(dim).unwrap() } @@ -276,7 +279,7 @@ impl ArrayBase { // Use the thread_rng once to seed a fast, non-crypto grade rng. // To use a crypto quality RNG, use randn_rng directly. - let mut rng : XorShiftRng = SeedableRng::from_seed(rand::thread_rng().gen::<[u32;4]>()); + let mut rng = rand::weak_rng(); ArrayBase::, D>::randn_rng(dim, &mut rng) } } From 5e43e0512385b9bd397cdf653b78fb04d49ac4b0 Mon Sep 17 00:00:00 2001 From: Daniel Vainsencher Date: Mon, 14 Mar 2016 11:40:57 -0400 Subject: [PATCH 3/3] Generalized code and signature to construct arrays with different data access. --- src/impl_constructors.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/impl_constructors.rs b/src/impl_constructors.rs index e58d048e0..2e43c39a9 100644 --- a/src/impl_constructors.rs +++ b/src/impl_constructors.rs @@ -266,20 +266,20 @@ impl ArrayBase A: NdFloat+AsPrim, D: Dimension, { - pub fn randn_rng(dim: D, rng: &mut R) -> ArrayBase, D> + pub fn randn_rng(dim: D, rng: &mut R) -> ArrayBase { let size = dim.size_checked().expect("Shape too large: overflow in size"); let normal = Normal::new(1.0, 0.0); let iter = (0..size).map(|_| normal.ind_sample(rng).as_()); - let arr = OwnedArray::from_iter(iter); + let arr = ArrayBase::from_iter(iter); arr.into_shape(dim).unwrap() } - pub fn randn(dim: D) -> ArrayBase, D> + pub fn randn(dim: D) -> ArrayBase { // Use the thread_rng once to seed a fast, non-crypto grade rng. // To use a crypto quality RNG, use randn_rng directly. let mut rng = rand::weak_rng(); - ArrayBase::, D>::randn_rng(dim, &mut rng) + ArrayBase::::randn_rng(dim, &mut rng) } }