1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//! 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,
))
}
}