use eunomia::{NumericElement, RealField};
use super::super::f;
pub(super) const ELEMENTS_PER_UNIT: usize = 5;
#[derive(Clone, Copy)]
pub(super) struct Scales<T> {
inv_12h: T,
inv_2h: T,
inv_h: T,
eight: T,
}
impl<T: RealField> Scales<T> {
pub(super) fn new(h: T) -> Self {
Self {
inv_12h: <T as NumericElement>::ONE / (f::<T>(12.0) * h),
inv_2h: <T as NumericElement>::ONE / (f::<T>(2.0) * h),
inv_h: <T as NumericElement>::ONE / h,
eight: f::<T>(8.0),
}
}
#[inline]
pub(super) fn fourth(self, m2: T, m1: T, p1: T, p2: T) -> T {
((-self.eight * m1) + (self.eight * p1) + (-p2) + m2) * self.inv_12h
}
#[inline]
pub(super) fn second(self, m1: T, p1: T) -> T {
(p1 - m1) * self.inv_2h
}
#[inline]
pub(super) fn first(self, lower: T, upper: T) -> T {
(upper - lower) * self.inv_h
}
}
#[derive(Clone, Copy)]
pub(super) enum Stencil {
Flat,
Forward,
Backward,
Second,
Fourth,
}
impl Stencil {
#[inline]
pub(super) fn at(c: usize, n: usize) -> Self {
if n == 1 {
Self::Flat
} else if c == 0 {
Self::Forward
} else if c == n - 1 {
Self::Backward
} else if c < 2 || c >= n - 2 {
Self::Second
} else {
Self::Fourth
}
}
#[inline]
pub(super) fn apply<T: RealField>(self, scales: Scales<T>, at: impl Fn(isize) -> T) -> T {
match self {
Self::Flat => <T as NumericElement>::ZERO,
Self::Forward => scales.first(at(0), at(1)),
Self::Backward => scales.first(at(-1), at(0)),
Self::Second => scales.second(at(-1), at(1)),
Self::Fourth => scales.fourth(at(-2), at(-1), at(1), at(2)),
}
}
#[inline]
pub(super) fn add_lane<'a, T: RealField + 'a>(
self,
scales: Scales<T>,
out: &mut [T],
lane: impl Fn(isize) -> &'a [T],
) {
match self {
Self::Flat => (),
Self::Forward => {
for (value, (&lower, &upper)) in out.iter_mut().zip(lane(0).iter().zip(lane(1))) {
*value += scales.first(lower, upper);
}
}
Self::Backward => {
for (value, (&lower, &upper)) in out.iter_mut().zip(lane(-1).iter().zip(lane(0))) {
*value += scales.first(lower, upper);
}
}
Self::Second => {
for (value, (&m1, &p1)) in out.iter_mut().zip(lane(-1).iter().zip(lane(1))) {
*value += scales.second(m1, p1);
}
}
Self::Fourth => {
let neighbours = lane(-2).iter().zip(lane(-1)).zip(lane(1)).zip(lane(2));
for (value, (((&m2, &m1), &p1), &p2)) in out.iter_mut().zip(neighbours) {
*value += scales.fourth(m2, m1, p1, p2);
}
}
}
}
#[inline]
pub(super) fn apply_lane<'a, T: RealField + 'a>(
self,
scales: Scales<T>,
out: &mut [T],
lane: impl Fn(isize) -> &'a [T],
) {
match self {
Self::Flat => out.fill(<T as NumericElement>::ZERO),
Self::Forward => {
for (value, (&lower, &upper)) in out.iter_mut().zip(lane(0).iter().zip(lane(1))) {
*value = scales.first(lower, upper);
}
}
Self::Backward => {
for (value, (&lower, &upper)) in out.iter_mut().zip(lane(-1).iter().zip(lane(0))) {
*value = scales.first(lower, upper);
}
}
Self::Second => {
for (value, (&m1, &p1)) in out.iter_mut().zip(lane(-1).iter().zip(lane(1))) {
*value = scales.second(m1, p1);
}
}
Self::Fourth => {
let neighbours = lane(-2).iter().zip(lane(-1)).zip(lane(1)).zip(lane(2));
for (value, (((&m2, &m1), &p1), &p2)) in out.iter_mut().zip(neighbours) {
*value = scales.fourth(m2, m1, p1, p2);
}
}
}
}
}