use std::sync::Arc;
use crate::algebra::Algebra;
use crate::quiver::{ArrowId, PathWord, Quiver, QuiverError};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum OppositeError {
AlgebraOutsidePair,
NotDualOfTarget,
NotDualOfSource,
SummandOutOfRange { vertex: u32, num_vertices: u32 },
RowCountMismatch { expected: usize, got: usize },
ColumnCountMismatch {
row: usize,
expected: usize,
got: usize,
},
CoefficientCountMismatch {
row: usize,
col: usize,
expected: usize,
got: usize,
},
NonCanonicalCoefficient {
row: usize,
col: usize,
index: usize,
},
SourceNotTheDeclaredSum,
TargetNotTheDeclaredSum,
}
display_error! { error OppositeError {
Self::AlgebraOutsidePair => "the algebra is neither side of the opposite pair";
Self::NotDualOfTarget => "dual_of_target is not the entrywise dual of the morphism's target";
Self::NotDualOfSource => "dual_of_source is not the entrywise dual of the morphism's source";
Self::SummandOutOfRange { vertex, num_vertices } => "summand vertex {vertex} outside 0..{num_vertices}";
Self::RowCountMismatch { expected, got } => "entries has {got} rows, sources has {expected} summands";
Self::ColumnCountMismatch { row, expected, got } => "entries row {row} has {got} columns, targets has {expected} summands";
Self::CoefficientCountMismatch { row, col, expected, got } => "entry ({row}, {col}) has {got} coefficients, its path component has {expected}";
Self::NonCanonicalCoefficient { row, col, index } => "entry ({row}, {col}) has a non-canonical coefficient at index {index} for the algebra's field";
Self::SourceNotTheDeclaredSum => "the morphism's source is not the standard direct sum of the declared source summands";
Self::TargetNotTheDeclaredSum => "the morphism's target is not the standard direct sum of the declared target summands";
} }
#[derive(Clone, Debug)]
pub struct OppositeMap {
pub(super) algebra: Arc<Algebra>,
pub(super) opposite: Arc<Algebra>,
}
impl OppositeMap {
accessor_methods! {
pub algebra() -> &Arc<Algebra> = |this| &this.algebra;
pub opposite() -> &Arc<Algebra> = |this| &this.opposite;
pub arrow_to_op(a: ArrowId) -> ArrowId = |this| checked_arrow(a, this.algebra.quiver().num_arrows(), "arrow_to_op");
pub arrow_from_op(a: ArrowId) -> ArrowId = |this| checked_arrow(a, this.opposite.quiver().num_arrows(), "arrow_from_op");
}
pub fn word_to_op(&self, word: &PathWord) -> Result<PathWord, QuiverError> {
word.validate_in(self.algebra.quiver())?;
Ok(reversed(word, self.opposite.quiver()))
}
pub fn word_from_op(&self, word: &PathWord) -> Result<PathWord, QuiverError> {
word.validate_in(self.opposite.quiver())?;
Ok(reversed(word, self.algebra.quiver()))
}
pub(super) fn other_side(
&self,
algebra: &Arc<Algebra>,
) -> Result<&Arc<Algebra>, OppositeError> {
if Arc::ptr_eq(algebra, &self.algebra) {
Ok(&self.opposite)
} else if Arc::ptr_eq(algebra, &self.opposite) {
Ok(&self.algebra)
} else {
Err(OppositeError::AlgebraOutsidePair)
}
}
}
fn checked_arrow(arrow: ArrowId, count: usize, site: &str) -> ArrowId {
assert!(
arrow.index() < count,
"{site}: arrow id {} out of range",
arrow.0
);
arrow
}
pub(super) fn reversed(word: &PathWord, quiver: &Quiver) -> PathWord {
if word.is_trivial() {
PathWord::trivial_unchecked(word.source())
} else {
let arrows = word.arrows().iter().rev().copied().collect();
PathWord::from_arrows_unchecked(quiver, arrows)
}
}