pub mod newton;
pub mod smooth;
pub mod toeplitz;
use crate::field::Fp;
use newton::NewtonFactors;
use smooth::choose_b;
use toeplitz::{lower_toeplitz_paper, series_inv, upper_toeplitz_paper, ShearEngine, ToeplitzPlan};
#[derive(Clone, Debug)]
pub struct AxisPlan {
pub a: usize,
pub q: Fp,
pub newton: NewtonFactors,
pub engine: ShearEngine,
t_plan: ToeplitzPlan,
tinv_plan: ToeplitzPlan,
}
impl AxisPlan {
pub fn geometric(a: usize, q: Fp) -> Self {
let newton = NewtonFactors::new(a, q);
let engine = ShearEngine::new(a, choose_b(a));
let u = engine.dft.root;
let t_plan = ToeplitzPlan::new(&newton.t, a, u);
let tinv_plan = ToeplitzPlan::new(&series_inv(&newton.t, a), a, u);
AxisPlan { a, q, newton, engine, t_plan, tinv_plan }
}
pub fn dft(a: usize) -> Self {
Self::geometric(a, Fp::root_of_unity(a as u64))
}
pub fn forward(&self, x: &[Fp], scratch: &mut Vec<Fp>) -> Vec<Fp> {
let nf = &self.newton;
let mut y: Vec<Fp> = x.iter().zip(&nf.h).map(|(a, b)| *a * *b).collect();
y = upper_toeplitz_paper(&self.engine, &self.t_plan, &y, scratch);
y.iter_mut().zip(&nf.mid).for_each(|(a, b)| *a *= *b);
y = lower_toeplitz_paper(&self.engine, &self.t_plan, &y, scratch);
y.iter_mut().zip(&nf.h).for_each(|(a, b)| *a *= *b);
y
}
pub fn inverse(&self, x: &[Fp], scratch: &mut Vec<Fp>) -> Vec<Fp> {
let nf = &self.newton;
let mut y: Vec<Fp> = x.iter().zip(&nf.t).map(|(a, b)| *a * *b).collect(); y = lower_toeplitz_paper(&self.engine, &self.tinv_plan, &y, scratch);
y.iter_mut().zip(&nf.mid).for_each(|(a, b)| *a *= b.inv());
y = upper_toeplitz_paper(&self.engine, &self.tinv_plan, &y, scratch);
y.iter_mut().zip(&nf.t).for_each(|(a, b)| *a *= *b);
y
}
}
#[derive(Clone, Debug)]
pub struct GroupDft {
pub dims: Vec<usize>,
pub axes: Vec<AxisPlan>,
}
impl GroupDft {
pub fn new(dims: &[usize]) -> Self {
GroupDft { dims: dims.to_vec(), axes: dims.iter().map(|&a| AxisPlan::dft(a)).collect() }
}
pub fn len(&self) -> usize {
self.dims.iter().product()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
fn apply(&self, x: &[Fp], inverse: bool) -> Vec<Fp> {
assert_eq!(x.len(), self.len());
let mut data = x.to_vec();
let mut stride = 1;
for ax in (0..self.dims.len()).rev() {
let a = self.dims[ax];
let plan = &self.axes[ax];
let mut scratch = vec![Fp::ZERO; a];
let block = a * stride;
for base in (0..data.len()).step_by(block) {
for off in 0..stride {
let fibre: Vec<Fp> = (0..a).map(|k| data[base + off + k * stride]).collect();
let out = if inverse {
plan.inverse(&fibre, &mut scratch)
} else {
plan.forward(&fibre, &mut scratch)
};
for (k, v) in out.into_iter().enumerate() {
data[base + off + k * stride] = v;
}
}
}
debug_assert!(scratch.iter().all(|v| *v == Fp::ZERO), "scratch not restored");
stride *= a;
}
data
}
pub fn forward(&self, x: &[Fp]) -> Vec<Fp> {
self.apply(x, false)
}
pub fn inverse(&self, x: &[Fp]) -> Vec<Fp> {
self.apply(x, true)
}
}
pub fn naive_group_dft(dims: &[usize], x: &[Fp]) -> Vec<Fp> {
let n: usize = dims.iter().product();
let roots: Vec<Fp> = dims.iter().map(|&a| Fp::root_of_unity(a as u64)).collect();
let digits = |mut idx: usize| {
let mut d = vec![0usize; dims.len()];
for i in (0..dims.len()).rev() {
d[i] = idx % dims[i];
idx /= dims[i];
}
d
};
(0..n)
.map(|xi| {
let dx = digits(xi);
(0..n).fold(Fp::ZERO, |acc, yi| {
let dy = digits(yi);
let w = (0..dims.len())
.fold(Fp::ONE, |w, i| w * roots[i].pow(((dx[i] * dy[i]) % dims[i]) as u64));
acc + w * x[yi]
})
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use newton::vandermonde;
fn seq(n: usize, s: u64) -> Vec<Fp> {
(0..n as u64).map(|i| Fp::new(i.wrapping_mul(2654435761) ^ s)).collect()
}
#[test]
fn axis_dft_and_geometric() {
let cases: Vec<(usize, Fp)> = vec![
(2, Fp::root_of_unity(2)),
(3, Fp::root_of_unity(3)),
(15, Fp::root_of_unity(15)),
(17, Fp::root_of_unity(17)),
(64, Fp::root_of_unity(64)),
(85, Fp::root_of_unity(85)),
(255, Fp::root_of_unity(255)),
(10, Fp::new(3)), ];
for (a, q) in cases {
let plan = AxisPlan::geometric(a, q);
let v = vandermonde(a, q);
let x = seq(a, 77);
let mut scratch = vec![Fp::ZERO; a];
let y = plan.forward(&x, &mut scratch);
let expect: Vec<Fp> = (0..a).map(|j| (0..a).fold(Fp::ZERO, |s, k| s + v[j][k] * x[k])).collect();
assert_eq!(y, expect, "forward a={a}");
assert_eq!(plan.inverse(&y, &mut scratch), x, "inverse a={a}");
assert!(scratch.iter().all(|v| *v == Fp::ZERO));
}
}
#[test]
fn abelian_group_dft() {
for dims in [vec![12], vec![3, 5], vec![4, 17], vec![2, 3, 5], vec![8, 15]] {
let g = GroupDft::new(&dims);
let x = seq(g.len(), 5);
let y = g.forward(&x);
assert_eq!(y, naive_group_dft(&dims, &x), "dims={dims:?}");
assert_eq!(g.inverse(&y), x);
}
}
}