From 7ee0c209f24075a3a1ac3330ee823c8de9a0354f Mon Sep 17 00:00:00 2001 From: Lorenzo Bertini Date: Fri, 4 Oct 2024 11:34:58 +0200 Subject: [PATCH 1/3] Refactored rand into randn --- src/util/non_macro.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/util/non_macro.rs b/src/util/non_macro.rs index 1656d33f..25ad061e 100644 --- a/src/util/non_macro.rs +++ b/src/util/non_macro.rs @@ -15,8 +15,9 @@ //! - zeros_shape //! - linspace //! - linspace_with_precision -//! - rand -//! - rand_with_rng +//! - randn +//! - randn_with_rng +//! - randn_with_distr //! //! # Numpy like non-macro functions //! @@ -31,6 +32,7 @@ extern crate rand; use self::rand::prelude::*; +use rand_distr::{Uniform, Distribution}; use crate::structure::{ matrix::Shape::{Col, Row}, matrix::{matrix, Matrix, Shape}, @@ -316,15 +318,9 @@ where /// # Description /// /// Range = from 0 to 1 -pub fn rand(r: usize, c: usize) -> Matrix { - let mut m = zeros(r, c); +pub fn randn(r: usize, c: usize) -> Matrix { let mut rng = thread_rng(); - for i in 0..r { - for j in 0..c { - m[(i, j)] = rng.gen_range(0f64..=1f64); - } - } - m + randn_with_rng(r, c, &mut rng) } /// Rand matrix with specific rng @@ -332,14 +328,18 @@ pub fn rand(r: usize, c: usize) -> Matrix { /// # Description /// /// Range = from 0 to 1 -pub fn rand_with_rng(r: usize, c: usize, rng: &mut R) -> Matrix { - let mut m = zeros(r, c); - for i in 0..r { - for j in 0..c { - m[(i, j)] = rng.gen_range(0f64..=1f64); - } - } - m +pub fn randn_with_rng(r: usize, c: usize, rng: &mut R) -> Matrix { + let uniform = Uniform::new_inclusive(0f64, 1f64); + randn_with_distr(r, c, rng, uniform) +} + +/// Rand matrix with specific rng and distribution +/// +/// # Description +/// +/// Any range +pub fn randn_with_distr, R: Rng, D: Distribution>(r: usize, c: usize, rng: &mut R, distr: D) -> Matrix { + matrix(rng.sample_iter(distr).take(r*c).collect(), r, c, Row) } // ┌─────────────────────────────────────────────────────────┐ From 41f48ae2395602ce04e56b43a7e1aa5d76530726 Mon Sep 17 00:00:00 2001 From: Lorenzo Bertini <106469113+bertini97@users.noreply.github.com> Date: Fri, 4 Oct 2024 20:42:57 +0200 Subject: [PATCH 2/3] Revert randn to rand I realized that randn meant rand "normal" --- src/util/non_macro.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/util/non_macro.rs b/src/util/non_macro.rs index 25ad061e..8b2b11ea 100644 --- a/src/util/non_macro.rs +++ b/src/util/non_macro.rs @@ -15,9 +15,9 @@ //! - zeros_shape //! - linspace //! - linspace_with_precision -//! - randn -//! - randn_with_rng -//! - randn_with_distr +//! - rand +//! - rand_with_rng +//! - rand_with_distr //! //! # Numpy like non-macro functions //! @@ -318,9 +318,9 @@ where /// # Description /// /// Range = from 0 to 1 -pub fn randn(r: usize, c: usize) -> Matrix { +pub fn rand(r: usize, c: usize) -> Matrix { let mut rng = thread_rng(); - randn_with_rng(r, c, &mut rng) + rand_with_rng(r, c, &mut rng) } /// Rand matrix with specific rng @@ -328,9 +328,9 @@ pub fn randn(r: usize, c: usize) -> Matrix { /// # Description /// /// Range = from 0 to 1 -pub fn randn_with_rng(r: usize, c: usize, rng: &mut R) -> Matrix { +pub fn rand_with_rng(r: usize, c: usize, rng: &mut R) -> Matrix { let uniform = Uniform::new_inclusive(0f64, 1f64); - randn_with_distr(r, c, rng, uniform) + rand_with_distr(r, c, rng, uniform) } /// Rand matrix with specific rng and distribution @@ -338,7 +338,7 @@ pub fn randn_with_rng(r: usize, c: usize, rng: &mut R) -> Matrix { /// # Description /// /// Any range -pub fn randn_with_distr, R: Rng, D: Distribution>(r: usize, c: usize, rng: &mut R, distr: D) -> Matrix { +pub fn rand_with_distr, R: Rng, D: Distribution>(r: usize, c: usize, rng: &mut R, distr: D) -> Matrix { matrix(rng.sample_iter(distr).take(r*c).collect(), r, c, Row) } From 2e4402beb4dd68dac03ea65471872cdcbd16d9b8 Mon Sep 17 00:00:00 2001 From: Lorenzo Bertini <106469113+bertini97@users.noreply.github.com> Date: Sat, 5 Oct 2024 10:31:04 +0200 Subject: [PATCH 3/3] Dirst to dist Better naming convention --- src/util/non_macro.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/util/non_macro.rs b/src/util/non_macro.rs index 8b2b11ea..8c6f69b2 100644 --- a/src/util/non_macro.rs +++ b/src/util/non_macro.rs @@ -17,7 +17,7 @@ //! - linspace_with_precision //! - rand //! - rand_with_rng -//! - rand_with_distr +//! - rand_with_dist //! //! # Numpy like non-macro functions //! @@ -330,7 +330,7 @@ pub fn rand(r: usize, c: usize) -> Matrix { /// Range = from 0 to 1 pub fn rand_with_rng(r: usize, c: usize, rng: &mut R) -> Matrix { let uniform = Uniform::new_inclusive(0f64, 1f64); - rand_with_distr(r, c, rng, uniform) + rand_with_dist(r, c, rng, uniform) } /// Rand matrix with specific rng and distribution @@ -338,8 +338,8 @@ pub fn rand_with_rng(r: usize, c: usize, rng: &mut R) -> Matrix { /// # Description /// /// Any range -pub fn rand_with_distr, R: Rng, D: Distribution>(r: usize, c: usize, rng: &mut R, distr: D) -> Matrix { - matrix(rng.sample_iter(distr).take(r*c).collect(), r, c, Row) +pub fn rand_with_dist, R: Rng, D: Distribution>(r: usize, c: usize, rng: &mut R, dist: D) -> Matrix { + matrix(rng.sample_iter(dist).take(r*c).collect(), r, c, Row) } // ┌─────────────────────────────────────────────────────────┐