1111
1212import RealModule
1313
14- /// A complex number represented by real and imaginary parts .
14+ /// A [ complex number](https://en.wikipedia.org/wiki/Complex_number) .
1515///
16- /// TODO: introductory text on complex numbers
17- ///
18- /// Implementation notes:
19- ///
20- /// This type does not provide heterogeneous real/complex arithmetic,
21- /// not even the natural vector-space operations like real * complex.
22- /// There are two reasons for this choice: first, Swift broadly avoids
23- /// mixed-type arithmetic when the operation can be adequately expressed
24- /// by a conversion and homogeneous arithmetic. Second, with the current
25- /// typechecker rules, it would lead to undesirable ambiguity in common
26- /// expressions (see README.md for more details).
27- ///
28- /// Unlike C's `_Complex` and C++'s `std::complex<>` types, we do not
29- /// attempt to make meaningful semantic distinctions between different
30- /// representations of infinity or NaN. Any Complex value with at least
31- /// one non-finite component is simply "non-finite". In as much as
32- /// possible, we use the semantics of the point at infinity on the
33- /// Riemann sphere for such values. This approach simplifies the number of
34- /// edge cases that need to be considered for multiplication, division, and
35- /// the elementary functions considerably.
36- ///
37- /// `.magnitude` does not return the Euclidean norm; it uses the "infinity
38- /// norm" (`max(|real|,|imaginary|)`) instead. There are two reasons for this
39- /// choice: first, it's simply faster to compute on most hardware. Second,
40- /// there exist values for which the Euclidean norm cannot be represented
41- /// (consider a number with `.real` and `.imaginary` both equal to
42- /// `RealType.greatestFiniteMagnitude`; the Euclidean norm would be
43- /// `.sqrt(2) * .greatestFiniteMagnitude`, which overflows). Using
44- /// the infinity norm avoids this problem entirely without significant
45- /// downsides. You can access the Euclidean norm using the `length`
46- /// property.
16+ /// `Complex` is an `AlgebraicField`, so it has all the normal arithmetic
17+ /// operators. It conforms to `ElementaryFunctions`, so it has all the usual
18+ /// math functions.
4719@frozen
4820public struct Complex < RealType> where RealType: Real {
4921 // A note on the `x` and `y` properties
@@ -53,11 +25,11 @@ public struct Complex<RealType> where RealType: Real {
5325 // `.real` and `.imaginary` properties, which wrap this storage and
5426 // fixup the semantics for non-finite values.
5527
56- /// The real component of the value.
28+ /// The storage for the real component of the value.
5729 @usableFromInline @inline ( __always)
5830 internal var x : RealType
5931
60- /// The imaginary part of the value.
32+ /// The storage for the imaginary part of the value.
6133 @usableFromInline @inline ( __always)
6234 internal var y : RealType
6335
@@ -95,11 +67,24 @@ extension Complex {
9567 set { y = newValue }
9668 }
9769
70+ /// The raw representation of the value.
71+ ///
72+ /// Use this when you need the underlying RealType values,
73+ /// without fixup for NaN or infinity.
74+ public var rawStorage : ( x: RealType , y: RealType ) {
75+ @_transparent
76+ get { ( x, y) }
77+ @_transparent
78+ set { ( x, y) = newValue }
79+ }
80+
9881 /// The raw representation of the real part of this value.
82+ @available ( * , deprecated, message: " Use rawStorage " )
9983 @_transparent
10084 public var _rawX : RealType { x }
10185
10286 /// The raw representation of the imaginary part of this value.
87+ @available ( * , deprecated, message: " Use rawStorage " )
10388 @_transparent
10489 public var _rawY : RealType { y }
10590}
0 commit comments