From 04f5444c689b9d52284c124b8fce5eff01bf6cd7 Mon Sep 17 00:00:00 2001 From: asteri Date: Wed, 22 Apr 2026 00:43:03 +0200 Subject: [PATCH] add sub ops for flat polynomial --- src/bin/flat.rs | 33 ++++++++------------------------- src/poly/flat.rs | 23 +++++++++++++++++++---- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/bin/flat.rs b/src/bin/flat.rs index 1569a8b..0edbac9 100644 --- a/src/bin/flat.rs +++ b/src/bin/flat.rs @@ -1,34 +1,17 @@ use circuit_polynomial::poly::flat; -use circuit_polynomial::poly::flat::Mono; -use circuit_polynomial::{var,poly::var::StaticVar}; +use circuit_polynomial::var; fn main() { - let poly: flat::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), - ], - ), - ] - .into(); + 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!("y"); - let mono = 3*((&x^2)*(&y^4)); + let x = var!("x"); + let y = var!("y"); + let other = -3 * ((&x ^ 2) * (&y ^ 4)); println!("{poly}"); - let z=poly+mono; + let z = poly - other; println!("{z}"); } diff --git a/src/poly/flat.rs b/src/poly/flat.rs index e6b36c5..9b7d214 100644 --- a/src/poly/flat.rs +++ b/src/poly/flat.rs @@ -1,7 +1,7 @@ use itertools::Itertools; use std::fmt::{self, Display}; -use std::ops::{Add, BitXor, Mul}; +use std::ops::{Add, Sub, BitXor, Mul}; use super::var::{StaticVar, Var}; use std::collections::HashMap; @@ -75,9 +75,11 @@ impl> FromIterator<(U, u32)> for Mono { term.sort(); // Check duplicate variables - assert!((term[..]) - .windows(2) - .all(|window| window[0].0 != window[1].0)); + assert!( + (term[..]) + .windows(2) + .all(|window| window[0].0 != window[1].0) + ); Mono { term } } @@ -179,3 +181,16 @@ impl Add for Poly { self } } + +impl Sub for Poly { + type Output = Poly; + + fn sub(mut self, other: Poly) -> Self::Output { + for (mono, coeff) in other.mono { + let entry = self.mono.entry(mono).or_insert(0); + *entry -= coeff; + } + self.mono.retain(|_, &mut coeff| coeff != 0); + self + } +}