Fix complex value substitution in Complex{Num} expressions #1710
+67
−9
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.
Summary
Fixes #1109 - Complex number substitution error when substituting complex values into expressions with complex coefficients.
Before this fix:
After this fix:
Changes
(s::SymbolicUtils.Substituter)(x::Complex{Num})insrc/complex.jlto properly handle the case where substituted values are complex constants(a + b*im)whereaandbcan themselves be complextest/complex.jlfor complex value substitutionRoot Cause
The previous implementation applied substitution to real and imaginary parts separately:
This works fine for real substitutions, but when substituting a complex value like
0.2 + 1.0iminto1.7im*x:1.7*xbecomes0.34 + 1.7im(a complex constant wrapped inNum)Complex{Num}(0.4, 0.34+1.7im)creates an invalid structureThe fix detects when both parts are constants and properly evaluates the complex expression.
Test plan
Symbolics.value,simplify, andexpandwork on resultstest/complex.jltests pass🤖 Generated with Claude Code