expand: fold literal complex(re, im) Terms in to_poly!#922
Open
oameye wants to merge 2 commits into
Open
Conversation
`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.
Contributor
Benchmark Results (Julia vlts)Time benchmarks
Memory benchmarks
|
Contributor
Benchmark Results (Julia v1)Time benchmarks
Memory benchmarks
|
complex(re, im) Terms to additive formcomplex(re, im) Terms in to_poly!
AayushSabharwal
approved these changes
May 19, 2026
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #921.
Term(complex, [re, im])(fromSymbolics.unwrap(::Complex{Num})) has no case into_poly!, soexpandtreats it as an opaquePolyVar. Identically-zero diffs that contain one don't reduce:Fix is a single case in
to_poly!: treatcomplex(re, im)asre + 1im * impolynomially.I originally tried this as a
simplifyrule but that broke downstream tests (Symbolics overloads, ODE solver) because theTerm(complex, ...)literal is also the shapeBase.real/imag/conjpattern-match for O(1) extraction and the shapeComplex{Num}round-trips through. Keeping the rewrite local toto_poly!leaves that canonical form alone.