use crate::endo::EndoAlgebra;
use crate::iso::Fingerprint;
use crate::module::Module;
use crate::profile::{Site, hit};
use super::split::Split;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Certificate {
Indecomposable,
Undetermined { attempts: u32 },
}
#[derive(Clone, Debug)]
pub struct Decomposition {
split: Split,
certificates: Vec<Certificate>,
endos: Vec<EndoAlgebra>,
}
impl Decomposition {
pub(crate) fn from_parts(
split: Split,
certificates: Vec<Certificate>,
endos: Vec<EndoAlgebra>,
) -> Self {
Self {
split,
certificates,
endos,
}
}
accessor_methods! {
pub split() -> &Split = |this| &this.split;
pub certificates() -> &[Certificate] = |this| &this.certificates;
pub summands() -> &[Module] = |this| this.split.summands();
pub endos() -> &[EndoAlgebra] = |this| &this.endos;
}
}
#[derive(Clone, Debug)]
pub struct IsoClass {
pub representative: Module,
pub endo: EndoAlgebra,
pub multiplicity: usize,
}
#[derive(Clone, Debug)]
pub enum KrullSchmidtOutcome {
Classes(Vec<IsoClass>),
Unknown {
reason: String,
},
}
pub fn krull_schmidt(m: &Module) -> KrullSchmidtOutcome {
hit(Site::KrullSchmidt);
let d = super::algorithm::decompose(m);
krull_schmidt_from_decomposition(&d)
}
pub(crate) fn krull_schmidt_from_decomposition(d: &Decomposition) -> KrullSchmidtOutcome {
let mut classes: Vec<(usize, Fingerprint, usize)> = Vec::new();
for (k, certificate) in d.certificates().iter().enumerate() {
if let Certificate::Undetermined { attempts } = certificate {
return KrullSchmidtOutcome::Unknown {
reason: format!("a summand stayed undetermined after {attempts} split attempts"),
};
}
let summand = &d.summands()[k];
let print = Fingerprint::of(summand, &d.endos()[k]);
match classes.iter_mut().find(|(rep, rep_print, _)| {
*rep_print == print
&& crate::iso::indecomposable_iso(&d.summands()[*rep], summand, &d.endos()[*rep])
.is_some()
}) {
Some((_, _, multiplicity)) => *multiplicity += 1,
None => classes.push((k, print, 1)),
}
}
KrullSchmidtOutcome::Classes(
classes
.into_iter()
.map(|(rep, _, multiplicity)| IsoClass {
representative: d.summands()[rep].clone(),
endo: d.endos()[rep].clone(),
multiplicity,
})
.collect(),
)
}