use crate::complex::{ComplexError, ExactComplex, ExactnessOutcome, NonExactWitness};
use crate::homotopy::{BoundedComplex, BoundedComplexError, ChainMap};
#[derive(Clone, Debug)]
pub enum QuasiIsomorphismError {
Cone(BoundedComplexError),
Complex(ComplexError),
NotExact(NonExactWitness),
}
display_error! { QuasiIsomorphismError {
Self::Cone(error) => "mapping cone construction failed: {error}";
Self::Complex(error) => "mapping cone is invalid: {error}";
Self::NotExact(witness) => "mapping cone has nonzero homology at term {}", witness.homology().index();
} }
error_source! { QuasiIsomorphismError {
Self::Cone(error) => Some(error),
Self::Complex(error) => Some(error),
Self::NotExact(_) => None,
} }
#[derive(Clone, Debug)]
pub struct QuasiIsomorphism {
map: ChainMap,
exact_cone: ExactComplex,
}
impl QuasiIsomorphism {
pub fn new(map: ChainMap) -> Result<QuasiIsomorphism, QuasiIsomorphismError> {
let checked = map
.mapping_cone()
.map_err(QuasiIsomorphismError::Cone)?
.to_checked()
.map_err(QuasiIsomorphismError::Complex)?;
match checked.exactness() {
ExactnessOutcome::Exact(exact_cone) => Ok(QuasiIsomorphism { map, exact_cone }),
ExactnessOutcome::NotExact(witness) => Err(QuasiIsomorphismError::NotExact(witness)),
}
}
accessor_methods! {
pub map() -> &ChainMap = |this| &this.map;
pub exact_cone() -> &ExactComplex = |this| &this.exact_cone;
}
pub fn verify(&self) -> bool {
self.map.mapping_cone().is_ok_and(|cone| {
cone.to_checked().is_ok_and(|checked| {
self.map.verify() && self.exact_cone.verify_reconstruction(&checked)
})
})
}
}
pub(super) fn same_nominal_complex(left: &BoundedComplex, right: &BoundedComplex) -> bool {
left.range() == right.range()
&& left
.terms()
.iter()
.zip(right.terms())
.all(|(a, b)| a.ptr_eq(b))
&& left.differentials() == right.differentials()
}
pub(super) fn agrees_with_zero_padding(input: &BoundedComplex, padded: &BoundedComplex) -> bool {
input
.padded_to(padded.range())
.is_ok_and(|rebuilt| rebuilt.agrees_with(padded))
}