use std::sync::Arc;
use crate::algebra::Algebra;
use crate::field::PrimeField;
use crate::hom::{Morphism, identity, zero_morphism};
use crate::linalg::DenseMat;
use crate::module::{Module, direct_sum};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SplitError {
CountMismatch,
EndpointMismatch { index: usize },
NotIdentityOnSummand { index: usize },
CrossTermNonzero { from: usize, to: usize },
SumNotIdentity,
}
display_error! { error SplitError {
Self::CountMismatch => "summand, inclusion, and projection counts differ";
Self::EndpointMismatch { index } => "inclusion/projection {index} has wrong endpoints";
Self::NotIdentityOnSummand { index } => "projection {index} does not split inclusion {index}";
Self::CrossTermNonzero { from, to } => "inclusion {from} followed by projection {to} is nonzero";
Self::SumNotIdentity => "the projections and inclusions do not sum to the identity";
} }
pub(crate) fn direct_sum_or_zero<'a>(
algebra: &Arc<Algebra>,
parts: impl IntoIterator<Item = &'a Module>,
) -> (Module, Vec<Morphism>, Vec<Morphism>) {
let parts: Vec<&Module> = parts.into_iter().collect();
match parts.as_slice() {
[] => (Module::zero(algebra), Vec::new(), Vec::new()),
_ => direct_sum(&parts),
}
}
#[derive(Clone, Debug)]
pub struct Split {
total: Module,
summands: Vec<Module>,
inclusions: Vec<Morphism>,
projections: Vec<Morphism>,
}
pub(crate) fn add_morphisms(f: &Morphism, g: &Morphism) -> Morphism {
assert!(
f.source().ptr_eq(g.source()) && f.target().ptr_eq(g.target()),
"add_morphisms: endpoints differ"
);
let field = f.source().field();
let maps = (0..f.source().algebra().quiver().num_vertices())
.map(|v| f.map_at(v).add(g.map_at(v), &field))
.collect();
Morphism::new_unchecked(f.source(), f.target(), maps)
}
pub(crate) fn matrix_inverse(a: &DenseMat, field: &PrimeField) -> Option<DenseMat> {
(a.rows() == a.cols()).then(|| a.inverse(field))?
}
pub(crate) fn inverse_morphism(f: &Morphism) -> Option<Morphism> {
let field = f.source().field();
let maps = (0..f.source().algebra().quiver().num_vertices())
.map(|v| matrix_inverse(f.map_at(v), &field))
.collect::<Option<Vec<_>>>()?;
Morphism::new(f.target(), f.source(), maps).ok()
}
pub(crate) fn mutually_inverse(f: &Morphism, g: &Morphism) -> bool {
matches!(
(f.then(g), g.then(f)),
(Ok(round), Ok(round_back))
if round == identity(f.source()) && round_back == identity(g.source())
)
}
fn check_counts(
summands: &[Module],
inclusions: &[Morphism],
projections: &[Morphism],
) -> Result<(), SplitError> {
if summands.len() != inclusions.len() || summands.len() != projections.len() {
return Err(SplitError::CountMismatch);
}
Ok(())
}
fn check_endpoints(
total: &Module,
summands: &[Module],
inclusions: &[Morphism],
projections: &[Morphism],
) -> Result<(), SplitError> {
for (index, summand) in summands.iter().enumerate() {
let (incl, proj) = (&inclusions[index], &projections[index]);
if !endpoints_match(total, summand, incl, proj) {
return Err(SplitError::EndpointMismatch { index });
}
}
Ok(())
}
fn endpoints_match(
total: &Module,
summand: &Module,
inclusion: &Morphism,
projection: &Morphism,
) -> bool {
inclusion.source().ptr_eq(summand)
&& inclusion.target().ptr_eq(total)
&& projection.source().ptr_eq(total)
&& projection.target().ptr_eq(summand)
}
fn check_round_trips(
summands: &[Module],
inclusions: &[Morphism],
projections: &[Morphism],
) -> Result<(), SplitError> {
for (index, summand) in summands.iter().enumerate() {
let round_trip = inclusions[index]
.then(&projections[index])
.expect("endpoints were checked");
if round_trip != identity(summand) {
return Err(SplitError::NotIdentityOnSummand { index });
}
}
Ok(())
}
fn check_cross_terms(inclusions: &[Morphism], projections: &[Morphism]) -> Result<(), SplitError> {
for (from, incl) in inclusions.iter().enumerate() {
for (to, proj) in projections.iter().enumerate() {
if from != to && !incl.then(proj).expect("endpoints were checked").is_zero() {
return Err(SplitError::CrossTermNonzero { from, to });
}
}
}
Ok(())
}
fn check_sum(
total: &Module,
inclusions: &[Morphism],
projections: &[Morphism],
) -> Result<(), SplitError> {
let mut sum = zero_morphism(total, total).expect("a module is parallel to itself");
for (proj, incl) in projections.iter().zip(inclusions) {
sum = add_morphisms(&sum, &proj.then(incl).expect("endpoints were checked"));
}
if sum != identity(total) {
return Err(SplitError::SumNotIdentity);
}
Ok(())
}
fn validate_identities(
total: &Module,
summands: &[Module],
inclusions: &[Morphism],
projections: &[Morphism],
) -> Result<(), SplitError> {
check_round_trips(summands, inclusions, projections)?;
check_cross_terms(inclusions, projections)?;
check_sum(total, inclusions, projections)?;
Ok(())
}
impl Split {
pub fn new(
total: &Module,
summands: Vec<Module>,
inclusions: Vec<Morphism>,
projections: Vec<Morphism>,
) -> Result<Split, SplitError> {
check_counts(&summands, &inclusions, &projections)?;
check_endpoints(total, &summands, &inclusions, &projections)?;
validate_identities(total, &summands, &inclusions, &projections)?;
Ok(Split {
total: total.clone(),
summands,
inclusions,
projections,
})
}
pub fn verify(&self) -> bool {
Split::new(
&self.total,
self.summands.clone(),
self.inclusions.clone(),
self.projections.clone(),
)
.is_ok()
}
accessor_methods! {
pub total() -> &Module = |this| &this.total;
pub summands() -> &[Module] = |this| &this.summands;
pub inclusions() -> &[Morphism] = |this| &this.inclusions;
pub projections() -> &[Morphism] = |this| &this.projections;
}
}