use crate::field::Fp;
use crate::hom::Morphism;
use crate::homspace::{row_times, rref_coords};
use crate::linalg::DenseMat;
use crate::module::{same_morphism_data, same_representation};
use super::foundation::{
chain_maps, chain_terms, cochain_from_coordinates, coordinates, layout, lift_through,
};
use super::space::{ExtClassError, ExtSpace, product_degree};
#[derive(Clone, Debug)]
pub struct ExtClass {
pub(super) space: ExtSpace,
pub(super) coords: Vec<Fp>,
}
impl ExtClass {
accessor_methods! {
pub space() -> &ExtSpace = |this| &this.space;
pub coordinates() -> &[Fp] = |this| &this.coords;
}
pub fn is_zero(&self) -> bool {
self.coords.iter().all(|c| c.is_zero())
}
fn with_coordinates(&self, coords: Vec<Fp>) -> ExtClass {
ExtClass {
space: self.space.clone(),
coords,
}
}
fn require_compatible(&self, other: &ExtClass) -> Result<(), ExtClassError> {
self.space
.is_compatible(&other.space)
.then_some(())
.ok_or(ExtClassError::IncompatibleSpaces)
}
pub fn add(&self, other: &ExtClass) -> Result<ExtClass, ExtClassError> {
self.require_compatible(other)?;
let field = self.space.0.source.field();
let coords = self
.coords
.iter()
.zip(&other.coords)
.map(|(&a, &b)| field.add(a, b))
.collect();
Ok(self.with_coordinates(coords))
}
pub fn neg(&self) -> ExtClass {
let field = self.space.0.source.field();
self.with_coordinates(self.coords.iter().map(|&c| field.neg(c)).collect())
}
pub fn scale(&self, c: Fp) -> ExtClass {
let field = self.space.0.source.field();
self.with_coordinates(self.coords.iter().map(|&x| field.mul(c, x)).collect())
}
pub fn equals(&self, other: &ExtClass) -> Result<bool, ExtClassError> {
self.require_compatible(other)?;
Ok(self.coords == other.coords)
}
pub fn representative(&self) -> Morphism {
let inner = &self.space.0;
let field = inner.source.field();
let row = row_times(&self.coords, &inner.complement, &field);
let lay = layout(&inner.term);
cochain_from_coordinates(&inner.term, &lay, &inner.target, &row)
}
pub fn then(&self, other: &ExtClass) -> Result<ExtClass, ExtClassError> {
self.then_with_witness(other).map(|(class, _)| class)
}
pub fn then_with_witness(
&self,
other: &ExtClass,
) -> Result<(ExtClass, ProductWitness), ExtClassError> {
let product_space = self.product_space(other)?;
self.then_with_witness_in(other, &product_space)
}
pub fn then_in(
&self,
other: &ExtClass,
product_space: &ExtSpace,
) -> Result<ExtClass, ExtClassError> {
self.then_with_witness_in(other, product_space)
.map(|(class, _)| class)
}
pub fn then_with_witness_in(
&self,
other: &ExtClass,
product_space: &ExtSpace,
) -> Result<(ExtClass, ProductWitness), ExtClassError> {
if !self.space.0.target.ptr_eq(&other.space.0.source) {
return Err(ExtClassError::MiddleMismatch);
}
let n = other.space.0.degree;
let product_degree = product_degree(self.space.0.degree, n)?;
if !product_space.0.source.ptr_eq(&self.space.0.source)
|| !product_space.0.target.ptr_eq(&other.space.0.target)
|| product_space.0.degree != product_degree
{
return Err(ExtClassError::IncompatibleSpaces);
}
let lifts = chain_lifts(self, other, product_space)?;
let g = other.representative();
let h = lifts[n]
.then(&g)
.expect("the last lift targets the right class's cochain term");
let class = product_space
.class_from_cocycle(&h)
.expect("a Yoneda product of cocycles is a cocycle");
Ok((class, ProductWitness { lifts }))
}
pub fn product_space(&self, other: &ExtClass) -> Result<ExtSpace, ExtClassError> {
if !self.space.0.target.ptr_eq(&other.space.0.source) {
return Err(ExtClassError::MiddleMismatch);
}
let degree = product_degree(self.space.0.degree, other.space.0.degree)?;
Ok(
ExtSpace::new(&self.space.0.source, &other.space.0.target, degree)
.expect("the middle module fixes one algebra and the degree has a successor"),
)
}
}
pub(super) fn chain_lifts(
alpha: &ExtClass,
beta: &ExtClass,
product_space: &ExtSpace,
) -> Result<Vec<Morphism>, ExtClassError> {
let m = alpha.space.0.degree;
let n = beta.space.0.degree;
let deep = &product_space.0.resolution;
let own = &alpha.space.0.resolution;
for (degree, (x, y)) in own.terms.iter().zip(&deep.terms).enumerate() {
if !same_representation(x, y) {
return Err(ExtClassError::ResolutionDisagreement { degree });
}
}
for (k, (x, y)) in own.maps.iter().zip(&deep.maps).enumerate() {
if !same_morphism_data(x, y) {
return Err(ExtClassError::ResolutionDisagreement { degree: k + 1 });
}
}
let algebra = alpha.space.0.source.algebra();
let chain_m = chain_terms(deep, m, m + n, &product_space.0.term, algebra);
let chain_dm = chain_maps(deep, m, n, &chain_m);
let res_n = &beta.space.0.resolution;
let chain_n = chain_terms(res_n, 0, n, &beta.space.0.term, algebra);
let chain_dn = chain_maps(res_n, 0, n, &chain_n);
let rep = alpha.representative();
let nv = algebra.quiver().num_vertices();
let rep_maps: Vec<DenseMat> = (0..nv).map(|v| rep.map_at(v).clone()).collect();
let f = Morphism::new(&chain_m[0], &beta.space.0.source, rep_maps)
.expect("the compared resolution prefix matches, so the representative transports");
let mut lifts = vec![lift_through(&chain_m[0], &res_n.augmentation, &f)];
for i in 1..=n {
let rhs = chain_dm[i - 1]
.then(&lifts[i - 1])
.expect("chain endpoints line up by construction");
lifts.push(lift_through(&chain_m[i], &chain_dn[i - 1], &rhs));
}
Ok(lifts)
}
#[derive(Clone, Debug)]
pub struct ProductWitness {
pub(super) lifts: Vec<Morphism>,
}
impl ProductWitness {
accessor_methods! {
pub lifts() -> &[Morphism] = |this| &this.lifts;
}
pub fn verify(&self, alpha: &ExtClass, beta: &ExtClass, product: &ExtClass) -> bool {
let inner = &product.space.0;
let_or_false!(Ok(fresh) = ExtSpace::new(&inner.source, &inner.target, inner.degree));
self.verify_against(alpha, beta, product, &fresh)
}
pub fn verify_against(
&self,
alpha: &ExtClass,
beta: &ExtClass,
product: &ExtClass,
recomputed: &ExtSpace,
) -> bool {
let m = alpha.space.0.degree;
let n = beta.space.0.degree;
verify_guard!(
product.space.0.degree == m + n
&& beta.space.0.source.ptr_eq(&alpha.space.0.target)
&& product.space.0.source.ptr_eq(&alpha.space.0.source)
&& product.space.0.target.ptr_eq(&beta.space.0.target)
&& self.lifts.len() == n + 1
&& product.coords.len() == product.space.dim()
&& product.space.matches(recomputed)
);
let deep = &product.space.0.resolution;
let res_n = &beta.space.0.resolution;
let field = alpha.space.0.source.field();
let algebra = alpha.space.0.source.algebra();
let nv = algebra.quiver().num_vertices();
let rep = alpha.representative();
let rep_maps: Vec<DenseMat> = (0..nv).map(|v| rep.map_at(v).clone()).collect();
let_or_false!(
Ok(f) = Morphism::new(self.lifts[0].source(), &beta.space.0.source, rep_maps)
);
let_or_false!(Ok(lhs) = self.lifts[0].then(&res_n.augmentation));
verify_guard!(lhs == f);
let terms_m = chain_terms(deep, m, m + n, &product.space.0.term, algebra);
let maps_m = chain_maps(deep, m, n, &terms_m);
let terms_n = chain_terms(res_n, 0, n, &beta.space.0.term, algebra);
let maps_n = chain_maps(res_n, 0, n, &terms_n);
verify_guard!(
maps_m
.iter()
.zip(&self.lifts)
.zip(self.lifts.iter().skip(1).zip(&maps_n))
.all(|((left, lift), (next_lift, right))| {
matches!(
(left.then(lift), next_lift.then(right)),
(Ok(left), Ok(right)) if left == right
)
})
);
let g = beta.representative();
let_or_false!(Ok(h) = self.lifts[n].then(&g));
verify_guard!(h.source().ptr_eq(&product.space.0.term));
let lay = layout(&product.space.0.term);
let h_coords = coordinates(&h, &lay, &product.space.0.target);
let expected = row_times(&product.coords, &product.space.0.complement, &field);
let diff: Vec<Fp> = h_coords
.iter()
.zip(&expected)
.map(|(&a, &b)| field.sub(a, b))
.collect();
rref_coords(&product.space.0.coboundaries, &diff, &field).is_some()
}
}