-
Notifications
You must be signed in to change notification settings - Fork 158
Various documentation improvements. #265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
5d84256
129e496
d2f6811
95e9441
4506917
5b34639
5cc83cc
2361b49
af0352e
acbf425
9c54880
c3757dc
61a8b91
b43c10c
8b2fd7b
2213879
2c19b7b
aa90164
b3fe435
1e94d23
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# ``Complex`` | ||
|
||
A Complex number type that conforms to `AlgebraicField` | ||
(so all the normal arithmetic operations are available) and | ||
`ElementaryFunctions` (so all the usual math functions are | ||
available). | ||
stephentyrone marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
A `Complex` value is represented with two `RealType` values, corresponding to | ||
the real and imaginary parts of the number. | ||
|
||
You can access these Cartesian components using the real and imaginary | ||
properties. | ||
|
||
```swift | ||
let z = Complex(1,-1) // 1 - i | ||
let re = z.real // 1 | ||
let im = z.imaginary // -1 | ||
``` | ||
|
||
All `Complex` numbers with a non-finite component are treated as a single | ||
"point at infinity," with infinite magnitude and indeterminant phase. Thus, | ||
the real and imaginary parts of an infinity are nan. | ||
|
||
```swift | ||
let w = Complex<Double>.infinity | ||
w == -w // true | ||
let re = w.real // .nan | ||
let im = w.imag // .nan | ||
``` | ||
|
||
See <doc:Infinity> for more details. | ||
|
||
The ``magnitude`` property of a complex number is the infinity norm of the | ||
value (a.k.a. “maximum norm” or “Чебышёв norm”). To get the two norm (a.k.a | ||
"Euclidean norm"), use the ``length`` property. See <doc:Magnitude> for more | ||
details. | ||
|
||
## Topics | ||
|
||
### Real and imaginary parts | ||
|
||
- ``real`` | ||
- ``imaginary`` | ||
- ``rawStorage`` | ||
- ``init(_:_:)`` | ||
- ``init(_:)-5aesj`` | ||
- ``init(imaginary:)`` | ||
|
||
### Phase, length and magnitude | ||
|
||
- ``magnitude`` | ||
- ``length`` | ||
- ``lengthSquared`` | ||
- ``normalized`` | ||
- ``phase`` | ||
- ``polar`` | ||
- ``init(length:phase:)`` | ||
|
||
### Scaling by real numbers | ||
- ``multiplied(by:)`` | ||
- ``divided(by:)`` | ||
|
||
### Complex-specific operations | ||
- ``conjugate`` | ||
|
||
### Classification | ||
- ``isZero`` | ||
- ``isSubnormal`` | ||
- ``isNormal`` | ||
- ``isFinite`` |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,88 @@ | ||||||||||||||
# ``ComplexModule`` | ||||||||||||||
|
||||||||||||||
Types and operations for working with complex numbers. | ||||||||||||||
|
||||||||||||||
## Overview | ||||||||||||||
|
||||||||||||||
### Representation | ||||||||||||||
|
||||||||||||||
The `Complex` type is generic over an associated `RealType`; complex numbers | ||||||||||||||
are represented as two `RealType` values, the real and imaginary parts of the | ||||||||||||||
number. | ||||||||||||||
|
||||||||||||||
``` | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For broader compatibility with various Markdown viewer implementations, would it be possible to add an extra line before and after such code blocks? |
||||||||||||||
let z = Complex<Double>(1, 2) | ||||||||||||||
let re = z.real | ||||||||||||||
let im = z.imaginary | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
### Memory layout | ||||||||||||||
|
||||||||||||||
A `Complex` value is stored as two `RealType` values arranged consecutively | ||||||||||||||
in memory. Thus it has the same memory layout as: | ||||||||||||||
- A Fortran complex value built on the corresponding real type (as used | ||||||||||||||
by BLAS and LAPACK). | ||||||||||||||
- A C struct with real and imaginary parts and nothing else (as used by | ||||||||||||||
computational libraries predating C99). | ||||||||||||||
- A C99 `_Complex` value built on the corresponding real type. | ||||||||||||||
- A C++ `std::complex` value built on the corresponding real type. | ||||||||||||||
Functions taking complex arguments in these other languages are not | ||||||||||||||
automatically converted on import, but you can safely write shims that | ||||||||||||||
map them into Swift types by converting pointers. | ||||||||||||||
|
||||||||||||||
### Real-Complex arithmetic | ||||||||||||||
|
||||||||||||||
Because the real numbers are a subset of the complex numbers, many | ||||||||||||||
languages support arithmetic with mixed real and complex operands. | ||||||||||||||
For example, C allows the following: | ||||||||||||||
|
||||||||||||||
```c | ||||||||||||||
#include <complex.h> | ||||||||||||||
double r = 1; | ||||||||||||||
double complex z = CMPLX(0, 2); // 2i | ||||||||||||||
double complex w = r + z; // 1 + 2i | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
The `Complex` type does not provide such mixed operators: | ||||||||||||||
|
||||||||||||||
```swift | ||||||||||||||
let r = 1.0 | ||||||||||||||
let z = Complex(imaginary: 2.0) | ||||||||||||||
let w = r + z // error: binary operator '+' cannot be applied to operands of type 'Double' and 'Complex<Double>' | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
In order to write the example from C above in Swift, you have to perform an | ||||||||||||||
explicit conversion: | ||||||||||||||
|
||||||||||||||
```swift | ||||||||||||||
let r = 1.0 | ||||||||||||||
let z = Complex(imaginary: 2.0) | ||||||||||||||
let w = Complex(r) + z // OK | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
There are two reasons for this choice. Most importantly, Swift generally avoids | ||||||||||||||
mixed-type arithmetic. Second, if we _did_ provide such heterogeneous operators, | ||||||||||||||
it would lead to undesirable behavior in common expressions when combined with | ||||||||||||||
literal type inference. Consider the following example: | ||||||||||||||
|
||||||||||||||
```swift | ||||||||||||||
let a: Double = 1 | ||||||||||||||
let b = 2*a | ||||||||||||||
Comment on lines
+69
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is easier to follow along the arithmetic examples (especially the next one), if this unsupported heterogeneous operator example would re-use the example from C above (and below) – I may be wrong.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The point is to provide an example that would break if we did have a heterogeneous operator. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right - my example is misleading. Does it make sense to adjust the other two examples then, and make them use multiplication over addition? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to rearrange stuff to clarify it a little bit, might revisit later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM! |
||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
`b` ought to have type `Double`, but if we did have a Complex-by-Real `*` | ||||||||||||||
operation, `2*a` would either be ambiguous (if there were no type context), | ||||||||||||||
or be inferred to have type `Complex<Double>` (if the expression appeared | ||||||||||||||
in the context of an extension defined on `Complex`). | ||||||||||||||
|
||||||||||||||
Note that we _do_ provide heterogeneous multiplication and division by a real | ||||||||||||||
value, spelled as ``Complex/divided(by:)`` and ``Complex/multiplied(by:)`` | ||||||||||||||
to avoid ambiguity. | ||||||||||||||
|
||||||||||||||
```swift | ||||||||||||||
let z = Complex<Double>(1,3) | ||||||||||||||
let w = z.multiplied(by: 2) | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
These operations are generally more efficient than converting the scale to | ||||||||||||||
a complex number and then using `*` or `/`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Zero and infinity | ||
|
||
Semantics of `Complex` zero and infinity values, and important considerations | ||
when porting code from other languages. | ||
|
||
## Overview | ||
|
||
Unlike C and C++'s complex types, `Complex` does not attempt to make a | ||
semantic distinction between different infinity and NaN values. Any `Complex` | ||
datum with a non-finite component is treated as the "point at infinity" on | ||
the Riemann sphere--a value with infinite magnitude and unspecified phase. | ||
|
||
As a consequence, all values with either component infinite or NaN compare | ||
equal, and hash the same. Similarly, all zero values compare equal and hash | ||
the same. | ||
|
||
### Rationale | ||
|
||
This choice has some drawbacks,¹ but also some significant advantages. | ||
In particular, complex multiplication is the most common operation performed | ||
with a complex type, and one would like to be able to use the usual naive | ||
arithmetic implementation, consisting of four real multiplications and two | ||
real additions: | ||
|
||
``` | ||
(a + bi) * (c + di) = (ac - bd) + (ad + bc)i | ||
``` | ||
|
||
`Complex` can use this implementation, because we do not differentiate between | ||
infinities and NaN values. C and C++, by contrast, cannot use this | ||
implementation by default, because, for example: | ||
|
||
``` | ||
(1 + ∞i) * (0 - 2i) = (1*0 - ∞*(-2)) + (1*(-2) + ∞*0)i | ||
= (0 - ∞) + (-2 + nan)i | ||
= -∞ + nan i | ||
``` | ||
|
||
`Complex` treats this as "infinity", which is the correct result. C and C++ | ||
treat it as a nan value, however, which is incorrect; infinity multiplied | ||
by a non-zero number should be infinity. Thus, C and C++ (by default) must | ||
detect these special cases and fix them up, which makes multiplication a | ||
more computationally expensive operation.² | ||
|
||
### Footnotes: | ||
¹ W. Kahan, Branch Cuts for Complex Elementary Functions, or Much Ado | ||
About Nothing's Sign Bit. In A. Iserles and M.J.D. Powell, editors, | ||
_Proceedings The State of Art in Numerical Analysis_, pages 165–211, 1987. | ||
|
||
² This can be addressed in C programs by use of the `STDC CX_LIMITED_RANGE` | ||
pragma, which instructs the compiler to simply not care about these cases. | ||
Unfortunately, this pragma is not often used in real C or C++ programs | ||
(though it does see some use in _libraries_). Programmers tend to specify | ||
`-ffast-math` or maybe `-ffinite-math-only` instead, which has other | ||
undesirable consequences. |
Uh oh!
There was an error while loading. Please reload this page.