Skip to content

Commit

Permalink
Fixes for printing
Browse files Browse the repository at this point in the history
- Rename pretty_str to format
  • Loading branch information
benruijl committed Nov 15, 2024
1 parent 97c6d5d commit 0331da4
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 148 deletions.
28 changes: 14 additions & 14 deletions src/api/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2345,7 +2345,7 @@ impl PythonExpression {
/// Examples
/// --------
/// >>> a = Expression.parse('128378127123 z^(2/3)*w^2/x/y + y^4 + z^34 + x^(x+2)+3/5+f(x,x^2)')
/// >>> print(a.pretty_str(number_thousands_separator='_', multiplication_operator=' '))
/// >>> print(a.format(number_thousands_separator='_', multiplication_operator=' '))
#[pyo3(signature =
(terms_on_new_line = false,
color_top_level_sum = true,
Expand All @@ -2361,7 +2361,7 @@ impl PythonExpression {
latex = false,
precision = None)
)]
pub fn pretty_str(
pub fn format(
&self,
terms_on_new_line: bool,
color_top_level_sum: bool,
Expand Down Expand Up @@ -5354,7 +5354,7 @@ impl PythonPolynomial {
/// Examples
/// --------
/// >>> p = FiniteFieldPolynomial.parse("3*x^2+2*x+7*x^3", ['x'], 11)
/// >>> print(p.pretty_str(symmetric_representation_for_finite_field=True))
/// >>> print(p.format(symmetric_representation_for_finite_field=True))
#[pyo3(signature =
(terms_on_new_line = false,
color_top_level_sum = true,
Expand All @@ -5370,7 +5370,7 @@ impl PythonPolynomial {
latex = false,
precision = None)
)]
pub fn pretty_str(
pub fn format(
&self,
terms_on_new_line: bool,
color_top_level_sum: bool,
Expand Down Expand Up @@ -6177,7 +6177,7 @@ impl PythonFiniteFieldPolynomial {
/// Examples
/// --------
/// >>> p = FiniteFieldPolynomial.parse("3*x^2+2*x+7*x^3", ['x'], 11)
/// >>> print(p.pretty_str(symmetric_representation_for_finite_field=True))
/// >>> print(p.format(symmetric_representation_for_finite_field=True))
#[pyo3(signature =
(terms_on_new_line = false,
color_top_level_sum = true,
Expand All @@ -6193,7 +6193,7 @@ impl PythonFiniteFieldPolynomial {
latex = false,
precision = None)
)]
pub fn pretty_str(
pub fn format(
&self,
terms_on_new_line: bool,
color_top_level_sum: bool,
Expand Down Expand Up @@ -6757,7 +6757,7 @@ impl PythonPrimeTwoPolynomial {
/// Examples
/// --------
/// >>> p = FiniteFieldPolynomial.parse("3*x^2+2*x+7*x^3", ['x'], 11)
/// >>> print(p.pretty_str(symmetric_representation_for_finite_field=True))
/// >>> print(p.format(symmetric_representation_for_finite_field=True))
#[pyo3(signature =
(terms_on_new_line = false,
color_top_level_sum = true,
Expand All @@ -6773,7 +6773,7 @@ impl PythonPrimeTwoPolynomial {
latex = false,
precision = None)
)]
pub fn pretty_str(
pub fn format(
&self,
terms_on_new_line: bool,
color_top_level_sum: bool,
Expand Down Expand Up @@ -7297,7 +7297,7 @@ impl PythonGaloisFieldPrimeTwoPolynomial {
/// Examples
/// --------
/// >>> p = FiniteFieldPolynomial.parse("3*x^2+2*x+7*x^3", ['x'], 11)
/// >>> print(p.pretty_str(symmetric_representation_for_finite_field=True))
/// >>> print(p.format(symmetric_representation_for_finite_field=True))
#[pyo3(signature =
(terms_on_new_line = false,
color_top_level_sum = true,
Expand All @@ -7313,7 +7313,7 @@ impl PythonGaloisFieldPrimeTwoPolynomial {
latex = false,
precision = None)
)]
pub fn pretty_str(
pub fn format(
&self,
terms_on_new_line: bool,
color_top_level_sum: bool,
Expand Down Expand Up @@ -7841,7 +7841,7 @@ impl PythonGaloisFieldPolynomial {
/// Examples
/// --------
/// >>> p = FiniteFieldPolynomial.parse("3*x^2+2*x+7*x^3", ['x'], 11)
/// >>> print(p.pretty_str(symmetric_representation_for_finite_field=True))
/// >>> print(p.format(symmetric_representation_for_finite_field=True))
#[pyo3(signature =
(terms_on_new_line = false,
color_top_level_sum = true,
Expand All @@ -7857,7 +7857,7 @@ impl PythonGaloisFieldPolynomial {
latex = false,
precision = None)
)]
pub fn pretty_str(
pub fn format(
&self,
terms_on_new_line: bool,
color_top_level_sum: bool,
Expand Down Expand Up @@ -8386,7 +8386,7 @@ impl PythonNumberFieldPolynomial {
/// Examples
/// --------
/// >>> p = FiniteFieldPolynomial.parse("3*x^2+2*x+7*x^3", ['x'], 11)
/// >>> print(p.pretty_str(symmetric_representation_for_finite_field=True))
/// >>> print(p.format(symmetric_representation_for_finite_field=True))
#[pyo3(signature =
(terms_on_new_line = false,
color_top_level_sum = true,
Expand All @@ -8402,7 +8402,7 @@ impl PythonNumberFieldPolynomial {
latex = false,
precision = None)
)]
pub fn pretty_str(
pub fn format(
&self,
terms_on_new_line: bool,
color_top_level_sum: bool,
Expand Down
8 changes: 4 additions & 4 deletions src/domains/factorized_rational_polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ impl<R: Ring, E: Exponent> SelfRing for FactorizedRationalPolynomial<R, E> {
if self.numerator.is_one() {
return Ok(false);
}
f.write_char('*')?;
f.write_char(opts.multiplication_operator)?;
}

if write_par {
Expand Down Expand Up @@ -311,15 +311,15 @@ impl<R: Ring, E: Exponent> SelfRing for FactorizedRationalPolynomial<R, E> {
return Ok(false);
}

state.in_product |= has_numer_coeff && !self.numerator.is_one();
state.in_product = true;
if has_numer_coeff || self.numerator.is_one() {
self.numerator
.ring
.format(&self.numer_coeff, opts, state, f)?;
state.in_sum = false;

if !self.numerator.is_one() {
f.write_char('*')?;
f.write_char(opts.multiplication_operator)?;
}
}

Expand Down Expand Up @@ -355,7 +355,7 @@ impl<R: Ring, E: Exponent> SelfRing for FactorizedRationalPolynomial<R, E> {

for (i, (d, p)) in self.denominators.iter().enumerate() {
if has_denom_coeff || i > 0 {
f.write_char('*')?;
f.write_char(opts.multiplication_operator)?;
}

d.format(opts, state.step(false, true, *p != 1), f)?;
Expand Down
16 changes: 8 additions & 8 deletions src/domains/rational_polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,8 @@ where
) -> RationalPolynomial<R, E> {
if f.is_zero() {
return RationalPolynomial {
numerator: MultivariatePolynomial::new_zero(&f.field.ring),
denominator: MultivariatePolynomial::new_one(&f.field.ring),
numerator: MultivariatePolynomial::new_zero(&f.ring.ring),
denominator: MultivariatePolynomial::new_one(&f.ring.ring),
};
}

Expand All @@ -535,7 +535,7 @@ where
});

let mut res =
RationalPolynomial::new(&f.field.ring, f.coefficients[0].get_variables().clone());
RationalPolynomial::new(&f.ring.ring, f.coefficients[0].get_variables().clone());

let mut exp = vec![E::zero(); f.coefficients[0].get_variables().len()];
exp[pos] = E::one();
Expand Down Expand Up @@ -1103,7 +1103,7 @@ where

d_exp[i - 1] = d_exp[i - 1].clone()
+ s_cor
+ t_cor.derivative().div_coeff(&(t_cor.field.nth(i as u64)));
+ t_cor.derivative().div_coeff(&(t_cor.ring.nth(i as u64)));

let t_full = Self::from_univariate(t_cor);

Expand Down Expand Up @@ -1186,7 +1186,7 @@ where
.to_multivariate_polynomial_list(&[var, new_var], true);

let mut bivar_poly = MultivariatePolynomial::new(
&p.field,
&p.ring,
Some(ll.len()),
p.coefficients[0].get_variables().clone(),
);
Expand All @@ -1197,7 +1197,7 @@ where
// convert defining polynomial to a univariate polynomial in t with rational polynomial coefficients
let def_uni = sqf
.to_univariate(new_var)
.map_coeff(|c| c.clone().into(), p.field.clone());
.map_coeff(|c| c.clone().into(), p.ring.clone());

// write the polynomial in x and t as a polynomial in x with rational polynomial coefficients in t and
// all other variables and solve a diophantine equation
Expand Down Expand Up @@ -1227,7 +1227,7 @@ where
let monic = bivar_poly_scaled.rem(&def_biv);

// convert the result to a multivariate rational polynomial
let mut res = p.field.zero();
let mut res = p.ring.zero();
for t in &monic {
let mut exp = vec![E::zero(); p.lcoeff().numerator.nvars()];
exp.copy_from_slice(t.exponents);
Expand All @@ -1250,7 +1250,7 @@ where

let eval = monic.replace(new_var, &sol);

let mut res = p.field.zero();
let mut res = p.ring.zero();
for t in &eval {
let mut exp = vec![E::zero(); p.lcoeff().numerator.nvars()];
exp.copy_from_slice(t.exponents);
Expand Down
7 changes: 4 additions & 3 deletions src/poly/polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,8 @@ impl<F: Ring, E: Exponent, O: MonomialOrder> SelfRing for MultivariatePolynomial
let add_paren = self.nterms() > 1 && state.in_product
|| (state.in_exp
&& (self.nterms() > 1
|| self.exponents(0).iter().filter(|e| **e > E::zero()).count() > 1));
|| self.exponents(0).iter().filter(|e| **e > E::zero()).count() > 1
|| !self.ring.is_one(&self.coefficients[0])));

if add_paren {
if state.in_sum {
Expand Down Expand Up @@ -781,7 +782,7 @@ impl<F: Ring, E: Exponent, O: MonomialOrder> SelfRing for MultivariatePolynomial
if suppressed_one {
suppressed_one = false;
} else if !opts.latex {
write!(f, "*")?;
f.write_char(opts.multiplication_operator)?;
}

f.write_str(var_id)?;
Expand Down Expand Up @@ -1789,7 +1790,7 @@ impl<F: Ring, E: Exponent> MultivariatePolynomial<F, E, LexOrder> {
return p;
}

p.coefficients = vec![p.field.zero(); self.degree(var).to_u32() as usize + 1];
p.coefficients = vec![p.ring.zero(); self.degree(var).to_u32() as usize + 1];
for (q, e) in self.coefficients.iter().zip(self.exponents_iter()) {
p.coefficients[e[var].to_u32() as usize] = q.clone();
}
Expand Down
Loading

0 comments on commit 0331da4

Please sign in to comment.