Skip to content

[WIP] Commutative operations in rules #752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

Bumblebee00
Copy link
Contributor

@Bumblebee00 Bumblebee00 commented Jun 18, 2025

this pr builds on top of defslot and negative exponent matching. Before this pr julia symbolic rewriting rules had this unfortunate behaviour:

julia> @syms a x
(a, x)

julia> rule = @rule sin(~x) + cos(~x) => "yes"
sin(~x) + cos(~x) => 1

julia> arguments(sin(x) + cos(x))
2-element SymbolicUtils.SmallVec{Any, Vector{Any}}:
 cos(x)
 sin(x)

julia> rule(sin(x)+cos(x)) # doesnt match bc arguments are ordered cos first and then sin


julia> arguments(sin(a) + cos(a))
2-element SymbolicUtils.SmallVec{Any, Vector{Any}}:
 sin(a)
 cos(a)

julia> rule(sin(a)+cos(a)) # match bc arguments are ordered sin first and then cos
"yes"

there was the acrule macro that helped, but was working only on the first operation of the expression, not every level. with this pr instead every * and + operation creates a matcher that checks all possible orderings of its arguments.

This can also be a bad thing becuse the cases to check could be too many...

Also I refacored the code from my last two pr to make everything clearer and cleaner

Copy link
Contributor

github-actions bot commented Jun 18, 2025

Benchmark Results

master 70e36ed... master / 70e36ed...
overhead/acrule/a+2 0.937 ± 0.27 μs 1.02 ± 0.024 μs 0.917 ± 0.26
overhead/acrule/a+2+b 0.925 ± 0.23 μs 0.979 ± 0.015 μs 0.945 ± 0.23
overhead/acrule/a+b 0.262 ± 0.025 μs 0.26 ± 0.095 μs 1.01 ± 0.38
overhead/acrule/noop:Int 1.56 ± 0.01 ns 1.56 ± 0.01 ns 1 ± 0.0091
overhead/acrule/noop:Sym 28.2 ± 6.4 ns 16.2 ± 2.8 ns 1.74 ± 0.5
overhead/get_degrees/large_poly 0.156 ± 0.0087 s 0.445 ± 0.038 s 0.351 ± 0.036
overhead/rule/noop:Int 0.0363 ± 0.024 μs 0.0349 ± 0.023 μs 1.04 ± 0.97
overhead/rule/noop:Sym 0.035 ± 0.023 μs 0.034 ± 0.024 μs 1.03 ± 0.99
overhead/rule/noop:Term 0.0353 ± 0.023 μs 0.0341 ± 0.024 μs 1.04 ± 1
overhead/ruleset/noop:Int 0.118 ± 0.036 μs 0.115 ± 0.035 μs 1.02 ± 0.44
overhead/ruleset/noop:Sym 0.127 ± 0.04 μs 0.126 ± 0.041 μs 1.01 ± 0.45
overhead/ruleset/noop:Term 5.2 ± 0.59 μs 4.6 ± 0.47 μs 1.13 ± 0.17
overhead/simplify/noop:Int 0.32 ± 0.0088 μs 0.246 ± 0.035 μs 1.3 ± 0.19
overhead/simplify/noop:Sym 0.324 ± 0.0086 μs 0.241 ± 0.034 μs 1.35 ± 0.19
overhead/simplify/noop:Term 0.0506 ± 0.0056 ms 0.089 ± 0.0099 ms 0.569 ± 0.089
overhead/simplify/randterm (+, *):serial 0.124 ± 0.011 s 0.388 ± 0.026 s 0.32 ± 0.035
overhead/simplify/randterm (+, *):thread 0.0706 ± 0.0059 s 0.255 ± 0.035 s 0.276 ± 0.045
overhead/simplify/randterm (/, *):serial 0.248 ± 0.023 ms 0.286 ± 0.025 ms 0.867 ± 0.11
overhead/simplify/randterm (/, *):thread 0.281 ± 0.027 ms 0.319 ± 0.029 ms 0.879 ± 0.12
overhead/substitute/a 0.106 ± 0.011 ms 0.104 ± 0.012 ms 1.02 ± 0.16
overhead/substitute/a,b 0.0896 ± 0.0094 ms 0.0906 ± 0.0074 ms 0.989 ± 0.13
overhead/substitute/a,b,c 20.2 ± 1.8 μs 20.1 ± 2.1 μs 1 ± 0.14
polyform/easy_iszero 0.0422 ± 0.006 ms 0.0403 ± 0.0011 ms 1.05 ± 0.15
polyform/isone 3.71 ± 0.92 ns 3.1 ± 0.01 ns 1.19 ± 0.3
polyform/iszero 1.41 ± 0.037 ms 1.41 ± 0.038 ms 1 ± 0.038
polyform/simplify_fractions 1.92 ± 0.064 ms 1.93 ± 0.083 ms 0.995 ± 0.054
time_to_load 1.13 ± 0.0087 s 1.14 ± 0.0086 s 0.992 ± 0.011

Benchmark Plots

A plot of the benchmark results have been uploaded as an artifact to the workflow run for this PR.
Go to "Actions"->"Benchmark a pull request"->[the most recent run]->"Artifacts" (at the bottom).

@Bumblebee00 Bumblebee00 changed the title Commutative operations Commutative operations in rules Jun 18, 2025
@Bumblebee00 Bumblebee00 force-pushed the commutative_operations branch 2 times, most recently from a9e1c22 to 7a0f514 Compare June 19, 2025 15:11
@Bumblebee00 Bumblebee00 force-pushed the commutative_operations branch from 7a0f514 to 562cb99 Compare June 19, 2025 15:51
@Bumblebee00 Bumblebee00 changed the title Commutative operations in rules [WIP] Commutative operations in rules Jun 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant