1use std::sync::Arc;
4
5use crate::field::Fp;
6use crate::hom::Morphism;
7use crate::homspace::{deterministic_complement, rref_coords_many, stack_rows};
8use crate::linalg::DenseMat;
9use crate::module::{Module, same_morphism_data, same_representation, same_slice};
10use crate::resolution::{ProjectiveResolution, resolve};
11
12use super::foundation::{
13 ExtError, check_pair, cochain_from_coordinates, coordinates, delta_matrix, hom_space_dim,
14 layout,
15};
16use super::product::ExtClass;
17
18#[derive(Clone, Debug, PartialEq, Eq)]
20pub enum ExtClassError {
21 IncompatibleSpaces,
24 MiddleMismatch,
27 DegreeOverflow { left: usize, right: usize },
29 SourceMismatch,
32 TargetMismatch,
35 NotCocycle { composite: Vec<Fp> },
38 CoordinateCountMismatch { expected: usize, got: usize },
40 NonCanonicalCoordinate { index: usize },
43 NotDegreeZeroEndo,
46 ResolutionDisagreement { degree: usize },
51}
52
53display_error! { error ExtClassError {
54 Self::IncompatibleSpaces => "the classes live in incompatible Ext spaces";
55 Self::MiddleMismatch => "the left class's target module is not the right class's source module";
56 Self::DegreeOverflow { left, right } => "Ext degree sum {left} + {right} is not representable";
57 Self::SourceMismatch => "the cochain's source is not the space's cochain term";
58 Self::TargetMismatch => "the cochain's target is not the space's target module";
59 Self::NotCocycle { .. } => "the cochain is not a cocycle";
60 Self::CoordinateCountMismatch { expected, got } => "coordinate vector has {got} entries, the space has dimension {expected}";
61 Self::NonCanonicalCoordinate { index } => "coordinate {index} is not a canonical element of the modules' field";
62 Self::NotDegreeZeroEndo => "the identity class lives only in Ext^0(M, M)";
63 Self::ResolutionDisagreement { degree } => "two resolutions of one module disagree at degree {degree}; crate defect";
64} }
65
66pub(super) fn product_degree(left: usize, right: usize) -> Result<usize, ExtClassError> {
67 left.checked_add(right)
68 .filter(|°ree| degree != usize::MAX)
69 .ok_or(ExtClassError::DegreeOverflow { left, right })
70}
71
72pub(super) struct ExtSpaceInner {
73 pub(super) source: Module,
74 pub(super) target: Module,
75 pub(super) degree: usize,
76 pub(super) resolution: ProjectiveResolution,
79 pub(super) term: Module,
81 pub(super) cocycles: DenseMat,
83 pub(super) coboundaries: DenseMat,
85 pub(super) complement: DenseMat,
87 pub(super) reps: Vec<Morphism>,
89}
90
91#[derive(Clone)]
103pub struct ExtSpace(pub(super) Arc<ExtSpaceInner>);
104
105debug_fields!(ExtSpace |this| {
106 "source_dim" => this.0.source.dim_vector();
107 "target_dim" => this.0.target.dim_vector();
108 "degree" => this.0.degree;
109 "dim" => this.dim();
110});
111
112impl ExtSpace {
113 pub fn new(m: &Module, n: &Module, k: usize) -> Result<ExtSpace, ExtError> {
119 check_pair(m, n)?;
120 let field = m.field();
121 let steps = k
122 .checked_add(1)
123 .ok_or(ExtError::DegreeOverflow { degree: k })?;
124 let resolution = resolve(m, steps);
125 let (term, cocycles, coboundaries) = if k < resolution.terms.len() {
126 let term = resolution.terms[k].clone();
127 let lay = layout(&term);
128 let width = hom_space_dim(&lay, n);
129 let cocycles = if k + 1 < resolution.terms.len() {
130 let next_lay = layout(&resolution.terms[k + 1]);
131 let delta = delta_matrix(&resolution.maps[k], &term, &lay, &next_lay, n);
132 delta.left_kernel_basis(&field).into_row_space_basis(&field)
133 } else {
134 DenseMat::identity(width)
135 };
136 let coboundaries = if k == 0 {
137 DenseMat::zero(0, width)
138 } else {
139 let prev = &resolution.terms[k - 1];
140 let prev_lay = layout(prev);
141 delta_matrix(&resolution.maps[k - 1], prev, &prev_lay, &lay, n)
142 .into_row_space_basis(&field)
143 };
144 (term, cocycles, coboundaries)
145 } else {
146 (
147 Module::zero(m.algebra()),
148 DenseMat::zero(0, 0),
149 DenseMat::zero(0, 0),
150 )
151 };
152 assert!(
153 rref_coords_many(&cocycles, &coboundaries, &field).is_some(),
154 "im delta^{} is not contained in ker delta^{k}; this is a bug in auslander",
155 k.wrapping_sub(1)
156 );
157 let complement = deterministic_complement(&cocycles, &coboundaries, &field);
158 let lay = layout(&term);
159 let reps = (0..complement.rows())
160 .map(|r| cochain_from_coordinates(&term, &lay, n, complement.row(r)))
161 .collect();
162 Ok(ExtSpace(Arc::new(ExtSpaceInner {
163 source: m.clone(),
164 target: n.clone(),
165 degree: k,
166 resolution,
167 term,
168 cocycles,
169 coboundaries,
170 complement,
171 reps,
172 })))
173 }
174
175 accessor_methods! {
176 pub dim() -> usize = |this| this.0.complement.rows();
179 pub source() -> &Module = |this| &this.0.source;
181 pub target() -> &Module = |this| &this.0.target;
183 pub degree() -> usize = |this| this.0.degree;
185 pub cochain_term() -> &Module = |this| &this.0.term;
188 pub representatives() -> &[Morphism] = |this| &this.0.reps;
190 pub cocycle_basis() -> &DenseMat = |this| &this.0.cocycles;
193 pub coboundary_basis() -> &DenseMat = |this| &this.0.coboundaries;
196 pub complement_basis() -> &DenseMat = |this| &this.0.complement;
199 pub(crate) resolution() -> &ProjectiveResolution = |this| &this.0.resolution;
201 }
202
203 pub fn is_compatible(&self, other: &ExtSpace) -> bool {
206 self.0.source.ptr_eq(&other.0.source)
207 && self.0.target.ptr_eq(&other.0.target)
208 && self.0.degree == other.0.degree
209 }
210
211 pub fn matches(&self, other: &ExtSpace) -> bool {
220 let (a, b) = (&*self.0, &*other.0);
221 self.is_compatible(other)
222 && a.resolution.agrees_with(&b.resolution)
223 && same_representation(&a.term, &b.term)
224 && a.cocycles == b.cocycles
225 && a.coboundaries == b.coboundaries
226 && a.complement == b.complement
227 && same_slice(&a.reps, &b.reps, same_morphism_data)
228 }
229
230 pub fn matches_recomputation(&self) -> bool {
237 let_or_false!(Ok(fresh) = ExtSpace::new(&self.0.source, &self.0.target, self.0.degree));
238 self.matches(&fresh)
239 }
240
241 pub fn zero_class(&self) -> ExtClass {
243 ExtClass {
244 space: self.clone(),
245 coords: vec![Fp::ZERO; self.dim()],
246 }
247 }
248
249 pub fn identity_class(&self) -> Result<ExtClass, ExtClassError> {
252 if self.0.degree != 0 || !self.0.source.ptr_eq(&self.0.target) {
253 return Err(ExtClassError::NotDegreeZeroEndo);
254 }
255 self.class_from_cocycle(&self.0.resolution.augmentation)
256 }
257
258 pub fn class_from_cocycle(&self, f: &Morphism) -> Result<ExtClass, ExtClassError> {
263 let inner = &self.0;
264 if !f.source().ptr_eq(&inner.term) {
265 return Err(ExtClassError::SourceMismatch);
266 }
267 if !f.target().ptr_eq(&inner.target) {
268 return Err(ExtClassError::TargetMismatch);
269 }
270 let field = inner.source.field();
271 if inner.degree + 1 < inner.resolution.terms.len() {
272 let composite = inner.resolution.maps[inner.degree]
273 .then(f)
274 .expect("d_{k+1} targets the cochain term");
275 if !composite.is_zero() {
276 let next_lay = layout(&inner.resolution.terms[inner.degree + 1]);
277 return Err(ExtClassError::NotCocycle {
278 composite: coordinates(&composite, &next_lay, &inner.target),
279 });
280 }
281 }
282 let lay = layout(&inner.term);
283 let f_coords = coordinates(f, &lay, &inner.target);
284 let stacked = stack_rows(
285 &[&inner.complement, &inner.coboundaries],
286 inner.complement.cols(),
287 );
288 let x = stacked
289 .transpose()
290 .solve(&f_coords, &field)
291 .expect("a cocycle lies in the span of the complement plus the coboundaries; this is a bug in auslander");
292 Ok(ExtClass {
293 space: self.clone(),
294 coords: x[..inner.complement.rows()].to_vec(),
295 })
296 }
297
298 #[cfg(test)]
305 pub(crate) fn with_bases(
306 &self,
307 cocycles: DenseMat,
308 coboundaries: DenseMat,
309 complement: DenseMat,
310 ) -> ExtSpace {
311 let inner = &self.0;
312 let lay = layout(&inner.term);
313 let reps = (0..complement.rows())
314 .map(|r| cochain_from_coordinates(&inner.term, &lay, &inner.target, complement.row(r)))
315 .collect();
316 self.with_parts(cocycles, coboundaries, complement, reps)
317 }
318
319 #[cfg(test)]
322 pub(crate) fn with_representatives(&self, reps: Vec<Morphism>) -> ExtSpace {
323 let inner = &self.0;
324 self.with_parts(
325 inner.cocycles.clone(),
326 inner.coboundaries.clone(),
327 inner.complement.clone(),
328 reps,
329 )
330 }
331
332 #[cfg(test)]
333 fn with_parts(
334 &self,
335 cocycles: DenseMat,
336 coboundaries: DenseMat,
337 complement: DenseMat,
338 reps: Vec<Morphism>,
339 ) -> ExtSpace {
340 let inner = &self.0;
341 ExtSpace(Arc::new(ExtSpaceInner {
342 source: inner.source.clone(),
343 target: inner.target.clone(),
344 degree: inner.degree,
345 resolution: ProjectiveResolution {
346 terms: inner.resolution.terms.clone(),
347 maps: inner.resolution.maps.clone(),
348 augmentation: inner.resolution.augmentation.clone(),
349 end: inner.resolution.end,
350 },
351 term: inner.term.clone(),
352 cocycles,
353 coboundaries,
354 complement,
355 reps,
356 }))
357 }
358
359 pub fn class_from_coordinates(&self, coords: &[Fp]) -> Result<ExtClass, ExtClassError> {
363 if coords.len() != self.dim() {
364 return Err(ExtClassError::CoordinateCountMismatch {
365 expected: self.dim(),
366 got: coords.len(),
367 });
368 }
369 let modulus = self.0.source.field().modulus();
370 if let Some(index) = coords.iter().position(|c| c.raw() >= modulus) {
371 return Err(ExtClassError::NonCanonicalCoordinate { index });
372 }
373 Ok(ExtClass {
374 space: self.clone(),
375 coords: coords.to_vec(),
376 })
377 }
378}