Compare commits
5 Commits
3a83340c8f
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 5caccbaf50 | |||
| 0c4bddf3b0 | |||
| 3fbd7e3773 | |||
| 627a2d88f4 | |||
| 723033f3aa |
@@ -1,26 +1,25 @@
|
|||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use circuit_cas::circuit::probabilistic::ProbCircuit;
|
||||||
use circuit_cas::circuit::dag::{Circuit, CircuitExt};
|
use circuit_cas::circuit::dag::CircuitExt;
|
||||||
use circuit_cas::var;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let circuit = Rc::new(RefCell::new(Circuit::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.leaf(var!("x"));
|
let x = circuit.var("x");
|
||||||
let y = circuit.leaf(var!("y"));
|
let y = circuit.var(("y", 1)); // indexed variable y_1
|
||||||
let z = circuit.leaf(var!("z"));
|
let z = circuit.var(("z", 0, 1)); // doubly-indexed variable z_{0,1}
|
||||||
|
|
||||||
let x_plus_y = circuit.leaf(var!("x")) + y;
|
let x_plus_y = circuit.var("x") + y;
|
||||||
let x_plus_z = circuit.leaf(var!("x")) + z;
|
let x_plus_z = circuit.var("x") + z;
|
||||||
let expr = x_plus_y * x_plus_z;
|
let expr = x_plus_y * x_plus_z;
|
||||||
|
|
||||||
// Deduplication: both x leaves share the same NodeId
|
// Deduplication: both x leaves share the same NodeId
|
||||||
let x2 = circuit.leaf(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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +1,31 @@
|
|||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use circuit_cas::circuit::dag::CircuitExt;
|
||||||
use circuit_cas::circuit::quotient::Quotient;
|
use circuit_cas::circuit::quotient::QuotientCircuit;
|
||||||
use circuit_cas::poly::var::StaticVar;
|
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 mut quotient: Quotient<StaticVar> = idem.into_iter().collect();
|
let quotient: Rc<RefCell<QuotientCircuit>> = idem.into();
|
||||||
|
|
||||||
println!("{quotient}");
|
// 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 nxn = quotient.var("x\u{0304}");
|
||||||
|
let prod = xn * nxn;
|
||||||
|
let xn2 = quotient.var("x");
|
||||||
|
let expr = prod + xn2;
|
||||||
|
|
||||||
|
println!("dag size: {}", quotient.borrow().len());
|
||||||
|
println!("expr node id: {:?}", expr.id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,57 +1,29 @@
|
|||||||
use slotmap::{SlotMap, new_key_type};
|
use slotmap::{SlotMap, new_key_type};
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::ops::{Add, Mul};
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use crate::poly::var::Var;
|
use super::traits::{Circuit, Node};
|
||||||
|
|
||||||
new_key_type! { pub struct NodeId; }
|
new_key_type! { pub struct NodeId; }
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
|
||||||
pub enum Node<V: Var> {
|
|
||||||
Leaf(V),
|
|
||||||
Scale(NodeId, i32),
|
|
||||||
Sum(NodeId, NodeId),
|
|
||||||
Prod(NodeId, NodeId),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<V: Var> Node<V> {
|
|
||||||
pub fn children(&self) -> impl Iterator<Item = NodeId> {
|
|
||||||
match self {
|
|
||||||
Node::Leaf(_) => [None, None],
|
|
||||||
Node::Scale(n, _) => [Some(*n), None],
|
|
||||||
Node::Sum(l, r) | Node::Prod(l, r) => [Some(*l), Some(*r)],
|
|
||||||
}
|
|
||||||
.into_iter()
|
|
||||||
.flatten()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Circuit<V: Var> {
|
pub struct Dag<N: Node> {
|
||||||
nodes: SlotMap<NodeId, Node<V>>,
|
nodes: SlotMap<NodeId, N>,
|
||||||
intern: HashMap<Node<V>, NodeId>,
|
intern: HashMap<N, NodeId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V: Var> Default for Circuit<V> {
|
impl<N: Node> Default for Dag<N> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Circuit {
|
Dag {
|
||||||
nodes: Default::default(),
|
nodes: Default::default(),
|
||||||
intern: Default::default(),
|
intern: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V: Var> Circuit<V> {
|
impl<N: Node> Dag<N> {
|
||||||
pub fn new() -> Self {
|
pub(super) fn node(&mut self, n: N) -> NodeId {
|
||||||
Circuit {
|
|
||||||
nodes: SlotMap::with_key(),
|
|
||||||
intern: HashMap::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn node(&mut self, n: Node<V>) -> NodeId {
|
|
||||||
if let Some(&id) = self.intern.get(&n) {
|
if let Some(&id) = self.intern.get(&n) {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
@@ -60,90 +32,33 @@ impl<V: Var> Circuit<V> {
|
|||||||
id
|
id
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&self, id: NodeId) -> Option<&Node<V>> {
|
pub(super) fn get(&self, id: NodeId) -> Option<&N> {
|
||||||
self.nodes.get(id)
|
self.nodes.get(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add(&mut self, left: NodeId, right: NodeId) -> NodeId {
|
pub(super) fn len(&self) -> usize {
|
||||||
let (l, r) = if left <= right {
|
|
||||||
(left, right)
|
|
||||||
} else {
|
|
||||||
(right, left)
|
|
||||||
};
|
|
||||||
self.node(Node::Sum(l, r))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn mul(&mut self, left: NodeId, right: NodeId) -> NodeId {
|
|
||||||
let (l, r) = if left <= right {
|
|
||||||
(left, right)
|
|
||||||
} else {
|
|
||||||
(right, left)
|
|
||||||
};
|
|
||||||
self.node(Node::Prod(l, r))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn len(&self) -> usize {
|
|
||||||
self.nodes.len()
|
self.nodes.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn children(&self, id: NodeId) -> impl Iterator<Item = NodeId> + '_ {
|
pub(super) fn children(&self, id: NodeId) -> impl Iterator<Item = NodeId> + '_ {
|
||||||
self.nodes.get(id).into_iter().flat_map(Node::children)
|
self.nodes.get(id).into_iter().flat_map(Node::children)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remove(&mut self, id: NodeId) {
|
pub(super) fn remove(&mut self, id: NodeId) {
|
||||||
if let Some(node) = self.nodes.remove(id) {
|
if let Some(node) = self.nodes.remove(id) {
|
||||||
self.intern.remove(&node);
|
self.intern.remove(&node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CircuitNode<V: Var> {
|
pub struct RefNode<C: Circuit> {
|
||||||
pub id: NodeId,
|
pub id: NodeId,
|
||||||
circuit: Rc<RefCell<Circuit<V>>>,
|
pub(super) circuit: Rc<RefCell<C>>,
|
||||||
}
|
|
||||||
pub trait CircuitExt<V: Var> {
|
|
||||||
fn leaf(&self, v: V) -> CircuitNode<V>;
|
|
||||||
fn get_node(&self, id: NodeId) -> Option<CircuitNode<V>>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V: Var> CircuitExt<V> for Rc<RefCell<Circuit<V>>> {
|
|
||||||
fn leaf(&self, v: V) -> CircuitNode<V> {
|
|
||||||
let id = self.borrow_mut().node(Node::Leaf(v));
|
|
||||||
CircuitNode {
|
|
||||||
id,
|
|
||||||
circuit: self.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_node(&self, id: NodeId) -> Option<CircuitNode<V>> {
|
pub trait CircuitExt {
|
||||||
self.borrow().get(id)?;
|
type C: Circuit;
|
||||||
Some(CircuitNode {
|
type Var;
|
||||||
id,
|
fn var<T: Into<Self::Var>>(&self, v: T) -> RefNode<Self::C>;
|
||||||
circuit: self.clone(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<V: Var> Add for CircuitNode<V> {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn add(self, rhs: Self) -> Self {
|
|
||||||
let id = self.circuit.borrow_mut().add(self.id, rhs.id);
|
|
||||||
CircuitNode {
|
|
||||||
id,
|
|
||||||
circuit: self.circuit,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<V: Var> Mul for CircuitNode<V> {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn mul(self, rhs: Self) -> Self {
|
|
||||||
let id = self.circuit.borrow_mut().mul(self.id, rhs.id);
|
|
||||||
CircuitNode {
|
|
||||||
id,
|
|
||||||
circuit: self.circuit,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
|
pub mod traits;
|
||||||
pub mod dag;
|
pub mod dag;
|
||||||
|
pub mod probabilistic;
|
||||||
pub mod quotient;
|
pub mod quotient;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
68
src/circuit/probabilistic.rs
Normal file
68
src/circuit/probabilistic.rs
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
use std::cell::RefCell;
|
||||||
|
use std::ops::{Deref, DerefMut};
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use crate::poly::var::{StaticVar, Var};
|
||||||
|
use super::dag::{Dag, NodeId};
|
||||||
|
use super::traits::{Node, SumProdCircuit};
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||||
|
pub enum PNode<V: Var> {
|
||||||
|
Var(V),
|
||||||
|
Sum(NodeId, NodeId),
|
||||||
|
Prod(NodeId, NodeId),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<V: Var> Node for PNode<V> {
|
||||||
|
fn children(&self) -> impl Iterator<Item = NodeId> {
|
||||||
|
match self {
|
||||||
|
Self::Var(_) => [None, None],
|
||||||
|
Self::Sum(l, r) | Self::Prod(l, r) => [Some(*l), Some(*r)],
|
||||||
|
}
|
||||||
|
.into_iter()
|
||||||
|
.flatten()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct ProbCircuit<V: Var = StaticVar> {
|
||||||
|
dag: Dag<PNode<V>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<V: Var> Default for ProbCircuit<V> {
|
||||||
|
fn default() -> Self { ProbCircuit { dag: Dag::default() } }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<V: Var> ProbCircuit<V> {
|
||||||
|
pub fn new() -> Rc<RefCell<Self>> {
|
||||||
|
Rc::new(RefCell::new(Self::default()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<V: Var> SumProdCircuit for ProbCircuit<V> {
|
||||||
|
type 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 {
|
||||||
|
let (l, r) = if l <= r { (l, r) } else { (r, l) };
|
||||||
|
self.node(PNode::Sum(l, r))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mul(&mut self, l: NodeId, r: NodeId) -> NodeId {
|
||||||
|
let (l, r) = if l <= r { (l, r) } else { (r, l) };
|
||||||
|
self.node(PNode::Prod(l, r))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<V: Var> Deref for ProbCircuit<V> {
|
||||||
|
type Target = Dag<PNode<V>>;
|
||||||
|
fn deref(&self) -> &Self::Target { &self.dag }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<V: Var> DerefMut for ProbCircuit<V> {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.dag }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1,52 +1,73 @@
|
|||||||
use super::dag::{Circuit, Node, NodeId};
|
use std::cell::RefCell;
|
||||||
use crate::poly::{
|
use std::ops::{Deref, DerefMut};
|
||||||
flat::Poly,
|
use std::rc::Rc;
|
||||||
ideal::{Generators, GroebnerBasis, Ideal},
|
|
||||||
var::Var,
|
|
||||||
};
|
|
||||||
|
|
||||||
use std::fmt::{self, Display};
|
use crate::poly::var::{StaticVar, Var};
|
||||||
|
use crate::poly::ideal::{Generators, GroebnerBasis, Ideal};
|
||||||
|
use super::dag::{Dag, NodeId};
|
||||||
|
use super::traits::{Node, SumProdCircuit};
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||||
|
pub enum QNode<V: Var> {
|
||||||
|
Var(V),
|
||||||
|
Sum(NodeId, NodeId),
|
||||||
|
Prod(NodeId, NodeId),
|
||||||
|
DivStep(NodeId, NodeId),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<V: Var> Node for QNode<V> {
|
||||||
|
fn children(&self) -> impl Iterator<Item = NodeId> {
|
||||||
|
match self {
|
||||||
|
Self::Var(_) => [None, None],
|
||||||
|
Self::Sum(l, r) | Self::Prod(l, r) | Self::DivStep(l, r) => [Some(*l), Some(*r)],
|
||||||
|
}
|
||||||
|
.into_iter()
|
||||||
|
.flatten()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Quotient<V: Var> {
|
pub struct QuotientCircuit<V: Var = StaticVar> {
|
||||||
basis: Ideal<V, GroebnerBasis>,
|
basis: Ideal<V, GroebnerBasis>,
|
||||||
circuit: Circuit<V>,
|
dag: Dag<QNode<V>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V: Var> From<Ideal<V, GroebnerBasis>> for Quotient<V> {
|
impl<V: Var> From<Ideal<V, Generators>> for Rc<RefCell<QuotientCircuit<V>>> {
|
||||||
|
fn from(ideal: Ideal<V, Generators>) -> Self {
|
||||||
|
ideal.groebner_basis().into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
Quotient {
|
Rc::new(RefCell::new(QuotientCircuit { basis, dag: Default::default() }))
|
||||||
basis,
|
|
||||||
circuit: Default::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<V: Var> FromIterator<Poly<V>> for Quotient<V> {
|
|
||||||
fn from_iter<T: IntoIterator<Item = Poly<V>>>(iter: T) -> Self {
|
|
||||||
let ideal: Ideal<V, Generators> = iter.into_iter().collect();
|
|
||||||
Quotient {
|
|
||||||
basis: ideal.groebner_basis(),
|
|
||||||
circuit: Default::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<V: Var> Display for Quotient<V> {
|
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
|
||||||
write!(fmt, "C/{}", self.basis)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V: Var> Quotient<V> {
|
impl<V: Var> Deref for QuotientCircuit<V> {
|
||||||
pub fn node(&mut self, n: Node<V>) -> NodeId {
|
type Target = Dag<QNode<V>>;
|
||||||
self.circuit.node(n)
|
fn deref(&self) -> &Self::Target { &self.dag }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add(&mut self, left: NodeId, right: NodeId) -> NodeId {
|
impl<V: Var> DerefMut for QuotientCircuit<V> {
|
||||||
self.circuit.add(left, right)
|
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.dag }
|
||||||
}
|
}
|
||||||
pub fn mul(&mut self, left: NodeId, right: NodeId) -> NodeId {
|
|
||||||
self.circuit.mul(left, right)
|
impl<V: Var> SumProdCircuit for QuotientCircuit<V> {
|
||||||
|
type 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 {
|
||||||
|
let (l, r) = if l <= r { (l, r) } else { (r, l) };
|
||||||
|
self.node(QNode::Sum(l, r))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mul(&mut self, l: NodeId, r: NodeId) -> NodeId {
|
||||||
|
let (l, r) = if l <= r { (l, r) } else { (r, l) };
|
||||||
|
self.node(QNode::Prod(l, r))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,50 +1,48 @@
|
|||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use super::probabilistic::ProbCircuit;
|
||||||
use super::dag::{Circuit, CircuitExt};
|
use super::dag::CircuitExt;
|
||||||
use crate::poly::var::StaticVar;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_deduplication() {
|
fn test_deduplication() {
|
||||||
let circuit = Rc::new(RefCell::new(Circuit::new()));
|
let circuit: Rc<RefCell<ProbCircuit>> = ProbCircuit::new();
|
||||||
|
|
||||||
// Same leaf constructed twice returns the same NodeId
|
// Same leaf constructed twice returns the same NodeId
|
||||||
let x1 = circuit.leaf(StaticVar::from("x"));
|
let x1 = circuit.var("x");
|
||||||
let x2 = circuit.leaf(StaticVar::from("x"));
|
let x2 = circuit.var("x");
|
||||||
assert_eq!(x1.id, x2.id);
|
assert_eq!(x1.id, x2.id);
|
||||||
assert_eq!(circuit.borrow().len(), 1);
|
assert_eq!(circuit.borrow().len(), 1);
|
||||||
|
|
||||||
// Same sum constructed twice returns the same NodeId
|
// Same sum constructed twice returns the same NodeId
|
||||||
let _y = circuit.leaf(StaticVar::from("y"));
|
let _y = circuit.var("y");
|
||||||
let sum1 = circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y"));
|
let sum1 = circuit.var("x") + circuit.var("y");
|
||||||
let sum2 = circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y"));
|
let sum2 = circuit.var("x") + circuit.var("y");
|
||||||
assert_eq!(sum1.id, sum2.id);
|
assert_eq!(sum1.id, sum2.id);
|
||||||
assert_eq!(circuit.borrow().len(), 3); // x, y, x+y
|
assert_eq!(circuit.borrow().len(), 3); // x, y, x+y
|
||||||
|
|
||||||
// Shared subexpression: (x + y) * (x + y) reuses the x+y node
|
// Shared subexpression: (x + y) * (x + y) reuses the x+y node
|
||||||
let xy = circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y"));
|
let xy = circuit.var("x") + circuit.var("y");
|
||||||
let xy2 = circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y"));
|
let xy2 = circuit.var("x") + circuit.var("y");
|
||||||
let _sq = xy * xy2;
|
let _sq = xy * xy2;
|
||||||
assert_eq!(circuit.borrow().len(), 4); // x, y, x+y, (x+y)*(x+y)
|
assert_eq!(circuit.borrow().len(), 4); // x, y, x+y, (x+y)*(x+y)
|
||||||
|
|
||||||
// Commutativity: x+y and y+x are the same node
|
// Commutativity: x+y and y+x are the same node
|
||||||
let xy = circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y"));
|
let xy = circuit.var("x") + circuit.var("y");
|
||||||
let yx = circuit.leaf(StaticVar::from("y")) + circuit.leaf(StaticVar::from("x"));
|
let yx = circuit.var("y") + circuit.var("x");
|
||||||
assert_eq!(xy.id, yx.id);
|
assert_eq!(xy.id, yx.id);
|
||||||
|
|
||||||
// Associativity: (x+y)+z and x+(y+z) are distinct nodes
|
// Associativity: (x+y)+z and x+(y+z) are distinct nodes
|
||||||
let _z = circuit.leaf(StaticVar::from("z"));
|
let _z = circuit.var("z");
|
||||||
let xy_z = (circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y")))
|
let xy_z = (circuit.var("x") + circuit.var("y"))
|
||||||
+ circuit.leaf(StaticVar::from("z"));
|
+ circuit.var("z");
|
||||||
let x_yz = circuit.leaf(StaticVar::from("x"))
|
let x_yz = circuit.var("x")
|
||||||
+ (circuit.leaf(StaticVar::from("y")) + circuit.leaf(StaticVar::from("z")));
|
+ (circuit.var("y") + circuit.var("z"));
|
||||||
assert_ne!(xy_z.id, x_yz.id);
|
assert_ne!(xy_z.id, x_yz.id);
|
||||||
|
|
||||||
// Deep shared structure: (x+y)*z appears twice in ((x+y)*z) + ((x+y)*z)
|
// Deep shared structure: (x+y)*z appears twice in ((x+y)*z) + ((x+y)*z)
|
||||||
let xyz1 = (circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y")))
|
let xyz1 = (circuit.var("x") + circuit.var("y"))
|
||||||
* circuit.leaf(StaticVar::from("z"));
|
* circuit.var("z");
|
||||||
let xyz2 = (circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y")))
|
let xyz2 = (circuit.var("x") + circuit.var("y"))
|
||||||
* circuit.leaf(StaticVar::from("z"));
|
* circuit.var("z");
|
||||||
assert_eq!(xyz1.id, xyz2.id);
|
assert_eq!(xyz1.id, xyz2.id);
|
||||||
let _sum = xyz1 + xyz2;
|
let _sum = xyz1 + xyz2;
|
||||||
// x, y, z, x+y(==y+x), (x+y)*z, (x+y)+z, y+z, x+(y+z), (x+y)*z+(x+y)*z, sq
|
// x, y, z, x+y(==y+x), (x+y)*z, (x+y)+z, y+z, x+(y+z), (x+y)*z+(x+y)*z, sq
|
||||||
|
|||||||
63
src/circuit/traits.rs
Normal file
63
src/circuit/traits.rs
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
use std::cell::RefCell;
|
||||||
|
use std::hash::Hash;
|
||||||
|
use std::ops::{Add, DerefMut, Mul};
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use super::dag::{CircuitExt, Dag, NodeId, RefNode};
|
||||||
|
|
||||||
|
pub trait Node: Clone + PartialEq + Eq + Hash {
|
||||||
|
fn children(&self) -> impl Iterator<Item = NodeId>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Circuit: Clone + DerefMut<Target = Dag<Self::Node>> {
|
||||||
|
type Node: Node;
|
||||||
|
|
||||||
|
fn node(&mut self, n: Self::Node) -> NodeId { (**self).node(n) }
|
||||||
|
fn remove(&mut self, id: NodeId) { (**self).remove(id) }
|
||||||
|
fn get(&self, id: NodeId) -> Option<&Self::Node> { (**self).get(id) }
|
||||||
|
fn len(&self) -> usize { (**self).len() }
|
||||||
|
fn children(&self, id: NodeId) -> impl Iterator<Item = NodeId> + '_ { (**self).children(id) }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, N> Circuit for T
|
||||||
|
where
|
||||||
|
T: Clone + DerefMut<Target = Dag<N>>,
|
||||||
|
N: Node,
|
||||||
|
{
|
||||||
|
type Node = N;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait SumProdCircuit: Circuit {
|
||||||
|
type Var;
|
||||||
|
fn var<T: Into<Self::Var>>(&mut self, v: T) -> NodeId;
|
||||||
|
fn add(&mut self, l: NodeId, r: NodeId) -> NodeId;
|
||||||
|
fn mul(&mut self, l: NodeId, r: NodeId) -> NodeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<C: SumProdCircuit> Add for RefNode<C> {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn add(self, rhs: Self) -> Self {
|
||||||
|
let id = self.circuit.borrow_mut().add(self.id, rhs.id);
|
||||||
|
RefNode { id, circuit: self.circuit }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<C: SumProdCircuit> Mul for RefNode<C> {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn mul(self, rhs: Self) -> Self {
|
||||||
|
let id = self.circuit.borrow_mut().mul(self.id, rhs.id);
|
||||||
|
RefNode { id, circuit: self.circuit }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<C: SumProdCircuit> CircuitExt for Rc<RefCell<C>> {
|
||||||
|
type C = C;
|
||||||
|
type Var = C::Var;
|
||||||
|
|
||||||
|
fn var<T: Into<C::Var>>(&self, v: T) -> RefNode<C> {
|
||||||
|
let id = self.borrow_mut().var(v);
|
||||||
|
RefNode { id, circuit: self.clone() }
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user