use std::sync::Arc;
use crate::algebra::Algebra;
use crate::field::Fp;
use crate::module::ModuleError;
use crate::quiver::ArrowId;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct FamilyCoordinate {
pub(super) index: usize,
pub(super) arrow: ArrowId,
pub(super) row: usize,
pub(super) column: usize,
}
impl FamilyCoordinate {
accessor_methods! {
pub index() -> usize = |this| this.index;
pub arrow() -> ArrowId = |this| this.arrow;
pub row() -> usize = |this| this.row;
pub column() -> usize = |this| this.column;
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct FamilyCoordinateRange {
pub(super) arrow: ArrowId,
pub(super) start: usize,
pub(super) length: usize,
}
impl FamilyCoordinateRange {
accessor_methods! {
pub arrow() -> ArrowId = |this| this.arrow;
pub start() -> usize = |this| this.start;
pub length() -> usize = |this| this.length;
}
pub fn end(&self) -> usize {
self.start + self.length
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct FamilyArrowLayout {
pub(super) arrow: ArrowId,
pub(super) source: u32,
pub(super) target: u32,
pub(super) rows: usize,
pub(super) columns: usize,
pub(super) range: FamilyCoordinateRange,
}
impl FamilyArrowLayout {
accessor_methods! {
pub arrow() -> ArrowId = |this| this.arrow;
pub source() -> u32 = |this| this.source;
pub target() -> u32 = |this| this.target;
pub rows() -> usize = |this| this.rows;
pub columns() -> usize = |this| this.columns;
pub coordinate_range() -> FamilyCoordinateRange = |this| this.range;
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FamilyLayout {
pub(super) dimensions: Vec<usize>,
pub(super) arrows: Vec<FamilyArrowLayout>,
pub(super) coordinates: Vec<FamilyCoordinate>,
pub(super) hom_offsets: Vec<usize>,
pub(super) hom_variable_count: usize,
}
impl FamilyLayout {
accessor_methods! {
pub dimensions() -> &[usize] = |this| &this.dimensions;
pub arrows() -> &[FamilyArrowLayout] = |this| &this.arrows;
pub coordinates() -> &[FamilyCoordinate] = |this| &this.coordinates;
pub coordinate_count() -> usize = |this| this.coordinates.len();
pub hom_variable_count() -> usize = |this| this.hom_variable_count;
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct FamilyFixedEntry {
pub(super) arrow: ArrowId,
pub(super) row: usize,
pub(super) column: usize,
pub(super) value: Fp,
}
impl FamilyFixedEntry {
pub fn new(arrow: ArrowId, row: usize, column: usize, value: Fp) -> Self {
Self {
arrow,
row,
column,
value,
}
}
accessor_methods! {
pub arrow() -> ArrowId = |this| this.arrow;
pub row() -> usize = |this| this.row;
pub column() -> usize = |this| this.column;
pub value() -> Fp = |this| this.value;
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct FamilyParameter {
pub(super) name: String,
pub(super) arrow: ArrowId,
pub(super) row: usize,
pub(super) column: usize,
}
impl FamilyParameter {
pub fn new(name: impl Into<String>, arrow: ArrowId, row: usize, column: usize) -> Self {
Self {
name: name.into(),
arrow,
row,
column,
}
}
accessor_methods! {
pub name() -> &str = |this| &this.name;
pub arrow() -> ArrowId = |this| this.arrow;
pub row() -> usize = |this| this.row;
pub column() -> usize = |this| this.column;
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct FamilyParameterPosition {
pub(super) name: String,
pub(super) coordinate: FamilyCoordinate,
pub(super) index: usize,
}
impl FamilyParameterPosition {
accessor_methods! {
pub name() -> &str = |this| &this.name;
pub coordinate() -> FamilyCoordinate = |this| this.coordinate;
pub index() -> usize = |this| this.index;
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RelationTermLayout {
pub(super) coefficient: Fp,
pub(super) path: Vec<ArrowId>,
pub(super) factors: Vec<FamilyCoordinateRange>,
}
impl RelationTermLayout {
accessor_methods! {
pub coefficient() -> Fp = |this| this.coefficient;
pub path() -> &[ArrowId] = |this| &this.path;
pub factors() -> &[FamilyCoordinateRange] = |this| &this.factors;
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RelationEvaluationLayout {
pub(super) relation: usize,
pub(super) source: u32,
pub(super) target: u32,
pub(super) rows: usize,
pub(super) columns: usize,
pub(super) terms: Vec<RelationTermLayout>,
pub(super) coordinate_support: Vec<FamilyCoordinateRange>,
}
impl RelationEvaluationLayout {
accessor_methods! {
pub relation() -> usize = |this| this.relation;
pub source() -> u32 = |this| this.source;
pub target() -> u32 = |this| this.target;
pub rows() -> usize = |this| this.rows;
pub columns() -> usize = |this| this.columns;
pub terms() -> &[RelationTermLayout] = |this| &this.terms;
pub coordinate_support() -> &[FamilyCoordinateRange] = |this| &this.coordinate_support;
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct HomVariable {
pub(super) index: usize,
pub(super) vertex: u32,
pub(super) row: usize,
pub(super) column: usize,
}
impl HomVariable {
accessor_methods! {
pub index() -> usize = |this| this.index;
pub vertex() -> u32 = |this| this.vertex;
pub row() -> usize = |this| this.row;
pub column() -> usize = |this| this.column;
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum HomCoefficientSide {
Target,
Source,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct HomEquationTerm {
pub(super) variable: HomVariable,
pub(super) coefficient: FamilyCoordinate,
pub(super) side: HomCoefficientSide,
}
impl HomEquationTerm {
accessor_methods! {
pub variable() -> HomVariable = |this| this.variable;
pub coefficient() -> FamilyCoordinate = |this| this.coefficient;
pub side() -> HomCoefficientSide = |this| this.side;
}
pub fn sign(&self) -> i8 {
match self.side {
HomCoefficientSide::Target => 1,
HomCoefficientSide::Source => -1,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HomEquationLayout {
pub(super) index: usize,
pub(super) arrow: ArrowId,
pub(super) source_row: usize,
pub(super) target_column: usize,
pub(super) terms: Vec<HomEquationTerm>,
}
impl HomEquationLayout {
accessor_methods! {
pub index() -> usize = |this| this.index;
pub arrow() -> ArrowId = |this| this.arrow;
pub source_row() -> usize = |this| this.source_row;
pub target_column() -> usize = |this| this.target_column;
pub terms() -> &[HomEquationTerm] = |this| &this.terms;
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FamilyError {
DimensionVectorLength { expected: usize, got: usize },
ArrowOutOfRange { arrow: ArrowId, num_arrows: usize },
CoordinateOutOfRange {
arrow: ArrowId,
row: usize,
column: usize,
rows: usize,
columns: usize,
},
MatrixEntryOverflow {
arrow: ArrowId,
rows: usize,
columns: usize,
},
CoordinateCountOverflow,
CoordinateAllocationFailed { coordinates: usize },
HomVariableCountOverflow { vertex: u32, dimension: usize },
HomEquationCountOverflow {
arrow: ArrowId,
rows: usize,
columns: usize,
},
HomEquationAllocationFailed { equations: usize },
HomTermAllocationFailed {
arrow: ArrowId,
row: usize,
column: usize,
},
RelationOutputOverflow {
relation: usize,
rows: usize,
columns: usize,
},
RelationTermAllocationFailed { relation: usize, term: usize },
RelationSupportAllocationFailed { relation: usize },
EmptyParameterName,
DuplicateParameterName { name: String },
DuplicateFixedCoordinate {
arrow: ArrowId,
row: usize,
column: usize,
},
DuplicateParameterCoordinate {
arrow: ArrowId,
row: usize,
column: usize,
},
FixedParameterCollision {
arrow: ArrowId,
row: usize,
column: usize,
},
NonCanonicalFixedValue {
arrow: ArrowId,
row: usize,
column: usize,
value: u64,
},
ParameterCountMismatch { expected: usize, got: usize },
NonCanonicalParameterValue { index: usize, value: u64 },
Module(ModuleError),
}
display_error! { FamilyError {
Self::DimensionVectorLength { expected, got } => "dimension vector has {got} entries, quiver has {expected} vertices";
Self::ArrowOutOfRange { arrow, num_arrows } => "arrow {} outside 0..{num_arrows}", arrow.0;
Self::CoordinateOutOfRange { arrow, row, column, rows, columns } => "coordinate ({row}, {column}) for arrow {} is outside {rows}x{columns}", arrow.0;
Self::MatrixEntryOverflow { arrow, rows, columns } => "matrix for arrow {} has {rows}x{columns} entries, which overflow usize", arrow.0;
Self::CoordinateCountOverflow => "the total family coordinate count overflows usize";
Self::CoordinateAllocationFailed { coordinates } => "the family coordinate layout could not reserve {coordinates} entries";
Self::HomVariableCountOverflow { vertex, dimension } => "Hom variables at vertex {vertex} with dimension {dimension} overflow usize";
Self::HomEquationCountOverflow { arrow, rows, columns } => "Hom equations for arrow {} have {rows}x{columns} entries, which overflow usize", arrow.0;
Self::HomEquationAllocationFailed { equations } => "the Hom equation layout could not reserve {equations} equations";
Self::HomTermAllocationFailed { arrow, row, column } => "the Hom equation for arrow {} at ({row}, {column}) could not reserve its terms", arrow.0;
Self::RelationOutputOverflow { relation, rows, columns } => "relation {relation} output has {rows}x{columns} entries, which overflow usize";
Self::RelationTermAllocationFailed { relation, term } => "relation {relation} term {term} could not reserve its factors";
Self::RelationSupportAllocationFailed { relation } => "relation {relation} could not reserve its coordinate support";
Self::EmptyParameterName => "parameter name is empty";
Self::DuplicateParameterName { name } => "parameter name {name:?} occurs more than once";
Self::DuplicateFixedCoordinate { arrow, row, column } => "fixed coordinate ({row}, {column}) for arrow {} occurs more than once", arrow.0;
Self::DuplicateParameterCoordinate { arrow, row, column } => "parameter coordinate ({row}, {column}) for arrow {} occurs more than once", arrow.0;
Self::FixedParameterCollision { arrow, row, column } => "fixed coordinate ({row}, {column}) for arrow {} is also a parameter", arrow.0;
Self::NonCanonicalFixedValue { arrow, row, column, value } => "fixed coordinate ({row}, {column}) for arrow {} has non-canonical value {value}", arrow.0;
Self::ParameterCountMismatch { expected, got } => "specialization has {got} values, family has {expected} parameters";
Self::NonCanonicalParameterValue { index, value } => "parameter {index} has non-canonical value {value}";
Self::Module(error) => "specialization is not a module: {error}";
} }
error_source! { FamilyError {
Self::Module(error) => Some(error),
_ => None,
} }
impl From<ModuleError> for FamilyError {
fn from(error: ModuleError) -> Self {
Self::Module(error)
}
}
#[derive(Clone, Debug)]
pub struct CompiledModuleFamily {
pub(super) algebra: Arc<Algebra>,
pub(super) layout: FamilyLayout,
pub(super) fixed: Vec<FamilyFixedEntry>,
pub(super) parameters: Vec<FamilyParameterPosition>,
pub(super) parameter_indices: std::collections::BTreeMap<String, usize>,
pub(super) relation_evaluations: Vec<RelationEvaluationLayout>,
pub(super) hom_equations: Vec<HomEquationLayout>,
}