Skip to main content

ruxla_core/
scipy.rs

1//! Functions following `jax.scipy`.
2
3/// Special functions following `jax.scipy.special`, with NumPy's
4/// broadcasting.
5///
6/// # Examples
7///
8/// ```
9/// use ruxla_core::scipy::special;
10/// use ruxla_core::{Array, Native};
11///
12/// let x = Array::<Native, f64>::from_vec(vec![1.0, 4.0], [2]);
13/// let lg = special::gammaln(&x).to_vec();
14/// assert!(lg[0].abs() < 1e-12);
15/// assert!((lg[1] - 6f64.ln()).abs() < 1e-12);
16/// ```
17pub mod special {
18    use ruxla_kernels::{BinaryOp, TernaryOp, UnaryOp};
19
20    use crate::backend::Backend;
21    use crate::value::{Array, ArrayLike, Float, broadcast_binary, broadcast_shapes};
22
23    macro_rules! unary {
24        ($($(#[$m:meta])* $name:ident => $op:ident;)*) => {$(
25            $(#[$m])*
26            pub fn $name<B: Backend, T: Float>(x: impl ArrayLike<B, T>) -> Array<B, T> {
27                x.with_array(|x| x.unary(UnaryOp::$op))
28            }
29        )*};
30    }
31
32    unary! {
33        /// The logarithm of the absolute value of the gamma function.
34        gammaln => Lgamma;
35        /// The digamma function.
36        digamma => Digamma;
37        /// The error function.
38        erf => Erf;
39        /// The complementary error function.
40        erfc => Erfc;
41        /// The inverse error function.
42        erfinv => ErfInv;
43        /// The exponentially scaled modified Bessel function of order 0.
44        i0e => BesselI0e;
45        /// The exponentially scaled modified Bessel function of order 1.
46        i1e => BesselI1e;
47    }
48
49    macro_rules! binary {
50        ($($(#[$m:meta])* $name:ident => $op:ident;)*) => {$(
51            $(#[$m])*
52            #[track_caller]
53            pub fn $name<B: Backend, T: Float>(
54                x: impl ArrayLike<B, T>,
55                y: impl ArrayLike<B, T>,
56            ) -> Array<B, T> {
57                x.with_array(|x| y.with_array(|y| broadcast_binary(BinaryOp::$op, x, y)))
58            }
59        )*};
60    }
61
62    binary! {
63        /// The regularized lower incomplete gamma function `P(a, x)`.
64        gammainc => Igamma;
65        /// The regularized upper incomplete gamma function `Q(a, x)`.
66        gammaincc => Igammac;
67        /// The polygamma function `psi^(n)(x)` (`n` a natural number).
68        polygamma => Polygamma;
69        /// The Hurwitz zeta function `zeta(x, q)`.
70        zeta => Zeta;
71    }
72
73    /// The regularized incomplete beta function `I_x(a, b)`.
74    #[track_caller]
75    pub fn betainc<B: Backend, T: Float>(
76        a: impl ArrayLike<B, T>,
77        b: impl ArrayLike<B, T>,
78        x: impl ArrayLike<B, T>,
79    ) -> Array<B, T> {
80        let (a, b, x) = (a.into_array(), b.into_array(), x.into_array());
81        let shape = broadcast_shapes(&broadcast_shapes(&a.shape(), &b.shape()), &x.shape());
82        let [a, b, x] = [a, b, x].map(|v| v.broadcast_to(shape.clone()));
83        Array(B::a_ternary::<T>(
84            TernaryOp::RegularizedIncompleteBeta,
85            &a.0,
86            &b.0,
87            &x.0,
88        ))
89    }
90}