Skip to content

expand: fold literal complex(re, im) Terms in to_poly!#922

Open
oameye wants to merge 2 commits into
JuliaSymbolics:masterfrom
oameye:fold-complex-literal-in-simplify
Open

expand: fold literal complex(re, im) Terms in to_poly!#922
oameye wants to merge 2 commits into
JuliaSymbolics:masterfrom
oameye:fold-complex-literal-in-simplify

Conversation

@oameye
Copy link
Copy Markdown
Contributor

@oameye oameye commented May 18, 2026

Fixes #921.

Term(complex, [re, im]) (from Symbolics.unwrap(::Complex{Num})) has no case in to_poly!, so expand treats it as an opaque PolyVar. Identically-zero diffs that contain one don't reduce:

simplify(1im * Δ * z - z * Δ * 1im; expand = true)
# was:  z*complex(0, Δ) + (0 - 1im)*z*Δ
# now:  0

Fix is a single case in to_poly!: treat complex(re, im) as re + 1im * im polynomially.

I originally tried this as a simplify rule but that broke downstream tests (Symbolics overloads, ODE solver) because the Term(complex, ...) literal is also the shape Base.real/imag/conj pattern-match for O(1) extraction and the shape Complex{Num} round-trips through. Keeping the rewrite local to to_poly! leaves that canonical form alone.

`Symbolics.unwrap(::Complex{Num})` emits a literal `Term(complex, [re, im])`
when either component is symbolic. That node is opaque to `expand` (and
hence to `simplify(...; expand=true)`), so identically-zero polynomial
differences containing such a literal do not reduce to 0.

Add `complex(~re, ~im) => ~re + 1im * ~im` to `ASSORTED_RULES`. The
literal's children are always Real-typed (`Complex` requires `T<:Real`),
so the rewrite stays in clean `Complex{Bool} * BasicSymbolic{<:Real}`
arithmetic and does not loop back into a literal.

The `Term(complex, [re, im])` canonical form remains in place; the existing
O(1) pattern matches for `real` / `imag` / `conj` in `methods.jl` are
unaffected. The fold only fires when the user opts in to simplification.
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 18, 2026

Benchmark Results (Julia vlts)

