update claude settings and wip DAG with SlotMap
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
use circuit_cas::poly::flat;
|
||||
use circuit_cas::var;
|
||||
|
||||
fn main() {
|
||||
let poly = (2
|
||||
* ((var!("x", 1, 5) ^ 5) * (var!("x", 1, 2) ^ 5) * (var!("x", 2, 5) ^ 1)))
|
||||
+ (3 * ((var!("x", 1, 9) ^ 5) * (var!("x", 1, 2) ^ 5) * (var!("x", 2, 5) ^ 1)));
|
||||
|
||||
let x = var!("x");
|
||||
let y = var!("x\u{0304}");
|
||||
let z = var!("z");
|
||||
let other = -3 * ((&x ^ 2) * (&y ^ 4));
|
||||
|
||||
let mono = (&x^2)*(&y^4);
|
||||
|
||||
let inside = (&x^2)*(&y^2)*(&z^1);
|
||||
|
||||
if mono.contains(&inside){
|
||||
println!("{inside}\u{2286}{mono}");
|
||||
}else{
|
||||
println!("{inside}\u{2284}{mono}");
|
||||
}
|
||||
|
||||
println!("{poly}");
|
||||
|
||||
let z = poly - other;
|
||||
println!("{z}");
|
||||
}
|
||||
83
src/circuit/dag.rs
Normal file
83
src/circuit/dag.rs
Normal file
@@ -0,0 +1,83 @@
|
||||
use slotmap::{new_key_type, SlotMap};
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::ops::{Add, Mul};
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::poly::var::Var;
|
||||
|
||||
new_key_type! { pub struct NodeId; }
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub enum Node<V: Var> {
|
||||
Leaf(V),
|
||||
Sum(NodeId, NodeId),
|
||||
Prod(NodeId, NodeId),
|
||||
}
|
||||
|
||||
pub struct Circuit<V: Var> {
|
||||
nodes: SlotMap<NodeId, Node<V>>,
|
||||
intern: HashMap<Node<V>, NodeId>,
|
||||
}
|
||||
|
||||
impl<V: Var> Circuit<V> {
|
||||
pub fn new() -> Self {
|
||||
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) {
|
||||
return id;
|
||||
}
|
||||
let id = self.nodes.insert(n.clone());
|
||||
self.intern.insert(n, id);
|
||||
id
|
||||
}
|
||||
|
||||
pub fn get(&self, id: NodeId) -> Option<&Node<V>> {
|
||||
self.nodes.get(id)
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, id: NodeId) {
|
||||
if let Some(node) = self.nodes.remove(id) {
|
||||
self.intern.remove(&node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CircuitNode<V: Var> {
|
||||
pub id: NodeId,
|
||||
circuit: Rc<RefCell<Circuit<V>>>,
|
||||
}
|
||||
|
||||
pub trait CircuitExt<V: Var> {
|
||||
fn leaf(&self, v: V) -> 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() }
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Var> Add for CircuitNode<V> {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, rhs: Self) -> Self {
|
||||
let id = self.circuit.borrow_mut().node(Node::Sum(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().node(Node::Prod(self.id, rhs.id));
|
||||
CircuitNode { id, circuit: self.circuit }
|
||||
}
|
||||
}
|
||||
1
src/circuit/mod.rs
Normal file
1
src/circuit/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod dag;
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod poly;
|
||||
pub mod circuit;
|
||||
pub mod fmt;
|
||||
|
||||
Reference in New Issue
Block a user