Skip to content

Add Complex<T> generic struct mirroring Complex API surface#127333

Draft
Copilot wants to merge 4 commits intocopilot/add-generic-complex-supportfrom
copilot/sub-pr-127331
Draft

Add Complex<T> generic struct mirroring Complex API surface#127333
Copilot wants to merge 4 commits intocopilot/add-generic-complex-supportfrom
copilot/sub-pr-127331

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 23, 2026

Implements a generic Complex<T> where T : IFloatingPointIeee754<T>, IMinMaxValue<T> that mirrors the full public API of the existing Complex struct, as scoped by @tannergooding. The IComplexNumber<TSelf, T> interface and new members on non-generic Complex are deferred to a follow-up PR.

Description

New type: Complex<T>

Direct generic port of Complex in a new Complex.Generic.cs source file:

  • Arithmetic operators: all Complex<T>×Complex<T>, T×Complex<T>, and Complex<T>×T overloads for +, -, *, /, unary -/+, ++, --
  • Static fields: Zero, One, ImaginaryOne, NaN, Infinity
  • Properties: Real, Imaginary
  • Instance methods: GetMagnitude(), GetPhase() (per approved API surface — exposed as named methods rather than properties)
  • Math: Abs, Conjugate, Reciprocal, Log/Log10, Exp, Sqrt, Pow, Sin/Cos/Tan, Sinh/Cosh/Tanh, Asin/Acos/Atan, FromPolarCoordinates
  • Predicates: full Is* set matching INumberBase<T>
  • Generic math: implements INumberBase<Complex<T>>, ISignedNumber<Complex<T>>, IAdditiveIdentity, IMultiplicativeIdentity, IEquality/Comparison operators
  • Parsing / formatting: full IParsable, ISpanParsable, IUtf8SpanParsable, ISpanFormattable, IUtf8SpanFormattable
  • Conversions: CreateChecked/CreateSaturating/CreateTruncating each delegate to the matching T.TryConvertFrom{Checked,Saturating,Truncating} so saturation/truncation semantics are correct per mode

Constraint rationale

IMinMaxValue<T> is added alongside IFloatingPointIeee754<T> because the ported Sqrt and Asin_Internal algorithms compute per-instantiation overflow thresholds from T.MaxValue (e.g., s_sqrtRescaleThreshold, s_asinOverflowThreshold). Every practical floating-point type (float, double, Half, BFloat16, NFloat) implements both interfaces.

Shared constants

Complex.DefaultNumberStyle and Complex.InvalidNumberStyles are promoted from private to internal in Complex.cs so that Complex<T> can reference them directly rather than maintaining duplicate definitions.

Code sharing between Complex and Complex<T>

To ensure the non-generic Complex and Complex<T> stay in sync, all Complex methods with more than 4 lines of implementation now delegate to the corresponding Complex<double> method. This covers:

  • Mixed-type operators: operator *(Complex, double), operator *(double, Complex), and all three / overloads
  • Trig/math functions: Asin, Acos, Tan, Sqrt, Pow(Complex, Complex)
  • Magnitude comparisons: MaxMagnitude, MinMagnitude
  • Formatting: ToString(string?, IFormatProvider?)

As a result, the now-unused private fields (s_sqrtRescaleThreshold, s_asinOverflowThreshold, s_log2) and helpers (Log1P, Asin_Internal) are removed from Complex.cs.

Example usage

Complex<float> z = Complex<float>.FromPolarCoordinates(2.0f, MathF.PI / 4);
float mag   = z.GetMagnitude();
float phase = z.GetPhase();
Complex<float> w = z * 3.0f + new Complex<float>(1.0f, -1.0f);
var product = Complex<float>.Sin(z) * Complex<double>.Exp(new Complex<double>(0, Math.PI));

Files changed

File Change
src/System/Numerics/Complex.cs Promote DefaultNumberStyle and InvalidNumberStyles to internal; delegate long methods to Complex<double>
src/System/Numerics/Complex.Generic.cs New — full Complex<T> implementation with GetMagnitude()/GetPhase() methods
src/System.Runtime.Numerics.csproj Add new file to build
ref/System.Runtime.Numerics.cs Add Complex<T> public surface to ref assembly
tests/ComplexTests.cs Add ComplexTests_Generic with 37 tests

Note

This PR description was authored by GitHub Copilot.

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

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/6ae7397b-1811-41d7-a5d8-55ef50af02cd

Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
Copilot AI changed the title [WIP] Add Complex<T> and IComplexNumber<TSelf, T> API surface Add Complex<T> generic struct mirroring Complex API surface Apr 23, 2026
Copilot AI requested a review from tannergooding April 23, 2026 18:27
Comment thread src/libraries/System.Runtime.Numerics/src/System/Numerics/Complex.Generic.cs Outdated
Comment thread src/libraries/System.Runtime.Numerics/src/System/Numerics/Complex.Generic.cs Outdated
…se in Complex<T>

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/e09763ab-e8b2-4ccf-8029-2cc8c6dbd47a

Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
… Complex.cs methods >4 lines to Complex<double>

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/4edc4c27-249d-40a9-92b4-fda815b84714

Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
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.

2 participants