Skip to content

Commit

Permalink
Further refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
benruijl committed Nov 7, 2024
1 parent f7d299d commit 1ed7ea0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 53 deletions.
15 changes: 4 additions & 11 deletions examples/fuel_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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();
}
Expand Down
16 changes: 8 additions & 8 deletions src/api/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IntegerRing, $exp_size> = token
.to_rational_polynomial(
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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()
};
}

Expand Down Expand Up @@ -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<IntegerRing, $exp_size> = token
.to_factorized_rational_polynomial(
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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();
}
Expand Down
17 changes: 9 additions & 8 deletions src/domains/factorized_rational_polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl<R: Ring, E: Exponent> FactorizedRationalPolynomial<R, E> {
&self,
opts: &PrintOptions,
in_sum: bool,
in_product: bool,
mut in_product: bool,
f: &mut W,
) -> Result<(), Error> {
if opts.explicit_rational_polynomial {
Expand All @@ -176,7 +176,7 @@ impl<R: Ring, E: Exponent> FactorizedRationalPolynomial<R, E> {
} else {
f.write_char('[')?;
self.numerator.format(opts, false, false, f)?;
f.write_str("]*")?;
f.write_str("]")?;
}
} else {
f.write_char('[')?;
Expand Down Expand Up @@ -212,9 +212,10 @@ impl<R: Ring, E: Exponent> FactorizedRationalPolynomial<R, E> {

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)
Expand All @@ -238,15 +239,15 @@ impl<R: Ring, E: Exponent> FactorizedRationalPolynomial<R, E> {
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{")?;
Expand All @@ -264,8 +265,7 @@ impl<R: Ring, E: Exponent> FactorizedRationalPolynomial<R, E> {
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))?;
}
}
Expand Down Expand Up @@ -314,7 +314,8 @@ impl<R: Ring, E: Exponent> FactorizedRationalPolynomial<R, E> {

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!(
Expand Down
9 changes: 5 additions & 4 deletions src/domains/rational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,16 @@ impl<R: EuclideanDomain> Ring for FractionField<R> {
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(())
Expand Down
30 changes: 9 additions & 21 deletions src/domains/rational_polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,15 @@ impl<R: Ring, E: Exponent> RationalPolynomial<R, E> {
pub fn format<W: std::fmt::Write>(
&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')?;
Expand All @@ -197,13 +196,7 @@ impl<R: Ring, E: Exponent> RationalPolynomial<R, E> {
}

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 {
Expand All @@ -216,14 +209,9 @@ impl<R: Ring, E: Exponent> RationalPolynomial<R, E> {
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
Expand All @@ -245,7 +233,7 @@ impl<R: Ring, E: Exponent> RationalPolynomial<R, E> {
}

f.write_str("/(")?;
self.denominator.format(opts, false, true, f)?;
self.denominator.format(opts, false, false, f)?;
f.write_char(')')
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/poly/polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ impl<F: Ring, E: Exponent, O: MonomialOrder> MultivariatePolynomial<F, E, O> {
}

if self.nterms() < 2 {
//in_product = false; // TODO
in_product = false;
}

if in_sum && in_product {
Expand Down

0 comments on commit 1ed7ea0

Please sign in to comment.