refactor wip
This commit is contained in:
@@ -1,12 +1,9 @@
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use circuit_cas::circuit::traits::Circuit;
|
||||
use circuit_cas::circuit::dag::{ProbCircuit, CircuitExt};
|
||||
use circuit_cas::circuit::probabilistic::ProbCircuit;
|
||||
use circuit_cas::circuit::dag::CircuitExt;
|
||||
use circuit_cas::var;
|
||||
|
||||
fn main() {
|
||||
let mut circuit = ProbCircuit::new();
|
||||
let circuit = ProbCircuit::new();
|
||||
|
||||
// Build (x + y) * (x + z)
|
||||
let x = circuit.leaf(var!("x"));
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use circuit_cas::circuit::quotient::Quotient;
|
||||
use circuit_cas::poly::var::StaticVar;
|
||||
use circuit_cas::var;
|
||||
@@ -15,7 +12,7 @@ fn main() {
|
||||
1 * ((&x ^ 1) * (&nx ^ 1)) - 1 * (&x ^ 1),
|
||||
];
|
||||
|
||||
let mut quotient: Quotient<StaticVar> = idem.into_iter().collect();
|
||||
let quotient: Quotient<StaticVar> = idem.into_iter().collect();
|
||||
|
||||
println!("{quotient}");
|
||||
println!("{quotient:?}");
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use slotmap::{SlotMap, new_key_type};
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
use super::traits::{Circuit, Node};
|
||||
|
||||
@@ -21,7 +23,7 @@ impl<N: Node> Default for Dag<N> {
|
||||
}
|
||||
|
||||
impl<N: Node> Dag<N> {
|
||||
fn node(&mut self, n: N) -> NodeId {
|
||||
pub(super) fn node(&mut self, n: N) -> NodeId {
|
||||
if let Some(&id) = self.intern.get(&n) {
|
||||
return id;
|
||||
}
|
||||
@@ -30,44 +32,40 @@ impl<N:Node> Dag<N>{
|
||||
id
|
||||
}
|
||||
|
||||
fn get(&self, id: NodeId) -> Option<&N> {
|
||||
pub(super) fn get(&self, id: NodeId) -> Option<&N> {
|
||||
self.nodes.get(id)
|
||||
}
|
||||
|
||||
fn len(&self) -> usize {
|
||||
pub(super) fn len(&self) -> usize {
|
||||
self.nodes.len()
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
fn remove(&mut self, id: NodeId) {
|
||||
pub(super) fn remove(&mut self, id: NodeId) {
|
||||
if let Some(node) = self.nodes.remove(id) {
|
||||
self.intern.remove(&node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RefNode<T:DerefMut<Target=Dag>>{
|
||||
pub struct RefNode<C: Circuit> {
|
||||
pub id: NodeId,
|
||||
circuit: Rc<RefCell<T>>
|
||||
pub(super) circuit: Rc<RefCell<C>>,
|
||||
}
|
||||
|
||||
impl<C: Circuit> RefNode<C> {
|
||||
fn leaf(&mut self, variable: C::Var)->Self{
|
||||
let mut c = self.circuit.borrow_mut();
|
||||
let id = c.leaf(variable);
|
||||
RefNode {
|
||||
id,
|
||||
circuit: self.circuit.clone(),
|
||||
}
|
||||
}
|
||||
fn get_node(&self, id:NodeId)->Option<Self>{
|
||||
pub fn get_node(&self, id: NodeId) -> Option<Self> {
|
||||
self.circuit.borrow().get(id)?;
|
||||
Some(RefNode {
|
||||
id,
|
||||
circuit: self.circuit.clone(),
|
||||
})
|
||||
Some(RefNode { id, circuit: self.circuit.clone() })
|
||||
}
|
||||
}
|
||||
|
||||
pub trait CircuitExt {
|
||||
type C: Circuit;
|
||||
type Var;
|
||||
fn leaf(&self, var: Self::Var) -> RefNode<Self::C>;
|
||||
fn len(&self) -> usize;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::cell::RefCell;
|
||||
use std::ops::{Add, Deref, DerefMut, Mul};
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::poly::var::Var;
|
||||
use super::dag::{Dag,NodeId};
|
||||
use super::traits::{Circuit,Node,RefNode};
|
||||
use super::dag::{CircuitExt, Dag, NodeId, RefNode};
|
||||
use super::traits::Node;
|
||||
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
@@ -13,7 +14,6 @@ pub enum PNode<V: Var> {
|
||||
Prod(NodeId, NodeId),
|
||||
}
|
||||
|
||||
|
||||
impl<V: Var> Node for PNode<V> {
|
||||
fn children(&self) -> impl Iterator<Item = NodeId> {
|
||||
match self {
|
||||
@@ -25,55 +25,68 @@ impl<V:Var> Node for PNode<V>{
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ProbCircuit<V: Var> {
|
||||
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()))
|
||||
}
|
||||
|
||||
pub fn leaf(&mut self, v: V) -> NodeId { self.node(PNode::Leaf(v)) }
|
||||
|
||||
pub 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))
|
||||
}
|
||||
|
||||
pub 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
|
||||
}
|
||||
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
|
||||
}
|
||||
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.dag }
|
||||
}
|
||||
|
||||
|
||||
impl<V: Var> Rc<ProbCircuit<V>> {
|
||||
pub fn var<T:Into<V>>(variable:T)->RefNode<Self>{
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<V:Var> Circuit for ProbCircuit<V>{
|
||||
type Node=PNode<V>;
|
||||
impl<V: Var> CircuitExt for Rc<RefCell<ProbCircuit<V>>> {
|
||||
type C = ProbCircuit<V>;
|
||||
type Var = V;
|
||||
|
||||
fn leaf(&mut self, variable:V)->NodeId{
|
||||
self.node(Self::Node::Leaf(variable))
|
||||
fn leaf(&self, var: V) -> RefNode<ProbCircuit<V>> {
|
||||
let id = self.borrow_mut().leaf(var);
|
||||
RefNode { id, circuit: self.clone() }
|
||||
}
|
||||
|
||||
fn add(&mut self, left: NodeId, right: NodeId) -> NodeId {
|
||||
let (l, r) = if left <= right {
|
||||
(left, right)
|
||||
} else {
|
||||
(right, left)
|
||||
};
|
||||
self.node(Self::Node::Sum(l, r))
|
||||
fn len(&self) -> usize { self.borrow().len() }
|
||||
}
|
||||
|
||||
fn mul(&mut self, left: NodeId, right: NodeId) -> NodeId {
|
||||
let (l, r) = if left <= right {
|
||||
(left, right)
|
||||
} else {
|
||||
(right, left)
|
||||
};
|
||||
self.node(Self::Node::Prod(l, r))
|
||||
impl<V: Var> Add for RefNode<ProbCircuit<V>> {
|
||||
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<V: Var> Mul for RefNode<ProbCircuit<V>> {
|
||||
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 }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::cell::RefCell;
|
||||
use std::ops::{Add, Deref, DerefMut, Mul};
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::poly::var::Var;
|
||||
use crate::poly::flat::Poly;
|
||||
use crate::poly::ideal::{Generators, GroebnerBasis, Ideal};
|
||||
use super::dag::{Dag,NodeId};
|
||||
use super::traits::{Circuit,Node};
|
||||
use super::dag::{CircuitExt, Dag, NodeId, RefNode};
|
||||
use super::traits::Node;
|
||||
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
@@ -12,10 +14,9 @@ pub enum QNode<V: Var> {
|
||||
Leaf(V),
|
||||
Sum(NodeId, NodeId),
|
||||
Prod(NodeId, NodeId),
|
||||
DivStep(NodeId, NodeId)
|
||||
DivStep(NodeId, NodeId),
|
||||
}
|
||||
|
||||
|
||||
impl<V: Var> Node for QNode<V> {
|
||||
fn children(&self) -> impl Iterator<Item = NodeId> {
|
||||
match self {
|
||||
@@ -35,60 +36,68 @@ pub struct QuotientCircuit<V: Var> {
|
||||
|
||||
impl<V: Var> From<Ideal<V, GroebnerBasis>> for QuotientCircuit<V> {
|
||||
fn from(basis: Ideal<V, GroebnerBasis>) -> Self {
|
||||
Self {
|
||||
basis,
|
||||
dag: Default::default(),
|
||||
}
|
||||
Self { basis, dag: Default::default() }
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Var> FromIterator<Poly<V>> for QuotientCircuit<V> {
|
||||
fn from_iter<T: IntoIterator<Item = Poly<V>>>(iter: T) -> Self {
|
||||
let ideal: Ideal<V, Generators> = iter.into_iter().collect();
|
||||
Self {
|
||||
basis: ideal.groebner_basis(),
|
||||
dag: Default::default(),
|
||||
}
|
||||
Self { basis: ideal.groebner_basis(), dag: Default::default() }
|
||||
}
|
||||
}
|
||||
|
||||
pub type Quotient<V> = QuotientCircuit<V>;
|
||||
|
||||
impl<V: Var> Deref for QuotientCircuit<V> {
|
||||
type Target = Dag<QNode<V>>;
|
||||
fn deref(&self)->&Self::Target{
|
||||
&self.dag
|
||||
}
|
||||
fn deref(&self) -> &Self::Target { &self.dag }
|
||||
}
|
||||
|
||||
impl<V: Var> DerefMut for QuotientCircuit<V> {
|
||||
fn deref_mut(&mut self)->&mut Self::Target{
|
||||
&mut self.dag
|
||||
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.dag }
|
||||
}
|
||||
|
||||
impl<V: Var> QuotientCircuit<V> {
|
||||
pub fn leaf(&mut self, v: V) -> NodeId { self.node(QNode::Leaf(v)) }
|
||||
|
||||
pub 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))
|
||||
}
|
||||
|
||||
pub 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))
|
||||
}
|
||||
}
|
||||
|
||||
impl<V:Var> Circuit for QuotientCircuit<V>{
|
||||
type Node=QNode<V>;
|
||||
impl<V: Var> CircuitExt for Rc<RefCell<QuotientCircuit<V>>> {
|
||||
type C = QuotientCircuit<V>;
|
||||
type Var = V;
|
||||
|
||||
fn leaf(&mut self, variable:V)->NodeId{
|
||||
self.node(Self::Node::Leaf(variable))
|
||||
fn leaf(&self, var: V) -> RefNode<QuotientCircuit<V>> {
|
||||
let id = self.borrow_mut().leaf(var);
|
||||
RefNode { id, circuit: self.clone() }
|
||||
}
|
||||
|
||||
fn add(&mut self, left: NodeId, right: NodeId) -> NodeId {
|
||||
let (l, r) = if left <= right {
|
||||
(left, right)
|
||||
} else {
|
||||
(right, left)
|
||||
};
|
||||
self.node(Self::Node::Sum(l, r))
|
||||
fn len(&self) -> usize { self.borrow().len() }
|
||||
}
|
||||
|
||||
fn mul(&mut self, left: NodeId, right: NodeId) -> NodeId {
|
||||
let (l, r) = if left <= right {
|
||||
(left, right)
|
||||
} else {
|
||||
(right, left)
|
||||
};
|
||||
self.node(Self::Node::Prod(l, r))
|
||||
impl<V: Var> Add for RefNode<QuotientCircuit<V>> {
|
||||
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<V: Var> Mul for RefNode<QuotientCircuit<V>> {
|
||||
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 }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +1,29 @@
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use super::dag::{ProbCircuit, CircuitExt};
|
||||
use super::traits::Circuit;
|
||||
use super::probabilistic::ProbCircuit;
|
||||
use super::dag::CircuitExt;
|
||||
use crate::poly::var::StaticVar;
|
||||
|
||||
#[test]
|
||||
fn test_deduplication() {
|
||||
let mut circuit = ProbCircuit::new();
|
||||
let circuit = ProbCircuit::new();
|
||||
|
||||
// Same leaf constructed twice returns the same NodeId
|
||||
let x1 = circuit.leaf(StaticVar::from("x"));
|
||||
let x2 = circuit.leaf(StaticVar::from("x"));
|
||||
assert_eq!(x1.id, x2.id);
|
||||
assert_eq!(circuit.borrow().len(), 1);
|
||||
assert_eq!(circuit.len(), 1);
|
||||
|
||||
// Same sum constructed twice returns the same NodeId
|
||||
let _y = circuit.leaf(StaticVar::from("y"));
|
||||
let sum1 = circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y"));
|
||||
let sum2 = circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y"));
|
||||
assert_eq!(sum1.id, sum2.id);
|
||||
assert_eq!(circuit.borrow().len(), 3); // x, y, x+y
|
||||
assert_eq!(circuit.len(), 3); // x, y, x+y
|
||||
|
||||
// Shared subexpression: (x + y) * (x + y) reuses the x+y node
|
||||
let xy = circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y"));
|
||||
let xy2 = circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y"));
|
||||
let _sq = xy * xy2;
|
||||
assert_eq!(circuit.borrow().len(), 4); // x, y, x+y, (x+y)*(x+y)
|
||||
assert_eq!(circuit.len(), 4); // x, y, x+y, (x+y)*(x+y)
|
||||
|
||||
// Commutativity: x+y and y+x are the same node
|
||||
let xy = circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y"));
|
||||
|
||||
@@ -1,51 +1,26 @@
|
||||
use std::hash::Hash;
|
||||
use std::ops::{Add,Mul};
|
||||
use std::{rc::Rc,cell::RefCell};
|
||||
use std::ops::DerefMut;
|
||||
|
||||
use super::dag::NodeId;
|
||||
use super::dag::{Dag, NodeId};
|
||||
|
||||
pub trait Node: Clone + PartialEq + Eq + Hash {
|
||||
fn children(&self) -> impl Iterator<Item = NodeId>;
|
||||
}
|
||||
|
||||
pub trait Circuit:Clone{
|
||||
type Var;
|
||||
type Node;
|
||||
pub trait Circuit: Clone + DerefMut<Target = Dag<Self::Node>> {
|
||||
type Node: Node;
|
||||
|
||||
fn node(&mut self, n:Self::Node)->NodeId;
|
||||
fn remove(&mut self, id:NodeId);
|
||||
fn get(&self, id:NodeId)->Option<&Self::Node>;
|
||||
|
||||
fn leaf(&mut self, var: Self::Var)->NodeId;
|
||||
fn add(&mut self, left: NodeId, right: NodeId)->NodeId;
|
||||
fn mul(&mut self, left: NodeId, right: NodeId)->NodeId;
|
||||
|
||||
fn len(&self)->usize;
|
||||
fn children(&self, id:NodeId)->impl Iterator<Item=NodeId>+'_;
|
||||
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<C:Circuit> 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<T, N> Circuit for T
|
||||
where
|
||||
T: Clone + DerefMut<Target = Dag<N>>,
|
||||
N: Node,
|
||||
{
|
||||
type Node = N;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<C:Circuit> 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user