xsf/scipy_special/spfun_stats.rs
1//! Translated from `scipy/special/_spfun_stats.py`
2
3use crate::utils::LN_PI;
4
5/// Log of multivariate gamma, $\ln \Gamma_d(a)$, sometimes called the generalized gamma
6///
7/// Pure rust translation of [`scipy.special.multigammaln`][scipy].
8///
9/// [scipy]: https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.multigammaln.html
10///
11/// # Definition
12///
13/// The multivariate gamma function of dimension $d$ for real $a$ is
14///
15/// $$
16/// \Gamma_d(a) = \pi^{d (d-1) \over 4} \prod_{j=0}^{d-1} \Gamma\left(a - {j \over 2}\right)
17/// $$
18///
19/// # Panics
20/// - if $a \le (d - 1) / 2$
21///
22/// # See also
23/// - [`gamma`](crate::gamma): Gamma function $\Gamma$
24#[must_use]
25#[inline]
26pub fn multigammaln(a: f64, d: u32) -> f64 {
27 let d_f64 = f64::from(d);
28 let a_min = 0.5 * (d_f64 - 1.0);
29 assert!(
30 a > a_min,
31 "condition a ({a}) > 0.5 * (d-1) ({a_min}) not met"
32 );
33
34 // res = (d * (d-1) * 0.25) * np.log(np.pi)
35 // res += np.sum(loggam([(a - (j - 1.)/2) for j in range(1, d+1)]), axis=0)
36 // return res
37 d_f64 * (d_f64 - 1.0) * 0.25 * LN_PI
38 + (0..d)
39 .map(|j| crate::gammaln(a - f64::from(j) / 2.0))
40 .sum::<f64>()
41}