11
11
12
12
import RealModule
13
13
14
- /// A complex number represented by real and imaginary parts .
14
+ /// A [ complex number](https://en.wikipedia.org/wiki/Complex_number) .
15
15
///
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.
47
19
@frozen
48
20
public struct Complex < RealType> where RealType: Real {
49
21
// A note on the `x` and `y` properties
@@ -53,11 +25,11 @@ public struct Complex<RealType> where RealType: Real {
53
25
// `.real` and `.imaginary` properties, which wrap this storage and
54
26
// fixup the semantics for non-finite values.
55
27
56
- /// The real component of the value.
28
+ /// The storage for the real component of the value.
57
29
@usableFromInline @inline ( __always)
58
30
internal var x : RealType
59
31
60
- /// The imaginary part of the value.
32
+ /// The storage for the imaginary part of the value.
61
33
@usableFromInline @inline ( __always)
62
34
internal var y : RealType
63
35
@@ -95,11 +67,24 @@ extension Complex {
95
67
set { y = newValue }
96
68
}
97
69
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
+
98
81
/// The raw representation of the real part of this value.
82
+ @available ( * , deprecated, message: " Use rawStorage " )
99
83
@_transparent
100
84
public var _rawX : RealType { x }
101
85
102
86
/// The raw representation of the imaginary part of this value.
87
+ @available ( * , deprecated, message: " Use rawStorage " )
103
88
@_transparent
104
89
public var _rawY : RealType { y }
105
90
}
0 commit comments