use super::Smoothing;
#[must_use]
pub(crate) const fn scratch_len(trend: bool, m: usize) -> usize {
let q = states(trend) * m;
7 * q * q + 3 * q
}
const fn states(trend: bool) -> usize {
if trend { 2 } else { 1 }
}
#[expect(
clippy::too_many_arguments,
reason = "the states, the scratch and both outputs are separate borrows"
)]
pub(crate) fn moments(
smoothing: &Smoothing,
level: f64,
slope: f64,
seasonal: &[f64],
sigma2: f64,
scratch: &mut [f64],
mean: &mut [f64],
variance: &mut [f64],
) {
let m = seasonal.len();
let t = states(smoothing.trend);
let q = t * m;
let (matrices, rest) = scratch[..scratch_len(smoothing.trend, m)].split_at_mut(7 * q * q);
let (f21, rest_matrices) = matrices.split_at_mut(q * q);
let (g21, rest_matrices) = rest_matrices.split_at_mut(q * q);
let (gains, rest_matrices) = rest_matrices.split_at_mut(q * q);
let (vh, rest_matrices) = rest_matrices.split_at_mut(q * q);
let (outer, rest_matrices) = rest_matrices.split_at_mut(q * q);
let (product, accumulator) = rest_matrices.split_at_mut(q * q);
let (mh, rest) = rest.split_at_mut(q);
let (next_mh, column) = rest.split_at_mut(q);
let small = Small::of(smoothing, m);
small.kronecker_into(f21, gains, g21);
vh.fill(0.0);
let lead = [level, slope];
for (j, &season) in seasonal.iter().enumerate() {
for (i, &state) in lead.iter().take(t).enumerate() {
mh[j * t + i] = state * season;
}
}
for (mu_out, value) in mean.iter_mut().zip(variance.iter_mut()) {
let mu: f64 = (0..t).map(|i| small.h1[i] * mh[(m - 1) * t + i]).sum();
*mu_out = mu;
let mut block = 0.0;
for i in 0..t {
for j in 0..t {
let index = ((m - 1) * t + i) * q + (m - 1) * t + j;
block += small.h1[i] * small.h1[j] * vh[index];
}
}
*value = (1.0 + sigma2) * block + sigma2 * mu * mu;
for (i, row) in outer.chunks_exact_mut(q).enumerate() {
for (j, cell) in row.iter_mut().enumerate() {
*cell = vh[i * q + j] + mh[i] * mh[j];
}
}
let next = Step {
f21,
g21,
k: gains,
sigma2,
q,
};
next.covariance(vh, outer, product, accumulator, column);
small.mean(sigma2, mh, next_mh);
}
}
struct Small {
t: usize,
m: usize,
f1: [f64; 4],
g1: [f64; 4],
h1: [f64; 2],
gamma: f64,
}
impl Small {
fn of(smoothing: &Smoothing, m: usize) -> Self {
let t = states(smoothing.trend);
let (f1, g1, h1) = if t == 2 {
let Smoothing {
alpha, beta, phi, ..
} = *smoothing;
(
[1.0, phi, 0.0, phi],
[alpha, alpha * phi, beta, beta * phi],
[1.0, phi],
)
} else {
(
[1.0, 0.0, 0.0, 0.0],
[smoothing.alpha, 0.0, 0.0, 0.0],
[1.0, 0.0],
)
};
Self {
t,
m,
f1,
g1,
h1,
gamma: smoothing.gamma,
}
}
fn f2(&self, a: usize, b: usize) -> f64 {
let source = if a == 0 { self.m - 1 } else { a - 1 };
if b == source { 1.0 } else { 0.0 }
}
fn g2(&self, a: usize, b: usize) -> f64 {
if a == 0 && b == self.m - 1 {
self.gamma
} else {
0.0
}
}
fn kronecker_into(&self, f21: &mut [f64], k: &mut [f64], g21: &mut [f64]) {
let (t, q) = (self.t, self.t * self.m);
for a in 0..self.m {
for b in 0..self.m {
let (f2, g2) = (self.f2(a, b), self.g2(a, b));
for i in 0..t {
for j in 0..t {
let index = (a * t + i) * q + b * t + j;
let (f1, g1) = (self.f1[i * 2 + j], self.g1[i * 2 + j]);
f21[index] = f2 * f1;
g21[index] = g2 * g1;
k[index] = g2 * f1 + f2 * g1;
}
}
}
}
}
fn mean(&self, sigma2: f64, mh: &mut [f64], next: &mut [f64]) {
let (t, m) = (self.t, self.m);
for b in 0..m {
for i in 0..t {
let mut value = 0.0;
for a in 0..m {
let (f2, g2) = (self.f2(b, a), self.g2(b, a));
if f2 == 0.0 && g2 == 0.0 {
continue;
}
for j in 0..t {
let state = mh[a * t + j];
value += self.f1[i * 2 + j] * state * f2;
value += sigma2 * self.g1[i * 2 + j] * state * g2;
}
}
next[b * t + i] = value;
}
}
mh.copy_from_slice(&next[..t * m]);
}
}
struct Step<'a> {
f21: &'a [f64],
g21: &'a [f64],
k: &'a [f64],
sigma2: f64,
q: usize,
}
impl Step<'_> {
fn covariance(
&self,
vh: &mut [f64],
w: &mut [f64],
product: &mut [f64],
sum: &mut [f64],
column: &mut [f64],
) {
let s = self.sigma2;
sum.fill(0.0);
self.sandwich(self.f21, vh, self.f21, 1.0, product, sum, column);
self.sandwich(self.f21, vh, self.g21, s, product, sum, column);
self.sandwich(self.g21, vh, self.f21, s, product, sum, column);
self.sandwich(self.k, w, self.k, s, product, sum, column);
for (cell, &v) in w.iter_mut().zip(vh.iter()) {
*cell = 2.0 * *cell + v;
}
self.sandwich(self.g21, w, self.g21, s * s, product, sum, column);
vh.copy_from_slice(sum);
}
#[expect(
clippy::too_many_arguments,
reason = "scratch buffers keep the recursion heap-free"
)]
fn sandwich(
&self,
a: &[f64],
x: &[f64],
b: &[f64],
scale: f64,
product: &mut [f64],
sum: &mut [f64],
column: &mut [f64],
) {
let q = self.q;
for i in 0..q {
column.fill(0.0);
for l in 0..q {
let a_il = a[i * q + l];
if a_il == 0.0 {
continue;
}
for (c, &x_lj) in column.iter_mut().zip(&x[l * q..(l + 1) * q]) {
*c += a_il * x_lj;
}
}
product[i * q..(i + 1) * q].copy_from_slice(column);
}
for i in 0..q {
let row = &product[i * q..(i + 1) * q];
for j in 0..q {
let dot: f64 = row
.iter()
.zip(&b[j * q..(j + 1) * q])
.map(|(p, b)| p * b)
.sum();
sum[i * q + j] += scale * dot;
}
}
}
}