Skip to content

Commit 6372f1a

Browse files
committed
FEAT: Use from_shape_fn_memory_order in ndarray-rand
This simplifies random/random_using methods by skipping the index iteration in the constructor, which is unused anyway and only used for figuring out how many elments to make.
1 parent 443266c commit 6372f1a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

ndarray-rand/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ where
246246
R: Rng + ?Sized,
247247
Sh: ShapeBuilder<Dim = D>,
248248
{
249-
Self::from_shape_fn(shape, |_| dist.sample(rng))
249+
Self::from_shape_fn_memory_order(shape, |_| dist.sample(rng))
250250
}
251251

252252
fn sample_axis(&self, axis: Axis, n_samples: usize, strategy: SamplingStrategy) -> Array<A, D>

ndarray-rand/tests/tests.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use ndarray::{Array, Array2, ArrayView1, Axis};
22
#[cfg(feature = "quickcheck")]
33
use ndarray_rand::rand::{distributions::Distribution, thread_rng};
4+
5+
use ndarray::ShapeBuilder;
46
use ndarray_rand::rand_distr::Uniform;
57
use ndarray_rand::{RandomExt, SamplingStrategy};
68
use quickcheck::quickcheck;
@@ -14,6 +16,21 @@ fn test_dim() {
1416
assert_eq!(a.shape(), &[m, n]);
1517
assert!(a.iter().all(|x| *x < 2.));
1618
assert!(a.iter().all(|x| *x >= 0.));
19+
assert!(a.is_standard_layout());
20+
}
21+
}
22+
}
23+
24+
#[test]
25+
fn test_dim_f() {
26+
let (mm, nn) = (5, 5);
27+
for m in 0..mm {
28+
for n in 0..nn {
29+
let a = Array::random((m, n).f(), Uniform::new(0., 2.));
30+
assert_eq!(a.shape(), &[m, n]);
31+
assert!(a.iter().all(|x| *x < 2.));
32+
assert!(a.iter().all(|x| *x >= 0.));
33+
assert!(a.t().is_standard_layout());
1734
}
1835
}
1936
}

0 commit comments

Comments
 (0)