Skip to content

Fix complex num power #1741

Open
volkerkarle wants to merge 3 commits intoJuliaSymbolics:masterfrom
volkerkarle:fix-complex-num-power
Open

Fix complex num power #1741
volkerkarle wants to merge 3 commits intoJuliaSymbolics:masterfrom
volkerkarle:fix-complex-num-power

Conversation

@volkerkarle
Copy link

As before in #1740, but with the proposal of @AayushSabharwal.

Complex{Num}^Real operations were failing with UndefVarError: Pow not defined because the method in solver/main.jl referenced non-existent Symbolics.Pow.

Changes

  • Add ^(::Complex{Num}, ::Real) method using polar form in src/num.jl
  • Fix ^(::Complex{T}, ::Num) in solver/main.jl to use type-stable Complex{Num} constructor
  • Add tests for Complex{Num} power operations including shape inference

Why polar form?

The polar form implementation r^p * (cos(p*θ) + i*sin(p*θ)) avoids boolean conditionals that fail with symbolic values.

Fixes

Enables Cardano's cubic formula and similar operations requiring complex symbolic intermediate values.

Complex{Num}^Real operations were failing with 'UndefVarError: Pow not defined'
because the method in solver/main.jl referenced non-existent Symbolics.Pow.

Changes:
- Add ^(::Complex{Num}, ::Real) method using polar form in src/num.jl
- Fix ^(::Complex{T}, ::Num) in solver/main.jl to use SymbolicUtils.term
- Add tests for Complex{Num} power operations in test/complex.jl

The polar form implementation avoids boolean conditionals that fail with
symbolic values, using r^p * (cos(p*θ) + i*sin(p*θ)) instead.
…e tests

- Replace Symbolics.wrap with Complex{Num} for type stability in solver/main.jl
- Expand Rational method to use polar form directly (preserves exact Rational values)
- Add shape inference tests for Complex{T}^Num as requested

The Rational method is needed to resolve ambiguity between:
- ^(::Complex{Num}, ::Real) (our method)
- Base.^(::Complex{T}, ::Rational) where T<:Real (Base's method)
@codecov-commenter
Copy link

codecov-commenter commented Jan 4, 2026

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

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.66%. Comparing base (e1d8802) to head (082af5d).
⚠️ Report is 38 commits behind head on master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff             @@
##           master    #1741       +/-   ##
===========================================
+ Coverage   22.92%   80.66%   +57.73%     
===========================================
  Files          55       56        +1     
  Lines        5082     5337      +255     
===========================================
+ Hits         1165     4305     +3140     
+ Misses       3917     1032     -2885     

☔ 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.

@AayushSabharwal
Copy link
Member

Could you please rebase this PR? While I'm all but certain tests will pass with the latest changes, it would be nice to make sure. I'll merge as soon as they do.

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.

3 participants