use std::fmt::Debug;
use std::ops::Neg;
use num_traits::NumAssign;
pub type C<T> = num_complex::Complex<T>;
pub trait Scalar:
Copy + Send + Sync + Debug + Default + PartialEq + PartialOrd + 'static + NumAssign + Neg<Output = Self>
{
fn from_f64(v: f64) -> Self;
fn re(self) -> f64;
fn sqrt(self) -> Self;
fn sin(self) -> Self;
fn cos(self) -> Self;
fn exp(self) -> Self;
fn ln(self) -> Self;
fn atan2(self, x: Self) -> Self;
fn abs(self) -> Self;
fn powi(self, n: i32) -> Self;
fn sinc(self) -> Self;
}
pub(crate) const SINC_SERIES_BELOW: f64 = 1e-4;
pub(crate) fn sinc_f64(x: f64) -> f64 {
if x.abs() < SINC_SERIES_BELOW {
let x2 = x * x;
1.0 - x2 / 6.0 + x2 * x2 / 120.0
} else {
x.sin() / x
}
}
pub(crate) fn sinc_derivative_f64(x: f64) -> f64 {
if x.abs() < SINC_SERIES_BELOW {
let x2 = x * x;
-x / 3.0 + x * x2 / 30.0
} else {
(x * x.cos() - x.sin()) / (x * x)
}
}
impl Scalar for f64 {
#[inline]
fn from_f64(v: f64) -> Self {
v
}
#[inline]
fn re(self) -> f64 {
self
}
#[inline]
fn sqrt(self) -> Self {
f64::sqrt(self)
}
#[inline]
fn sin(self) -> Self {
f64::sin(self)
}
#[inline]
fn cos(self) -> Self {
f64::cos(self)
}
#[inline]
fn exp(self) -> Self {
f64::exp(self)
}
#[inline]
fn ln(self) -> Self {
f64::ln(self)
}
#[inline]
fn atan2(self, x: Self) -> Self {
f64::atan2(self, x)
}
#[inline]
fn abs(self) -> Self {
f64::abs(self)
}
#[inline]
fn powi(self, n: i32) -> Self {
f64::powi(self, n)
}
#[inline]
fn sinc(self) -> Self {
sinc_f64(self)
}
}