use std::sync::Arc;
use crate::algebra::{Algebra, BasisIdx};
use crate::field::Fp;
use crate::linalg::DenseMat;
use super::bar::{Layout, bar_hochschild, decode_tuple, input_rank, tuple_starts};
use super::limits::{
BarBudgetDiagnostics, BarCoordinate, BarInput, BarLimits, BarRunDiagnostics, HochschildError,
};
use super::verification::{row_times, same_complete, same_cut, same_degree};
#[derive(Clone, Debug)]
pub struct HochschildCohomology {
pub(super) algebra: Arc<Algebra>,
pub(super) requested_degree: usize,
pub(super) limits: BarLimits,
pub(super) degrees: Vec<HochschildDegree>,
pub(super) diagnostics: BarRunDiagnostics,
}
impl HochschildCohomology {
accessor_methods! {
pub algebra() -> &Arc<Algebra> = |this| &this.algebra;
pub requested_degree() -> usize = |this| this.requested_degree;
pub limits() -> BarLimits = |this| this.limits;
pub degrees() -> &[HochschildDegree] = |this| &this.degrees;
pub diagnostics() -> BarRunDiagnostics = |this| this.diagnostics;
pub degree(degree: usize) -> Option<&HochschildDegree> = |this| this.degrees.get(degree);
pub verify() -> bool = |this| matches!(
bar_hochschild(&this.algebra, this.requested_degree, this.limits),
Ok(HochschildOutcome::Complete(rebuilt)) if same_complete(this, &rebuilt)
);
}
}
#[derive(Clone, Debug)]
pub struct IncompleteHochschildCohomology {
pub(super) algebra: Arc<Algebra>,
pub(super) requested_degree: usize,
pub(super) limits: BarLimits,
pub(super) degrees: Vec<HochschildDegree>,
pub(super) diagnostics: BarBudgetDiagnostics,
}
impl IncompleteHochschildCohomology {
accessor_methods! {
pub algebra() -> &Arc<Algebra> = |this| &this.algebra;
pub requested_degree() -> usize = |this| this.requested_degree;
pub limits() -> BarLimits = |this| this.limits;
pub completed_degrees() -> &[HochschildDegree] = |this| &this.degrees;
pub diagnostics() -> &BarBudgetDiagnostics = |this| &this.diagnostics;
pub verify() -> bool = |this| matches!(
bar_hochschild(&this.algebra, this.requested_degree, this.limits),
Ok(HochschildOutcome::Cut(rebuilt)) if same_cut(this, &rebuilt)
);
}
}
#[derive(Clone, Debug)]
pub enum HochschildOutcome {
Complete(HochschildCohomology),
Cut(IncompleteHochschildCohomology),
}
#[derive(Clone, Debug)]
pub(super) struct DegreeInner {
pub(super) algebra: Arc<Algebra>,
pub(super) degree: usize,
pub(super) limits: BarLimits,
pub(super) layout: Layout,
pub(super) differential: DenseMat,
pub(super) cocycles: DenseMat,
pub(super) coboundaries: DenseMat,
pub(super) complement: DenseMat,
}
#[derive(Clone, Debug)]
pub struct HochschildDegree(pub(super) Arc<DegreeInner>);
impl HochschildDegree {
accessor_methods! {
pub algebra() -> &Arc<Algebra> = |this| &this.0.algebra;
pub degree() -> usize = |this| this.0.degree;
pub dim() -> usize = |this| this.0.complement.rows();
pub cochain_basis() -> &[BarCoordinate] = |this| &this.0.layout.coordinates;
pub cocycle_basis() -> &DenseMat = |this| &this.0.cocycles;
pub coboundary_basis() -> &DenseMat = |this| &this.0.coboundaries;
pub complement_basis() -> &DenseMat = |this| &this.0.complement;
pub differential() -> &DenseMat = |this| &this.0.differential;
}
pub fn input_for_rank(&self, tuple_rank: usize) -> Option<BarInput> {
if tuple_rank >= self.0.layout.offsets.len() - 1 {
return None;
}
if self.0.degree == 0 {
return Some(BarInput::Vertex(tuple_rank as u32));
}
let starts = tuple_starts(&self.0.algebra, self.0.degree);
let mut tuple = Vec::with_capacity(self.0.degree);
decode_tuple(
&self.0.algebra,
self.0.degree,
tuple_rank,
&starts,
&mut tuple,
);
Some(BarInput::Tuple(tuple))
}
pub fn zero_class(&self) -> HochschildClass {
HochschildClass {
degree: self.clone(),
coordinates: vec![self.0.algebra.field().zero(); self.dim()],
}
}
pub fn class_from_coordinates(
&self,
coordinates: Vec<Fp>,
) -> Result<HochschildClass, HochschildError> {
if coordinates.len() != self.dim() {
return Err(HochschildError::CoordinateCount {
expected: self.dim(),
got: coordinates.len(),
});
}
Ok(HochschildClass {
degree: self.clone(),
coordinates,
})
}
pub fn verify(&self) -> bool {
match bar_hochschild(&self.0.algebra, self.0.degree, self.0.limits) {
Ok(HochschildOutcome::Complete(rebuilt)) => rebuilt
.degree(self.0.degree)
.is_some_and(|degree| same_degree(self, degree)),
Ok(HochschildOutcome::Cut(_)) | Err(_) => false,
}
}
}
#[derive(Clone, Debug)]
pub struct HochschildClass {
degree: HochschildDegree,
coordinates: Vec<Fp>,
}
impl HochschildClass {
accessor_methods! {
pub degree() -> &HochschildDegree = |this| &this.degree;
pub coordinates() -> &[Fp] = |this| &this.coordinates;
pub is_zero() -> bool = |this| this.coordinates.iter().all(|value| value.is_zero());
}
pub fn representative(&self) -> Vec<Fp> {
row_times(
&self.coordinates,
&self.degree.0.complement,
&self.degree.0.algebra.field(),
)
}
pub fn evaluate(&self, input: &BarInput) -> Result<Vec<(BasisIdx, Fp)>, HochschildError> {
let inner = &self.degree.0;
let rank = input_rank(&inner.algebra, inner.degree, input)?;
let representative = self.representative();
let start = inner.layout.offsets[rank];
let end = inner.layout.offsets[rank + 1];
Ok(inner.layout.coordinates[start..end]
.iter()
.enumerate()
.filter_map(|(offset, coordinate)| {
let value = representative[start + offset];
(!value.is_zero()).then_some((coordinate.output, value))
})
.collect())
}
pub fn verify(&self) -> bool {
self.coordinates.len() == self.degree.dim() && self.degree.verify()
}
}