Time benchmarks
master 72b1f61... master / 72b1f61...
arithmetic/2-arg mul 12.7 ± 0.45 μs 12.7 ± 0.35 μs 0.996 ± 0.045
arithmetic/addition 0.0775 ± 0.0024 ms 0.0774 ± 0.0015 ms 1 ± 0.036
arithmetic/division 24.1 ± 2.2 μs 24.3 ± 1.2 μs 0.992 ± 0.1
arithmetic/multiplication 0.059 ± 0.002 ms 0.0573 ± 0.0018 ms 1.03 ± 0.048
codegen/arrayop_nested/fast_toexpr 10.3 ± 1.8 μs 10.3 ± 1.4 μs 0.994 ± 0.22
codegen/arrayop_nested/toexpr 0.0539 ± 0.0053 ms 0.053 ± 0.0027 ms 1.02 ± 0.11
codegen/deep_poly/deg=10:fast_toexpr 0.0757 ± 0.0097 ms 0.0751 ± 0.0061 ms 1.01 ± 0.15
codegen/deep_poly/deg=10:toexpr 0.0651 ± 0.0062 ms 0.0644 ± 0.0053 ms 1.01 ± 0.13
codegen/deep_poly/deg=14:fast_toexpr 0.14 ± 0.016 ms 0.136 ± 0.012 ms 1.03 ± 0.15
codegen/deep_poly/deg=14:toexpr 0.117 ± 0.011 ms 0.117 ± 0.012 ms 1 ± 0.14
codegen/deep_poly/deg=6:fast_toexpr 0.0349 ± 0.0043 ms 0.0339 ± 0.0028 ms 1.03 ± 0.15
codegen/deep_poly/deg=6:toexpr 28.1 ± 3.4 μs 28.1 ± 2.8 μs 1 ± 0.16
codegen/makearray/n=100:fast_toexpr 0.0802 ± 0.01 ms 0.0794 ± 0.0067 ms 1.01 ± 0.16
codegen/makearray/n=100:toexpr 0.533 ± 0.022 ms 0.528 ± 0.018 ms 1.01 ± 0.055
codegen/makearray/n=200:fast_toexpr 0.135 ± 0.015 ms 0.132 ± 0.012 ms 1.02 ± 0.15
codegen/makearray/n=200:toexpr 0.766 ± 0.026 ms 0.76 ± 0.024 ms 1.01 ± 0.047
codegen/makearray/n=400:fast_toexpr 0.253 ± 0.029 ms 0.248 ± 0.02 ms 1.02 ± 0.14
codegen/makearray/n=400:toexpr 1.24 ± 0.046 ms 1.22 ± 0.035 ms 1.01 ± 0.047
codegen/wide_deep_poly/fast_toexpr 0.548 ± 0.043 ms 0.542 ± 0.029 ms 1.01 ± 0.097
codegen/wide_deep_poly/toexpr 0.43 ± 0.021 ms 0.426 ± 0.025 ms 1.01 ± 0.078
codegen/wide_poly/n=100:fast_toexpr 0.244 ± 0.028 ms 0.249 ± 0.028 ms 0.981 ± 0.16
codegen/wide_poly/n=100:toexpr 0.26 ± 0.024 ms 0.261 ± 0.019 ms 0.995 ± 0.12
codegen/wide_poly/n=25:fast_toexpr 0.0547 ± 0.0077 ms 0.0537 ± 0.0044 ms 1.02 ± 0.17
codegen/wide_poly/n=25:toexpr 0.0634 ± 0.0065 ms 0.0644 ± 0.0058 ms 0.986 ± 0.13
codegen/wide_poly/n=50:fast_toexpr 0.114 ± 0.015 ms 0.111 ± 0.011 ms 1.03 ± 0.17
codegen/wide_poly/n=50:toexpr 0.129 ± 0.014 ms 0.13 ± 0.012 ms 0.993 ± 0.14
irstructure/search_variables/common:IRStructure 0.23 ± 0.0075 ms 0.225 ± 0.0071 ms 1.02 ± 0.046
irstructure/search_variables/common:reference 1.21 ± 0.01 ms 1.22 ± 0.011 ms 0.995 ± 0.012
irstructure/search_variables/dissimilar:IRStructure 0.0581 ± 0.0019 ms 0.0564 ± 0.002 ms 1.03 ± 0.049
irstructure/search_variables/dissimilar:reference 0.256 ± 0.0092 ms 0.26 ± 0.0093 ms 0.984 ± 0.05
irstructure/substitute/IRSubstituter 12.2 ± 0.83 ms 9.13 ± 0.18 ms 1.33 ± 0.094
irstructure/substitute/reference 12.2 ± 0.34 ms 10 ± 0.18 ms 1.21 ± 0.04
irstructure/substitute/sparse IRSubstituter 1.38 ± 0.042 ms 1.39 ± 0.035 ms 0.995 ± 0.039
irstructure/substitute/sparse reference 2.57 ± 0.32 ms 2.19 ± 0.039 ms 1.17 ± 0.15
overhead/acrule/a+2 2.62 ± 0.1 μs 2.46 ± 0.11 μs 1.06 ± 0.063
overhead/acrule/a+2+b 0.061 ± 0.01 μs 0.061 ± 0.01 μs 1 ± 0.23
overhead/acrule/a+b 4.4 ± 0.2 μs 4.34 ± 0.16 μs 1.01 ± 0.06
overhead/acrule/noop:Int 0.05 ± 0 μs 0.05 ± 0 μs 1 ± 0
overhead/acrule/noop:Sym 0.051 ± 0.01 μs 0.051 ± 0.01 μs 1 ± 0.28
overhead/get_degrees/large_poly 0.08 ± 0.001 μs 0.08 ± 0 μs 1 ± 0.013
overhead/rule/noop:Int 0.06 ± 0.01 μs 0.06 ± 0.01 μs 1 ± 0.24
overhead/rule/noop:Sym 0.06 ± 0.001 μs 0.06 ± 0.001 μs 1 ± 0.024
overhead/rule/noop:Term 0.06 ± 0.001 μs 0.06 ± 0.001 μs 1 ± 0.024
overhead/ruleset/noop:Int 30 ± 0 ns 30 ± 0 ns 1 ± 0
overhead/ruleset/noop:Sym 0.24 ± 0.01 μs 0.231 ± 0.01 μs 1.04 ± 0.062
overhead/ruleset/noop:Term 1.08 ± 0.03 μs 1.05 ± 0.03 μs 1.03 ± 0.041
overhead/simplify/noop:Int 30 ± 0 ns 30 ± 0 ns 1 ± 0
overhead/simplify/noop:Sym 0.04 ± 0.01 μs 0.04 ± 0.01 μs 1 ± 0.35
overhead/simplify/noop:Term 30.6 ± 1.7 μs 28.9 ± 1.3 μs 1.06 ± 0.077
overhead/simplify/randterm (+, *):serial 0.293 ± 0.0099 s 0.273 ± 0.011 s 1.07 ± 0.055
overhead/simplify/randterm (+, *):thread 0.327 ± 0.012 s 0.303 ± 0.0092 s 1.08 ± 0.051
overhead/simplify/randterm (/, *):serial 0.18 ± 0.012 ms 0.17 ± 0.01 ms 1.05 ± 0.094
overhead/simplify/randterm (/, *):thread 0.183 ± 0.013 ms 0.172 ± 0.01 ms 1.06 ± 0.098
overhead/substitute/a 0.04 ± 0.00089 ms 0.0398 ± 0.00078 ms 1.01 ± 0.03
overhead/substitute/a,b 0.0482 ± 0.0011 ms 0.0478 ± 0.00089 ms 1.01 ± 0.029
overhead/substitute/a,b,c 0.0445 ± 0.00099 ms 0.0433 ± 0.001 ms 1.03 ± 0.033
polyform/easy_iszero 22.1 ± 0.57 μs 22 ± 0.44 μs 1.01 ± 0.033
polyform/isone 1.05 ± 0.046 ms 1.01 ± 0.037 ms 1.04 ± 0.06
polyform/isone:noop 0.08 ± 0 μs 0.08 ± 0 μs 1 ± 0
polyform/iszero 0.877 ± 0.036 ms 0.86 ± 0.035 ms 1.02 ± 0.059
polyform/iszero:noop 0.08 ± 0.001 μs 0.08 ± 0.001 μs 1 ± 0.018
polyform/simplify_fractions 1.13 ± 0.049 ms 1.1 ± 0.042 ms 1.03 ± 0.059
printing/large_poly 0.232 ± 0.0033 s 0.231 ± 0.0017 s 1 ± 0.016
time_to_load 1.67 ± 0.027 s 1.58 ± 0.021 s 1.06 ± 0.023
Memory benchmarks
master 72b1f61... master / 72b1f61...
arithmetic/2-arg mul 0.078 k allocs: 2.72 kB 0.077 k allocs: 2.69 kB 1.01
arithmetic/addition 0.468 k allocs: 16.9 kB 0.468 k allocs: 16.9 kB 1
arithmetic/division 0.142 k allocs: 5.47 kB 0.141 k allocs: 5.44 kB 1.01
arithmetic/multiplication 0.356 k allocs: 11.7 kB 0.356 k allocs: 11.7 kB 1
codegen/arrayop_nested/fast_toexpr 0.189 k allocs: 11.2 kB 0.183 k allocs: 11.1 kB 1.01
codegen/arrayop_nested/toexpr 0.474 k allocs: 20.6 kB 0.468 k allocs: 20.5 kB 1
codegen/deep_poly/deg=10:fast_toexpr 0.975 k allocs: 0.0564 MB 0.975 k allocs: 0.0564 MB 1
codegen/deep_poly/deg=10:toexpr 1.05 k allocs: 0.0497 MB 1.05 k allocs: 0.0497 MB 1
codegen/deep_poly/deg=14:fast_toexpr 1.71 k allocs: 0.117 MB 1.71 k allocs: 0.117 MB 1
codegen/deep_poly/deg=14:toexpr 1.88 k allocs: 0.0929 MB 1.88 k allocs: 0.0929 MB 1
codegen/deep_poly/deg=6:fast_toexpr 0.447 k allocs: 31.4 kB 0.447 k allocs: 31.4 kB 1
codegen/deep_poly/deg=6:toexpr 0.448 k allocs: 22.9 kB 0.448 k allocs: 22.9 kB 1
codegen/makearray/n=100:fast_toexpr 1.34 k allocs: 0.0702 MB 1.33 k allocs: 0.07 MB 1
codegen/makearray/n=100:toexpr 4.43 k allocs: 0.174 MB 4.42 k allocs: 0.174 MB 1
codegen/makearray/n=200:fast_toexpr 2.37 k allocs: 0.118 MB 2.35 k allocs: 0.117 MB 1
codegen/makearray/n=200:toexpr 6.36 k allocs: 0.246 MB 6.34 k allocs: 0.246 MB 1
codegen/makearray/n=400:fast_toexpr 4.21 k allocs: 0.213 MB 4.18 k allocs: 0.213 MB 1
codegen/makearray/n=400:toexpr 10.2 k allocs: 0.392 MB 10.1 k allocs: 0.392 MB 1
codegen/wide_deep_poly/fast_toexpr 5.56 k allocs: 0.3 MB 5.56 k allocs: 0.3 MB 1
codegen/wide_deep_poly/toexpr 6.88 k allocs: 0.319 MB 6.88 k allocs: 0.319 MB 1
codegen/wide_poly/n=100:fast_toexpr 2.98 k allocs: 0.178 MB 2.98 k allocs: 0.178 MB 1
codegen/wide_poly/n=100:toexpr 3.92 k allocs: 0.168 MB 3.92 k allocs: 0.168 MB 1
codegen/wide_poly/n=25:fast_toexpr 0.779 k allocs: 0.0464 MB 0.779 k allocs: 0.0464 MB 1
codegen/wide_poly/n=25:toexpr 0.992 k allocs: 0.0428 MB 0.992 k allocs: 0.0428 MB 1
codegen/wide_poly/n=50:fast_toexpr 1.52 k allocs: 0.109 MB 1.52 k allocs: 0.109 MB 1
codegen/wide_poly/n=50:toexpr 1.97 k allocs: 0.0883 MB 1.97 k allocs: 0.0883 MB 1
irstructure/search_variables/common:IRStructure 0.06 k allocs: 7.03 kB 0.06 k allocs: 7.03 kB 1
irstructure/search_variables/common:reference 5.81 k allocs: 0.0887 MB 5.81 k allocs: 0.0887 MB 1
irstructure/search_variables/dissimilar:IRStructure 0.049 k allocs: 5.27 kB 0.049 k allocs: 5.27 kB 1
irstructure/search_variables/dissimilar:reference 1.21 k allocs: 18.9 kB 1.21 k allocs: 18.9 kB 1
irstructure/substitute/IRSubstituter 0.0399 M allocs: 1.46 MB 0.0399 M allocs: 1.46 MB 1
irstructure/substitute/reference 0.0452 M allocs: 1.66 MB 0.0425 M allocs: 1.62 MB 1.03
irstructure/substitute/sparse IRSubstituter 4.7 k allocs: 0.174 MB 4.7 k allocs: 0.174 MB 1
irstructure/substitute/sparse reference 10 k allocs: 0.381 MB 7.37 k allocs: 0.341 MB 1.12
overhead/acrule/a+2 0.034 k allocs: 1.25 kB 0.033 k allocs: 1.23 kB 1.01
overhead/acrule/a+2+b 0 allocs: 0 B 0 allocs: 0 B
overhead/acrule/a+b 0.047 k allocs: 1.8 kB 0.045 k allocs: 1.77 kB 1.02
overhead/acrule/noop:Int 0 allocs: 0 B 0 allocs: 0 B
overhead/acrule/noop:Sym 0 allocs: 0 B 0 allocs: 0 B
overhead/get_degrees/large_poly 2 allocs: 32 B 2 allocs: 32 B 1
overhead/rule/noop:Int 2 allocs: 0.0625 kB 2 allocs: 0.0625 kB 1
overhead/rule/noop:Sym 2 allocs: 0.0625 kB 2 allocs: 0.0625 kB 1
overhead/rule/noop:Term 2 allocs: 0.0625 kB 2 allocs: 0.0625 kB 1
overhead/ruleset/noop:Int 0 allocs: 0 B 0 allocs: 0 B
overhead/ruleset/noop:Sym 3 allocs: 0.109 kB 3 allocs: 0.109 kB 1
overhead/ruleset/noop:Term 12 allocs: 0.391 kB 12 allocs: 0.391 kB 1
overhead/simplify/noop:Int 0 allocs: 0 B 0 allocs: 0 B
overhead/simplify/noop:Sym 0 allocs: 0 B 0 allocs: 0 B
overhead/simplify/noop:Term 0.298 k allocs: 11.5 kB 0.282 k allocs: 11.3 kB 1.02
overhead/simplify/randterm (+, *):serial 2.54 M allocs: 0.0973 GB 2.44 M allocs: 0.0957 GB 1.02
overhead/simplify/randterm (+, *):thread 2.6 M allocs: 0.255 GB 2.49 M allocs: 0.253 GB 1.01
overhead/simplify/randterm (/, *):serial 2.04 k allocs: 0.0733 MB 1.91 k allocs: 0.0713 MB 1.03
overhead/simplify/randterm (/, *):thread 2.07 k allocs: 0.0743 MB 1.95 k allocs: 0.0724 MB 1.03
overhead/substitute/a 0.233 k allocs: 8.62 kB 0.22 k allocs: 8.42 kB 1.02
overhead/substitute/a,b 0.28 k allocs: 10.3 kB 0.267 k allocs: 10.1 kB 1.02
overhead/substitute/a,b,c 0.25 k allocs: 8.8 kB 0.237 k allocs: 8.59 kB 1.02
polyform/easy_iszero 0.133 k allocs: 4.58 kB 0.127 k allocs: 4.48 kB 1.02
polyform/isone 8.28 k allocs: 0.569 MB 7.94 k allocs: 0.563 MB 1.01
polyform/isone:noop 1 allocs: 16 B 1 allocs: 16 B 1
polyform/iszero 6.86 k allocs: 0.474 MB 6.7 k allocs: 0.471 MB 1.01
polyform/iszero:noop 1 allocs: 16 B 1 allocs: 16 B 1
polyform/simplify_fractions 8.76 k allocs: 0.592 MB 8.59 k allocs: 0.589 MB 1.01
printing/large_poly 1.86 M allocs: 0.082 GB 1.86 M allocs: 0.082 GB 1
time_to_load 0.153 k allocs: 14.5 kB 0.153 k allocs: 14.5 kB 1

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 18, 2026

