Complex

A complex number represented by real and imaginary parts.

Complex.swift:48
@frozen struct Complex<RealType> where RealType : Real

TODO: introductory text on complex numbers

Implementation notes:

This type does not provide heterogeneous real/complex arithmetic, not even the natural vector-space operations like real * complex. There are two reasons for this choice: first, Swift broadly avoids mixed-type arithmetic when the operation can be adequately expressed by a conversion and homogeneous arithmetic. Second, with the current typechecker rules, it would lead to undesirable ambiguity in common expressions (see README.md for more details).

Unlike C’s _Complex and C++’s std::complex<> types, we do not attempt to make meaningful semantic distinctions between different representations of infinity or NaN. Any Complex value with at least one non-finite component is simply “non-finite”. In as much as possible, we use the semantics of the point at infinity on the Riemann sphere for such values. This approach simplifies the number of edge cases that need to be considered for multiplication, division, and the elementary functions considerably.

.magnitude does not return the Euclidean norm; it uses the “infinity norm” (max(|real|,|imaginary|)) instead. There are two reasons for this choice: first, it’s simply faster to compute on most hardware. Second, there exist values for which the Euclidean norm cannot be represented (consider a number with .real and .imaginary both equal to RealType.greatestFiniteMagnitude; the Euclidean norm would be .sqrt(2) * .greatestFiniteMagnitude, which overflows). Using the infinity norm avoids this problem entirely without significant downsides. You can access the Euclidean norm using the length property.