Skip to content

Commit d237595

Browse files
authored
Merge pull request #867 from rust-ndarray/update-rand
Update ndarray-rand to rand 0.8
2 parents fa35a35 + 72eb592 commit d237595

File tree

7 files changed

+59
-38
lines changed

7 files changed

+59
-38
lines changed

ndarray-rand/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[package]
22
name = "ndarray-rand"
3-
version = "0.12.0"
3+
version = "0.13.0"
44
edition = "2018"
55
authors = ["bluss"]
6-
license = "MIT/Apache-2.0"
6+
license = "MIT OR Apache-2.0"
77

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

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

2121
[dependencies.rand]
22-
version = "0.7.0"
22+
version = "0.8.0"
2323
features = ["small_rng"]
2424

2525
[dev-dependencies]
26-
rand_isaac = "0.2.0"
26+
rand_isaac = "0.3.0"
2727
quickcheck = { version = "0.9", default-features = false }
2828

2929
[package.metadata.release]

ndarray-rand/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ fn main() {
2626
Dependencies
2727
============
2828

29-
``ndarray-rand`` depends on ``rand`` 0.7.
29+
``ndarray-rand`` depends on ``rand``.
3030

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

ndarray-rand/RELEASES.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Recent Changes
22
--------------
33

4+
- 0.13.0
5+
6+
- Require ndarray 0.14 (unchanged from previous version)
7+
- Require rand 0.8
8+
- Require rand_distr 0.4
9+
- Fix methods `sample_axis` and `sample_axis_using` so that they can be used on array views too.
10+
411
- 0.12.0
512

613
- Require ndarray 0.14

ndarray-rand/src/lib.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
//!
1313
//! ## Note
1414
//!
15-
//! `ndarray-rand` depends on [`rand` 0.7][rand].
15+
//! `ndarray-rand` depends on [`rand` 0.8][rand].
1616
//!
1717
//! [`rand`][rand] and [`rand_distr`][rand_distr]
1818
//! are re-exported as sub-modules, [`ndarray_rand::rand`](rand/index.html)
1919
//! and [`ndarray_rand::rand_distr`](rand_distr/index.html) respectively.
2020
//! You can use these submodules for guaranteed version compatibility or
2121
//! convenience.
2222
//!
23-
//! [rand]: https://docs.rs/rand/0.7
24-
//! [rand_distr]: https://docs.rs/rand_distr/0.3
23+
//! [rand]: https://docs.rs/rand/0.8
24+
//! [rand_distr]: https://docs.rs/rand_distr/0.4
2525
//!
2626
//! If you want to use a random number generator or distribution from another crate
2727
//! with `ndarray-rand`, you need to make sure that the other crate also depends on the
@@ -35,16 +35,16 @@ use crate::rand::seq::index;
3535
use crate::rand::{thread_rng, Rng, SeedableRng};
3636

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

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

47-
/// [`rand-distr`](https://docs.rs/rand_distr/0.3), re-exported for convenience and version-compatibility.
47+
/// `rand-distr`, re-exported for convenience and version-compatibility.
4848
pub mod rand_distr {
4949
pub use rand_distr::*;
5050
}
@@ -55,16 +55,15 @@ pub mod rand_distr {
5555
/// for other types.
5656
///
5757
/// The default RNG is a fast automatically seeded rng (currently
58-
/// [`rand::rngs::SmallRng`](https://docs.rs/rand/0.7/rand/rngs/struct.SmallRng.html)
59-
/// seeded from [`rand::thread_rng`](https://docs.rs/rand/0.7/rand/fn.thread_rng.html)).
58+
/// [`rand::rngs::SmallRng`], seeded from [`rand::thread_rng`]).
6059
///
6160
/// Note that `SmallRng` is cheap to initialize and fast, but it may generate
6261
/// low-quality random numbers, and reproducibility is not guaranteed. See its
6362
/// documentation for information. You can select a different RNG with
6463
/// [`.random_using()`](#tymethod.random_using).
6564
pub trait RandomExt<S, A, D>
6665
where
67-
S: DataOwned<Elem = A>,
66+
S: RawData<Elem = A>,
6867
D: Dimension,
6968
{
7069
/// Create an array with shape `dim` with elements drawn from
@@ -88,6 +87,7 @@ where
8887
fn random<Sh, IdS>(shape: Sh, distribution: IdS) -> ArrayBase<S, D>
8988
where
9089
IdS: Distribution<S::Elem>,
90+
S: DataOwned<Elem = A>,
9191
Sh: ShapeBuilder<Dim = D>;
9292

9393
/// Create an array with shape `dim` with elements drawn from
@@ -118,6 +118,7 @@ where
118118
where
119119
IdS: Distribution<S::Elem>,
120120
R: Rng + ?Sized,
121+
S: DataOwned<Elem = A>,
121122
Sh: ShapeBuilder<Dim = D>;
122123

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

169171
/// Sample `n_samples` lanes slicing along `axis` using the specified RNG `rng`.
@@ -224,17 +226,19 @@ where
224226
where
225227
R: Rng + ?Sized,
226228
A: Copy,
229+
S: Data<Elem = A>,
227230
D: RemoveAxis;
228231
}
229232

230233
impl<S, A, D> RandomExt<S, A, D> for ArrayBase<S, D>
231234
where
232-
S: DataOwned<Elem = A>,
235+
S: RawData<Elem = A>,
233236
D: Dimension,
234237
{
235238
fn random<Sh, IdS>(shape: Sh, dist: IdS) -> ArrayBase<S, D>
236239
where
237240
IdS: Distribution<S::Elem>,
241+
S: DataOwned<Elem = A>,
238242
Sh: ShapeBuilder<Dim = D>,
239243
{
240244
Self::random_using(shape, dist, &mut get_rng())
@@ -244,6 +248,7 @@ where
244248
where
245249
IdS: Distribution<S::Elem>,
246250
R: Rng + ?Sized,
251+
S: DataOwned<Elem = A>,
247252
Sh: ShapeBuilder<Dim = D>,
248253
{
249254
Self::from_shape_simple_fn(shape, move || dist.sample(rng))
@@ -252,6 +257,7 @@ where
252257
fn sample_axis(&self, axis: Axis, n_samples: usize, strategy: SamplingStrategy) -> Array<A, D>
253258
where
254259
A: Copy,
260+
S: Data<Elem = A>,
255261
D: RemoveAxis,
256262
{
257263
self.sample_axis_using(axis, n_samples, strategy, &mut get_rng())
@@ -267,6 +273,7 @@ where
267273
where
268274
R: Rng + ?Sized,
269275
A: Copy,
276+
S: Data<Elem = A>,
270277
D: RemoveAxis,
271278
{
272279
let indices: Vec<_> = match strategy {
@@ -298,7 +305,7 @@ pub enum SamplingStrategy {
298305
#[cfg(feature = "quickcheck")]
299306
impl Arbitrary for SamplingStrategy {
300307
fn arbitrary<G: Gen>(g: &mut G) -> Self {
301-
if g.gen_bool(0.5) {
308+
if bool::arbitrary(g) {
302309
SamplingStrategy::WithReplacement
303310
} else {
304311
SamplingStrategy::WithoutReplacement

ndarray-rand/tests/tests.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ fn test_dim_f() {
3535
}
3636
}
3737

38+
#[test]
39+
fn sample_axis_on_view() {
40+
let m = 5;
41+
let a = Array::random((m, 4), Uniform::new(0., 2.));
42+
let _samples = a.view().sample_axis(Axis(0), m, SamplingStrategy::WithoutReplacement);
43+
}
44+
3845
#[test]
3946
#[should_panic]
4047
fn oversampling_without_replacement_should_panic() {

numeric-tests/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ publish = false
88
approx = "0.4"
99
ndarray = { path = "..", features = ["approx"] }
1010
ndarray-rand = { path = "../ndarray-rand/" }
11-
rand_distr = "0.3"
11+
rand_distr = "0.4"
1212

1313
[dependencies.rand]
14-
version = "0.7.0"
14+
version = "0.8.0"
1515
features = ["small_rng"]
1616

1717
[lib]

numeric-tests/tests/accuracy.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ fn accurate_eye_f32() {
7676
// pick a few random sizes
7777
let mut rng = SmallRng::from_entropy();
7878
for _ in 0..10 {
79-
let i = rng.gen_range(15, 512);
80-
let j = rng.gen_range(15, 512);
79+
let i = rng.gen_range(15..512);
80+
let j = rng.gen_range(15..512);
8181
println!("Testing size {} by {}", i, j);
8282
let a = gen(Ix2(i, j));
8383
let eye = Array::eye(i);
@@ -104,8 +104,8 @@ fn accurate_eye_f64() {
104104
// pick a few random sizes
105105
let mut rng = SmallRng::from_entropy();
106106
for _ in 0..10 {
107-
let i = rng.gen_range(15, 512);
108-
let j = rng.gen_range(15, 512);
107+
let i = rng.gen_range(15..512);
108+
let j = rng.gen_range(15..512);
109109
println!("Testing size {} by {}", i, j);
110110
let a = gen_f64(Ix2(i, j));
111111
let eye = Array::eye(i);
@@ -121,9 +121,9 @@ fn accurate_mul_f32() {
121121
// pick a few random sizes
122122
let mut rng = SmallRng::from_entropy();
123123
for i in 0..20 {
124-
let m = rng.gen_range(15, 512);
125-
let k = rng.gen_range(15, 512);
126-
let n = rng.gen_range(15, 1560);
124+
let m = rng.gen_range(15..512);
125+
let k = rng.gen_range(15..512);
126+
let n = rng.gen_range(15..1560);
127127
let a = gen(Ix2(m, k));
128128
let b = gen(Ix2(n, k));
129129
let b = b.t();
@@ -145,9 +145,9 @@ fn accurate_mul_f32_general() {
145145
// pick a few random sizes
146146
let mut rng = SmallRng::from_entropy();
147147
for i in 0..20 {
148-
let m = rng.gen_range(15, 512);
149-
let k = rng.gen_range(15, 512);
150-
let n = rng.gen_range(15, 1560);
148+
let m = rng.gen_range(15..512);
149+
let k = rng.gen_range(15..512);
150+
let n = rng.gen_range(15..1560);
151151
let a = gen(Ix2(m, k));
152152
let b = gen(Ix2(n, k));
153153
let mut c = gen(Ix2(m, n));
@@ -171,9 +171,9 @@ fn accurate_mul_f64() {
171171
// pick a few random sizes
172172
let mut rng = SmallRng::from_entropy();
173173
for i in 0..20 {
174-
let m = rng.gen_range(15, 512);
175-
let k = rng.gen_range(15, 512);
176-
let n = rng.gen_range(15, 1560);
174+
let m = rng.gen_range(15..512);
175+
let k = rng.gen_range(15..512);
176+
let n = rng.gen_range(15..1560);
177177
let a = gen_f64(Ix2(m, k));
178178
let b = gen_f64(Ix2(n, k));
179179
let b = b.t();
@@ -195,9 +195,9 @@ fn accurate_mul_f64_general() {
195195
// pick a few random sizes
196196
let mut rng = SmallRng::from_entropy();
197197
for i in 0..20 {
198-
let m = rng.gen_range(15, 512);
199-
let k = rng.gen_range(15, 512);
200-
let n = rng.gen_range(15, 1560);
198+
let m = rng.gen_range(15..512);
199+
let k = rng.gen_range(15..512);
200+
let n = rng.gen_range(15..1560);
201201
let a = gen_f64(Ix2(m, k));
202202
let b = gen_f64(Ix2(n, k));
203203
let mut c = gen_f64(Ix2(m, n));
@@ -221,8 +221,8 @@ fn accurate_mul_with_column_f64() {
221221
// pick a few random sizes
222222
let mut rng = SmallRng::from_entropy();
223223
for i in 0..10 {
224-
let m = rng.gen_range(1, 350);
225-
let k = rng.gen_range(1, 350);
224+
let m = rng.gen_range(1..350);
225+
let k = rng.gen_range(1..350);
226226
let a = gen_f64(Ix2(m, k));
227227
let b_owner = gen_f64(Ix2(k, k));
228228
let b_row_col;

0 commit comments

Comments
 (0)