Skip to content

Commit 0f5af66

Browse files
authored
Use const generics in Dirichlet (#1292)
* Use const generics in Dirichlet * Serialize const arrays with serde_with
1 parent 0f3eced commit 0f5af66

File tree

4 files changed

+25
-52
lines changed

4 files changed

+25
-52
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/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ serde1 = ["serde", "rand/serde1"]
2727
rand = { path = "..", version = "0.9.0", default-features = false }
2828
num-traits = { version = "0.2", default-features = false, features = ["libm"] }
2929
serde = { version = "1.0.103", features = ["derive"], optional = true }
30+
serde_with = { version = "1.14.0", optional = true }
3031

3132
[dev-dependencies]
3233
rand_pcg = { version = "0.4.0", path = "../rand_pcg" }

rand_distr/src/dirichlet.rs

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ 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};
16+
#[cfg(feature = "serde_with")]
17+
use serde_with::serde_as;
1718

1819
/// The Dirichlet distribution `Dirichlet(alpha)`.
1920
///
@@ -27,22 +28,23 @@ use alloc::{boxed::Box, vec, vec::Vec};
2728
/// use rand::prelude::*;
2829
/// use rand_distr::Dirichlet;
2930
///
30-
/// let dirichlet = Dirichlet::new(&[1.0, 2.0, 3.0]).unwrap();
31+
/// let dirichlet = Dirichlet::new([1.0, 2.0, 3.0]).unwrap();
3132
/// let samples = dirichlet.sample(&mut rand::thread_rng());
3233
/// println!("{:?} is from a Dirichlet([1.0, 2.0, 3.0]) distribution", samples);
3334
/// ```
3435
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
36+
#[cfg_attr(feature = "serde_with", serde_as)]
3537
#[derive(Clone, Debug, PartialEq)]
36-
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
37-
pub struct Dirichlet<F>
38+
pub struct Dirichlet<F, const N: usize>
3839
where
3940
F: Float,
4041
StandardNormal: Distribution<F>,
4142
Exp1: Distribution<F>,
4243
Open01: Distribution<F>,
4344
{
4445
/// Concentration parameters (alpha)
45-
alpha: Box<[F]>,
46+
#[cfg_attr(feature = "serde_with", serde_as(as = "[_; N]"))]
47+
alpha: [F; N],
4648
}
4749

4850
/// Error type returned from `Dirchlet::new`.
@@ -72,7 +74,7 @@ impl fmt::Display for Error {
7274
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
7375
impl std::error::Error for Error {}
7476

75-
impl<F> Dirichlet<F>
77+
impl<F, const N: usize> Dirichlet<F, N>
7678
where
7779
F: Float,
7880
StandardNormal: Distribution<F>,
@@ -83,8 +85,8 @@ where
8385
///
8486
/// Requires `alpha.len() >= 2`.
8587
#[inline]
86-
pub fn new(alpha: &[F]) -> Result<Dirichlet<F>, Error> {
87-
if alpha.len() < 2 {
88+
pub fn new(alpha: [F; N]) -> Result<Dirichlet<F, N>, Error> {
89+
if N < 2 {
8890
return Err(Error::AlphaTooShort);
8991
}
9092
for &ai in alpha.iter() {
@@ -93,36 +95,19 @@ where
9395
}
9496
}
9597

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-
})
98+
Ok(Dirichlet { alpha })
11399
}
114100
}
115101

116-
impl<F> Distribution<Vec<F>> for Dirichlet<F>
102+
impl<F, const N: usize> Distribution<[F; N]> for Dirichlet<F, N>
117103
where
118104
F: Float,
119105
StandardNormal: Distribution<F>,
120106
Exp1: Distribution<F>,
121107
Open01: Distribution<F>,
122108
{
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];
109+
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> [F; N] {
110+
let mut samples = [F::zero(); N];
126111
let mut sum = F::zero();
127112

128113
for (s, &a) in samples.iter_mut().zip(self.alpha.iter()) {
@@ -140,27 +125,12 @@ where
140125

141126
#[cfg(test)]
142127
mod test {
128+
use alloc::vec::Vec;
143129
use super::*;
144130

145131
#[test]
146132
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();
133+
let d = Dirichlet::new([1.0, 2.0, 3.0]).unwrap();
164134
let mut rng = crate::test::rng(221);
165135
let samples = d.sample(&mut rng);
166136
let _: Vec<f64> = samples
@@ -175,17 +145,17 @@ mod test {
175145
#[test]
176146
#[should_panic]
177147
fn test_dirichlet_invalid_length() {
178-
Dirichlet::new_with_size(0.5f64, 1).unwrap();
148+
Dirichlet::new([0.5]).unwrap();
179149
}
180150

181151
#[test]
182152
#[should_panic]
183153
fn test_dirichlet_invalid_alpha() {
184-
Dirichlet::new_with_size(0.0f64, 2).unwrap();
154+
Dirichlet::new([0.1, 0.0, 0.3]).unwrap();
185155
}
186156

187157
#[test]
188158
fn dirichlet_distributions_can_be_compared() {
189-
assert_eq!(Dirichlet::new(&[1.0, 2.0]), Dirichlet::new(&[1.0, 2.0]));
159+
assert_eq!(Dirichlet::new([1.0, 2.0]), Dirichlet::new([1.0, 2.0]));
190160
}
191161
}

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)