From 1ed7ea05a90ff604e599837c0fa17cf118c71c3b Mon Sep 17 00:00:00 2001 From: Ben Ruijl Date: Thu, 7 Nov 2024 11:42:11 +0100 Subject: [PATCH] Further refinements --- examples/fuel_backend.rs | 15 +++------- src/api/cpp.rs | 16 +++++----- src/domains/factorized_rational_polynomial.rs | 17 ++++++----- src/domains/rational.rs | 9 +++--- src/domains/rational_polynomial.rs | 30 ++++++------------- src/poly/polynomial.rs | 2 +- 6 files changed, 36 insertions(+), 53 deletions(-) diff --git a/examples/fuel_backend.rs b/examples/fuel_backend.rs index 8c20605..7f9ddc1 100644 --- a/examples/fuel_backend.rs +++ b/examples/fuel_backend.rs @@ -7,7 +7,7 @@ use smartstring::{LazyCompact, SmartString}; use symbolica::{ domains::{integer::Z, rational::Q, rational_polynomial::RationalPolynomial}, parser::Token, - printer::{PrintOptions, RationalPolynomialPrinter}, + printer::PrintOptions, state::State, }; @@ -56,16 +56,9 @@ fn main() { .to_rational_polynomial(&Q, &Z, &vars, &var_names) .unwrap(); - let out_str = format!( - "{}", - RationalPolynomialPrinter { - poly: &r, - opts: print_opt, - add_parentheses: false - } - ); - - writeln!(&mut stdout, "{}", out_str).unwrap(); + buffer.clear(); + r.format(&print_opt, false, false, &mut buffer).unwrap(); + writeln!(stdout, "{}", buffer).unwrap(); buffer.clear(); } diff --git a/src/api/cpp.rs b/src/api/cpp.rs index 31a67ed..6161882 100644 --- a/src/api/cpp.rs +++ b/src/api/cpp.rs @@ -167,6 +167,7 @@ unsafe extern "C" fn simplify( macro_rules! to_rational { ($in_field: expr, $exp_size: ty) => { + symbolica.local_state.buffer.clear(); if prime == 0 { let r: RationalPolynomial = token .to_rational_polynomial( @@ -177,7 +178,6 @@ unsafe extern "C" fn simplify( ) .unwrap(); - symbolica.local_state.buffer.clear(); r.format(&opts, false, false, &mut symbolica.local_state.buffer) .unwrap(); } else if prime <= u32::MAX as c_ulonglong { @@ -191,7 +191,6 @@ unsafe extern "C" fn simplify( ) .unwrap(); - symbolica.local_state.buffer.clear(); rf.format(&opts, false, false, &mut symbolica.local_state.buffer) .unwrap(); } else if prime == Mersenne64::PRIME { @@ -205,7 +204,6 @@ unsafe extern "C" fn simplify( ) .unwrap(); - symbolica.local_state.buffer.clear(); rf.format(&opts, false, false, &mut symbolica.local_state.buffer) .unwrap(); } else { @@ -219,10 +217,15 @@ unsafe extern "C" fn simplify( ) .unwrap(); - symbolica.local_state.buffer.clear(); rf.format(&opts, false, false, &mut symbolica.local_state.buffer) .unwrap(); } + + write!( + &mut symbolica.local_state.buffer, + "\0", // add the NUL character + ) + .unwrap() }; } @@ -270,6 +273,7 @@ unsafe extern "C" fn simplify_factorized( macro_rules! to_rational { ($in_field: expr, $exp_size: ty) => { + symbolica.local_state.buffer.clear(); if prime == 0 { let r: FactorizedRationalPolynomial = token .to_factorized_rational_polynomial( @@ -280,7 +284,6 @@ unsafe extern "C" fn simplify_factorized( ) .unwrap(); - symbolica.local_state.buffer.clear(); r.format(&opts, false, false, &mut symbolica.local_state.buffer) .unwrap(); } else if prime <= u32::MAX as c_ulonglong { @@ -294,7 +297,6 @@ unsafe extern "C" fn simplify_factorized( ) .unwrap(); - symbolica.local_state.buffer.clear(); rf.format(&opts, false, false, &mut symbolica.local_state.buffer) .unwrap(); } else if prime == Mersenne64::PRIME { @@ -308,7 +310,6 @@ unsafe extern "C" fn simplify_factorized( ) .unwrap(); - symbolica.local_state.buffer.clear(); rf.format(&opts, false, false, &mut symbolica.local_state.buffer) .unwrap(); } else { @@ -322,7 +323,6 @@ unsafe extern "C" fn simplify_factorized( ) .unwrap(); - symbolica.local_state.buffer.clear(); rf.format(&opts, false, false, &mut symbolica.local_state.buffer) .unwrap(); } diff --git a/src/domains/factorized_rational_polynomial.rs b/src/domains/factorized_rational_polynomial.rs index 59fd501..188b15d 100644 --- a/src/domains/factorized_rational_polynomial.rs +++ b/src/domains/factorized_rational_polynomial.rs @@ -150,7 +150,7 @@ impl FactorizedRationalPolynomial { &self, opts: &PrintOptions, in_sum: bool, - in_product: bool, + mut in_product: bool, f: &mut W, ) -> Result<(), Error> { if opts.explicit_rational_polynomial { @@ -176,7 +176,7 @@ impl FactorizedRationalPolynomial { } else { f.write_char('[')?; self.numerator.format(opts, false, false, f)?; - f.write_str("]*")?; + f.write_str("]")?; } } else { f.write_char('[')?; @@ -212,9 +212,10 @@ impl FactorizedRationalPolynomial { if self.denominators.is_empty() && self.numerator.ring.is_one(&self.denom_coeff) { if !self.numerator.ring.is_one(&self.numer_coeff) { + in_product |= !self.numerator.is_one(); self.numerator .ring - .format(&self.numer_coeff, opts, false, false, f)?; + .format(&self.numer_coeff, opts, false, in_product, f)?; } if (self.numerator.ring.is_one(&self.numer_coeff) && !in_product) @@ -238,15 +239,15 @@ impl FactorizedRationalPolynomial { f.write_char('*')?; } - // FIXME: which params? self.numerator.format(opts, in_sum, in_product, f) } } else { if opts.latex { if !self.numerator.ring.is_one(&self.numer_coeff) { + in_product |= !self.numerator.is_one(); self.numerator .ring - .format(&self.numer_coeff, opts, in_sum, false, f)?; + .format(&self.numer_coeff, opts, in_sum, in_product, f)?; } f.write_str("\\frac{")?; @@ -264,8 +265,7 @@ impl FactorizedRationalPolynomial { d.format(opts, false, true, f)?; } else { f.write_char('(')?; - d.format(opts, false, true, f)?; - f.write_str(")^")?; + d.format(opts, false, false, f)?; f.write_fmt(format_args!(")^{}", p))?; } } @@ -314,7 +314,8 @@ impl FactorizedRationalPolynomial { for (d, p) in &self.denominators { f.write_char('(')?; - d.format(opts, false, true, f)?; + d.format(opts, false, false, f)?; + f.write_char(')')?; if *p != 1 { f.write_fmt(format_args!( diff --git a/src/domains/rational.rs b/src/domains/rational.rs index 2c47dbb..d1d293b 100644 --- a/src/domains/rational.rs +++ b/src/domains/rational.rs @@ -328,15 +328,16 @@ impl Ring for FractionField { element: &Self::Element, opts: &PrintOptions, in_sum: bool, - _in_product: bool, + in_product: bool, f: &mut W, ) -> Result<(), Error> { + let has_denom = !self.ring.is_one(&element.denominator); self.ring - .format(&element.numerator, opts, in_sum, true, f)?; - if !self.ring.is_one(&element.denominator) { + .format(&element.numerator, opts, in_sum, in_product || has_denom, f)?; + if has_denom { f.write_char('/')?; self.ring - .format(&element.denominator, opts, false, true, f)?; + .format(&element.denominator, opts, false, true, f)?; // TODO: set in_pow } Ok(()) diff --git a/src/domains/rational_polynomial.rs b/src/domains/rational_polynomial.rs index 45dc910..d85d7a5 100644 --- a/src/domains/rational_polynomial.rs +++ b/src/domains/rational_polynomial.rs @@ -167,16 +167,15 @@ impl RationalPolynomial { pub fn format( &self, opts: &PrintOptions, - mut in_sum: bool, + in_sum: bool, in_product: bool, f: &mut W, ) -> Result<(), Error> { - if in_sum && in_product { - in_sum = false; - f.write_char('+')?; - } - if opts.explicit_rational_polynomial { + if in_sum { + f.write_char('+')?; + } + if self.denominator.is_one() { if self.numerator.is_zero() { f.write_char('0')?; @@ -197,13 +196,7 @@ impl RationalPolynomial { } if self.denominator.is_one() { - if !in_product || self.numerator.nterms() < 2 { - self.numerator.format(opts, in_sum, false, f) - } else { - f.write_char('(')?; - self.numerator.format(opts, false, false, f)?; - f.write_char(')') - } + self.numerator.format(opts, in_sum, in_product, f) } else { if opts.latex { if in_sum { @@ -216,14 +209,9 @@ impl RationalPolynomial { return f.write_str("}"); } - if self.numerator.nterms() < 2 { - self.numerator.format(opts, in_sum, true, f)?; - } else { - f.write_char('(')?; - self.numerator.format(opts, false, true, f)?; - f.write_char(')')?; - } + self.numerator.format(opts, in_sum, true, f)?; + // TODO: introduce in_pow flag if self.denominator.nterms() == 1 { let var_count = self .denominator @@ -245,7 +233,7 @@ impl RationalPolynomial { } f.write_str("/(")?; - self.denominator.format(opts, false, true, f)?; + self.denominator.format(opts, false, false, f)?; f.write_char(')') } } diff --git a/src/poly/polynomial.rs b/src/poly/polynomial.rs index 8db06cb..dc5053f 100755 --- a/src/poly/polynomial.rs +++ b/src/poly/polynomial.rs @@ -725,7 +725,7 @@ impl MultivariatePolynomial { } if self.nterms() < 2 { - //in_product = false; // TODO + in_product = false; } if in_sum && in_product {