xsf/scipy_special/polygamma.rs
1//! Translated from <https://github.com/scipy/scipy/blob/38ecfef/scipy/special/_basic.py#L1539-L1579>
2
3use crate::utils::neg_one_pow;
4
5/// Polygamma function $\psi^{(n)}(x)$
6///
7/// The $n$-th derivative of the [`digamma`](crate::digamma) function.
8///
9/// Corresponds to [`scipy.special.polygamma`][polygamma] in scipy
10///
11/// [polygamma]: https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.polygamma.html
12///
13/// # See also
14/// - [`digamma`](crate::digamma): Digamma function (0th derivative)
15#[must_use]
16#[inline]
17pub fn polygamma(n: u32, x: f64) -> f64 {
18 if n == 0 {
19 crate::digamma(x)
20 } else {
21 let n1p = f64::from(n) + 1.0;
22 let sign = -neg_one_pow(n);
23 sign * crate::gamma(n1p) * crate::zeta(n1p, x)
24 }
25}