@@ -52,13 +52,31 @@ extension Complex: AdditiveArithmetic {
52
52
// and turn these into operators if/when we have it.
53
53
// (https://github.com/apple/swift-numerics/issues/12)
54
54
extension Complex {
55
+ /// `self` scaled by `a`.
55
56
@usableFromInline @_transparent
56
57
internal func multiplied( by a: RealType ) -> Complex {
58
+ // This can be viewed in two different ways, which are mathematically
59
+ // equivalent: either we are computing `self * Complex(a)` (i.e.
60
+ // converting `a` to be a complex value, and then using the complex
61
+ // multiplication) or we are using the scalar product of the vector
62
+ // space structure: `Complex(a*real, a*imaginary)`.
63
+ //
64
+ // Although these two interpretations are _mathematically_ equivalent,
65
+ // they will generate different representations of the point at
66
+ // infinity in general. For example, suppose `self` is represented by
67
+ // `(infinity, 0)`. Then `self * Complex(1)` would evaluate as
68
+ // `(1*infinity - 0*0, 0*infinity + 1*0) = (infinity, nan)`, but
69
+ // the vector space interpretation produces `(infinity, 0)`. This does
70
+ // not matter much, because these are two representations of the same
71
+ // semantic value, but note that one requires four multiplies and two
72
+ // additions, while the one we use requires only two real multiplications.
57
73
Complex ( x*a, y*a)
58
74
}
59
75
76
+ /// `self` unscaled by `a`.
60
77
@usableFromInline @_transparent
61
78
internal func divided( by a: RealType ) -> Complex {
79
+ // See implementation notes for `multiplied` above.
62
80
Complex ( x/ a, y/ a)
63
81
}
64
82
}
@@ -147,17 +165,23 @@ extension Complex: AlgebraicField {
147
165
/// isolated division, but if you are dividing many values by a single
148
166
/// denominator, this will often be a significant performance win.
149
167
///
150
- /// Typical use looks like this:
168
+ /// A typical use case looks something like this:
151
169
/// ```
152
170
/// func divide<T: Real>(data: [Complex<T>], by divisor: Complex<T>) -> [Complex<T>] {
153
- /// // If divisor is well-scaled, use multiply by reciprocal.
171
+ /// // If divisor is well-scaled, multiply by reciprocal.
154
172
/// if let recip = divisor.reciprocal {
155
173
/// return data.map { $0 * recip }
156
174
/// }
157
175
/// // Fallback on using division.
158
176
/// return data.map { $0 / divisor }
159
177
/// }
160
178
/// ```
179
+ ///
180
+ /// Error Bounds:
181
+ /// -
182
+ /// Unlike real types, when working with complex types, multiplying by the
183
+ /// reciprocal instead of dividing cannot change the result. If the
184
+ /// reciprocal is non-nil, the two computations are always equivalent.
161
185
@inlinable
162
186
public var reciprocal : Complex ? {
163
187
let recip = 1 / self
0 commit comments