use super::contract::UnaryOp;
use crate::domain::RealScalar;
use eunomia::NumericElement;
fn bessel_j0_value<T: RealScalar>(x: T) -> T {
use core::f64::consts::{FRAC_PI_4, PI};
if x == <T as NumericElement>::ZERO {
return <T as NumericElement>::ONE;
}
let ax = <T as NumericElement>::abs(x);
if ax < T::from_f64(8.0) {
let y = x * x;
let num = T::from_f64(57568490574.0)
+ y * (T::from_f64(-13362590354.0)
+ y * (T::from_f64(651619640.7)
+ y * (T::from_f64(-11214424.18)
+ y * (T::from_f64(77392.33017) + y * T::from_f64(-184.9052456)))));
let den = T::from_f64(57568490411.0)
+ y * (T::from_f64(1029532985.0)
+ y * (T::from_f64(9494680.718)
+ y * (T::from_f64(59272.64853) + y * (T::from_f64(267.8532712) + y))));
num / den
} else {
let z = T::from_f64(8.0) / ax;
let y = z * z;
let xx = ax - T::from_f64(FRAC_PI_4);
let p = <T as NumericElement>::ONE
+ y * (T::from_f64(-0.001098628627)
+ y * (T::from_f64(0.000002734510407)
+ y * (T::from_f64(-2.073370639e-6) + y * T::from_f64(2.093887211e-7))));
let q = T::from_f64(-0.01562499995)
+ y * (T::from_f64(0.0001430488765)
+ y * (T::from_f64(-6.911147651e-5)
+ y * (T::from_f64(7.621095161e-5) - y * T::from_f64(9.34935152e-7))));
(T::from_f64(2.0) / (T::from_f64(PI) * ax)).sqrt() * (p * xx.cos() - z * q * xx.sin())
}
}
fn bessel_j1_value<T: RealScalar>(x: T) -> T {
use core::f64::consts::PI;
if x == <T as NumericElement>::ZERO {
return <T as NumericElement>::ZERO;
}
let ax = <T as NumericElement>::abs(x);
if ax < T::from_f64(8.0) {
let y = x * x;
let num = x
* (T::from_f64(72362614232.0)
+ y * (T::from_f64(-7895059235.0)
+ y * (T::from_f64(242396853.1)
+ y * (T::from_f64(-2972611.439)
+ y * (T::from_f64(15704.48260) + y * T::from_f64(-30.16036606))))));
let den = T::from_f64(144725228442.0)
+ y * (T::from_f64(2300535178.0)
+ y * (T::from_f64(18583304.74)
+ y * (T::from_f64(99447.43394) + y * (T::from_f64(376.9991397) + y))));
num / den
} else {
let z = T::from_f64(8.0) / ax;
let y = z * z;
let xx = ax - T::from_f64(3.0 * PI / 4.0);
let p = <T as NumericElement>::ONE
+ y * (T::from_f64(0.183105e-2)
+ y * (T::from_f64(-3.516396496e-5)
+ y * (T::from_f64(2.457520174e-5) - y * T::from_f64(2.400505341e-7))));
let q = T::from_f64(0.04687499995)
+ y * (T::from_f64(-0.2002690873e-3)
+ y * (T::from_f64(8.449199096e-5)
+ y * (T::from_f64(-8.8228987e-5) + y * T::from_f64(1.050343160e-6))));
let r =
(T::from_f64(2.0) / (T::from_f64(PI) * ax)).sqrt() * (p * xx.cos() - z * q * xx.sin());
if x < <T as NumericElement>::ZERO {
-r
} else {
r
}
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct J0Op;
impl<T: RealScalar> UnaryOp<T> for J0Op {
#[inline(always)]
fn apply(&self, x: T) -> T {
bessel_j0_value(x)
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct J1Op;
impl<T: RealScalar> UnaryOp<T> for J1Op {
#[inline(always)]
fn apply(&self, x: T) -> T {
bessel_j1_value(x)
}
}
fn bessel_k0_value<T: RealScalar>(x: T) -> T {
if !(x.is_finite() && x > <T as NumericElement>::ZERO) {
return <T as NumericElement>::NAN;
}
if x <= T::from_f64(2.0) {
let t1 = (x / T::from_f64(3.75)) * (x / T::from_f64(3.75));
let i0 = <T as NumericElement>::ONE
+ t1 * (T::from_f64(3.515_622_9)
+ t1 * (T::from_f64(3.089_942_4)
+ t1 * (T::from_f64(1.206_749_2)
+ t1 * (T::from_f64(0.265_973_2)
+ t1 * (T::from_f64(0.036_076_8) + t1 * T::from_f64(0.004_581_3))))));
let t2 = (x * T::from_f64(0.5)) * (x * T::from_f64(0.5));
let correction = T::from_f64(-0.577_215_66)
+ t2 * (T::from_f64(0.422_784_20)
+ t2 * (T::from_f64(0.230_697_56)
+ t2 * (T::from_f64(0.034_885_90)
+ t2 * (T::from_f64(0.002_626_98)
+ t2 * (T::from_f64(0.000_107_50) + t2 * T::from_f64(7.4e-6))))));
(x * T::from_f64(0.5)).ln() * -i0 + correction
} else {
let t = T::from_f64(2.0) / x;
let series = T::from_f64(1.253_314_14)
+ t * (T::from_f64(-0.078_323_58)
+ t * (T::from_f64(0.021_895_68)
+ t * (T::from_f64(-0.010_624_46)
+ t * (T::from_f64(0.005_878_72)
+ t * (T::from_f64(-0.002_515_40) + t * T::from_f64(0.000_532_08))))));
(-x).exp() / x.sqrt() * series
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct K0Op;
impl<T: RealScalar> UnaryOp<T> for K0Op {
#[inline(always)]
fn apply(&self, x: T) -> T {
bessel_k0_value(x)
}
}