Skip to content

Commit bf9b798

Browse files
committed
Add F32 newtype to ndarray-rand (a f32 helper)
1 parent 0c0213c commit bf9b798

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

ndarray-rand/src/lib.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use rand::distributions::IndependentSample;
1919
use ndarray::{
2020
ArrayBase,
2121
Dimension,
22-
Data,
2322
DataOwned,
2423
};
2524

@@ -103,3 +102,23 @@ unsafe fn to_vec<I>(iter: I) -> Vec<I::Item>
103102
debug_assert_eq!(size, result.len());
104103
result
105104
}
105+
106+
/// A wrapper type that allows casting f64 distributions to f32
107+
#[derive(Copy, Clone, Debug)]
108+
pub struct F32<S>(pub S);
109+
110+
impl<S> Sample<f32> for F32<S>
111+
where S: Sample<f64>
112+
{
113+
fn sample<R>(&mut self, rng: &mut R) -> f32 where R: Rng {
114+
self.0.sample(rng) as f32
115+
}
116+
}
117+
118+
impl<S> IndependentSample<f32> for F32<S>
119+
where S: IndependentSample<f64>
120+
{
121+
fn ind_sample<R>(&self, rng: &mut R) -> f32 where R: Rng {
122+
self.0.ind_sample(rng) as f32
123+
}
124+
}

0 commit comments

Comments
 (0)