Skip to content

Commit

Permalink
Merge pull request #75 from bertini97/master
Browse files Browse the repository at this point in the history
Rand matrix refactor
  • Loading branch information
Axect authored Oct 5, 2024
2 parents 37680c9 + 2e4402b commit 4eff518
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/util/non_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//! - linspace_with_precision
//! - rand
//! - rand_with_rng
//! - rand_with_dist
//!
//! # Numpy like non-macro functions
//!
Expand All @@ -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},
Expand Down Expand Up @@ -317,14 +319,8 @@ where
///
/// Range = from 0 to 1
pub fn rand(r: usize, c: usize) -> Matrix {
let mut m = zeros(r, c);
let mut rng = thread_rng();
for i in 0..r {
for j in 0..c {
m[(i, j)] = rng.gen_range(0f64..=1f64);
}
}
m
rand_with_rng(r, c, &mut rng)
}

/// Rand matrix with specific rng
Expand All @@ -333,13 +329,17 @@ pub fn rand(r: usize, c: usize) -> Matrix {
///
/// Range = from 0 to 1
pub fn rand_with_rng<R: 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
let uniform = Uniform::new_inclusive(0f64, 1f64);
rand_with_dist(r, c, rng, uniform)
}

/// Rand matrix with specific rng and distribution
///
/// # Description
///
/// Any range
pub fn rand_with_dist<T: Into<f64>, R: Rng, D: Distribution<T>>(r: usize, c: usize, rng: &mut R, dist: D) -> Matrix {
matrix(rng.sample_iter(dist).take(r*c).collect(), r, c, Row)
}

// ┌─────────────────────────────────────────────────────────┐
Expand Down

0 comments on commit 4eff518

Please sign in to comment.