use std::fmt;
use crate::algebra::AlgebraBuildError;
use crate::decompose::{Certificate, decompose};
use crate::endo::EndoAlgebra;
use crate::injective::injective_dimension;
use crate::module::Module;
use crate::resolution::{Bounded, projective_dimension};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum IndecError {
Zero,
Decomposable { summands: usize },
Undetermined { attempts: u32 },
}
impl fmt::Display for IndecError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Zero => f.write_str("the zero module is not indecomposable"),
Self::Decomposable { summands } => {
write!(f, "the module splits into {summands} summands")
}
Self::Undetermined { attempts } => write!(
f,
"the endomorphism algebra is not local and {attempts} split attempts failed"
),
}
}
}
impl std::error::Error for IndecError {}
pub struct IndecomposableModule {
module: Module,
endo: EndoAlgebra,
}
impl fmt::Debug for IndecomposableModule {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("IndecomposableModule")
.field("dim_vector", &self.module.dim_vector())
.field("endo_dim", &self.endo.dim())
.field("radical_dim", &self.endo.radical_dim())
.finish()
}
}
impl IndecomposableModule {
pub fn new(m: &Module) -> Result<IndecomposableModule, IndecError> {
if m.is_zero() {
return Err(IndecError::Zero);
}
let endo = EndoAlgebra::new(m);
if endo.is_local() {
return Ok(IndecomposableModule {
module: m.clone(),
endo,
});
}
let decomposition = decompose(m);
let summands = decomposition.summands().len();
if summands >= 2 {
return Err(IndecError::Decomposable { summands });
}
let attempts = decomposition
.certificates()
.iter()
.find_map(|c| match c {
Certificate::Undetermined { attempts } => Some(*attempts),
Certificate::Indecomposable => None,
})
.expect("a non-local unsplit module carries an Undetermined certificate");
Err(IndecError::Undetermined { attempts })
}
#[inline]
pub fn module(&self) -> &Module {
&self.module
}
#[inline]
pub fn endo(&self) -> &EndoAlgebra {
&self.endo
}
pub fn residue_degree(&self) -> usize {
self.endo.dim() - self.endo.radical_dim()
}
pub fn is_projective(&self) -> bool {
projective_dimension(&self.module, 0) == Bounded::Exact(0)
}
pub fn is_injective(&self) -> Result<bool, AlgebraBuildError> {
Ok(injective_dimension(&self.module, 0)? == Bounded::Exact(0))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::algebra::{commutative_square, dual_numbers, kronecker, linear_an};
use crate::field::PrimeField;
use crate::linalg::DenseMat;
use crate::module::direct_sum;
use std::sync::Arc;
fn fields() -> [PrimeField; 2] {
[PrimeField::new(2).unwrap(), PrimeField::new(5).unwrap()]
}
fn fixtures(field: PrimeField) -> Vec<Arc<crate::algebra::Algebra>> {
vec![
linear_an(3, field),
dual_numbers(field),
commutative_square(field),
]
}
#[test]
fn simples_are_accepted_with_residue_degree_1() {
for field in fields() {
for algebra in fixtures(field) {
for v in 0..algebra.quiver().num_vertices() {
let s = Module::simple(&algebra, v);
let ind = IndecomposableModule::new(&s).unwrap();
assert!(ind.module().ptr_eq(&s));
assert!(ind.endo().is_local());
assert_eq!(ind.residue_degree(), 1, "S_{v} over F_{}", field.modulus());
}
}
}
}
#[test]
fn projectives_are_accepted_and_report_projectivity() {
for field in fields() {
for algebra in fixtures(field) {
for v in 0..algebra.quiver().num_vertices() {
let p = Module::projective(&algebra, v);
let ind = IndecomposableModule::new(&p).unwrap();
assert!(ind.is_projective(), "P_{v} over F_{}", field.modulus());
assert_eq!(ind.residue_degree(), 1);
}
}
}
}
#[test]
fn the_zero_module_is_rejected() {
let algebra = linear_an(3, PrimeField::new(5).unwrap());
assert_eq!(
IndecomposableModule::new(&Module::zero(&algebra)).unwrap_err(),
IndecError::Zero
);
}
#[test]
fn direct_sums_are_rejected_with_the_summand_count() {
for field in fields() {
for algebra in fixtures(field) {
let last = algebra.quiver().num_vertices() - 1;
let p = Module::projective(&algebra, 0);
let s = Module::simple(&algebra, last);
let (pair, _, _) = direct_sum(&[&p, &s]);
assert_eq!(
IndecomposableModule::new(&pair).unwrap_err(),
IndecError::Decomposable { summands: 2 },
"P_0 + S_{last} over F_{}",
field.modulus()
);
let (triple, _, _) = direct_sum(&[&p, &s, &p]);
assert_eq!(
IndecomposableModule::new(&triple).unwrap_err(),
IndecError::Decomposable { summands: 3 },
"P_0 + S_{last} + P_0 over F_{}",
field.modulus()
);
}
}
}
#[test]
fn projectivity_and_injectivity_match_known_a3_facts() {
for field in fields() {
let algebra = linear_an(3, field);
let p0 = IndecomposableModule::new(&Module::projective(&algebra, 0)).unwrap();
assert!(p0.is_projective());
assert!(p0.is_injective().unwrap());
let s0 = IndecomposableModule::new(&Module::simple(&algebra, 0)).unwrap();
assert!(!s0.is_projective());
assert!(s0.is_injective().unwrap());
let s1 = IndecomposableModule::new(&Module::simple(&algebra, 1)).unwrap();
assert!(!s1.is_projective());
assert!(!s1.is_injective().unwrap());
let s2 = IndecomposableModule::new(&Module::simple(&algebra, 2)).unwrap();
assert!(s2.is_projective());
assert!(!s2.is_injective().unwrap());
}
}
#[test]
fn the_f8_endomorphism_kronecker_module_has_residue_degree_3() {
let field = PrimeField::new(2).unwrap();
let algebra = kronecker(2, field);
let identity3 = DenseMat::identity(3);
let mut companion = DenseMat::zero(3, 3);
companion.set(0, 1, field.one());
companion.set(1, 2, field.one());
companion.set(2, 0, field.one());
companion.set(2, 1, field.one());
let w = Module::new(algebra, vec![3, 3], vec![identity3, companion]).unwrap();
let ind = IndecomposableModule::new(&w).unwrap();
assert_eq!(ind.endo().dim(), 3);
assert_eq!(ind.endo().radical_dim(), 0);
assert_eq!(ind.residue_degree(), 3);
let (doubled, _, _) = direct_sum(&[&w, &w]);
assert_eq!(
IndecomposableModule::new(&doubled).unwrap_err(),
IndecError::Decomposable { summands: 2 }
);
}
}