Skip to content

Commit

Permalink
Revise formatting system
Browse files Browse the repository at this point in the history
- Add self-rings and a ring element wrapper
- Rename pretty_str to format
  • Loading branch information
benruijl committed Nov 15, 2024
1 parent dc2d96c commit 5be7a59
Show file tree
Hide file tree
Showing 20 changed files with 2,100 additions and 1,659 deletions.
18 changes: 6 additions & 12 deletions examples/fuel_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use std::{

use smartstring::{LazyCompact, SmartString};
use symbolica::{
domains::{integer::Z, rational::Q, rational_polynomial::RationalPolynomial},
domains::{integer::Z, rational::Q, rational_polynomial::RationalPolynomial, SelfRing},
parser::Token,
printer::{PrintOptions, RationalPolynomialPrinter},
printer::{PrintOptions, PrintState},
state::State,
};

Expand Down Expand Up @@ -56,16 +56,10 @@ 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, PrintState::new(), &mut buffer)
.unwrap();
writeln!(stdout, "{}", buffer).unwrap();

buffer.clear();
}
Expand Down
154 changes: 56 additions & 98 deletions src/api/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use smartstring::{LazyCompact, SmartString};
use crate::domains::finite_field::{FiniteField, FiniteFieldCore, Mersenne64, Zp, Zp64};
use crate::domains::integer::{IntegerRing, Z};
use crate::domains::rational::Q;
use crate::domains::SelfRing;
use crate::parser::Token;
use crate::poly::Variable;
use crate::LicenseManager;
use crate::{
domains::factorized_rational_polynomial::FactorizedRationalPolynomial,
domains::rational_polynomial::RationalPolynomial,
printer::{FactorizedRationalPolynomialPrinter, PrintOptions, RationalPolynomialPrinter},
domains::rational_polynomial::RationalPolynomial, printer::PrintOptions, printer::PrintState,
state::State,
};

Expand Down Expand Up @@ -169,6 +169,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 @@ -179,17 +180,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, PrintState::new(), &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 @@ -201,17 +193,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, PrintState::new(), &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 @@ -223,17 +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, PrintState::new(), &mut symbolica.local_state.buffer)
.unwrap();
} else {
let field = Zp64::new(prime as u64);
let rf: RationalPolynomial<Zp64, $exp_size> = token
Expand All @@ -245,29 +219,34 @@ 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, PrintState::new(), &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 Expand Up @@ -296,6 +275,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 @@ -306,17 +286,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, PrintState::new(), &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 @@ -328,17 +299,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, PrintState::new(), &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 @@ -350,17 +312,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, PrintState::new(), &mut symbolica.local_state.buffer)
.unwrap();
} else {
let field = Zp64::new(prime as u64);
let rf: FactorizedRationalPolynomial<Zp64, $exp_size> = token
Expand All @@ -372,29 +325,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, PrintState::new(), &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
Loading

0 comments on commit 5be7a59

Please sign in to comment.