Skip to content

Commit

Permalink
Revise formatting system
Browse files Browse the repository at this point in the history
  • Loading branch information
benruijl committed Nov 6, 2024
1 parent 60b1ece commit f7d299d
Show file tree
Hide file tree
Showing 14 changed files with 739 additions and 956 deletions.
138 changes: 47 additions & 91 deletions src/api/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ use crate::poly::Variable;
use crate::LicenseManager;
use crate::{
domains::factorized_rational_polynomial::FactorizedRationalPolynomial,
domains::rational_polynomial::RationalPolynomial,
printer::{FactorizedRationalPolynomialPrinter, PrintOptions, RationalPolynomialPrinter},
state::State,
domains::rational_polynomial::RationalPolynomial, printer::PrintOptions, state::State,
};

struct LocalState {
Expand Down Expand Up @@ -180,16 +178,8 @@ unsafe extern "C" fn simplify(
.unwrap();

symbolica.local_state.buffer.clear();
write!(
&mut symbolica.local_state.buffer,
"{}\0", // add the NUL character
RationalPolynomialPrinter {
poly: &r,
opts,
add_parentheses: false,
}
)
.unwrap();
r.format(&opts, false, false, &mut symbolica.local_state.buffer)
.unwrap();
} else if prime <= u32::MAX as c_ulonglong {
let field = Zp::new(prime as u32);
let rf: RationalPolynomial<Zp, $exp_size> = token
Expand All @@ -202,16 +192,8 @@ unsafe extern "C" fn simplify(
.unwrap();

symbolica.local_state.buffer.clear();
write!(
&mut symbolica.local_state.buffer,
"{}\0", // add the NUL character
RationalPolynomialPrinter {
poly: &rf,
opts,
add_parentheses: false,
}
)
.unwrap();
rf.format(&opts, false, false, &mut symbolica.local_state.buffer)
.unwrap();
} else if prime == Mersenne64::PRIME {
let field = FiniteField::<Mersenne64>::new(Mersenne64::new());
let rf: RationalPolynomial<FiniteField<Mersenne64>, $exp_size> = token
Expand All @@ -224,16 +206,8 @@ unsafe extern "C" fn simplify(
.unwrap();

symbolica.local_state.buffer.clear();
write!(
&mut symbolica.local_state.buffer,
"{}\0", // add the NUL character
RationalPolynomialPrinter {
poly: &rf,
opts,
add_parentheses: false,
}
)
.unwrap();
rf.format(&opts, false, false, &mut symbolica.local_state.buffer)
.unwrap();
} else {
let field = Zp64::new(prime as u64);
let rf: RationalPolynomial<Zp64, $exp_size> = token
Expand All @@ -246,16 +220,8 @@ unsafe extern "C" fn simplify(
.unwrap();

symbolica.local_state.buffer.clear();
write!(
&mut symbolica.local_state.buffer,
"{}\0", // add the NUL character
RationalPolynomialPrinter {
poly: &rf,
opts,
add_parentheses: false,
}
)
.unwrap();
rf.format(&opts, false, false, &mut symbolica.local_state.buffer)
.unwrap();
}
};
}
Expand All @@ -264,10 +230,18 @@ unsafe extern "C" fn simplify(
symbolica.local_state.input_has_rational_numbers,
symbolica.local_state.exp_fits_in_u8,
) {
(false, true) => to_rational!(Z, u8),
(true, true) => to_rational!(Q, u8),
(false, false) => to_rational!(Z, u16),
(true, false) => to_rational!(Q, u16),
(false, true) => {
to_rational!(Z, u8);
}
(true, true) => {
to_rational!(Q, u8);
}
(false, false) => {
to_rational!(Z, u16);
}
(true, false) => {
to_rational!(Q, u16);
}
}

unsafe { CStr::from_bytes_with_nul_unchecked(symbolica.local_state.buffer.as_bytes()) }.as_ptr()
Expand Down Expand Up @@ -307,16 +281,8 @@ unsafe extern "C" fn simplify_factorized(
.unwrap();

symbolica.local_state.buffer.clear();
write!(
&mut symbolica.local_state.buffer,
"{}\0", // add the NUL character
FactorizedRationalPolynomialPrinter {
poly: &r,
opts,
add_parentheses: false,
}
)
.unwrap();
r.format(&opts, false, false, &mut symbolica.local_state.buffer)
.unwrap();
} else if prime <= u32::MAX as c_ulonglong {
let field = Zp::new(prime as u32);
let rf: FactorizedRationalPolynomial<Zp, $exp_size> = token
Expand All @@ -329,16 +295,8 @@ unsafe extern "C" fn simplify_factorized(
.unwrap();

symbolica.local_state.buffer.clear();
write!(
&mut symbolica.local_state.buffer,
"{}\0", // add the NUL character
FactorizedRationalPolynomialPrinter {
poly: &rf,
opts,
add_parentheses: false,
}
)
.unwrap();
rf.format(&opts, false, false, &mut symbolica.local_state.buffer)
.unwrap();
} else if prime == Mersenne64::PRIME {
let field = FiniteField::<Mersenne64>::new(Mersenne64::new());
let rf: FactorizedRationalPolynomial<FiniteField<Mersenne64>, $exp_size> = token
Expand All @@ -351,16 +309,8 @@ unsafe extern "C" fn simplify_factorized(
.unwrap();

symbolica.local_state.buffer.clear();
write!(
&mut symbolica.local_state.buffer,
"{}\0", // add the NUL character
FactorizedRationalPolynomialPrinter {
poly: &rf,
opts,
add_parentheses: false,
}
)
.unwrap();
rf.format(&opts, false, false, &mut symbolica.local_state.buffer)
.unwrap();
} else {
let field = Zp64::new(prime as u64);
let rf: FactorizedRationalPolynomial<Zp64, $exp_size> = token
Expand All @@ -373,28 +323,34 @@ unsafe extern "C" fn simplify_factorized(
.unwrap();

symbolica.local_state.buffer.clear();
write!(
&mut symbolica.local_state.buffer,
"{}\0", // add the NUL character
FactorizedRationalPolynomialPrinter {
poly: &rf,
opts,
add_parentheses: false,
}
)
.unwrap();
rf.format(&opts, false, false, &mut symbolica.local_state.buffer)
.unwrap();
}

write!(
&mut symbolica.local_state.buffer,
"\0", // add the NUL character
)
.unwrap()
};
}

match (
symbolica.local_state.input_has_rational_numbers,
symbolica.local_state.exp_fits_in_u8,
) {
(false, true) => to_rational!(Z, u8),
(true, true) => to_rational!(Q, u8),
(false, false) => to_rational!(Z, u16),
(true, false) => to_rational!(Q, u16),
(false, true) => {
to_rational!(Z, u8);
}
(true, true) => {
to_rational!(Q, u8);
}
(false, false) => {
to_rational!(Z, u16);
}
(true, false) => {
to_rational!(Q, u16);
}
}

unsafe { CStr::from_bytes_with_nul_unchecked(symbolica.local_state.buffer.as_bytes()) }.as_ptr()
Expand Down
9 changes: 6 additions & 3 deletions src/domains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ pub trait Ring: Clone + PartialEq + Eq + Hash + Debug + Display {
fn size(&self) -> Integer;

fn sample(&self, rng: &mut impl rand::RngCore, range: (i64, i64)) -> Self::Element;
fn fmt_display(
fn format<W: std::fmt::Write>(
&self,
element: &Self::Element,
opts: &PrintOptions,
in_sum: bool, // can be used to add + or -, similar to {:+}
in_product: bool, // can be used to add parentheses
f: &mut Formatter<'_>,
f: &mut W,
) -> Result<(), Error>;

fn printer<'a>(&'a self, element: &'a Self::Element) -> RingPrinter<'a, Self> {
Expand All @@ -124,6 +125,7 @@ pub struct RingPrinter<'a, R: Ring> {
pub ring: &'a R,
pub element: &'a R::Element,
pub opts: PrintOptions,
pub in_sum: bool,
pub in_product: bool,
}

Expand All @@ -133,6 +135,7 @@ impl<'a, R: Ring> RingPrinter<'a, R> {
ring,
element,
opts: PrintOptions::default(),
in_sum: false,
in_product: false,
}
}
Expand All @@ -141,6 +144,6 @@ impl<'a, R: Ring> RingPrinter<'a, R> {
impl<'a, R: Ring> Display for RingPrinter<'a, R> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.ring
.fmt_display(self.element, &self.opts, self.in_product, f)
.format(self.element, &self.opts, self.in_sum, self.in_product, f)
}
}
31 changes: 5 additions & 26 deletions src/domains/algebraic_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::{
factor::Factorize, gcd::PolynomialGCD, polynomial::MultivariatePolynomial, Exponent,
Variable,
},
printer::PolynomialPrinter,
};

use super::{
Expand Down Expand Up @@ -488,35 +487,15 @@ impl<R: Ring> Ring for AlgebraicExtension<R> {
AlgebraicNumber { poly }
}

fn fmt_display(
fn format<W: std::fmt::Write>(
&self,
element: &Self::Element,
opts: &crate::printer::PrintOptions,
in_product: bool, // can be used to add parentheses
f: &mut std::fmt::Formatter<'_>,
in_sum: bool,
in_product: bool,
f: &mut W,
) -> Result<(), std::fmt::Error> {
if f.sign_plus() {
f.write_str("+")?;
}

if in_product {
f.write_str("(")?;
}

write!(
f,
"{}",
PolynomialPrinter {
poly: &element.poly,
opts: *opts,
}
)?;

if in_product {
f.write_str(")")?;
}

Ok(())
element.poly.format(opts, in_sum, in_product, f)
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/domains/atom.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::fmt::Write;

use crate::{
atom::{Atom, AtomView},
poly::Variable,
Expand Down Expand Up @@ -126,14 +124,17 @@ impl Ring for AtomField {
0.into()
}

fn fmt_display(
fn format<W: std::fmt::Write>(
&self,
element: &Self::Element,
_opts: &crate::printer::PrintOptions,
in_sum: bool,
mut in_product: bool, // can be used to add parentheses
f: &mut std::fmt::Formatter<'_>,
f: &mut W,
) -> Result<(), std::fmt::Error> {
if f.sign_plus() {
// FIXME: improve, pass minus sign info
// to atom writer
if in_sum {
f.write_char('+')?;
}

Expand All @@ -145,7 +146,7 @@ impl Ring for AtomField {
write!(f, "(")?;
}

std::fmt::Display::fmt(element, f)?;
f.write_fmt(format_args!("{}", element))?;

if in_product {
write!(f, ")")?;
Expand Down
Loading

0 comments on commit f7d299d

Please sign in to comment.