Skip to content

Commit a364cc8

Browse files
Re-export rand distributions (#663)
* Re-export distribution trait and implemented dists * Export from the crate module * Adapt doc tests * Run cargo fmt * Re-export the whole rand crate. Adjust docs, readme and examples. * Refactor ndarray-rand readme and changelog. * Re-export rand-distr. Update docs accordingly. Add hyperlinks where helpful. * Keep stuff in sync * Update docs to use rand_distr * Add doc example to random_using with seedable rng (Isaac64) * Run cargo fmt * Remove extern crate from doc tests * Remove extern crate from README
1 parent 7d04eb7 commit a364cc8

File tree

6 files changed

+174
-102
lines changed

6 files changed

+174
-102
lines changed

ndarray-rand/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@ license = "MIT/Apache-2.0"
77

88
repository = "https://github.com/rust-ndarray/ndarray"
99
documentation = "https://docs.rs/ndarray-rand/"
10+
readme = "README.md"
1011

1112
description = "Constructors for randomized arrays. `rand` integration for `ndarray`."
1213

1314
keywords = ["multidimensional", "matrix", "rand", "ndarray"]
1415

1516
[dependencies]
1617
ndarray = { version = "0.12.0", path = ".." }
18+
rand_distr = "0.2.1"
1719

1820
[dependencies.rand]
1921
version = "0.7.0"
2022
features = ["small_rng"]
2123

2224
[dev-dependencies]
23-
rand_distr = "0.2.1"
25+
rand_isaac = "0.2.0"
2426

2527
[package.metadata.release]
2628
no-dev-version = true

ndarray-rand/README.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
ndarray-rand
2+
============
3+
4+
Constructors for randomized arrays: `rand`'s integration with `ndarray`.
5+
6+
Example
7+
=======
8+
9+
Generate a 2-dimensional array with shape `(2,5)` and elements drawn from a uniform distribution
10+
over the `(0., 10.)` interval:
11+
12+
```rust
13+
use ndarray::Array;
14+
use ndarray_rand::RandomExt;
15+
use ndarray_rand::rand_distr::Uniform;
16+
17+
fn main() {
18+
let a = Array::random((2, 5), Uniform::new(0., 10.));
19+
println!("{:8.4}", a);
20+
// Example Output:
21+
// [[ 8.6900, 6.9824, 3.8922, 6.5861, 2.4890],
22+
// [ 0.0914, 5.5186, 5.8135, 5.2361, 3.1879]]
23+
}
24+
```
25+
26+
Dependencies
27+
============
28+
29+
``ndarray-rand`` depends on ``rand`` 0.7.
30+
31+
[`rand`](https://docs.rs/rand/0.7.0/rand/) and [`rand-distr`](https://docs.rs/rand_distr/0.2.1/rand_distr/) are
32+
re-exported as sub-modules, `ndarray_rand::rand` and `ndarray_rand::rand_distr` respectively.
33+
Please rely on these submodules for guaranteed version compatibility.
34+
35+
If you want to use a random number generator or distribution from another crate
36+
with `ndarray-rand`, you need to make sure that the other crate also depends on the
37+
same version of `rand`. Otherwise, the compiler may return errors saying
38+
that the items are not compatible (e.g. that a type doesn't implement a
39+
necessary trait).
40+
41+
Recent changes
42+
==============
43+
44+
0.10.0
45+
------
46+
47+
- Require `rand` 0.7
48+
- Require Rust 1.32 or later
49+
- Re-export `rand` as a submodule, `ndarray_rand::rand`
50+
- Re-export `rand-distr` as a submodule, `ndarray_rand::rand_distr`
51+
52+
Check _[Changelogs](https://github.com/rust-ndarray/ndarray/ndarray-rand/RELEASES.md)_ to see
53+
the changes introduced in previous releases.
54+
55+
56+
License
57+
=======
58+
59+
Dual-licensed to be compatible with the Rust project.
60+
61+
Licensed under the Apache License, Version 2.0
62+
http://www.apache.org/licenses/LICENSE-2.0 or the MIT license
63+
http://opensource.org/licenses/MIT, at your
64+
option. This file may not be copied, modified, or distributed
65+
except according to those terms.

ndarray-rand/README.rst

-76
This file was deleted.

ndarray-rand/RELEASES.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Recent Changes
2+
--------------
3+
4+
- 0.10.0
5+
6+
- Require `rand` 0.7
7+
- Require Rust 1.32 or later
8+
- Re-export `rand` as a submodule, `ndarray_rand::rand`
9+
- Re-export `rand-distr` as a submodule, `ndarray_rand::rand_distr`
10+
11+
- 0.9.0
12+
13+
- Require rand 0.6
14+
15+
- 0.8.0
16+
17+
- Require ndarray 0.12
18+
- Require rand 0.5
19+
20+
- 0.7.0
21+
22+
- Require ndarray 0.11
23+
- Require rand 0.4
24+
25+
- 0.6.1
26+
27+
- Clean up implementation of ``Array::random`` by @v-shmyhlo
28+
29+
- 0.6.0
30+
31+
- Require ndarray 0.10.0
32+
33+
- 0.5.0
34+
35+
- Require ndarray 0.9
36+
37+
- 0.4.0
38+
39+
- Require ndarray 0.8
40+
41+
- 0.3.0
42+
43+
- Require ndarray 0.7
44+
45+
- 0.2.0
46+
47+
- Require ndarray 0.6
48+
49+
- 0.1.0
50+
51+
- Initial release

ndarray-rand/src/lib.rs

+54-24
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,50 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
//! Constructors for randomized arrays. `rand` integration for `ndarray`.
9+
//! Constructors for randomized arrays: `rand` integration for `ndarray`.
1010
//!
1111
//! See [**`RandomExt`**](trait.RandomExt.html) for usage examples.
1212
//!
13-
//! **Note:** `ndarray-rand` depends on `rand` 0.7. If you use any other items
14-
//! from `rand`, you need to specify a compatible version of `rand` in your
15-
//! `Cargo.toml`. If you want to use a RNG or distribution from another crate
16-
//! with `ndarray-rand`, you need to make sure that crate also depends on the
17-
//! correct version of `rand`. Otherwise, the compiler will return errors
18-
//! saying that the items are not compatible (e.g. that a type doesn't
19-
//! implement a necessary trait).
13+
//! ## Note
14+
//!
15+
//! `ndarray-rand` depends on [`rand` 0.7.0](https://docs.rs/rand/0.7.0/rand/).
16+
//!
17+
//! [`rand`](https://docs.rs/rand/0.7.0/rand/) and [`rand-distr`](https://docs.rs/rand_distr/0.2.1/rand_distr/)
18+
//! are re-exported as sub-modules, [`ndarray_rand::rand`](rand/index.html)
19+
//! and [`ndarray_rand::rand_distr`](rand_distr/index.html) respectively.
20+
//! Please rely on these submodules for guaranteed version compatibility.
21+
//!
22+
//! If you want to use a random number generator or distribution from another crate
23+
//! with `ndarray-rand`, you need to make sure that the other crate also depends on the
24+
//! same version of `rand`. Otherwise, the compiler will return errors saying
25+
//! that the items are not compatible (e.g. that a type doesn't implement a
26+
//! necessary trait).
2027
21-
use rand::distributions::Distribution;
22-
use rand::rngs::SmallRng;
23-
use rand::{thread_rng, Rng, SeedableRng};
28+
use crate::rand::distributions::Distribution;
29+
use crate::rand::rngs::SmallRng;
30+
use crate::rand::{thread_rng, Rng, SeedableRng};
2431

2532
use ndarray::ShapeBuilder;
2633
use ndarray::{ArrayBase, DataOwned, Dimension};
2734

35+
/// [`rand`](https://docs.rs/rand/0.7.0/rand/), re-exported for convenience and version-compatibility.
36+
pub mod rand {
37+
pub use rand::*;
38+
}
39+
40+
/// [`rand-distr`](https://docs.rs/rand_distr/0.2.1/rand_distr/), re-exported for convenience and version-compatibility.
41+
pub mod rand_distr {
42+
pub use rand_distr::*;
43+
}
44+
2845
/// Constructors for n-dimensional arrays with random elements.
2946
///
3047
/// This trait extends ndarray’s `ArrayBase` and can not be implemented
3148
/// for other types.
3249
///
3350
/// The default RNG is a fast automatically seeded rng (currently
34-
/// [`rand::rngs::SmallRng`](https://docs.rs/rand/0.5/rand/rngs/struct.SmallRng.html)
35-
/// seeded from [`rand::thread_rng`](https://docs.rs/rand/0.5/rand/fn.thread_rng.html)).
51+
/// [`rand::rngs::SmallRng`](https://docs.rs/rand/0.7/rand/rngs/struct.SmallRng.html)
52+
/// seeded from [`rand::thread_rng`](https://docs.rs/rand/0.7/rand/fn.thread_rng.html)).
3653
///
3754
/// Note that `SmallRng` is cheap to initialize and fast, but it may generate
3855
/// low-quality random numbers, and reproducibility is not guaranteed. See its
@@ -50,13 +67,9 @@ where
5067
/// overflows usize.
5168
///
5269
/// ```
53-
/// extern crate rand;
54-
/// extern crate ndarray;
55-
/// extern crate ndarray_rand;
56-
///
57-
/// use rand::distributions::Uniform;
5870
/// use ndarray::Array;
5971
/// use ndarray_rand::RandomExt;
72+
/// use ndarray_rand::rand_distr::Uniform;
6073
///
6174
/// # fn main() {
6275
/// let a = Array::random((2, 5), Uniform::new(0., 10.));
@@ -74,6 +87,26 @@ where
7487
/// `distribution`, using a specific Rng `rng`.
7588
///
7689
/// ***Panics*** if the number of elements overflows usize.
90+
///
91+
/// ```
92+
/// use ndarray::Array;
93+
/// use ndarray_rand::RandomExt;
94+
/// use ndarray_rand::rand::SeedableRng;
95+
/// use ndarray_rand::rand_distr::Uniform;
96+
/// use rand_isaac::isaac64::Isaac64Rng;
97+
///
98+
/// # fn main() {
99+
/// // Get a seeded random number generator for reproducibility (Isaac64 algorithm)
100+
/// let seed = 42;
101+
/// let mut rng = Isaac64Rng::seed_from_u64(seed);
102+
///
103+
/// // Generate a random array using `rng`
104+
/// let a = Array::random_using((2, 5), Uniform::new(0., 10.), &mut rng);
105+
/// println!("{:8.4}", a);
106+
/// // Example Output:
107+
/// // [[ 8.6900, 6.9824, 3.8922, 6.5861, 2.4890],
108+
/// // [ 0.0914, 5.5186, 5.8135, 5.2361, 3.1879]]
109+
/// # }
77110
fn random_using<Sh, IdS, R>(shape: Sh, distribution: IdS, rng: &mut R) -> ArrayBase<S, D>
78111
where
79112
IdS: Distribution<S::Elem>,
@@ -109,16 +142,13 @@ where
109142
/// A wrapper type that allows casting f64 distributions to f32
110143
///
111144
/// ```
112-
/// extern crate rand;
113-
/// extern crate ndarray;
114-
/// extern crate ndarray_rand;
115-
///
116-
/// use rand::distributions::Normal;
117145
/// use ndarray::Array;
118146
/// use ndarray_rand::{RandomExt, F32};
147+
/// use ndarray_rand::rand_distr::Normal;
119148
///
120149
/// # fn main() {
121-
/// let a = Array::random((2, 5), F32(Normal::new(0., 1.)));
150+
/// let distribution_f64 = Normal::new(0., 1.).expect("Failed to create normal distribution");
151+
/// let a = Array::random((2, 5), F32(distribution_f64));
122152
/// println!("{:8.4}", a);
123153
/// // Example Output:
124154
/// // [[ -0.6910, 1.1730, 1.0902, -0.4092, -1.7340],

ndarray-rand/tests/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ndarray::Array;
2+
use ndarray_rand::rand_distr::Uniform;
23
use ndarray_rand::RandomExt;
3-
use rand::distributions::Uniform;
44

55
#[test]
66
fn test_dim() {

0 commit comments

Comments
 (0)