ruxla-core 0.1.0

Internal crate of ruxla (use the `ruxla` crate): the frontend of ruxla (backend-generic values, native and tracing backends)
Documentation
//! Functions following `jax.scipy`.

/// Special functions following `jax.scipy.special`, with NumPy's
/// broadcasting.
///
/// # Examples
///
/// ```
/// use ruxla_core::scipy::special;
/// use ruxla_core::{Array, Native};
///
/// let x = Array::<Native, f64>::from_vec(vec![1.0, 4.0], [2]);
/// let lg = special::gammaln(&x).to_vec();
/// assert!(lg[0].abs() < 1e-12);
/// assert!((lg[1] - 6f64.ln()).abs() < 1e-12);
/// ```
pub mod special {
    use ruxla_kernels::{BinaryOp, TernaryOp, UnaryOp};

    use crate::backend::Backend;
    use crate::value::{Array, ArrayLike, Float, broadcast_binary, broadcast_shapes};

    macro_rules! unary {
        ($($(#[$m:meta])* $name:ident => $op:ident;)*) => {$(
            $(#[$m])*
            pub fn $name<B: Backend, T: Float>(x: impl ArrayLike<B, T>) -> Array<B, T> {
                x.with_array(|x| x.unary(UnaryOp::$op))
            }
        )*};
    }

    unary! {
        /// The logarithm of the absolute value of the gamma function.
        gammaln => Lgamma;
        /// The digamma function.
        digamma => Digamma;
        /// The error function.
        erf => Erf;
        /// The complementary error function.
        erfc => Erfc;
        /// The inverse error function.
        erfinv => ErfInv;
        /// The exponentially scaled modified Bessel function of order 0.
        i0e => BesselI0e;
        /// The exponentially scaled modified Bessel function of order 1.
        i1e => BesselI1e;
    }

    macro_rules! binary {
        ($($(#[$m:meta])* $name:ident => $op:ident;)*) => {$(
            $(#[$m])*
            #[track_caller]
            pub fn $name<B: Backend, T: Float>(
                x: impl ArrayLike<B, T>,
                y: impl ArrayLike<B, T>,
            ) -> Array<B, T> {
                x.with_array(|x| y.with_array(|y| broadcast_binary(BinaryOp::$op, x, y)))
            }
        )*};
    }

    binary! {
        /// The regularized lower incomplete gamma function `P(a, x)`.
        gammainc => Igamma;
        /// The regularized upper incomplete gamma function `Q(a, x)`.
        gammaincc => Igammac;
        /// The polygamma function `psi^(n)(x)` (`n` a natural number).
        polygamma => Polygamma;
        /// The Hurwitz zeta function `zeta(x, q)`.
        zeta => Zeta;
    }

    /// The regularized incomplete beta function `I_x(a, b)`.
    #[track_caller]
    pub fn betainc<B: Backend, T: Float>(
        a: impl ArrayLike<B, T>,
        b: impl ArrayLike<B, T>,
        x: impl ArrayLike<B, T>,
    ) -> Array<B, T> {
        let (a, b, x) = (a.into_array(), b.into_array(), x.into_array());
        let shape = broadcast_shapes(&broadcast_shapes(&a.shape(), &b.shape()), &x.shape());
        let [a, b, x] = [a, b, x].map(|v| v.broadcast_to(shape.clone()));
        Array(B::a_ternary::<T>(
            TernaryOp::RegularizedIncompleteBeta,
            &a.0,
            &b.0,
            &x.0,
        ))
    }
}