Skip to content

Commit 7a2f236

Browse files
committed
Optimize struct size of Beta
1 parent 9a98b60 commit 7a2f236

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

rand_distr/src/gamma.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -544,8 +544,7 @@ where
544544
F: Float,
545545
Open01: Distribution<F>,
546546
{
547-
a: F, a0: F,
548-
b: F, b0: F,
547+
a: F, b: F, switched_params: bool,
549548
algorithm: BetaAlgorithm<F>,
550549
}
551550

@@ -587,15 +586,19 @@ where
587586
// From now on, we use the notation from the reference,
588587
// i.e. `alpha` and `beta` are renamed to `a0` and `b0`.
589588
let (a0, b0) = (alpha, beta);
590-
let (a, b) = if a0 < b0 { (a0, b0) } else { (b0, a0) };
589+
let (a, b, switched_params) = if a0 < b0 {
590+
(a0, b0, false)
591+
} else {
592+
(b0, a0, true)
593+
};
591594
if alpha > F::one() {
592595
let alpha = a + b;
593596
let beta = ((alpha - F::from(2.).unwrap())
594597
/ (F::from(2.).unwrap()*a*b - alpha)).sqrt();
595598
let gamma = a + F::one() / beta;
596599

597600
Ok(Beta {
598-
a, a0, b, b0,
601+
a, b, switched_params,
599602
algorithm: BetaAlgorithm::BB(BB {
600603
alpha, beta, gamma,
601604
})
@@ -611,7 +614,7 @@ where
611614
+ (F::from(0.5).unwrap() + F::from(0.25).unwrap()/delta)*b;
612615

613616
Ok(Beta {
614-
a, a0, b, b0,
617+
a, b, switched_params,
615618
algorithm: BetaAlgorithm::BC(BC {
616619
alpha, beta, delta, kappa1, kappa2,
617620
})
@@ -654,7 +657,7 @@ where
654657
}
655658
}
656659
// 5.
657-
if self.a == self.a0 {
660+
if !self.switched_params {
658661
w / (self.b + w)
659662
} else {
660663
self.b / (self.b + w)
@@ -696,7 +699,7 @@ where
696699
};
697700
}
698701
// 6.
699-
if self.a == self.a0 {
702+
if !self.switched_params {
700703
if w == F::infinity() {
701704
// Assuming `b` is finite, for large `w`:
702705
return F::one();

0 commit comments

Comments
 (0)