use super::linear::{
composition_rows, decomposition_holds, radical_combination_matches, row_relations,
};
use crate::context::VerificationContext;
use crate::field::Fp;
use crate::hom::Morphism;
use crate::homspace::HomSpace;
use crate::indec::IndecomposableModule;
use crate::linalg::DenseMat;
use crate::profile::{Site, hit};
#[derive(Debug)]
pub struct MinimalLeftApproximation {
pub(super) map: Morphism,
pub(super) summands: Vec<IndecomposableModule>,
pub(super) slots: Vec<usize>,
pub(super) inclusions: Vec<Morphism>,
pub(super) projections: Vec<Morphism>,
pub(super) factorizations: Vec<Vec<Vec<Fp>>>,
pub(super) kernel: DenseMat,
pub(super) radical: Vec<Vec<Fp>>,
}
impl MinimalLeftApproximation {
accessor_methods! {
pub map() -> &Morphism = |this| &this.map;
pub summands() -> &[IndecomposableModule] = |this| &this.summands;
pub slots() -> &[usize] = |this| &this.slots;
pub inclusions() -> &[Morphism] = |this| &this.inclusions;
pub projections() -> &[Morphism] = |this| &this.projections;
}
accessor_methods! {
pub multiplicity(summand: usize) -> usize = |this| this.slots.iter().filter(|&&i| i == summand).count();
pub factorization(summand: usize, basis_index: usize) -> &[Fp] = |this| &this.factorizations[summand][basis_index];
pub kernel_basis() -> &DenseMat = |this| &this.kernel;
pub radical_coordinates(row: usize) -> &[Fp] = |this| &this.radical[row];
}
verify_methods!(pub(crate), { hit(Site::LeftApproximationVerify); },
|self, context| {
let x = self.map.source();
let b = self.map.target();
verify_guard!(self.factorizations.len() == self.summands.len());
verify_guard!(self.slots.iter().all(|&i| i < self.summands.len()));
verify_guard!(decomposition_holds(
b,
&self.summands,
&self.slots,
&self.inclusions,
&self.projections,
));
for (i, n) in self.summands.iter().enumerate() {
let_or_false!((Ok(target_space), Ok(source_space)) =
(HomSpace::new(x, n.module()), HomSpace::new(b, n.module())));
verify_guard!(self.factorizations[i].len() == target_space.dim());
for (j, coords) in self.factorizations[i].iter().enumerate() {
verify_guard!(coords.len() == source_space.dim());
let_or_false!(Ok(composite) = self.map.then(&source_space.morphism(coords)));
verify_guard!(composite == target_space.basis()[j]);
}
}
let endo = context.endo_for(b);
let_or_false!(Ok(space) = HomSpace::new(x, b));
let field = x.field();
let_or_false!(Ok(rows) = composition_rows(&self.map, &endo, &space));
let kernel = row_relations(&rows, space.dim(), &field);
verify_guard!(kernel == self.kernel && self.radical.len() == kernel.rows());
(0..kernel.rows())
.all(|r| radical_combination_matches(&endo, &self.radical[r], kernel.row(r)))
});
}