Compare commits
2 Commits
3fbd7e3773
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 5caccbaf50 | |||
| 0c4bddf3b0 |
@@ -6,10 +6,10 @@ use circuit_cas::circuit::dag::CircuitExt;
|
|||||||
fn main() {
|
fn main() {
|
||||||
let circuit: Rc<RefCell<ProbCircuit>> = ProbCircuit::new();
|
let circuit: Rc<RefCell<ProbCircuit>> = ProbCircuit::new();
|
||||||
|
|
||||||
// Build (x + y) * (x + z)
|
// vars accept anything that implements Into<StaticVar>: &'static str, (&str, u32), (&str, u32, u32)
|
||||||
let x = circuit.var("x");
|
let x = circuit.var("x");
|
||||||
let y = circuit.var("y");
|
let y = circuit.var(("y", 1)); // indexed variable y_1
|
||||||
let z = circuit.var("z");
|
let z = circuit.var(("z", 0, 1)); // doubly-indexed variable z_{0,1}
|
||||||
|
|
||||||
let x_plus_y = circuit.var("x") + y;
|
let x_plus_y = circuit.var("x") + y;
|
||||||
let x_plus_z = circuit.var("x") + z;
|
let x_plus_z = circuit.var("x") + z;
|
||||||
@@ -19,7 +19,7 @@ fn main() {
|
|||||||
let x2 = circuit.var("x");
|
let x2 = circuit.var("x");
|
||||||
assert_eq!(x.id, x2.id);
|
assert_eq!(x.id, x2.id);
|
||||||
|
|
||||||
println!("(x + y) * (x + z) root node id: {:?}", expr.id);
|
println!("(x + y_1) * (x + z_{{0,1}}) root node id: {:?}", expr.id);
|
||||||
println!("x node id: {:?}", x.id);
|
println!("x node id: {:?}", x.id);
|
||||||
println!("x deduplicated node id: {:?}", x2.id);
|
println!("x deduplicated node id: {:?}", x2.id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,21 +3,23 @@ use std::rc::Rc;
|
|||||||
use circuit_cas::circuit::dag::CircuitExt;
|
use circuit_cas::circuit::dag::CircuitExt;
|
||||||
use circuit_cas::circuit::quotient::QuotientCircuit;
|
use circuit_cas::circuit::quotient::QuotientCircuit;
|
||||||
use circuit_cas::circuit::traits::Circuit;
|
use circuit_cas::circuit::traits::Circuit;
|
||||||
|
use circuit_cas::poly::ideal::{Generators, Ideal};
|
||||||
use circuit_cas::var;
|
use circuit_cas::var;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = var!("x");
|
let x = var!("x");
|
||||||
let nx = var!("x\u{0304}");
|
let nx = var!("x\u{0304}");
|
||||||
|
|
||||||
let idem = vec![
|
let idem: Ideal<_, Generators> = vec![
|
||||||
1 * (&x ^ 2) - 1 * (&x ^ 1),
|
1 * (&x ^ 2) - 1 * (&x ^ 1),
|
||||||
1 * (&nx ^ 2) - 1 * (&nx ^ 1),
|
1 * (&nx ^ 2) - 1 * (&nx ^ 1),
|
||||||
1 * ((&x ^ 1) * (&nx ^ 1)) - 1 * (&x ^ 1),
|
1 * ((&x ^ 1) * (&nx ^ 1)) - 1 * (&x ^ 1),
|
||||||
];
|
].into();
|
||||||
|
|
||||||
let quotient: Rc<RefCell<QuotientCircuit>> = idem.into_iter().collect();
|
let quotient: Rc<RefCell<QuotientCircuit>> = idem.into();
|
||||||
|
|
||||||
// Build x * x̄ + x in the DAG
|
// Build x * x̄ + x in the DAG
|
||||||
|
// var accepts anything that implements Into<StaticVar>: &'static str, (&str, u32), (&str, u32, u32)
|
||||||
let xn = quotient.var("x");
|
let xn = quotient.var("x");
|
||||||
let nxn = quotient.var("x\u{0304}");
|
let nxn = quotient.var("x\u{0304}");
|
||||||
let prod = xn * nxn;
|
let prod = xn * nxn;
|
||||||
|
|||||||
@@ -60,5 +60,5 @@ pub struct RefNode<C: Circuit> {
|
|||||||
pub trait CircuitExt {
|
pub trait CircuitExt {
|
||||||
type C: Circuit;
|
type C: Circuit;
|
||||||
type Var;
|
type Var;
|
||||||
fn var(&self, v: impl Into<Self::Var>) -> RefNode<Self::C>;
|
fn var<T: Into<Self::Var>>(&self, v: T) -> RefNode<Self::C>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ impl<V: Var> ProbCircuit<V> {
|
|||||||
impl<V: Var> SumProdCircuit for ProbCircuit<V> {
|
impl<V: Var> SumProdCircuit for ProbCircuit<V> {
|
||||||
type Var = V;
|
type Var = V;
|
||||||
|
|
||||||
fn var(&mut self, v: V) -> NodeId { self.node(PNode::Var(v)) }
|
fn var<T: Into<V>>(&mut self, v: T) -> NodeId { self.node(PNode::Var(v.into())) }
|
||||||
|
|
||||||
fn add(&mut self, l: NodeId, r: NodeId) -> NodeId {
|
fn add(&mut self, l: NodeId, r: NodeId) -> NodeId {
|
||||||
let (l, r) = if l <= r { (l, r) } else { (r, l) };
|
let (l, r) = if l <= r { (l, r) } else { (r, l) };
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ use std::ops::{Deref, DerefMut};
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use crate::poly::var::{StaticVar, Var};
|
use crate::poly::var::{StaticVar, Var};
|
||||||
use crate::poly::flat::Poly;
|
|
||||||
use crate::poly::ideal::{Generators, GroebnerBasis, Ideal};
|
use crate::poly::ideal::{Generators, GroebnerBasis, Ideal};
|
||||||
use super::dag::{Dag, NodeId};
|
use super::dag::{Dag, NodeId};
|
||||||
use super::traits::{Node, SumProdCircuit};
|
use super::traits::{Node, SumProdCircuit};
|
||||||
@@ -34,26 +33,15 @@ pub struct QuotientCircuit<V: Var = StaticVar> {
|
|||||||
dag: Dag<QNode<V>>,
|
dag: Dag<QNode<V>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V: Var> QuotientCircuit<V> {
|
impl<V: Var> From<Ideal<V, Generators>> for Rc<RefCell<QuotientCircuit<V>>> {
|
||||||
pub fn from_ideal(basis: Ideal<V, GroebnerBasis>) -> Rc<RefCell<Self>> {
|
fn from(ideal: Ideal<V, Generators>) -> Self {
|
||||||
Rc::new(RefCell::new(Self { basis, dag: Default::default() }))
|
ideal.groebner_basis().into()
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_polys(iter: impl IntoIterator<Item = Poly<V>>) -> Rc<RefCell<Self>> {
|
|
||||||
let ideal: Ideal<V, Generators> = iter.into_iter().collect();
|
|
||||||
Rc::new(RefCell::new(Self { basis: ideal.groebner_basis(), dag: Default::default() }))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V: Var> From<Ideal<V, GroebnerBasis>> for Rc<RefCell<QuotientCircuit<V>>> {
|
impl<V: Var> From<Ideal<V, GroebnerBasis>> for Rc<RefCell<QuotientCircuit<V>>> {
|
||||||
fn from(basis: Ideal<V, GroebnerBasis>) -> Self {
|
fn from(basis: Ideal<V, GroebnerBasis>) -> Self {
|
||||||
QuotientCircuit::from_ideal(basis)
|
Rc::new(RefCell::new(QuotientCircuit { basis, dag: Default::default() }))
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<V: Var> FromIterator<Poly<V>> for Rc<RefCell<QuotientCircuit<V>>> {
|
|
||||||
fn from_iter<T: IntoIterator<Item = Poly<V>>>(iter: T) -> Self {
|
|
||||||
QuotientCircuit::from_polys(iter)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +57,7 @@ impl<V: Var> DerefMut for QuotientCircuit<V> {
|
|||||||
impl<V: Var> SumProdCircuit for QuotientCircuit<V> {
|
impl<V: Var> SumProdCircuit for QuotientCircuit<V> {
|
||||||
type Var = V;
|
type Var = V;
|
||||||
|
|
||||||
fn var(&mut self, v: V) -> NodeId { self.node(QNode::Var(v)) }
|
fn var<T: Into<V>>(&mut self, v: T) -> NodeId { self.node(QNode::Var(v.into())) }
|
||||||
|
|
||||||
fn add(&mut self, l: NodeId, r: NodeId) -> NodeId {
|
fn add(&mut self, l: NodeId, r: NodeId) -> NodeId {
|
||||||
let (l, r) = if l <= r { (l, r) } else { (r, l) };
|
let (l, r) = if l <= r { (l, r) } else { (r, l) };
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ where
|
|||||||
|
|
||||||
pub trait SumProdCircuit: Circuit {
|
pub trait SumProdCircuit: Circuit {
|
||||||
type Var;
|
type Var;
|
||||||
fn var(&mut self, v: Self::Var) -> NodeId;
|
fn var<T: Into<Self::Var>>(&mut self, v: T) -> NodeId;
|
||||||
fn add(&mut self, l: NodeId, r: NodeId) -> NodeId;
|
fn add(&mut self, l: NodeId, r: NodeId) -> NodeId;
|
||||||
fn mul(&mut self, l: NodeId, r: NodeId) -> NodeId;
|
fn mul(&mut self, l: NodeId, r: NodeId) -> NodeId;
|
||||||
}
|
}
|
||||||
@@ -56,8 +56,8 @@ impl<C: SumProdCircuit> CircuitExt for Rc<RefCell<C>> {
|
|||||||
type C = C;
|
type C = C;
|
||||||
type Var = C::Var;
|
type Var = C::Var;
|
||||||
|
|
||||||
fn var(&self, v: impl Into<C::Var>) -> RefNode<C> {
|
fn var<T: Into<C::Var>>(&self, v: T) -> RefNode<C> {
|
||||||
let id = self.borrow_mut().var(v.into());
|
let id = self.borrow_mut().var(v);
|
||||||
RefNode { id, circuit: self.clone() }
|
RefNode { id, circuit: self.clone() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user