Skip to content

Commit

Permalink
rand_distr: fix unit circle and sphere type specifications
Browse files Browse the repository at this point in the history
These are required as a side-effect of the previous generalisation.
  • Loading branch information
dhardy committed May 14, 2019
1 parent ef59613 commit b330c21
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
18 changes: 13 additions & 5 deletions rand_distr/src/unit_circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::utils::Float;
/// ```
/// use rand_distr::{UnitCircle, Distribution};
///
/// let v = UnitCircle.sample(&mut rand::thread_rng());
/// let v: [f64; 2] = UnitCircle.sample(&mut rand::thread_rng());
/// println!("{:?} is from the unit circle.", v)
/// ```
///
Expand Down Expand Up @@ -76,16 +76,24 @@ mod tests {
fn norm() {
let mut rng = crate::test::rng(1);
for _ in 0..1000 {
let x = UnitCircle.sample(&mut rng);
let x: [f64; 2] = UnitCircle.sample(&mut rng);
assert_almost_eq!(x[0]*x[0] + x[1]*x[1], 1., 1e-15);
}
}

#[test]
fn value_stability() {
let mut rng = crate::test::rng(2);
assert_eq!(UnitCircle.sample(&mut rng), [-0.8032118336637037, 0.5956935036263119]);
assert_eq!(UnitCircle.sample(&mut rng), [-0.4742919588505423, -0.880367615130018]);
assert_eq!(UnitCircle.sample(&mut rng), [0.9297328981467168, 0.368234623716601]);
let expected = [
[-0.8032118336637037, 0.5956935036263119],
[-0.4742919588505423, -0.880367615130018],
[0.9297328981467168, 0.368234623716601],
];
let samples: [[f64; 2]; 3] = [
UnitCircle.sample(&mut rng),
UnitCircle.sample(&mut rng),
UnitCircle.sample(&mut rng),
];
assert_eq!(samples, expected);
}
}
21 changes: 13 additions & 8 deletions rand_distr/src/unit_sphere.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::utils::Float;
/// ```
/// use rand_distr::{UnitSphereSurface, Distribution};
///
/// let v = UnitSphereSurface.sample(&mut rand::thread_rng());
/// let v: [f64; 3] = UnitSphereSurface.sample(&mut rand::thread_rng());
/// println!("{:?} is from the unit sphere surface.", v)
/// ```
///
Expand Down Expand Up @@ -71,19 +71,24 @@ mod tests {
fn norm() {
let mut rng = crate::test::rng(1);
for _ in 0..1000 {
let x = UnitSphereSurface.sample(&mut rng);
let x: [f64; 3] = UnitSphereSurface.sample(&mut rng);
assert_almost_eq!(x[0]*x[0] + x[1]*x[1] + x[2]*x[2], 1., 1e-15);
}
}

#[test]
fn value_stability() {
let mut rng = crate::test::rng(2);
assert_eq!(UnitSphereSurface.sample(&mut rng),
[-0.24950027180862533, -0.7552572587896719, 0.6060825747478084]);
assert_eq!(UnitSphereSurface.sample(&mut rng),
[0.47604534507233487, -0.797200864987207, -0.3712837328763685]);
assert_eq!(UnitSphereSurface.sample(&mut rng),
[0.9795722330927367, 0.18692349236651176, 0.07414747571708524]);
let expected = [
[-0.24950027180862533, -0.7552572587896719, 0.6060825747478084],
[0.47604534507233487, -0.797200864987207, -0.3712837328763685],
[0.9795722330927367, 0.18692349236651176, 0.07414747571708524],
];
let samples: [[f64; 3]; 3] = [
UnitSphereSurface.sample(&mut rng),
UnitSphereSurface.sample(&mut rng),
UnitSphereSurface.sample(&mut rng),
];
assert_eq!(samples, expected);
}
}

0 comments on commit b330c21

Please sign in to comment.