Skip to content

Commit

Permalink
Merge branch 'benruijl:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnbr authored Dec 14, 2024
2 parents 371df9d + 5e9279b commit eaa15a6
Show file tree
Hide file tree
Showing 60 changed files with 2,362 additions and 1,448 deletions.
5 changes: 4 additions & 1 deletion examples/coefficient_ring.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::sync::Arc;

use symbolica::{atom::Atom, state::State};
use symbolica::{
atom::{Atom, AtomCore},
state::State,
};

fn main() {
let expr = Atom::parse("x*z+x*(y+2)^-1*(y+z+1)").unwrap();
Expand Down
14 changes: 9 additions & 5 deletions examples/collect.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
use symbolica::{atom::Atom, fun, state::State};
use symbolica::{
atom::{Atom, AtomCore},
fun,
state::State,
};

fn main() {
let input = Atom::parse("x*(1+a)+x*5*y+f(5,x)+2+y^2+x^2 + x^3").unwrap();
let x = State::get_symbol("x").into();
let x = Atom::new_var(State::get_symbol("x"));
let key = State::get_symbol("key");
let coeff = State::get_symbol("val");

let r = input.coefficient_list::<i8>(std::slice::from_ref(&x));
let r = input.coefficient_list::<i8, _>(std::slice::from_ref(&x));

println!("> Coefficient list:");
for (key, val) in r {
println!("\t{} {}", key, val);
}

println!("> Collect in x:");
let out = input.collect::<i8>(
let out = input.collect::<i8, _>(
&x,
Some(Box::new(|x, out| {
out.set_from_view(&x);
Expand All @@ -24,7 +28,7 @@ fn main() {
println!("\t{}", out);

println!("> Collect in x with wrapping:");
let out = input.collect::<i8>(
let out = input.collect::<i8, _>(
&x,
Some(Box::new(move |a, out| {
out.set_from_view(&a);
Expand Down
5 changes: 4 additions & 1 deletion examples/derivative.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use symbolica::{atom::Atom, state::State};
use symbolica::{
atom::{Atom, AtomCore},
state::State,
};

fn main() {
let x = State::get_symbol("x");
Expand Down
12 changes: 5 additions & 7 deletions examples/evaluate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ahash::HashMap;
use symbolica::atom::AtomCore;
use symbolica::evaluate::EvaluationFn;
use symbolica::{atom::Atom, state::State};

Expand All @@ -10,13 +11,11 @@ fn main() {
let a = Atom::parse("x*cos(x) + f(x, 1)^2 + g(g(x)) + p(0)").unwrap();

let mut const_map = HashMap::default();
let mut fn_map: HashMap<_, EvaluationFn<_>> = HashMap::default();
let mut cache = HashMap::default();
let mut fn_map: HashMap<_, _> = HashMap::default();

// x = 6 and p(0) = 7
let v = Atom::new_var(x);
const_map.insert(v.as_view(), 6.);
const_map.insert(p0.as_view(), 7.);
const_map.insert(Atom::new_var(x), 6.);
const_map.insert(p0, 7.);

// f(x, y) = x^2 + y
fn_map.insert(
Expand All @@ -36,7 +35,6 @@ fn main() {

println!(
"Result for x = 6.: {}",
a.evaluate::<f64, _>(|x| x.into(), &const_map, &fn_map, &mut cache)
.unwrap()
a.evaluate(|x| x.into(), &const_map, &fn_map).unwrap()
);
}
2 changes: 1 addition & 1 deletion examples/expansion.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use symbolica::atom::Atom;
use symbolica::atom::{Atom, AtomCore};

fn main() {
let input = Atom::parse("(1+x)^3").unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/factorization.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use symbolica::{
atom::Atom,
atom::{Atom, AtomCore},
domains::{finite_field::Zp, integer::Z},
poly::{factor::Factorize, polynomial::MultivariatePolynomial, Variable},
state::State,
Expand Down
14 changes: 7 additions & 7 deletions examples/fibonacci.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use symbolica::{
atom::{Atom, AtomView},
atom::{Atom, AtomCore, AtomView},
id::{Match, Pattern, WildcardRestriction},
state::{RecycledAtom, State},
};

fn main() {
// prepare all patterns
let pattern = Pattern::parse("f(x_)").unwrap();
let rhs = Pattern::parse("f(x_ - 1) + f(x_ - 2)").unwrap().into();
let rhs = Pattern::parse("f(x_ - 1) + f(x_ - 2)").unwrap();
let lhs_zero_pat = Pattern::parse("f(0)").unwrap();
let lhs_one_pat = Pattern::parse("f(1)").unwrap();
let rhs_one = Atom::new_num(1).into_pattern().into();
let rhs_one = Atom::new_num(1).to_pattern();

// prepare the pattern restriction `x_ > 1`
let restrictions = (
Expand All @@ -32,14 +32,14 @@ fn main() {

for _ in 0..9 {
let mut out = RecycledAtom::new();
pattern.replace_all_into(target.as_view(), &rhs, Some(&restrictions), None, &mut out);
target.replace_all_into(&pattern, &rhs, Some(&restrictions), None, &mut out);

let mut out2 = RecycledAtom::new();
out.expand_into(&mut out2);
out.expand_into(None, &mut out2);

lhs_zero_pat.replace_all_into(out2.as_view(), &rhs_one, None, None, &mut out);
out2.replace_all_into(&lhs_zero_pat, &rhs_one, None, None, &mut out);

lhs_one_pat.replace_all_into(out.as_view(), &rhs_one, None, None, &mut out2);
out.replace_all_into(&lhs_one_pat, &rhs_one, None, None, &mut out2);

println!("\t{}", out2);

Expand Down
2 changes: 1 addition & 1 deletion examples/groebner_basis.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use symbolica::{
atom::Atom,
atom::{Atom, AtomCore},
domains::finite_field::Zp,
poly::{groebner::GroebnerBasis, polynomial::MultivariatePolynomial, GrevLexOrder},
state::State,
Expand Down
12 changes: 6 additions & 6 deletions examples/nested_evaluation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use symbolica::{
atom::Atom,
atom::{Atom, AtomCore},
domains::rational::Rational,
evaluate::{CompileOptions, FunctionMap, InlineASM, OptimizationSettings},
state::State,
Expand All @@ -26,39 +26,39 @@ fn main() {
vec![Atom::new_num(1).into()],
"p1".to_string(),
vec![State::get_symbol("z")],
p1.as_view(),
p1,
)
.unwrap();
fn_map
.add_function(
State::get_symbol("f"),
"f".to_string(),
vec![State::get_symbol("y"), State::get_symbol("z")],
f.as_view(),
f,
)
.unwrap();
fn_map
.add_function(
State::get_symbol("g"),
"g".to_string(),
vec![State::get_symbol("y")],
g.as_view(),
g,
)
.unwrap();
fn_map
.add_function(
State::get_symbol("h"),
"h".to_string(),
vec![State::get_symbol("y")],
h.as_view(),
h,
)
.unwrap();
fn_map
.add_function(
State::get_symbol("i"),
"i".to_string(),
vec![State::get_symbol("y")],
i.as_view(),
i,
)
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion examples/optimize.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use symbolica::{
atom::Atom,
atom::{Atom, AtomCore},
domains::rational::Q,
poly::evaluate::{BorrowedHornerScheme, InstructionSetPrinter},
};
Expand Down
2 changes: 1 addition & 1 deletion examples/optimize_multiple.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use symbolica::{
atom::Atom,
atom::{Atom, AtomCore},
domains::rational::Q,
poly::evaluate::{HornerScheme, InstructionSetPrinter},
};
Expand Down
14 changes: 9 additions & 5 deletions examples/partition.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
use symbolica::{atom::Atom, id::Pattern, state::State, transformer::Transformer};
use symbolica::{
atom::{Atom, AtomCore},
id::Pattern,
state::State,
transformer::Transformer,
};

fn main() {
let input = Atom::parse("f(1,3,2,3,1)").unwrap();
let f = State::get_symbol("f");
let g = State::get_symbol("g");

let o = Pattern::parse("f(x__)").unwrap().replace_all(
input.as_view(),
let o = input.replace_all(
&Pattern::parse("f(x__)").unwrap(),
&Pattern::Transformer(Box::new((
Some(Pattern::parse("x__").unwrap()),
vec![Transformer::Partition(
vec![(f, 2), (g, 2), (f, 1)],
false,
false,
)],
)))
.into(),
))),
None,
None,
);
Expand Down
12 changes: 5 additions & 7 deletions examples/pattern_match.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use symbolica::{
atom::Atom,
id::{Condition, Match, MatchSettings},
atom::{Atom, AtomCore},
id::Match,
state::State,
};

Expand All @@ -9,14 +9,12 @@ fn main() {

let pat_expr = Atom::parse("z*x_*y___*g___(z___,x_,w___)").unwrap();

let pattern = pat_expr.as_view().into_pattern();
let conditions = Condition::default();
let settings = MatchSettings::default();
let pattern = pat_expr.to_pattern();

println!("> Matching pattern {} to {}:", pat_expr, expr.as_view());

let mut it = pattern.pattern_match(expr.as_view(), &conditions, &settings);
while let Some(m) = it.next() {
let mut it = expr.pattern_match(&pattern, None, None);
while let Some(m) = it.next_detailed() {
println!(
"\t Match at location {:?} - {:?}:",
m.position, m.used_flags
Expand Down
8 changes: 4 additions & 4 deletions examples/pattern_restrictions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use symbolica::{
atom::{Atom, AtomView},
atom::{Atom, AtomCore, AtomView},
coefficient::CoefficientView,
domains::finite_field,
id::{Condition, Match, MatchSettings, WildcardRestriction},
Expand All @@ -9,7 +9,7 @@ fn main() {
let expr = Atom::parse("f(1,2,3,4,5,6,7)").unwrap();
let pat_expr = Atom::parse("f(x__,y__,z__,w__)").unwrap();

let pattern = pat_expr.as_view().into_pattern();
let pattern = pat_expr.as_view().to_pattern();

let x = State::get_symbol("x__");
let y = State::get_symbol("y__");
Expand Down Expand Up @@ -58,8 +58,8 @@ fn main() {
expr
);

let mut it = pattern.pattern_match(expr.as_view(), &conditions, &settings);
while let Some(m) = it.next() {
let mut it = expr.pattern_match(&pattern, Some(&conditions), Some(&settings));
while let Some(m) = it.next_detailed() {
println!("\tMatch at location {:?} - {:?}:", m.position, m.used_flags);
for (id, v) in m.match_stack {
print!("\t\t{} = ", State::get_name(*id));
Expand Down
2 changes: 1 addition & 1 deletion examples/polynomial_gcd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use symbolica::{
atom::Atom,
atom::{Atom, AtomCore},
domains::{integer::Z, rational::Q},
};
use tracing_subscriber::{fmt, prelude::*, util::SubscriberInitExt, EnvFilter};
Expand Down
2 changes: 1 addition & 1 deletion examples/rational_polynomial.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use symbolica::{
atom::Atom,
atom::{Atom, AtomCore},
domains::{integer::Z, rational_polynomial::RationalPolynomial},
};

Expand Down
9 changes: 6 additions & 3 deletions examples/replace_all.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use symbolica::{atom::Atom, id::Pattern};
use symbolica::{
atom::{Atom, AtomCore},
id::Pattern,
};

fn main() {
let expr = Atom::parse(" f(1,2,x) + f(1,2,3)").unwrap();
let pat = Pattern::parse("f(1,2,y_)").unwrap();
let rhs = Pattern::parse("f(1,2,y_+1)").unwrap().into();
let rhs = Pattern::parse("f(1,2,y_+1)").unwrap();

let out = pat.replace_all(expr.as_view(), &rhs, None, None);
let out = expr.replace_all(&pat, &rhs, None, None);
println!("{}", out);
}
18 changes: 5 additions & 13 deletions examples/replace_once.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
use symbolica::{
atom::Atom,
id::{Condition, MatchSettings},
};
use symbolica::atom::{Atom, AtomCore};

fn main() {
let expr = Atom::parse("f(z)*f(f(x))*f(y)").unwrap();
let pat_expr = Atom::parse("f(x_)").unwrap();

let rhs_expr = Atom::parse("g(x_)").unwrap();
let rhs = rhs_expr.as_view().into_pattern().into();
let rhs = rhs_expr.as_view().to_pattern();

let pattern = pat_expr.as_view().into_pattern();
let restrictions = Condition::default();
let settings = MatchSettings::default();
let pattern = pat_expr.as_view().to_pattern();

println!(
"> Replace once {}={} in {}:",
Expand All @@ -21,10 +16,7 @@ fn main() {
expr.as_view()
);

let mut replaced = Atom::new();

let mut it = pattern.replace_iter(expr.as_view(), &rhs, &restrictions, &settings);
while let Some(()) = it.next(&mut replaced) {
println!("\t{}", replaced);
for x in expr.replace_iter(&pattern, &rhs, None, None) {
println!("\t{}", x);
}
}
5 changes: 4 additions & 1 deletion examples/series.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use symbolica::{atom::Atom, state::State};
use symbolica::{
atom::{Atom, AtomCore},
state::State,
};

fn main() {
let x = State::get_symbol("x");
Expand Down
Loading

0 comments on commit eaa15a6

Please sign in to comment.