Repository navigation
Add Complex<T> generic struct mirroring Complex API surface - #130414
Conversation
…se in Complex<T> Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/e09763ab-e8b2-4ccf-8029-2cc8c6dbd47a Co-authored-by: tannergooding <[email protected]>
… 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 <[email protected]>
…e to Complex<T>; delegate Complex explicit impls to them Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/423a9dba-89a1-41d2-8884-5915b7ffc74d Co-authored-by: tannergooding <[email protected]>
…-type conversion; share via Complex.TryConvertFrom delegation Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/0b63e9f7-7efe-4068-b899-dfda38c4dc5a Co-authored-by: tannergooding <[email protected]>
…T>; replace Complex.cs TryConvertTo* chains with delegation Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/ee27c47e-3787-4c63-9823-23e289245e0e Co-authored-by: tannergooding <[email protected]>
…s to static properties in Complex<T>; remove redundant INumberBase.One/Zero explicit impls Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/4daa6a5a-87a9-46b6-b055-56ce91e228a2 Co-authored-by: tannergooding <[email protected]>
|
Tagging subscribers to this area: @dotnet/area-system-numerics |
There was a problem hiding this comment.
Pull request overview
This PR adds a new public generic complex-number type System.Numerics.Complex<T> and updates the existing non-generic Complex implementation to delegate several operations/conversions to Complex<double>, along with adding initial unit test coverage for the generic type.
Changes:
- Introduces new public
Complex<T>implementation (Complex.Generic.cs) and wires it into the build. - Updates
Complexto expose shared parsing styles internally and to delegate several operators/math/conversion paths toComplex<double>. - Extends tests with a new
ComplexTests_Genericsuite validating basic construction, arithmetic, parsing, predicates, and conversions.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Runtime.Numerics/src/System/Numerics/Complex.Generic.cs | New Complex<T> implementation and generic-math/parsing/formatting/conversion helpers. |
| src/libraries/System.Runtime.Numerics/src/System/Numerics/Complex.cs | Makes parse styles internal and redirects several Complex operations to Complex<double>. |
| src/libraries/System.Runtime.Numerics/src/System.Runtime.Numerics.csproj | Includes the new Complex.Generic.cs in compilation. |
| src/libraries/System.Runtime.Numerics/ref/System.Runtime.Numerics.cs | Adds the Complex<T> public surface to the ref assembly. |
| src/libraries/System.Runtime.Numerics/tests/ComplexTests.cs | Adds ComplexTests_Generic coverage for Complex<T>. |
Copilot's findings
- Files reviewed: 5/5 changed files
- Comments generated: 2
|
This probably looks duplicative, and that's because it is. Turns out that copilot had #127333 target a random branch it created that wasn't The PR is otherwise unchanged, aside from the merge commit and resolving the "conflicts" that occurred in the interim (namely the AllowInvalidTrailingCharacter support). |
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 properties**: `Zero`, `One`, `ImaginaryOne`, `NaN`, `Infinity` (exposed as `static` properties per the approved API surface; `INumberBase.One` and `INumberBase.Zero` bind implicitly to the matching public properties) - **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` use `internal` `TryConvertFrom*Core` and `TryConvertTo*Core` helpers that correctly chain same-type, cross-type (`Complex` ↔ `Complex<T>`), `BigInteger`, and general numeric type special cases 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?)` - Explicit interface implementations `MaxMagnitudeNumber`, `MinMagnitudeNumber`, `MultiplyAddEstimate`: each has an `internal static` implementation on `Complex<T>`; `Complex<T>`'s explicit interface implementation delegates to it, and `Complex`'s explicit interface implementation calls through `Complex<double>` - Conversion helpers `TryConvertFrom*`: `Complex<T>` exposes `internal static TryConvertFromCheckedCore`, `TryConvertFromSaturatingCore`, `TryConvertFromTruncatingCore` that implement the full conversion logic (same-type, `Complex`→`Complex<T>`, signed types via `T.TryConvertFrom*`, unsigned/decimal types via `TOther.TryConvertTo*<T>`); `Complex.TryConvertFrom` replaces its long explicit type chain with a `Complex<double>` special case followed by delegation to `Complex<double>.TryConvertFromCheckedCore` - Conversion helpers `TryConvertTo*`: `Complex<T>` exposes `internal static TryConvertToCheckedCore`, `TryConvertToSaturatingCore`, `TryConvertToTruncatingCore` that implement the full conversion logic (same-type fast path returning real/NaN, `Complex` output via `double.Create*`, `BigInteger` output with appropriate imaginary-part handling, general types via `T.TryConvertTo*` with fallback to `TOther.TryConvertFrom*<T>`); `Complex`'s three long `TryConvertTo*` chains are replaced with a `Complex<double>` output special case followed by delegation to `Complex<double>.TryConvertTo*Core` As a result, the now-unused private fields (`s_sqrtRescaleThreshold`, `s_asinOverflowThreshold`, `s_log2`) and helpers (`Log1P`, `Asin_Internal`) are removed from `Complex.cs`. ### Cross-type conversion support Both types now correctly support bi-directional conversion: - `Complex.CreateChecked(new Complex<double>(1, 2))` — `Complex` accepts `Complex<double>` input - `Complex<float>.CreateChecked(new Complex(1, 2))` — `Complex<T>` accepts `Complex` (non-generic) input, with mode-appropriate component conversion - `int.CreateChecked(new Complex<double>(5, 0))` — real types can be extracted from `Complex<T>` via `TryConvertToCheckedCore` - `int.CreateChecked(new Complex(5, 0))` — real types can be extracted from `Complex` via delegation to `Complex<double>.TryConvertToCheckedCore` ### Example usage ```csharp 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)); // Cross-type conversions work in both directions: Complex c = Complex.CreateChecked(new Complex<double>(3, 4)); Complex<float> cf = Complex<float>.CreateChecked(new Complex(3, 4)); int n = int.CreateChecked(new Complex<double>(5, 0)); // Static properties: var zero = Complex<float>.Zero; var one = Complex<double>.One; ``` ### Files changed | File | Change | |---|---| | `src/System/Numerics/Complex.cs` | Promote `DefaultNumberStyle` and `InvalidNumberStyles` to `internal`; delegate long methods to `Complex<double>`; replace `TryConvertFrom`/`TryConvertTo*` chains with `Complex<double>.*Core` delegation | | `src/System/Numerics/Complex.Generic.cs` | New — full `Complex<T>` implementation with `static` properties for `Zero`/`One`/`ImaginaryOne`/`NaN`/`Infinity`; `GetMagnitude()`/`GetPhase()` methods; `internal static` shared implementations for `MaxMagnitudeNumber`, `MinMagnitudeNumber`, `MultiplyAddEstimate`, `TryConvertFrom*Core`, and `TryConvertTo*Core` | | `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. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: tannergooding <[email protected]>
Implements a generic
Complex<T> where T : IFloatingPointIeee754<T>, IMinMaxValue<T>that mirrors the full public API of the existingComplexstruct, as scoped by @tannergooding. TheIComplexNumber<TSelf, T>interface and new members on non-genericComplexare deferred to a follow-up PR.Description
New type:
Complex<T>Direct generic port of
Complexin a newComplex.Generic.cssource file:Complex<T>×Complex<T>,T×Complex<T>, andComplex<T>×Toverloads for+,-,*,/, unary-/+,++,--Zero,One,ImaginaryOne,NaN,Infinity(exposed asstaticproperties per the approved API surface;INumberBase.OneandINumberBase.Zerobind implicitly to the matching public properties)Real,ImaginaryGetMagnitude(),GetPhase()(per approved API surface — exposed as named methods rather than properties)Abs,Conjugate,Reciprocal,Log/Log10,Exp,Sqrt,Pow,Sin/Cos/Tan,Sinh/Cosh/Tanh,Asin/Acos/Atan,FromPolarCoordinatesIs*set matchingINumberBase<T>INumberBase<Complex<T>>,ISignedNumber<Complex<T>>,IAdditiveIdentity,IMultiplicativeIdentity,IEquality/ComparisonoperatorsIParsable,ISpanParsable,IUtf8SpanParsable,ISpanFormattable,IUtf8SpanFormattableCreateChecked/CreateSaturating/CreateTruncatinguseinternalTryConvertFrom*CoreandTryConvertTo*Corehelpers that correctly chain same-type, cross-type (Complex↔Complex<T>),BigInteger, and general numeric type special cases per modeConstraint rationale
IMinMaxValue<T>is added alongsideIFloatingPointIeee754<T>because the portedSqrtandAsin_Internalalgorithms compute per-instantiation overflow thresholds fromT.MaxValue(e.g.,s_sqrtRescaleThreshold,s_asinOverflowThreshold). Every practical floating-point type (float,double,Half,BFloat16,NFloat) implements both interfaces.Shared constants
Complex.DefaultNumberStyleandComplex.InvalidNumberStylesare promoted fromprivatetointernalinComplex.csso thatComplex<T>can reference them directly rather than maintaining duplicate definitions.Code sharing between
ComplexandComplex<T>To ensure the non-generic
ComplexandComplex<T>stay in sync, allComplexmethods with more than 4 lines of implementation now delegate to the correspondingComplex<double>method. This covers:operator *(Complex, double),operator *(double, Complex), and all three/overloadsAsin,Acos,Tan,Sqrt,Pow(Complex, Complex)MaxMagnitude,MinMagnitudeToString(string?, IFormatProvider?)MaxMagnitudeNumber,MinMagnitudeNumber,MultiplyAddEstimate: each has aninternal staticimplementation onComplex<T>;Complex<T>'s explicit interface implementation delegates to it, andComplex's explicit interface implementation calls throughComplex<double>TryConvertFrom*:Complex<T>exposesinternal static TryConvertFromCheckedCore,TryConvertFromSaturatingCore,TryConvertFromTruncatingCorethat implement the full conversion logic (same-type,Complex→Complex<T>, signed types viaT.TryConvertFrom*, unsigned/decimal types viaTOther.TryConvertTo*<T>);Complex.TryConvertFromreplaces its long explicit type chain with aComplex<double>special case followed by delegation toComplex<double>.TryConvertFromCheckedCoreTryConvertTo*:Complex<T>exposesinternal static TryConvertToCheckedCore,TryConvertToSaturatingCore,TryConvertToTruncatingCorethat implement the full conversion logic (same-type fast path returning real/NaN,Complexoutput viadouble.Create*,BigIntegeroutput with appropriate imaginary-part handling, general types viaT.TryConvertTo*with fallback toTOther.TryConvertFrom*<T>);Complex's three longTryConvertTo*chains are replaced with aComplex<double>output special case followed by delegation toComplex<double>.TryConvertTo*CoreAs a result, the now-unused private fields (
s_sqrtRescaleThreshold,s_asinOverflowThreshold,s_log2) and helpers (Log1P,Asin_Internal) are removed fromComplex.cs.Cross-type conversion support
Both types now correctly support bi-directional conversion:
Complex.CreateChecked(new Complex<double>(1, 2))—ComplexacceptsComplex<double>inputComplex<float>.CreateChecked(new Complex(1, 2))—Complex<T>acceptsComplex(non-generic) input, with mode-appropriate component conversionint.CreateChecked(new Complex<double>(5, 0))— real types can be extracted fromComplex<T>viaTryConvertToCheckedCoreint.CreateChecked(new Complex(5, 0))— real types can be extracted fromComplexvia delegation toComplex<double>.TryConvertToCheckedCoreExample usage
Files changed
src/System/Numerics/Complex.csDefaultNumberStyleandInvalidNumberStylestointernal; delegate long methods toComplex<double>; replaceTryConvertFrom/TryConvertTo*chains withComplex<double>.*Coredelegationsrc/System/Numerics/Complex.Generic.csComplex<T>implementation withstaticproperties forZero/One/ImaginaryOne/NaN/Infinity;GetMagnitude()/GetPhase()methods;internal staticshared implementations forMaxMagnitudeNumber,MinMagnitudeNumber,MultiplyAddEstimate,TryConvertFrom*Core, andTryConvertTo*Coresrc/System.Runtime.Numerics.csprojref/System.Runtime.Numerics.csComplex<T>public surface to ref assemblytests/ComplexTests.csComplexTests_Genericwith 37 testsNote
This PR description was authored by GitHub Copilot.