wip refactor

This commit is contained in:
2026-04-27 22:05:46 +02:00
parent 3a83340c8f
commit 723033f3aa
7 changed files with 234 additions and 134 deletions

View File

@@ -1,52 +1,94 @@
use super::dag::{Circuit, Node, NodeId};
use crate::poly::{
flat::Poly,
ideal::{Generators, GroebnerBasis, Ideal},
var::Var,
};
use std::ops::{Deref, DerefMut};
use std::fmt::{self, Display};
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};
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum QNode<V: Var> {
Leaf(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::Leaf(_) => [None, None],
Self::Sum(l, r) | Self::Prod(l, r) | Self::DivStep(l,r) => [Some(*l), Some(*r)],
}
.into_iter()
.flatten()
}
}
#[derive(Clone, Debug)]
pub struct Quotient<V: Var> {
pub struct QuotientCircuit<V: Var> {
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, GroebnerBasis>> for QuotientCircuit<V> {
fn from(basis: Ideal<V, GroebnerBasis>) -> Self {
Quotient {
Self {
basis,
circuit: Default::default(),
dag: Default::default(),
}
}
}
impl<V: Var> FromIterator<Poly<V>> for Quotient<V> {
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();
Quotient {
Self {
basis: ideal.groebner_basis(),
circuit: Default::default(),
dag: 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> Deref for QuotientCircuit<V>{
type Target = Dag<QNode<V>>;
fn deref(&self)->&Self::Target{
&self.dag
}
}
impl<V: Var> Quotient<V> {
pub fn node(&mut self, n: Node<V>) -> NodeId {
self.circuit.node(n)
}
pub fn add(&mut self, left: NodeId, right: NodeId) -> NodeId {
self.circuit.add(left, right)
}
pub fn mul(&mut self, left: NodeId, right: NodeId) -> NodeId {
self.circuit.mul(left, right)
impl<V:Var> DerefMut for QuotientCircuit<V>{
fn deref_mut(&mut self)->&mut Self::Target{
&mut self.dag
}
}
impl<V:Var> Circuit for QuotientCircuit<V>{
type Node=QNode<V>;
type Var=V;
fn leaf(&mut self, variable:V)->NodeId{
self.node(Self::Node::Leaf(variable))
}
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 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))
}
}