Skip to content

Commit 34bd7ef

Browse files
committed
Use const generics in Dirichlet
1 parent 0f3eced commit 34bd7ef

File tree

3 files changed

+19
-51
lines changed

3 files changed

+19
-51
lines changed

rand_distr/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Upgrade Rand
1111
- Fix Knuth's method so `Poisson` doesn't return -1.0 for small lambda
1212
- Fix `Poisson` distribution instantiation so it return an error if lambda is infinite
13+
- `Dirichlet` now uses `const` generics, which means that its size is required at compile time (#1292)
14+
- The `Dirichlet::new_with_size` constructor was removed (#1292)
1315

1416
## [0.4.3] - 2021-12-30
1517
- Fix `no_std` build (#1208)

rand_distr/src/dirichlet.rs

Lines changed: 14 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use num_traits::Float;
1313
use crate::{Distribution, Exp1, Gamma, Open01, StandardNormal};
1414
use rand::Rng;
1515
use core::fmt;
16-
use alloc::{boxed::Box, vec, vec::Vec};
1716

1817
/// The Dirichlet distribution `Dirichlet(alpha)`.
1918
///
@@ -27,22 +26,22 @@ use alloc::{boxed::Box, vec, vec::Vec};
2726
/// use rand::prelude::*;
2827
/// use rand_distr::Dirichlet;
2928
///
30-
/// let dirichlet = Dirichlet::new(&[1.0, 2.0, 3.0]).unwrap();
29+
/// let dirichlet = Dirichlet::new([1.0, 2.0, 3.0]).unwrap();
3130
/// let samples = dirichlet.sample(&mut rand::thread_rng());
3231
/// println!("{:?} is from a Dirichlet([1.0, 2.0, 3.0]) distribution", samples);
3332
/// ```
3433
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
3534
#[derive(Clone, Debug, PartialEq)]
3635
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
37-
pub struct Dirichlet<F>
36+
pub struct Dirichlet<F, const N: usize>
3837
where
3938
F: Float,
4039
StandardNormal: Distribution<F>,
4140
Exp1: Distribution<F>,
4241
Open01: Distribution<F>,
4342
{
4443
/// Concentration parameters (alpha)
45-
alpha: Box<[F]>,
44+
alpha: [F; N],
4645
}
4746

4847
/// Error type returned from `Dirchlet::new`.
@@ -72,7 +71,7 @@ impl fmt::Display for Error {
7271
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
7372
impl std::error::Error for Error {}
7473

75-
impl<F> Dirichlet<F>
74+
impl<F, const N: usize> Dirichlet<F, N>
7675
where
7776
F: Float,
7877
StandardNormal: Distribution<F>,
@@ -83,8 +82,8 @@ where
8382
///
8483
/// Requires `alpha.len() >= 2`.
8584
#[inline]
86-
pub fn new(alpha: &[F]) -> Result<Dirichlet<F>, Error> {
87-
if alpha.len() < 2 {
85+
pub fn new(alpha: [F; N]) -> Result<Dirichlet<F, N>, Error> {
86+
if N < 2 {
8887
return Err(Error::AlphaTooShort);
8988
}
9089
for &ai in alpha.iter() {
@@ -93,36 +92,19 @@ where
9392
}
9493
}
9594

96-
Ok(Dirichlet { alpha: alpha.to_vec().into_boxed_slice() })
97-
}
98-
99-
/// Construct a new `Dirichlet` with the given shape parameter `alpha` and `size`.
100-
///
101-
/// Requires `size >= 2`.
102-
#[inline]
103-
pub fn new_with_size(alpha: F, size: usize) -> Result<Dirichlet<F>, Error> {
104-
if !(alpha > F::zero()) {
105-
return Err(Error::AlphaTooSmall);
106-
}
107-
if size < 2 {
108-
return Err(Error::SizeTooSmall);
109-
}
110-
Ok(Dirichlet {
111-
alpha: vec![alpha; size].into_boxed_slice(),
112-
})
95+
Ok(Dirichlet { alpha })
11396
}
11497
}
11598

116-
impl<F> Distribution<Vec<F>> for Dirichlet<F>
99+
impl<F, const N: usize> Distribution<[F; N]> for Dirichlet<F, N>
117100
where
118101
F: Float,
119102
StandardNormal: Distribution<F>,
120103
Exp1: Distribution<F>,
121104
Open01: Distribution<F>,
122105
{
123-
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Vec<F> {
124-
let n = self.alpha.len();
125-
let mut samples = vec![F::zero(); n];
106+
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> [F; N] {
107+
let mut samples = [F::zero(); N];
126108
let mut sum = F::zero();
127109

128110
for (s, &a) in samples.iter_mut().zip(self.alpha.iter()) {
@@ -144,23 +126,7 @@ mod test {
144126

145127
#[test]
146128
fn test_dirichlet() {
147-
let d = Dirichlet::new(&[1.0, 2.0, 3.0]).unwrap();
148-
let mut rng = crate::test::rng(221);
149-
let samples = d.sample(&mut rng);
150-
let _: Vec<f64> = samples
151-
.into_iter()
152-
.map(|x| {
153-
assert!(x > 0.0);
154-
x
155-
})
156-
.collect();
157-
}
158-
159-
#[test]
160-
fn test_dirichlet_with_param() {
161-
let alpha = 0.5f64;
162-
let size = 2;
163-
let d = Dirichlet::new_with_size(alpha, size).unwrap();
129+
let d = Dirichlet::new([1.0, 2.0, 3.0]).unwrap();
164130
let mut rng = crate::test::rng(221);
165131
let samples = d.sample(&mut rng);
166132
let _: Vec<f64> = samples
@@ -175,17 +141,17 @@ mod test {
175141
#[test]
176142
#[should_panic]
177143
fn test_dirichlet_invalid_length() {
178-
Dirichlet::new_with_size(0.5f64, 1).unwrap();
144+
Dirichlet::new([0.5]).unwrap();
179145
}
180146

181147
#[test]
182148
#[should_panic]
183149
fn test_dirichlet_invalid_alpha() {
184-
Dirichlet::new_with_size(0.0f64, 2).unwrap();
150+
Dirichlet::new([0.1, 0.0, 0.3]).unwrap();
185151
}
186152

187153
#[test]
188154
fn dirichlet_distributions_can_be_compared() {
189-
assert_eq!(Dirichlet::new(&[1.0, 2.0]), Dirichlet::new(&[1.0, 2.0]));
155+
assert_eq!(Dirichlet::new([1.0, 2.0]), Dirichlet::new([1.0, 2.0]));
190156
}
191157
}

rand_distr/tests/value_stability.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,10 @@ fn weibull_stability() {
348348
fn dirichlet_stability() {
349349
let mut rng = get_rng(223);
350350
assert_eq!(
351-
rng.sample(Dirichlet::new(&[1.0, 2.0, 3.0]).unwrap()),
352-
vec![0.12941567177708177, 0.4702121891675036, 0.4003721390554146]
351+
rng.sample(Dirichlet::new([1.0, 2.0, 3.0]).unwrap()),
352+
[0.12941567177708177, 0.4702121891675036, 0.4003721390554146]
353353
);
354-
assert_eq!(rng.sample(Dirichlet::new_with_size(8.0, 5).unwrap()), vec![
354+
assert_eq!(rng.sample(Dirichlet::new([8.0; 5]).unwrap()), [
355355
0.17684200044809556,
356356
0.29915953935953055,
357357
0.1832858056608014,

0 commit comments

Comments
 (0)