@@ -52,13 +52,31 @@ extension Complex: AdditiveArithmetic {
5252// and turn these into operators if/when we have it.
5353// (https://github.com/apple/swift-numerics/issues/12)
5454extension Complex {
55+ /// `self` scaled by `a`.
5556 @usableFromInline @_transparent
5657 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.
5773 Complex ( x*a, y*a)
5874 }
5975
76+ /// `self` unscaled by `a`.
6077 @usableFromInline @_transparent
6178 internal func divided( by a: RealType ) -> Complex {
79+ // See implementation notes for `multiplied` above.
6280 Complex ( x/ a, y/ a)
6381 }
6482}
@@ -147,17 +165,23 @@ extension Complex: AlgebraicField {
147165 /// isolated division, but if you are dividing many values by a single
148166 /// denominator, this will often be a significant performance win.
149167 ///
150- /// Typical use looks like this:
168+ /// A typical use case looks something like this:
151169 /// ```
152170 /// 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.
154172 /// if let recip = divisor.reciprocal {
155173 /// return data.map { $0 * recip }
156174 /// }
157175 /// // Fallback on using division.
158176 /// return data.map { $0 / divisor }
159177 /// }
160178 /// ```
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.
161185 @inlinable
162186 public var reciprocal : Complex ? {
163187 let recip = 1 / self
0 commit comments