use std::sync::Arc;
use crate::algebra::Algebra;
use crate::field::unit_vector;
use crate::hom::Morphism;
use crate::homspace::{HomQuotient, HomSubspace};
use crate::indec::IndecomposableModule;
use crate::module::Module;
use super::radical::{category_radical, quotient_or_defect, span_of_composites};
use super::{ArQuiverError, IndecomposableCatalog};
#[derive(Debug)]
pub struct ArVertex {
id: usize,
module: Arc<IndecomposableModule>,
residue_degree: usize,
projective: bool,
injective: bool,
}
impl ArVertex {
accessor_methods! {
pub id() -> usize = |this| this.id;
pub module() -> &IndecomposableModule = |this| &this.module;
pub residue_degree() -> usize = |this| this.residue_degree;
pub projective() -> bool = |this| this.projective;
pub injective() -> bool = |this| this.injective;
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ArrowValuation {
Plain(usize),
Valued {
base_dim: usize,
over_source: usize,
over_target: usize,
},
}
#[derive(Debug)]
pub struct ArArrow {
pub(super) source: usize,
pub(super) target: usize,
pub(super) base_dim: usize,
pub(super) over_source_residue: usize,
pub(super) over_target_residue: usize,
pub(super) representatives: Vec<Morphism>,
}
impl ArArrow {
accessor_methods! {
pub source() -> usize = |this| this.source;
pub target() -> usize = |this| this.target;
pub base_dim() -> usize = |this| this.base_dim;
pub over_source_residue() -> usize = |this| this.over_source_residue;
pub over_target_residue() -> usize = |this| this.over_target_residue;
pub representatives() -> &[Morphism] = |this| &this.representatives;
}
pub fn valuation(&self) -> ArrowValuation {
if self.base_dim == self.over_source_residue && self.base_dim == self.over_target_residue {
ArrowValuation::Plain(self.base_dim)
} else {
ArrowValuation::Valued {
base_dim: self.base_dim,
over_source: self.over_source_residue,
over_target: self.over_target_residue,
}
}
}
}
#[derive(Debug)]
pub struct ArQuiver {
catalog: IndecomposableCatalog,
vertices: Vec<ArVertex>,
arrows: Vec<ArArrow>,
}
impl ArQuiver {
accessor_methods! {
pub catalog() -> &IndecomposableCatalog = |this| &this.catalog;
pub vertices() -> &[ArVertex] = |this| &this.vertices;
pub arrows() -> &[ArArrow] = |this| &this.arrows;
}
}
pub(crate) fn over_residue(
base_dim: usize,
residue_degree: usize,
module: &Module,
) -> Result<usize, ArQuiverError> {
if residue_degree == 0 || !base_dim.is_multiple_of(residue_degree) {
return Err(ArQuiverError::ResidueDegreeDoesNotDivide {
dim_vector: module.dim_vector().to_vec(),
base_dim,
residue_degree,
});
}
Ok(base_dim / residue_degree)
}
fn vertices_of(catalog: &IndecomposableCatalog) -> Result<Vec<ArVertex>, ArQuiverError> {
let mut vertices = Vec::with_capacity(catalog.len());
for (id, entry) in catalog.entries().iter().enumerate() {
vertices.push(ArVertex {
id,
residue_degree: entry.residue_degree(),
projective: entry.is_projective(),
injective: entry.is_injective().map_err(ArQuiverError::Injective)?,
module: entry.clone(),
})
}
Ok(vertices)
}
fn radicals_of(catalog: &IndecomposableCatalog) -> Result<Vec<HomSubspace>, ArQuiverError> {
let n = catalog.len();
let mut radicals = Vec::with_capacity(n * n);
for x in catalog.entries() {
for y in catalog.entries() {
radicals.push(category_radical(x, y)?);
}
}
Ok(radicals)
}
fn quotient_for_pair(
catalog: &IndecomposableCatalog,
radicals: &[HomSubspace],
source: usize,
target: usize,
) -> Result<Option<HomQuotient>, ArQuiverError> {
let n = catalog.len();
let (x, y) = (
catalog.entries()[source].module(),
catalog.entries()[target].module(),
);
let square = span_of_composites(
x,
y,
(0..n).map(|k| (&radicals[source * n + k], &radicals[k * n + target])),
)?;
let quotient = quotient_or_defect(&radicals[source * n + target], &square)?;
if quotient.dim() == 0 {
return Ok(None);
}
Ok(Some(quotient))
}
fn arrow_for_pair(
catalog: &IndecomposableCatalog,
vertices: &[ArVertex],
radicals: &[HomSubspace],
source: usize,
target: usize,
) -> Result<Option<ArArrow>, ArQuiverError> {
let Some(quotient) = quotient_for_pair(catalog, radicals, source, target)? else {
return Ok(None);
};
let base_dim = quotient.dim();
let (x, y) = (
catalog.entries()[source].module(),
catalog.entries()[target].module(),
);
Ok(Some(ArArrow {
source,
target,
base_dim,
over_source_residue: over_residue(base_dim, vertices[source].residue_degree, x)?,
over_target_residue: over_residue(base_dim, vertices[target].residue_degree, y)?,
representatives: (0..base_dim)
.map(|index| quotient.representative(&unit_vector(base_dim, index)))
.collect(),
}))
}
fn arrows_of_catalog(
catalog: &IndecomposableCatalog,
vertices: &[ArVertex],
radicals: &[HomSubspace],
) -> Result<Vec<ArArrow>, ArQuiverError> {
let mut arrows = Vec::new();
for source in 0..catalog.len() {
for target in 0..catalog.len() {
if let Some(arrow) = arrow_for_pair(catalog, vertices, radicals, source, target)? {
arrows.push(arrow);
}
}
}
Ok(arrows)
}
fn quiver_of(catalog: IndecomposableCatalog) -> Result<ArQuiver, ArQuiverError> {
let vertices = vertices_of(&catalog)?;
let radicals = radicals_of(&catalog)?;
let arrows = arrows_of_catalog(&catalog, &vertices, &radicals)?;
Ok(ArQuiver {
catalog,
vertices,
arrows,
})
}
pub fn ar_quiver(algebra: &Arc<Algebra>) -> Result<ArQuiver, ArQuiverError> {
let catalog = match IndecomposableCatalog::dynkin(algebra) {
Ok(catalog) => catalog,
Err(dynkin) => match IndecomposableCatalog::nakayama(algebra) {
Ok(catalog) => catalog,
Err(nakayama) => return Err(ArQuiverError::UnsupportedDomain { dynkin, nakayama }),
},
};
quiver_of(catalog)
}