-
Notifications
You must be signed in to change notification settings - Fork 147
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
Various documentation improvements. #265
Open
stephentyrone
wants to merge
6
commits into
apple:main
Choose a base branch
from
stephentyrone:docc-stuff
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
59f15cf
Initial work on docc setup.
stephentyrone b98cc1c
Further docc work.
stephentyrone e837bc8
More docc changes.
stephentyrone 94f8104
Changes from review feedback.
stephentyrone 3dbee65
typo.
stephentyrone e77a36a
Further documentation work for Complex.
stephentyrone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# ``Complex`` | ||
|
||
## Topics | ||
|
||
### Real and imaginary parts | ||
|
||
A `Complex` value is represented with two `RealType` values, corresponding to | ||
the real and imaginary parts of the number: | ||
|
||
```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 is 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. | ||
|
||
- ``init(_:_:)`` | ||
- ``init(_:)-5aesj`` | ||
- ``init(imaginary:)`` | ||
- ``real`` | ||
- ``imaginary`` | ||
|
||
### Magnitude and norms | ||
|
||
See the article <doc:Magnitude> for more details. | ||
|
||
- ``magnitude`` | ||
- ``length`` | ||
- ``lengthSquared`` | ||
- ``normalized`` | ||
|
||
### Polar representations | ||
|
||
- ``init(length:phase:)`` | ||
- ``phase`` | ||
- ``length`` | ||
- ``polar`` | ||
|
||
### Conversions from other types | ||
|
||
- ``init(_:)-4csd3`` | ||
- ``init(_:)-80jml`` | ||
- ``init(exactly:)-767k9`` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# ``ComplexModule`` | ||
|
||
Types and operations for working with complex numbers. | ||
|
||
## 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. | ||
|
||
``` | ||
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 | ||
``` | ||
|
||
`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`). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Zero and infinity | ||
|
||
Semantics of `Complex` zero and infinity values, and important considerations | ||
when porting code from other languages. | ||
|
||
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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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?