Skip to content

Commit

Permalink
Merge pull request #867 from rust-ndarray/update-rand
Browse files Browse the repository at this point in the history
Update ndarray-rand to rand 0.8
  • Loading branch information
bluss committed Dec 21, 2020
2 parents fa35a35 + 72eb592 commit d237595
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 38 deletions.
10 changes: 5 additions & 5 deletions ndarray-rand/Cargo.toml
@@ -1,9 +1,9 @@
[package]
name = "ndarray-rand"
version = "0.12.0"
version = "0.13.0"
edition = "2018"
authors = ["bluss"]
license = "MIT/Apache-2.0"
license = "MIT OR Apache-2.0"

repository = "https://github.com/rust-ndarray/ndarray"
documentation = "https://docs.rs/ndarray-rand/"
Expand All @@ -15,15 +15,15 @@ keywords = ["multidimensional", "matrix", "rand", "ndarray"]

[dependencies]
ndarray = { version = "0.14", path = ".." }
rand_distr = "0.3.0"
rand_distr = "0.4.0"
quickcheck = { version = "0.9", default-features = false, optional = true }

[dependencies.rand]
version = "0.7.0"
version = "0.8.0"
features = ["small_rng"]

[dev-dependencies]
rand_isaac = "0.2.0"
rand_isaac = "0.3.0"
quickcheck = { version = "0.9", default-features = false }

