Skip to content

Commit

Permalink
Release Symbolica 0.12.1
Browse files Browse the repository at this point in the history
- Fix rhs caching
  • Loading branch information
benruijl committed Oct 2, 2024
1 parent 7de3bd1 commit 09fc011
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 29 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/publish_pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,29 @@ permissions:
contents: read

jobs:
linux:
runs-on: ${{ matrix.runner[0] }}
strategy:
fail-fast: false
matrix:
runner: [[ubuntu-latest, x86_64]]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.x
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.runner[1] }}
args: --release --out dist
sccache: 'true'
manylinux: auto
- name: Upload wheels
uses: actions/upload-artifact@v4
with:
name: wheels-linux-${{ matrix.runner[1] }}
path: dist
macos:
runs-on: ${{ matrix.runner[0] }}
strategy:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ name = "symbolica"
readme = "Readme.md"
repository = "https://github.com/benruijl/symbolica"
rust-version = "1.73"
version = "0.12.0"
version = "0.12.1"

[profile.release]
codegen-units = 1
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ keywords = ["math", "algebra", "polynomial", "expression", "manipulation"]
license = {file = "License.md"}
name = "symbolica"
readme = "Readme.md"
version = "0.12.0"
version = "0.12.1"

classifiers = [
"Development Status :: 4 - Beta",
Expand Down
58 changes: 33 additions & 25 deletions src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,34 +357,33 @@ impl<'a> AtomView<'a> {

let mut it = AtomMatchIterator::new(r.pat, *self);
if let Some((_, used_flags)) = it.next(&mut match_stack) {
if let Some(rhs) = rhs_cache.get(&match_stack.stack) {
out.set_from_view(&rhs.as_view());
return true;
}

let mut rhs_subs = workspace.new_atom();

match r.rhs {
PatternOrMap::Pattern(rhs) => {
rhs.substitute_wildcards(workspace, &mut rhs_subs, &match_stack)
.unwrap(); // TODO: escalate?
}
PatternOrMap::Map(f) => {
let mut rhs = f(&match_stack);
std::mem::swap(rhs_subs.deref_mut(), &mut rhs);
if let Some(rhs) = rhs_cache.get(&match_stack.stack) {
rhs_subs.set_from_view(&rhs.as_view());
} else {
match r.rhs {
PatternOrMap::Pattern(rhs) => {
rhs.substitute_wildcards(workspace, &mut rhs_subs, &match_stack)
.unwrap(); // TODO: escalate?
}
PatternOrMap::Map(f) => {
let mut rhs = f(&match_stack);
std::mem::swap(rhs_subs.deref_mut(), &mut rhs);
}
}
}

if used_flags.iter().all(|x| *x) {
// all used, return rhs
out.set_from_view(&rhs_subs.as_view());

if rhs_cache.len() < settings.rhs_cache_size
&& !matches!(r.rhs, PatternOrMap::Pattern(Pattern::Literal(_)))
{
rhs_cache.insert(match_stack.stack.clone(), rhs_subs.into_inner());
rhs_cache
.insert(match_stack.stack.clone(), rhs_subs.deref_mut().clone());
}
}

if used_flags.iter().all(|x| *x) {
// all used, return rhs
out.set_from_view(&rhs_subs.as_view());
return true;
}

Expand Down Expand Up @@ -416,12 +415,6 @@ impl<'a> AtomView<'a> {
}
}

if rhs_cache.len() < settings.rhs_cache_size
&& !matches!(r.rhs, PatternOrMap::Pattern(Pattern::Literal(_)))
{
rhs_cache.insert(match_stack.stack.clone(), rhs_subs.into_inner());
}

return true;
}
}
Expand Down Expand Up @@ -3034,4 +3027,19 @@ mod test {
let r = b.replace_all(&p1, &rhs1, Some(&rest), None);
assert_eq!(r, b);
}

#[test]
fn match_cache() {
let mut expr = Atom::parse("f1(1)*f1(2)+f1(1)*f1(2)*f2").unwrap();

expr = Pattern::parse("v1_(id1_)*v2_(id2_)").unwrap().replace_all(
expr.as_view(),
&Pattern::parse("f1(id1_)").unwrap().into(),
None,
None,
);

let res = Atom::parse("f1(1)+f2*f1(1)").unwrap();
assert_eq!(expr, res);
}
}
4 changes: 2 additions & 2 deletions symbolica.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ class Expression:
--------
>>> from symbolica import *
>>> a = Expression.nsolve_system([E("5x^2+x*y^2+sin(2y)^2 - 2"), E("exp(2x-y)+4y - 3")], [S("x"), S("y")],
[Decimal("1.00000000000000000"), Decimal("1.00000000000000000")], 1e-20, 1000000)
[1., 1.], 1e-20, 1000000)
"""

@overload
Expand All @@ -1216,7 +1216,7 @@ class Expression:
--------
>>> from symbolica import *
>>> a = Expression.nsolve_system([E("5x^2+x*y^2+sin(2y)^2 - 2"), E("exp(2x-y)+4y - 3")], [S("x"), S("y")],
[1., 1.], 1e-20, 1000000)
[Decimal("1.00000000000000000"), Decimal("1.00000000000000000")], 1e-20, 1000000)
"""

def evaluate(
Expand Down

0 comments on commit 09fc011

Please sign in to comment.