use std::sync::Arc;
use crate::algebra::Algebra;
use crate::control::{ComputationControl, ProgressStage};
use crate::field::Fp;
use crate::hom::{HomError, Morphism};
use crate::iso::{is_isomorphic, IsoOutcome};
use crate::linalg::DenseMat;
use crate::module::{same_representation, Module, ModuleError};
use crate::quiver::ArrowId;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct CensusCoordinate {
arrow: ArrowId,
row: usize,
column: usize,
}
impl CensusCoordinate {
accessor_methods! {
pub arrow() -> ArrowId = |this| this.arrow;
pub row() -> usize = |this| this.row;
pub column() -> usize = |this| this.column;
}
}
#[derive(Clone, Debug)]
pub struct CensusDomain {
algebra: Arc<Algebra>,
dimensions: Vec<usize>,
coordinates: Vec<CensusCoordinate>,
raw_space_size: u128,
}
impl PartialEq for CensusDomain {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.algebra, &other.algebra)
&& self.dimensions == other.dimensions
&& self.coordinates == other.coordinates
&& self.raw_space_size == other.raw_space_size
}
}
impl Eq for CensusDomain {}
impl CensusDomain {
pub fn new(algebra: &Arc<Algebra>, dimensions: Vec<usize>) -> Result<Self, CensusError> {
validate_dimensions(algebra, &dimensions)?;
let coordinates = coordinate_layout(algebra, &dimensions)?;
let raw_space_size = checked_power(algebra.field().modulus(), coordinates.len())?;
Ok(Self {
algebra: algebra.clone(),
dimensions,
coordinates,
raw_space_size,
})
}
accessor_methods! {
pub algebra() -> &Arc<Algebra> = |this| &this.algebra;
pub dimensions() -> &[usize] = |this| &this.dimensions;
pub coordinates() -> &[CensusCoordinate] = |this| &this.coordinates;
pub coordinate_count() -> usize = |this| this.coordinates.len();
pub raw_space_size() -> u128 = |this| this.raw_space_size;
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum CensusRetention {
AllAssignments,
RepresentativesOnly,
}
impl CensusRetention {
pub const fn as_str(self) -> &'static str {
match self {
Self::AllAssignments => "all_assignments",
Self::RepresentativesOnly => "representatives_only",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct CensusLimits {
pub retention: CensusRetention,
pub max_candidates: usize,
pub max_representatives: usize,
pub max_assignments: usize,
pub max_isomorphism_checks: usize,
pub max_work_units: usize,
}
impl Default for CensusLimits {
fn default() -> Self {
Self {
retention: CensusRetention::AllAssignments,
max_candidates: 1_000_000,
max_representatives: 100_000,
max_assignments: 1_000_000,
max_isomorphism_checks: 1_000_000,
max_work_units: 10_000_000,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum CensusWorkStage {
Candidate,
Isomorphism,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CensusCutReason {
Cancelled,
CandidateLimit { limit: usize },
RepresentativeLimit { limit: usize },
AssignmentLimit { limit: usize },
IsomorphismLimit { limit: usize },
WorkLimit {
stage: CensusWorkStage,
limit: usize,
},
UnknownIsomorphism {
representative: usize,
reason: String,
},
}
display_error! { CensusCutReason {
Self::Cancelled => "census was cancelled";
Self::CandidateLimit { limit } => "candidate limit {limit} reached";
Self::RepresentativeLimit { limit } => "representative limit {limit} reached";
Self::AssignmentLimit { limit } => "assignment limit {limit} reached";
Self::IsomorphismLimit { limit } => "isomorphism limit {limit} reached";
Self::WorkLimit { stage, limit } => "work limit {limit} reached at {stage:?}";
Self::UnknownIsomorphism { representative, reason } => "isomorphism against representative {representative} is undetermined: {reason}";
} }
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CensusError {
DimensionVectorLength { expected: usize, got: usize },
MatrixEntryOverflow {
arrow: ArrowId,
rows: usize,
columns: usize,
},
CoordinateCountOverflow,
CoordinateAllocationFailed { coordinates: usize },
SearchSpaceOverflow { coordinates: usize, modulus: u64 },
}
display_error! { error CensusError {
Self::DimensionVectorLength { expected, got } => "dimension vector has {got} entries, quiver has {expected} vertices";
Self::MatrixEntryOverflow { arrow, rows, columns } => "matrix for arrow {} has {rows}x{columns} entries, which overflow usize", arrow.0;
Self::CoordinateCountOverflow => "the total matrix-coordinate count overflows usize";
Self::CoordinateAllocationFailed { coordinates } => "the census coordinate layout could not reserve {coordinates} entries";
Self::SearchSpaceOverflow { coordinates, modulus } => "the raw search space {modulus}^{coordinates} overflows u128";
} }
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CensusFailure {
Isomorphism {
candidate_cursor: u128,
representative: usize,
error: HomError,
},
CandidateConstruction {
candidate_cursor: u128,
error: ModuleError,
},
WorkOverflow { candidate_cursor: u128 },
}
display_error! { CensusFailure {
Self::Isomorphism { candidate_cursor, representative, error } => "isomorphism check at candidate {candidate_cursor} against representative {representative} failed: {error}";
Self::CandidateConstruction { candidate_cursor, error } => "candidate {candidate_cursor} could not be constructed: {error}";
Self::WorkOverflow { candidate_cursor } => "census work counter overflowed at candidate {candidate_cursor}";
} }
error_source! { CensusFailure {
Self::Isomorphism { error, .. } => Some(error),
Self::CandidateConstruction { error, .. } => Some(error),
Self::WorkOverflow { .. } => None,
} }