[package.metadata.release]
Expand Down
4 changes: 2 additions & 2 deletions ndarray-rand/README.md
Expand Up @@ -26,9 +26,9 @@ fn main() {
Dependencies
============

``ndarray-rand`` depends on ``rand`` 0.7.
``ndarray-rand`` depends on ``rand``.

[`rand`](https://docs.rs/rand/0.7.0/rand/) and [`rand-distr`](https://docs.rs/rand_distr/0.3/) are
[`rand`](https://docs.rs/rand/) and [`rand-distr`](https://docs.rs/rand_distr/) are
re-exported as sub-modules, `ndarray_rand::rand` and `ndarray_rand::rand_distr` respectively.
Please rely on these submodules for guaranteed version compatibility.

Expand Down
7 changes: 7 additions & 0 deletions ndarray-rand/RELEASES.md
@@ -1,6 +1,13 @@
Recent Changes
--------------

- 0.13.0

- Require ndarray 0.14 (unchanged from previous version)
- Require rand 0.8
- Require rand_distr 0.4
- Fix methods `sample_axis` and `sample_axis_using` so that they can be used on array views too.

- 0.12.0

- Require ndarray 0.14
Expand Down
29 changes: 18 additions & 11 deletions ndarray-rand/src/lib.rs
Expand Up @@ -12,16 +12,16 @@
//!
//! ## Note
//!
//! `ndarray-rand` depends on [`rand` 0.7][rand].
//! `ndarray-rand` depends on [`rand` 0.8][rand].
//!
//! [`rand`][rand] and [`rand_distr`][rand_distr]
//! are re-exported as sub-modules, [`ndarray_rand::rand`](rand/index.html)
//! and [`ndarray_rand::rand_distr`](rand_distr/index.html) respectively.
//! You can use these submodules for guaranteed version compatibility or
//! convenience.
//!
//! [rand]: https://docs.rs/rand/0.7
//! [rand_distr]: https://docs.rs/rand_distr/0.3
//! [rand]: https://docs.rs/rand/0.8
//! [rand_distr]: https://docs.rs/rand_distr/0.4
//!
//! If you want to use a random number generator or distribution from another crate
//! with `ndarray-rand`, you need to make sure that the other crate also depends on the
Expand All @@ -35,16 +35,16 @@ use crate::rand::seq::index;
use crate::rand::{thread_rng, Rng, SeedableRng};

use ndarray::{Array, Axis, RemoveAxis, ShapeBuilder};
use ndarray::{ArrayBase, DataOwned, Dimension};
use ndarray::{ArrayBase, DataOwned, RawData, Data, Dimension};
#[cfg(feature = "quickcheck")]
use quickcheck::{Arbitrary, Gen};

/// [`rand`](https://docs.rs/rand/0.7), re-exported for convenience and version-compatibility.
/// `rand`, re-exported for convenience and version-compatibility.
pub mod rand {
pub use rand::*;
}

/// [`rand-distr`](https://docs.rs/rand_distr/0.3), re-exported for convenience and version-compatibility.
/// `rand-distr`, re-exported for convenience and version-compatibility.
pub mod rand_distr {
pub use rand_distr::*;
}
Expand All @@ -55,16 +55,15 @@ pub mod rand_distr {
/// for other types.
///
/// The default RNG is a fast automatically seeded rng (currently
/// [`rand::rngs::SmallRng`](https://docs.rs/rand/0.7/rand/rngs/struct.SmallRng.html)
/// seeded from [`rand::thread_rng`](https://docs.rs/rand/0.7/rand/fn.thread_rng.html)).
/// [`rand::rngs::SmallRng`], seeded from [`rand::thread_rng`]).
///
/// Note that `SmallRng` is cheap to initialize and fast, but it may generate
/// low-quality random numbers, and reproducibility is not guaranteed. See its
/// documentation for information. You can select a different RNG with
/// [`.random_using()`](#tymethod.random_using).
pub trait RandomExt<S, A, D>
where
S: DataOwned<Elem = A>,
S: RawData<Elem = A>,
D: Dimension,
{
/// Create an array with shape `dim` with elements drawn from
Expand All @@ -88,6 +87,7 @@ where
fn random<Sh, IdS>(shape: Sh, distribution: IdS) -> ArrayBase<S, D>
where
IdS: Distribution<S::Elem>,
S: DataOwned<Elem = A>,
Sh: ShapeBuilder<Dim = D>;

/// Create an array with shape `dim` with elements drawn from
Expand Down Expand Up @@ -118,6 +118,7 @@ where
where
IdS: Distribution<S::Elem>,
R: Rng + ?Sized,
S: DataOwned<Elem = A>,
Sh: ShapeBuilder<Dim = D>;

/// Sample `n_samples` lanes slicing along `axis` using the default RNG.
Expand Down Expand Up @@ -164,6 +165,7 @@ where
fn sample_axis(&self, axis: Axis, n_samples: usize, strategy: SamplingStrategy) -> Array<A, D>
where
A: Copy,
S: Data<Elem = A>,
D: RemoveAxis;

/// Sample `n_samples` lanes slicing along `axis` using the specified RNG `rng`.
Expand Down Expand Up @@ -224,17 +226,19 @@ where
where
R: Rng + ?Sized,
A: Copy,
S: Data<Elem = A>,
D: RemoveAxis;
}

impl<S, A, D> RandomExt<S, A, D> for ArrayBase<S, D>
where
S: DataOwned<Elem = A>,
S: RawData<Elem = A>,
D: Dimension,
{
fn random<Sh, IdS>(shape: Sh, dist: IdS) -> ArrayBase<S, D>
where
IdS: Distribution<S::Elem>,
S: DataOwned<Elem = A>,
Sh: ShapeBuilder<Dim = D>,
{
Self::random_using(shape, dist, &mut get_rng())
Expand All @@ -244,6 +248,7 @@ where
where
IdS: Distribution<S::Elem>,
R: Rng + ?Sized,
S: DataOwned<Elem = A>,
Sh: ShapeBuilder<Dim = D>,
{
Self::from_shape_simple_fn(shape, move || dist.sample(rng))
Expand All @@ -252,6 +257,7 @@ where
fn sample_axis(&self, axis: Axis, n_samples: usize, strategy: SamplingStrategy) -> Array<A, D>
where
A: Copy,
S: Data<Elem = A>,
D: RemoveAxis,
{
self.sample_axis_using(axis, n_samples, strategy, &mut get_rng())
Expand All @@ -267,6 +273,7 @@ where
where
R: Rng + ?Sized,
A: Copy,
S: Data<Elem = A>,
D: RemoveAxis,
{
let indices: Vec<_> = match strategy {
Expand Down Expand Up @@ -298,7 +305,7 @@ pub enum SamplingStrategy {
#[cfg(feature = "quickcheck")]
impl Arbitrary for SamplingStrategy {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
if g.gen_bool(0.5) {
if bool::arbitrary(g) {
SamplingStrategy::WithReplacement
} else {
SamplingStrategy::WithoutReplacement
Expand Down
7 changes: 7 additions & 0 deletions ndarray-rand/tests/tests.rs
Expand Up @@ -35,6 +35,13 @@ fn test_dim_f() {
}
}

#[test]
fn sample_axis_on_view() {
let m = 5;
let a = Array::random((m, 4), Uniform::new(0., 2.));
let _samples = a.view().sample_axis(Axis(0), m, SamplingStrategy::WithoutReplacement);
}

#[test]
#[should_panic]
fn oversampling_without_replacement_should_panic() {
Expand Down
4 changes: 2 additions & 2 deletions numeric-tests/Cargo.toml
Expand Up @@ -8,10 +8,10 @@ publish = false
approx = "0.4"
ndarray = { path = "..", features = ["approx"] }
ndarray-rand = { path = "../ndarray-rand/" }
rand_distr = "0.3"
rand_distr = "0.4"

[dependencies.rand]
version = "0.7.0"
version = "0.8.0"
features = ["small_rng"]

[lib]
Expand Down
36 changes: 18 additions & 18 deletions numeric-tests/tests/accuracy.rs
Expand Up @@ -76,8 +76,8 @@ fn accurate_eye_f32() {
// pick a few random sizes
let mut rng = SmallRng::from_entropy();
for _ in 0..10 {
let i = rng.gen_range(15, 512);
let j = rng.gen_range(15, 512);
let i = rng.gen_range(15..512);
let j = rng.gen_range(15..512);
println!("Testing size {} by {}", i, j);
let a = gen(Ix2(i, j));
let eye = Array::eye(i);
Expand All @@ -104,8 +104,8 @@ fn accurate_eye_f64() {
// pick a few random sizes
let mut rng = SmallRng::from_entropy();
for _ in 0..10 {
let i = rng.gen_range(15, 512);
let j = rng.gen_range(15, 512);
let i = rng.gen_range(15..512);
let j = rng.gen_range(15..512);
println!("Testing size {} by {}", i, j);
let a = gen_f64(Ix2(i, j));
let eye = Array::eye(i);
Expand All @@ -121,9 +121,9 @@ fn accurate_mul_f32() {
// pick a few random sizes
let mut rng = SmallRng::from_entropy();
for i in 0..20 {
let m = rng.gen_range(15, 512);
let k = rng.gen_range(15, 512);
let n = rng.gen_range(15, 1560);
let m = rng.gen_range(15..512);
let k = rng.gen_range(15..512);
let n = rng.gen_range(15..1560);
let a = gen(Ix2(m, k));
let b = gen(Ix2(n, k));
let b = b.t();
Expand All @@ -145,9 +145,9 @@ fn accurate_mul_f32_general() {
// pick a few random sizes
let mut rng = SmallRng::from_entropy();
for i in 0..20 {
let m = rng.gen_range(15, 512);
let k = rng.gen_range(15, 512);
let n = rng.gen_range(15, 1560);
let m = rng.gen_range(15..512);
let k = rng.gen_range(15..512);
let n = rng.gen_range(15..1560);
let a = gen(Ix2(m, k));
let b = gen(Ix2(n, k));
let mut c = gen(Ix2(m, n));
Expand All @@ -171,9 +171,9 @@ fn accurate_mul_f64() {
// pick a few random sizes
let mut rng = SmallRng::from_entropy();
for i in 0..20 {
let m = rng.gen_range(15, 512);
let k = rng.gen_range(15, 512);
let n = rng.gen_range(15, 1560);
let m = rng.gen_range(15..512);
let k = rng.gen_range(15..512);
let n = rng.gen_range(15..1560);
let a = gen_f64(Ix2(m, k));
let b = gen_f64(Ix2(n, k));
let b = b.t();
Expand All @@ -195,9 +195,9 @@ fn accurate_mul_f64_general() {
// pick a few random sizes
let mut rng = SmallRng::from_entropy();
for i in 0..20 {
let m = rng.gen_range(15, 512);
let k = rng.gen_range(15, 512);
let n = rng.gen_range(15, 1560);
let m = rng.gen_range(15..512);
let k = rng.gen_range(15..512);
let n = rng.gen_range(15..1560);
let a = gen_f64(Ix2(m, k));
let b = gen_f64(Ix2(n, k));
let mut c = gen_f64(Ix2(m, n));
Expand All @@ -221,8 +221,8 @@ fn accurate_mul_with_column_f64() {
// pick a few random sizes
let mut rng = SmallRng::from_entropy();
for i in 0..10 {
let m = rng.gen_range(1, 350);
let k = rng.gen_range(1, 350);
let m = rng.gen_range(1..350);
let k = rng.gen_range(1..350);
let a = gen_f64(Ix2(m, k));
let b_owner = gen_f64(Ix2(k, k));
let b_row_col;
Expand Down

0 comments on commit d237595

Please sign in to comment.