Skip to content

Commit a50d957

Browse files
Merge pull request #265 from stephentyrone/docc-stuff
Documentation overhaul, including broad .docc adoption and organization.
2 parents 8c4747f + 1e94d23 commit a50d957

31 files changed

+833
-306
lines changed

Sources/ComplexModule/Complex+AdditiveArithmetic.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Numerics open source project
44
//
5-
// Copyright (c) 2019-2021 Apple Inc. and the Swift Numerics project authors
5+
// Copyright (c) 2019-2025 Apple Inc. and the Swift Numerics project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -14,7 +14,7 @@ import RealModule
1414
extension Complex: AdditiveArithmetic {
1515
/// The additive identity, with real and imaginary parts both zero.
1616
///
17-
/// See also: `one`, `i`, `infinity`
17+
/// See also: ``one``, ``i``, ``infinity``
1818
@_transparent
1919
public static var zero: Complex {
2020
Complex(0, 0)

Sources/ComplexModule/Complex+AlgebraicField.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Numerics open source project
44
//
5-
// Copyright (c) 2019-2024 Apple Inc. and the Swift Numerics project authors
5+
// Copyright (c) 2019-2025 Apple Inc. and the Swift Numerics project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -13,6 +13,8 @@ import RealModule
1313

1414
extension Complex: AlgebraicField {
1515
/// The multiplicative identity `1 + 0i`.
16+
///
17+
/// See also: ``zero``, ``i``, ``infinity``
1618
@_transparent
1719
public static var one: Complex {
1820
Complex(1, 0)

Sources/ComplexModule/Complex+Codable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Numerics open source project
44
//
5-
// Copyright (c) 2019 - 2021 Apple Inc. and the Swift Numerics project authors
5+
// Copyright (c) 2019-2025 Apple Inc. and the Swift Numerics project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information

Sources/ComplexModule/Complex+ElementaryFunctions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Numerics open source project
44
//
5-
// Copyright (c) 2019-2020 Apple Inc. and the Swift Numerics project authors
5+
// Copyright (c) 2019-2025 Apple Inc. and the Swift Numerics project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information

Sources/ComplexModule/Complex+Hashable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Numerics open source project
44
//
5-
// Copyright (c) 2019 - 2021 Apple Inc. and the Swift Numerics project authors
5+
// Copyright (c) 2019-2025 Apple Inc. and the Swift Numerics project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information

Sources/ComplexModule/Complex+IntegerLiteral.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Numerics open source project
44
//
5-
// Copyright (c) 2019 - 2021 Apple Inc. and the Swift Numerics project authors
5+
// Copyright (c) 2019-2025 Apple Inc. and the Swift Numerics project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information

Sources/ComplexModule/Complex+Numeric.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Numerics open source project
44
//
5-
// Copyright (c) 2019 - 2021 Apple Inc. and the Swift Numerics project authors
5+
// Copyright (c) 2019-2025 Apple Inc. and the Swift Numerics project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -37,17 +37,20 @@ extension Complex: Numeric {
3737
self.init(real, 0)
3838
}
3939

40-
/// The ∞-norm of the value (`max(abs(real), abs(imaginary))`).
40+
/// The infinity-norm of the value (a.k.a. "maximum norm" or "Чебышёв
41+
/// [Chebyshev] norm").
4142
///
42-
/// If you need the Euclidean norm (a.k.a. 2-norm) use the `length` or
43-
/// `lengthSquared` properties instead.
43+
/// Equal to `max(abs(real), abs(imaginary))`.
4444
///
45-
/// Edge cases:
46-
/// - If `z` is not finite, `z.magnitude` is `.infinity`.
47-
/// - If `z` is zero, `z.magnitude` is `0`.
48-
/// - Otherwise, `z.magnitude` is finite and non-zero.
45+
/// If you need to work with the Euclidean norm (a.k.a. 2-norm) instead,
46+
/// use the ``length`` or ``lengthSquared`` properties. If you just need
47+
/// to know "how big" a number is, use this property.
48+
///
49+
/// **Edge cases:**
4950
///
50-
/// See also `.length` and `.lengthSquared`.
51+
/// - If `z` is not finite, `z.magnitude` is infinity.
52+
/// - If `z` is zero, `z.magnitude` is zero.
53+
/// - Otherwise, `z.magnitude` is finite and non-zero.
5154
@_transparent
5255
public var magnitude: RealType {
5356
guard isFinite else { return .infinity }

Sources/ComplexModule/Complex+StringConvertible.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Numerics open source project
44
//
5-
// Copyright (c) 2019 - 2021 Apple Inc. and the Swift Numerics project authors
5+
// Copyright (c) 2019-2025 Apple Inc. and the Swift Numerics project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information

Sources/ComplexModule/Complex.swift

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Numerics open source project
44
//
5-
// Copyright (c) 2019 - 2021 Apple Inc. and the Swift Numerics project authors
5+
// Copyright (c) 2019-2025 Apple Inc. and the Swift Numerics project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -11,39 +11,8 @@
1111

1212
import RealModule
1313

14-
/// A complex number represented by real and imaginary parts.
15-
///
16-
/// TODO: introductory text on complex numbers
17-
///
18-
/// Implementation notes:
19-
///
20-
/// This type does not provide heterogeneous real/complex arithmetic,
21-
/// not even the natural vector-space operations like real * complex.
22-
/// There are two reasons for this choice: first, Swift broadly avoids
23-
/// mixed-type arithmetic when the operation can be adequately expressed
24-
/// by a conversion and homogeneous arithmetic. Second, with the current
25-
/// typechecker rules, it would lead to undesirable ambiguity in common
26-
/// expressions (see README.md for more details).
27-
///
28-
/// Unlike C's `_Complex` and C++'s `std::complex<>` types, we do not
29-
/// attempt to make meaningful semantic distinctions between different
30-
/// representations of infinity or NaN. Any Complex value with at least
31-
/// one non-finite component is simply "non-finite". In as much as
32-
/// possible, we use the semantics of the point at infinity on the
33-
/// Riemann sphere for such values. This approach simplifies the number of
34-
/// edge cases that need to be considered for multiplication, division, and
35-
/// the elementary functions considerably.
36-
///
37-
/// `.magnitude` does not return the Euclidean norm; it uses the "infinity
38-
/// norm" (`max(|real|,|imaginary|)`) instead. There are two reasons for this
39-
/// choice: first, it's simply faster to compute on most hardware. Second,
40-
/// there exist values for which the Euclidean norm cannot be represented
41-
/// (consider a number with `.real` and `.imaginary` both equal to
42-
/// `RealType.greatestFiniteMagnitude`; the Euclidean norm would be
43-
/// `.sqrt(2) * .greatestFiniteMagnitude`, which overflows). Using
44-
/// the infinity norm avoids this problem entirely without significant
45-
/// downsides. You can access the Euclidean norm using the `length`
46-
/// property.
14+
// A [complex number](https://en.wikipedia.org/wiki/Complex_number).
15+
// See Documentation.docc/Complex.md for more details.
4716
@frozen
4817
public struct Complex<RealType> where RealType: Real {
4918
// A note on the `x` and `y` properties
@@ -53,11 +22,11 @@ public struct Complex<RealType> where RealType: Real {
5322
// `.real` and `.imaginary` properties, which wrap this storage and
5423
// fixup the semantics for non-finite values.
5524

56-
/// The real component of the value.
25+
/// The storage for the real component of the value.
5726
@usableFromInline @inline(__always)
5827
internal var x: RealType
5928

60-
/// The imaginary part of the value.
29+
/// The storage for the imaginary part of the value.
6130
@usableFromInline @inline(__always)
6231
internal var y: RealType
6332

@@ -95,27 +64,40 @@ extension Complex {
9564
set { y = newValue }
9665
}
9766

67+
/// The raw representation of the value.
68+
///
69+
/// Use this when you need the underlying RealType values,
70+
/// without fixup for NaN or infinity.
71+
public var rawStorage: (x: RealType, y: RealType) {
72+
@_transparent
73+
get { (x, y) }
74+
@_transparent
75+
set { (x, y) = newValue }
76+
}
77+
9878
/// The raw representation of the real part of this value.
79+
@available(*, deprecated, message: "Use rawStorage")
9980
@_transparent
10081
public var _rawX: RealType { x }
10182

10283
/// The raw representation of the imaginary part of this value.
84+
@available(*, deprecated, message: "Use rawStorage")
10385
@_transparent
10486
public var _rawY: RealType { y }
10587
}
10688

10789
extension Complex {
10890
/// The imaginary unit.
10991
///
110-
/// See also `.zero`, `.one` and `.infinity`.
92+
/// See also ``zero``, ``one`` and ``infinity``.
11193
@_transparent
11294
public static var i: Complex {
11395
Complex(0, 1)
11496
}
11597

11698
/// The point at infinity.
11799
///
118-
/// See also `.zero`, `.one` and `.i`.
100+
/// See also ``zero``, ``one`` and ``i``.
119101
@_transparent
120102
public static var infinity: Complex {
121103
Complex(.infinity, 0)
@@ -125,7 +107,7 @@ extension Complex {
125107
///
126108
/// A complex value is finite if neither component is an infinity or nan.
127109
///
128-
/// See also `.isNormal`, `.isSubnormal` and `.isZero`.
110+
/// See also ``isNormal``, ``isSubnormal`` and ``isZero``.
129111
@_transparent
130112
public var isFinite: Bool {
131113
x.isFinite && y.isFinite
@@ -138,7 +120,7 @@ extension Complex {
138120
/// one of the components is normal if its exponent allows a full-precision
139121
/// representation.
140122
///
141-
/// See also `.isFinite`, `.isSubnormal` and `.isZero`.
123+
/// See also ``isFinite``, ``isSubnormal`` and ``isZero``.
142124
@_transparent
143125
public var isNormal: Bool {
144126
isFinite && (x.isNormal || y.isNormal)
@@ -150,7 +132,7 @@ extension Complex {
150132
/// When the result of a computation is subnormal, underflow has occurred and
151133
/// the result generally does not have full precision.
152134
///
153-
/// See also `.isFinite`, `.isNormal` and `.isZero`.
135+
/// See also ``isFinite``, ``isNormal`` and ``isZero``.
154136
@_transparent
155137
public var isSubnormal: Bool {
156138
isFinite && !isNormal && !isZero
@@ -161,7 +143,7 @@ extension Complex {
161143
/// A complex number is zero if *both* the real and imaginary components
162144
/// are zero.
163145
///
164-
/// See also `.isFinite`, `.isNormal` and `isSubnormal`.
146+
/// See also ``isFinite``, ``isNormal`` and ``isSubnormal``.
165147
@_transparent
166148
public var isZero: Bool {
167149
x == 0 && y == 0
@@ -200,7 +182,7 @@ extension Complex {
200182
self.init(real, 0)
201183
}
202184

203-
/// The complex number with specified imaginary part and zero real part.
185+
/// The complex number with zero real part and specified imaginary part.
204186
///
205187
/// Equivalent to `Complex(0, imaginary)`.
206188
@inlinable
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# ``Complex``
2+
3+
A complex number type represented by its real and imaginary parts, and equipped
4+
with the usual arithmetic operators and math functions.
5+
6+
## Overview
7+
8+
You can access these Cartesian components using the real and imaginary
9+
properties.
10+
11+
```swift
12+
let z = Complex(1,-1) // 1 - i
13+
let re = z.real // 1
14+
let im = z.imaginary // -1
15+
```
16+
17+
All `Complex` numbers with a non-finite component are treated as a single
18+
"point at infinity," with infinite magnitude and indeterminant phase. Thus,
19+
the real and imaginary parts of an infinity are nan.
20+
21+
```swift
22+
let w = Complex<Double>.infinity
23+
w == -w // true
24+
let re = w.real // .nan
25+
let im = w.imag // .nan
26+
```
27+
28+
See <doc:Infinity> for more details.
29+
30+
The ``magnitude`` property of a complex number is the infinity norm of the
31+
value (a.k.a. “maximum norm” or “Чебышёв [Chebyshev] norm”). To get the two
32+
norm (a.k.a. "Euclidean norm"), use the ``length`` property. See
33+
<doc:Magnitude> for more details.
34+
35+
## Topics
36+
37+
### Real and imaginary parts
38+
39+
- ``real``
40+
- ``imaginary``
41+
- ``rawStorage``
42+
- ``init(_:_:)``
43+
- ``init(_:)-5aesj``
44+
- ``init(imaginary:)``
45+
46+
### Phase, length and magnitude
47+
48+
- ``magnitude``
49+
- ``length``
50+
- ``lengthSquared``
51+
- ``normalized``
52+
- ``phase``
53+
- ``polar``
54+
- ``init(length:phase:)``
55+
56+
### Scaling by real numbers
57+
- ``multiplied(by:)``
58+
- ``divided(by:)``
59+
60+
### Complex-specific operations
61+
- ``conjugate``
62+
63+
### Classification
64+
- ``isZero``
65+
- ``isSubnormal``
66+
- ``isNormal``
67+
- ``isFinite``

0 commit comments

Comments
 (0)