Skip to content

Conversation

@ChrisRackauckas-Claude
Copy link
Contributor

Summary

Fixes #1109 - Complex number substitution error when substituting complex values into expressions with complex coefficients.

Before this fix:

using Symbolics
@variables x
p = 0.4 + 1.7im*x
substitute(p, Dict(x => 0.2 + 1.0im))
# ERROR: InexactError: Real(0.34 + 1.7im)
# or returns malformed result: 0.4 + (0.34 + 1.7im)*im

After this fix:

using Symbolics
@variables x
p = 0.4 + 1.7im*x
substitute(p, Dict(x => 0.2 + 1.0im))
# Returns: -1.2999999999999998 + 0.34im  (correct!)

Changes

  • Modified (s::SymbolicUtils.Substituter)(x::Complex{Num}) in src/complex.jl to properly handle the case where substituted values are complex constants
  • When both the real and imaginary parts become constant after substitution, properly compute the complex result using standard complex arithmetic: (a + b*im) where a and b can themselves be complex
  • Added comprehensive tests in test/complex.jl for complex value substitution

Root Cause

The previous implementation applied substitution to real and imaginary parts separately:

Complex{Num}(s(real(x)), s(imag(x)))

This works fine for real substitutions, but when substituting a complex value like 0.2 + 1.0im into 1.7im*x:

  • The imaginary part 1.7*x becomes 0.34 + 1.7im (a complex constant wrapped in Num)
  • Constructing Complex{Num}(0.4, 0.34+1.7im) creates an invalid structure

The fix detects when both parts are constants and properly evaluates the complex expression.

Test plan

🤖 Generated with Claude Code

…olics#1109)

When substituting a complex value into a Complex{Num} expression like
`0.4 + 1.7im*x`, the Substituter was applying substitution to real
and imaginary parts separately without properly handling the case where
the substituted values themselves are complex.

For example, substituting `x => 0.2 + 1.0im` into `0.4 + 1.7im*x` would:
- Apply substitution to real part: 0.4 (unchanged)
- Apply substitution to imaginary part: 1.7*x becomes 0.34 + 1.7im (complex!)
- Construct Complex{Num}(0.4, 0.34+1.7im) which is malformed

The fix detects when both substituted parts are constants (potentially
complex), and properly computes the final complex result using the
formula: (a + b*im) where a, b can be complex.

Added tests for:
- Basic complex substitution (the original issue case)
- Both real and imag parts containing the variable
- Real value substitution (regression test)
- Two-variable complex substitution
- Symbolics.value extraction
- simplify and expand operations

Fixes JuliaSymbolics#1109

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@ChrisRackauckas
Copy link
Member

Looks pretty reasonable to me.

@codecov-commenter
Copy link

codecov-commenter commented Dec 5, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 86.66667% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.81%. Comparing base (9df21d7) to head (389dd6c).
⚠️ Report is 9 commits behind head on master.

Files with missing lines Patch % Lines
src/complex.jl 86.66% 2 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1710      +/-   ##
==========================================
+ Coverage   80.10%   80.81%   +0.70%     
==========================================
  Files          55       55              
  Lines        5187     5196       +9     
==========================================
+ Hits         4155     4199      +44     
+ Misses       1032      997      -35     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Error with substitution of a Complex{Float64} value in polynomial with complex coefficients

3 participants