Skip to content

Add Complex<T> and IComplexNumber<TSelf, T> (WIP — scoping question)#127331

Closed
Copilot wants to merge 1 commit intomainfrom
copilot/add-generic-complex-support
Closed

Add Complex<T> and IComplexNumber<TSelf, T> (WIP — scoping question)#127331
Copilot wants to merge 1 commit intomainfrom
copilot/add-generic-complex-support

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 23, 2026

Implement the approved API surface from #117451 for a generic Complex<T> where T : IFloatingPointIeee754<T> plus a corresponding IComplexNumber<TSelf, T> interface, and expand the non-generic Complex to implement the new interface.

Status: no code committed yet — need scoping direction before proceeding

After analyzing the approved surface against the existing implementation, the scope is substantially larger than a single surgical change and I want to confirm direction before committing code. Specifically, landing this requires:

  • A new IComplexNumber<TSelf, T> interface (generic-math family).
  • A new Complex<T> struct with ~100+ members: operators, Create/CreateFromPolarCoordinates, GetMagnitude/GetPhase, Conjugate/Reciprocal/Abs, full transcendental suite (Sqrt, Exp/ExpM1/Exp2/Exp10/…, Log/LogP1/Log2/Log10/…, Sin/Cos/Tan + *h + *Pi, Asin/Acos/Atan + *h + *Pi), Hypot, Pow, RootN, DegreesToRadians/RadiansToDegrees, MultiplyAddEstimate (5 overloads), MaxMagnitude/MinMagnitude(+Number) overloads, Is* predicates, parsing/formatting for char/byte/string, and the full INumberBase/ISignedNumber/IFloatingPointConstants/IAdditiveIdentity/IMultiplicativeIdentity explicit impls.
  • Generalizing ~2,300 lines of double-specific algorithms in Complex.cs to arbitrary T : IFloatingPointIeee754<T>, including several precomputed magic constants (s_sqrtRescaleThreshold, s_asinOverflowThreshold, s_log2, InverseOfLog10, …) and IEEE‑754 edge-case handling (NaN/±∞/signed‑zero propagation) that today rely on double‑specific values.
  • Adding the new members on non-generic Complex (Acosh/Asinh/Atanh, CosPi/SinPi/TanPi, Cbrt, DegreesToRadians/RadiansToDegrees, ExpM1/Exp2/Exp2M1/Exp10/Exp10M1, Hypot, LogP1/Log2/Log2P1/Log10P1, new Log/MaxMagnitude/MinMagnitude/*Number/Pow overloads, MultiplyAddEstimate, RootN) and the IComplexNumber<Complex, double> explicit impls.
  • Corresponding ref/System.Runtime.Numerics.cs updates and test coverage in ComplexTests / ComplexTests.GenericMath.cs.

Per repo policy any commit must compile and pass tests, so I'd rather not push a large approximation of the math.

Description

Proposed approaches (please pick one):

  • Phased — API shape first (recommended). Land IComplexNumber<TSelf, T>, the Complex<T> skeleton (layout, ctor, Real/Imaginary, arithmetic operators, Conjugate, Abs, Create/CreateFromPolarCoordinates, GetMagnitude/GetPhase, Is*, Equals/GetHashCode/ToString, generic-math explicit impls that port cleanly to T), the full ref assembly surface, and the non-generic Complex interface wiring. Defer transcendental math (Sqrt, Log*, Exp*, Asin/Acos/Atan + variants, Pow, Sinh/Cosh/Tanh, Hypot, RootN) to follow-up PRs so each port of a double-tuned algorithm to generic T can be reviewed and tested on its own.
  • Scope down. Ship only IComplexNumber<TSelf, T> + the new methods on non-generic Complex (which can reuse existing double algorithms), and leave Complex<T> for a separate PR.
  • All-in, multi-commit. Implement the full surface in one PR across many commits; expect iterative questions on algorithmic choices (e.g. how to derive T-generic equivalents of the double rescale thresholds for types like Half/float).

Example of the target shape once landed:

Complex<float> z = Complex<float>.CreateFromPolarCoordinates(2.0f, MathF.PI / 4);
float mag   = Complex<float>.GetMagnitude(z);
float phase = Complex<float>.GetPhase(z);
Complex<float> w = z * 3.0f + Complex<float>.Create(1.0f, -1.0f);

No files are modified in this PR yet — awaiting direction on scope.

Note

This PR description was authored by GitHub Copilot.

Copilot AI self-assigned this Apr 23, 2026
Copilot AI review requested due to automatic review settings April 23, 2026 17:27
Copilot AI review requested due to automatic review settings April 23, 2026 17:27
Copilot AI linked an issue Apr 23, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Add support for Complex<T> with IFloatingPointIeee754<T> Add Complex<T> and IComplexNumber<TSelf, T> (WIP — scoping question) Apr 23, 2026
Copilot AI requested a review from tannergooding April 23, 2026 17:33
@tannergooding
Copy link
Copy Markdown
Member

@copilot, lets go with a third different approach instead where we expose Complex<T> only mirroring the API surface that Complex already exposes. We can then have a follow up PR which exposes the IComplex<T> interface with the new members that require more complex implementation work on both the existing non-generic and new generic Complex types.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 23, 2026

@tannergooding I've opened a new pull request, #127333, to work on those changes. Once the pull request is ready, I'll request review from you.

@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose a new Complex<T> given where T : IFloatingPointIeee754<T>

2 participants