diff --git a/examples/quotient.rs b/examples/quotient.rs new file mode 100644 index 0000000..c0e92af --- /dev/null +++ b/examples/quotient.rs @@ -0,0 +1,21 @@ +use std::cell::RefCell; +use std::rc::Rc; + +use circuit_cas::circuit::quotient::Quotient; +use circuit_cas::poly::var::StaticVar; +use circuit_cas::var; + +fn main() { + let x=var!("x"); + let nx=var!("x\u{0304}"); + + let idem = vec![ + 1*(&x^2)-1*(&x^1), + 1*(&nx^2)-1*(&nx^1), + 1*((&x^1)*(&nx^1))-1*(&x^1), + ]; + + let mut quotient:Quotient=idem.into_iter().collect(); + + println!("{quotient}"); +} diff --git a/src/circuit/dag.rs b/src/circuit/dag.rs index 107e1f0..577952d 100644 --- a/src/circuit/dag.rs +++ b/src/circuit/dag.rs @@ -8,7 +8,7 @@ use crate::poly::var::Var; new_key_type! { pub struct NodeId; } -#[derive(Clone, PartialEq, Eq, Hash)] +#[derive(Clone, Debug, PartialEq, Eq, Hash)] pub enum Node { Leaf(V), Sum(NodeId, NodeId), @@ -26,11 +26,21 @@ impl Node { } } +#[derive(Clone, Debug)] pub struct Circuit { nodes: SlotMap>, intern: HashMap, NodeId>, } +impl Default for Circuit{ + fn default()->Self{ + Circuit{ + nodes:Default::default(), + intern:Default::default() + } + } +} + impl Circuit { pub fn new() -> Self { Circuit { diff --git a/src/circuit/quotient.rs b/src/circuit/quotient.rs index 8b13789..67c8f0d 100644 --- a/src/circuit/quotient.rs +++ b/src/circuit/quotient.rs @@ -1 +1,28 @@ +use crate::poly::{flat::Poly,var::Var}; +use super::dag::Circuit; + +use itertools::Itertools; + +use std::fmt::{self,Display}; + +#[derive(Clone, Debug)] +pub struct Quotient{ + basis: Vec>, + circuit: Circuit, +} + +impl FromIterator> for Quotient { + fn from_iter>>(iter: T) -> Self { + Quotient { + basis:iter.into_iter().collect(), + circuit:Default::default() + } + } +} + +impl Display for Quotient{ + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(),fmt::Error>{ + write!(fmt, "C/<{}>", self.basis.iter().map(|p| format!("{p}")).join(",")) + } +} diff --git a/src/poly/fmt.rs b/src/poly/fmt.rs index 40e7cf5..ee87310 100644 --- a/src/poly/fmt.rs +++ b/src/poly/fmt.rs @@ -33,7 +33,11 @@ impl Display for Poly { "{}", self.mono .iter() - .map(|(m, c)| format!("{}{}", c, m)) + .map(|(m, c)| match c{ + 1=>format!("{}", m), + -1=>format!("-{}", m), + _=>format!("{}{}", c, m), + }) .join(" + ") ), }