Benchmark Results (Julia v1)

Time benchmarks
master 72b1f61... master / 72b1f61...
arithmetic/2-arg mul 10.6 ± 0.24 μs 10.7 ± 0.26 μs 0.987 ± 0.033
arithmetic/addition 0.0724 ± 0.00091 ms 0.0698 ± 0.00076 ms 1.04 ± 0.017
arithmetic/division 25.6 ± 0.67 μs 25.2 ± 0.61 μs 1.02 ± 0.036
arithmetic/multiplication 0.0551 ± 0.0023 ms 0.0523 ± 0.0022 ms 1.05 ± 0.062
codegen/arrayop_nested/fast_toexpr 7.84 ± 0.35 μs 7.84 ± 0.38 μs 1 ± 0.066
codegen/arrayop_nested/toexpr 0.0564 ± 0.0013 ms 0.0574 ± 0.0016 ms 0.982 ± 0.035
codegen/deep_poly/deg=10:fast_toexpr 0.0722 ± 0.011 ms 0.0693 ± 0.0099 ms 1.04 ± 0.22
codegen/deep_poly/deg=10:toexpr 0.0654 ± 0.02 ms 0.0666 ± 0.02 ms 0.983 ± 0.42
codegen/deep_poly/deg=14:fast_toexpr 0.165 ± 0.029 ms 0.161 ± 0.027 ms 1.02 ± 0.25
codegen/deep_poly/deg=14:toexpr 0.12 ± 0.036 ms 0.123 ± 0.037 ms 0.975 ± 0.42
codegen/deep_poly/deg=6:fast_toexpr 31.2 ± 6 μs 30.3 ± 6 μs 1.03 ± 0.28
codegen/deep_poly/deg=6:toexpr 27.9 ± 0.84 μs 28.8 ± 1.1 μs 0.968 ± 0.047
codegen/makearray/n=100:fast_toexpr 0.112 ± 0.037 ms 0.117 ± 0.035 ms 0.964 ± 0.43
codegen/makearray/n=100:toexpr 0.584 ± 0.073 ms 0.597 ± 0.067 ms 0.977 ± 0.16
codegen/makearray/n=200:fast_toexpr 0.173 ± 0.049 ms 0.175 ± 0.052 ms 0.988 ± 0.4
codegen/makearray/n=200:toexpr 0.992 ± 0.14 ms 1.01 ± 0.13 ms 0.98 ± 0.18
codegen/makearray/n=400:fast_toexpr 0.336 ± 0.042 ms 0.333 ± 0.053 ms 1.01 ± 0.2
codegen/makearray/n=400:toexpr 1.63 ± 0.23 ms 1.66 ± 0.22 ms 0.987 ± 0.19
codegen/wide_deep_poly/fast_toexpr 0.615 ± 0.029 ms 0.615 ± 0.028 ms 1 ± 0.066
codegen/wide_deep_poly/toexpr 0.446 ± 0.014 ms 0.45 ± 0.015 ms 0.992 ± 0.046
codegen/wide_poly/n=100:fast_toexpr 0.246 ± 0.016 ms 0.238 ± 0.017 ms 1.03 ± 0.099
codegen/wide_poly/n=100:toexpr 0.257 ± 0.02 ms 0.265 ± 0.038 ms 0.97 ± 0.16
codegen/wide_poly/n=25:fast_toexpr 0.0463 ± 0.018 ms 0.0448 ± 0.016 ms 1.03 ± 0.56
codegen/wide_poly/n=25:toexpr 0.0623 ± 0.017 ms 0.0645 ± 0.016 ms 0.965 ± 0.36
codegen/wide_poly/n=50:fast_toexpr 0.109 ± 0.018 ms 0.111 ± 0.018 ms 0.983 ± 0.23
codegen/wide_poly/n=50:toexpr 0.127 ± 0.035 ms 0.131 ± 0.033 ms 0.97 ± 0.37
irstructure/search_variables/common:IRStructure 0.23 ± 0.0065 ms 0.228 ± 0.005 ms 1.01 ± 0.036
irstructure/search_variables/common:reference 1.53 ± 0.064 ms 1.53 ± 0.059 ms 0.995 ± 0.056
irstructure/search_variables/dissimilar:IRStructure 0.0637 ± 0.0025 ms 0.0653 ± 0.0013 ms 0.977 ± 0.044
irstructure/search_variables/dissimilar:reference 0.339 ± 0.0098 ms 0.353 ± 0.012 ms 0.961 ± 0.043
irstructure/substitute/IRSubstituter 7.97 ± 0.28 ms 7.6 ± 0.26 ms 1.05 ± 0.051
irstructure/substitute/reference 8.51 ± 0.36 ms 7.97 ± 0.31 ms 1.07 ± 0.062
irstructure/substitute/sparse IRSubstituter 1.27 ± 0.032 ms 1.22 ± 0.029 ms 1.04 ± 0.036
irstructure/substitute/sparse reference 1.76 ± 0.043 ms 1.71 ± 0.039 ms 1.03 ± 0.034
overhead/acrule/a+2 2.28 ± 0.061 μs 2.33 ± 0.07 μs 0.979 ± 0.039
overhead/acrule/a+2+b 0.071 ± 0.01 μs 0.07 ± 0.01 μs 1.01 ± 0.2
overhead/acrule/a+b 4.01 ± 0.1 μs 4.02 ± 0.1 μs 0.998 ± 0.035
overhead/acrule/noop:Int 30 ± 0 ns 30 ± 0 ns 1 ± 0
overhead/acrule/noop:Sym 0.06 ± 0 μs 0.06 ± 0.009 μs 1 ± 0.15
overhead/get_degrees/large_poly 0.07 ± 0 μs 0.07 ± 0.001 μs 1 ± 0.014
overhead/rule/noop:Int 0.06 ± 0.001 μs 0.07 ± 0.01 μs 0.857 ± 0.12
overhead/rule/noop:Sym 0.06 ± 0.01 μs 0.07 ± 0.01 μs 0.857 ± 0.19
overhead/rule/noop:Term 0.06 ± 0.01 μs 0.07 ± 0.01 μs 0.857 ± 0.19
overhead/ruleset/noop:Int 30 ± 0 ns 30 ± 0 ns 1 ± 0
overhead/ruleset/noop:Sym 0.291 ± 0.021 μs 0.33 ± 0.011 μs 0.882 ± 0.07
overhead/ruleset/noop:Term 1.18 ± 0.03 μs 1.15 ± 0.03 μs 1.03 ± 0.037
overhead/simplify/noop:Int 30 ± 0 ns 30 ± 0 ns 1 ± 0
overhead/simplify/noop:Sym 30 ± 10 ns 30 ± 10 ns 1 ± 0.47
overhead/simplify/noop:Term 28.5 ± 0.52 μs 28.2 ± 0.56 μs 1.01 ± 0.027
overhead/simplify/randterm (+, *):serial 0.241 ± 0.034 s 0.227 ± 0.029 s 1.06 ± 0.2
overhead/simplify/randterm (+, *):thread 0.293 ± 0.066 s 0.28 ± 0.062 s 1.05 ± 0.33
overhead/simplify/randterm (/, *):serial 0.174 ± 0.021 ms 0.17 ± 0.02 ms 1.03 ± 0.17
overhead/simplify/randterm (/, *):thread 0.187 ± 0.027 ms 0.182 ± 0.024 ms 1.03 ± 0.2
overhead/substitute/a 0.0348 ± 0.00078 ms 0.034 ± 0.00077 ms 1.02 ± 0.033
overhead/substitute/a,b 0.043 ± 0.001 ms 0.0425 ± 0.00091 ms 1.01 ± 0.032
overhead/substitute/a,b,c 0.0409 ± 0.00098 ms 0.0404 ± 0.00084 ms 1.01 ± 0.032
polyform/easy_iszero 19.2 ± 0.39 μs 19.6 ± 0.72 μs 0.983 ± 0.041
polyform/isone 0.941 ± 0.04 ms 0.936 ± 0.018 ms 1 ± 0.047
polyform/isone:noop 0.07 ± 0 μs 0.07 ± 0.001 μs 1 ± 0.014
polyform/iszero 0.824 ± 0.032 ms 0.811 ± 0.019 ms 1.02 ± 0.046
polyform/iszero:noop 0.07 ± 0.001 μs 0.07 ± 0.01 μs 1 ± 0.14
polyform/simplify_fractions 1.01 ± 0.025 ms 1.02 ± 0.028 ms 0.992 ± 0.037
printing/large_poly 0.193 ± 0.012 s 0.199 ± 0.015 s 0.968 ± 0.094
time_to_load 1.67 ± 0.045 s 1.62 ± 0.016 s 1.03 ± 0.03
Memory benchmarks
master 72b1f61... master / 72b1f61...
arithmetic/2-arg mul 0.056 k allocs: 1.78 kB 0.056 k allocs: 1.78 kB 1
arithmetic/addition 0.33 k allocs: 11.3 kB 0.33 k allocs: 11.3 kB 1
arithmetic/division 0.132 k allocs: 4.77 kB 0.132 k allocs: 4.77 kB 1
arithmetic/multiplication 0.252 k allocs: 6.5 kB 0.252 k allocs: 6.5 kB 1
codegen/arrayop_nested/fast_toexpr 0.203 k allocs: 9.53 kB 0.203 k allocs: 9.53 kB 1
codegen/arrayop_nested/toexpr 0.527 k allocs: 19.8 kB 0.527 k allocs: 19.8 kB 1
codegen/deep_poly/deg=10:fast_toexpr 1.25 k allocs: 0.0565 MB 1.25 k allocs: 0.0565 MB 1
codegen/deep_poly/deg=10:toexpr 1.41 k allocs: 0.0488 MB 1.41 k allocs: 0.0488 MB 1
codegen/deep_poly/deg=14:fast_toexpr 2.22 k allocs: 0.12 MB 2.22 k allocs: 0.12 MB 1
codegen/deep_poly/deg=14:toexpr 2.55 k allocs: 0.0908 MB 2.55 k allocs: 0.0908 MB 1
codegen/deep_poly/deg=6:fast_toexpr 0.561 k allocs: 28.9 kB 0.561 k allocs: 28.9 kB 1
codegen/deep_poly/deg=6:toexpr 0.6 k allocs: 22.2 kB 0.6 k allocs: 22.2 kB 1
codegen/makearray/n=100:fast_toexpr 1.97 k allocs: 0.0834 MB 1.97 k allocs: 0.0834 MB 1
codegen/makearray/n=100:toexpr 4.93 k allocs: 0.17 MB 4.93 k allocs: 0.17 MB 1
codegen/makearray/n=200:fast_toexpr 3.21 k allocs: 0.135 MB 3.21 k allocs: 0.135 MB 1
codegen/makearray/n=200:toexpr 8.51 k allocs: 0.293 MB 8.51 k allocs: 0.293 MB 1
codegen/makearray/n=400:fast_toexpr 6.31 k allocs: 0.268 MB 6.31 k allocs: 0.268 MB 1
codegen/makearray/n=400:toexpr 14.1 k allocs: 0.477 MB 14.1 k allocs: 0.477 MB 1
codegen/wide_deep_poly/fast_toexpr 7.43 k allocs: 0.352 MB 7.43 k allocs: 0.352 MB 1
codegen/wide_deep_poly/toexpr 9.37 k allocs: 0.327 MB 9.37 k allocs: 0.327 MB 1
codegen/wide_poly/n=100:fast_toexpr 3.5 k allocs: 0.155 MB 3.5 k allocs: 0.155 MB 1
codegen/wide_poly/n=100:toexpr 4.92 k allocs: 0.166 MB 4.92 k allocs: 0.166 MB 1
codegen/wide_poly/n=25:fast_toexpr 0.911 k allocs: 0.0388 MB 0.911 k allocs: 0.0388 MB 1
codegen/wide_poly/n=25:toexpr 1.24 k allocs: 0.0421 MB 1.24 k allocs: 0.0421 MB 1
codegen/wide_poly/n=50:fast_toexpr 1.78 k allocs: 0.0928 MB 1.78 k allocs: 0.0928 MB 1
codegen/wide_poly/n=50:toexpr 2.47 k allocs: 0.0862 MB 2.47 k allocs: 0.0862 MB 1
irstructure/search_variables/common:IRStructure 0.09 k allocs: 6.25 kB 0.09 k allocs: 6.25 kB 1
irstructure/search_variables/common:reference 5.81 k allocs: 0.0887 MB 5.81 k allocs: 0.0887 MB 1
irstructure/search_variables/dissimilar:IRStructure 0.073 k allocs: 5.33 kB 0.073 k allocs: 5.33 kB 1
irstructure/search_variables/dissimilar:reference 1.21 k allocs: 18.9 kB 1.21 k allocs: 18.9 kB 1
irstructure/substitute/IRSubstituter 0.0327 M allocs: 1.06 MB 0.0327 M allocs: 1.06 MB 1
irstructure/substitute/reference 0.0353 M allocs: 1.23 MB 0.0353 M allocs: 1.23 MB 1
irstructure/substitute/sparse IRSubstituter 3.81 k allocs: 0.126 MB 3.81 k allocs: 0.126 MB 1
irstructure/substitute/sparse reference 6.49 k allocs: 0.293 MB 6.49 k allocs: 0.293 MB 1
overhead/acrule/a+2 0.034 k allocs: 1.12 kB 0.034 k allocs: 1.12 kB 1
overhead/acrule/a+2+b 0 allocs: 0 B 0 allocs: 0 B
overhead/acrule/a+b 0.046 k allocs: 1.55 kB 0.046 k allocs: 1.55 kB 1
overhead/acrule/noop:Int 0 allocs: 0 B 0 allocs: 0 B
overhead/acrule/noop:Sym 0 allocs: 0 B 0 allocs: 0 B
overhead/get_degrees/large_poly 2 allocs: 32 B 2 allocs: 32 B 1
overhead/rule/noop:Int 2 allocs: 0.0625 kB 2 allocs: 0.0625 kB 1
overhead/rule/noop:Sym 2 allocs: 0.0625 kB 2 allocs: 0.0625 kB 1
overhead/rule/noop:Term 2 allocs: 0.0625 kB 2 allocs: 0.0625 kB 1
overhead/ruleset/noop:Int 0 allocs: 0 B 0 allocs: 0 B
overhead/ruleset/noop:Sym 3 allocs: 0.109 kB 3 allocs: 0.109 kB 1
overhead/ruleset/noop:Term 12 allocs: 0.391 kB 12 allocs: 0.391 kB 1
overhead/simplify/noop:Int 0 allocs: 0 B 0 allocs: 0 B
overhead/simplify/noop:Sym 0 allocs: 0 B 0 allocs: 0 B
overhead/simplify/noop:Term 0.284 k allocs: 9.91 kB 0.284 k allocs: 9.91 kB 1
overhead/simplify/randterm (+, *):serial 2.39 M allocs: 0.0829 GB 2.39 M allocs: 0.0829 GB 1
overhead/simplify/randterm (+, *):thread 2.55 M allocs: 0.245 GB 2.55 M allocs: 0.245 GB 1
overhead/simplify/randterm (/, *):serial 1.91 k allocs: 0.0655 MB 1.91 k allocs: 0.0655 MB 1
overhead/simplify/randterm (/, *):thread 2.05 k allocs: 0.0704 MB 2.05 k allocs: 0.0704 MB 1
overhead/substitute/a 0.172 k allocs: 6.05 kB 0.172 k allocs: 6.05 kB 1
overhead/substitute/a,b 0.223 k allocs: 7.69 kB 0.223 k allocs: 7.69 kB 1
overhead/substitute/a,b,c 0.229 k allocs: 7.81 kB 0.229 k allocs: 7.81 kB 1
polyform/easy_iszero 0.092 k allocs: 2.94 kB 0.092 k allocs: 2.94 kB 1
polyform/isone 10.9 k allocs: 0.574 MB 10.9 k allocs: 0.574 MB 1
polyform/isone:noop 1 allocs: 16 B 1 allocs: 16 B 1
polyform/iszero 8.96 k allocs: 0.48 MB 8.96 k allocs: 0.48 MB 1
polyform/iszero:noop 1 allocs: 16 B 1 allocs: 16 B 1
polyform/simplify_fractions 11.3 k allocs: 0.591 MB 11.3 k allocs: 0.591 MB 1
printing/large_poly 2.15 M allocs: 0.079 GB 2.15 M allocs: 0.079 GB 1
time_to_load 0.145 k allocs: 11 kB 0.145 k allocs: 11 kB 1

@oameye oameye changed the title simplify: fold literal complex(re, im) Terms to additive form expand: fold literal complex(re, im) Terms in to_poly! May 18, 2026
@AayushSabharwal
Copy link
Copy Markdown
Member

This seems to break a symbolic solver test. If I have time over the weekend, I'll try and take a look at it. The symbolic solvers are somewhat annoyingly capricious at times.

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.

simplify(...; expand=true) doesn't reduce diffs with literal complex(re, im) Terms

2 participants