Skip to content

Commit d2f6811

Browse files
committed
More docc changes.
1 parent 129e496 commit d2f6811

File tree

11 files changed

+96
-104
lines changed

11 files changed

+96
-104
lines changed

Sources/ComplexModule/Complex+Numeric.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,18 @@ 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 "Чебышёв norm").
4141
///
42-
/// If you need the Euclidean norm (a.k.a. 2-norm) use the `length` or
43-
/// `lengthSquared` properties instead.
42+
/// Equal to `max(abs(real), abs(imaginary))`.
4443
///
45-
/// Edge cases:
46-
/// - If `z` is not finite, `z.magnitude` is `.infinity`.
47-
/// - If `z` is zero, `z.magnitude` is `0`.
44+
/// If you need to work with the Euclidean norm (a.k.a. 2-norm) instead,
45+
/// use the `length` or `lengthSquared` properties. If you just need to
46+
/// know "how big" a number is, use this property.
47+
///
48+
/// **Edge cases:**
49+
///
50+
/// - If `z` is not finite, `z.magnitude` is infinity.
51+
/// - If `z` is zero, `z.magnitude` is zero.
4852
/// - Otherwise, `z.magnitude` is finite and non-zero.
4953
///
5054
/// See also `.length` and `.lengthSquared`.

Sources/ComplexModule/Documentation.docc/Infinity.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ the same.
1414

1515
## Rationale
1616

17-
This decision has some drawbacks,¹ but also some significant advantages.
18-
In particular, complex multiplication is one of the most common operations with
19-
a complex type, and one would like to be able to use the usual naive arithmetic
20-
implementation, consisting of four real multiplications and two real additions:
17+
This choice has some drawbacks,¹ but also some significant advantages.
18+
In particular, complex multiplication is the most common operation performed
19+
with a complex type, and one would like to be able to use the usual naive
20+
arithmetic implementation, consisting of four real multiplications and two
21+
real additions:
2122
```
2223
(a + bi) * (c + di) = (ac - bd) + (ad + bc)i
2324
```

Sources/ComplexModule/Polar.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import RealModule
1313

1414
extension Complex {
15-
/// The Euclidean norm (a.k.a. 2-norm, `sqrt(real*real + imaginary*imaginary)`).
15+
/// The Euclidean norm (a.k.a. 2-norm).
1616
///
1717
/// This property takes care to avoid spurious over- or underflow in
1818
/// this computation. For example:
@@ -28,7 +28,7 @@ extension Complex {
2828
///
2929
/// For most use cases, you can use the cheaper `.magnitude`
3030
/// property (which computes the ∞-norm) instead, which always produces
31-
/// a representable result.
31+
/// a representable result. See <doc:Magnitude> for more details.
3232
///
3333
/// Edge cases:
3434
/// - If a complex value is not finite, its `.length` is `infinity`.
@@ -63,25 +63,24 @@ extension Complex {
6363
/// For many cases, `.magnitude` can be used instead, which is similarly
6464
/// cheap to compute and always returns a representable value.
6565
///
66-
/// See also `.length` and `.magnitude`.
66+
/// Note that because of how `lengthSquared` is used, it is a primary
67+
/// design goal that it be as fast as possible. Therefore, it does not
68+
/// normalize infinities, and may return either `.infinity` or `.nan`
69+
/// for non-finite values.
70+
///
71+
/// See also ``length`` and ``magnitude``.
6772
@_transparent
6873
public var lengthSquared: RealType {
6974
x*x + y*y
7075
}
7176

72-
@available(*, unavailable, renamed: "lengthSquared")
73-
public var unsafeLengthSquared: RealType { lengthSquared }
74-
7577
/// The phase (angle, or "argument").
7678
///
77-
/// Returns the angle (measured above the real axis) in radians. If
79+
/// - Returns: The angle (measured above the real axis) in radians. If
7880
/// the complex value is zero or infinity, the phase is not defined,
7981
/// and `nan` is returned.
8082
///
81-
/// Edge cases:
82-
/// - If the complex value is zero or non-finite, phase is `nan`.
83-
///
84-
/// See also `.length`, `.polar` and `init(r:θ:)`.
83+
/// See also ``length``, ``polar`` and ``init(length:phase:)``.
8584
@inlinable
8685
public var phase: RealType {
8786
guard isFinite && !isZero else { return .nan }

Sources/ComplexModule/Scale.swift

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,22 @@
2020
// what is the type of b? If we don't have a type context, it's ambiguous.
2121
// If we have a Complex type context, then b will be inferred to have type
2222
// Complex! Obviously, that doesn't help anyone.
23-
//
24-
// TODO: figure out if there's some way to avoid these surprising results
25-
// and turn these into operators if/when we have it.
26-
// (https://github.com/apple/swift-numerics/issues/12)
23+
2724
extension Complex {
28-
/// `self` scaled by `a`.
29-
@usableFromInline @_transparent
30-
internal func multiplied(by a: RealType) -> Complex {
31-
// This can be viewed in two different ways, which are mathematically
32-
// equivalent: either we are computing `self * Complex(a)` (i.e.
33-
// converting `a` to be a complex value, and then using the complex
34-
// multiplication) or we are using the scalar product of the vector
35-
// space structure: `Complex(a*real, a*imaginary)`.
36-
//
37-
// Although these two interpretations are _mathematically_ equivalent,
38-
// they will generate different representations of the point at
39-
// infinity in general. For example, suppose `self` is represented by
40-
// `(infinity, 0)`. Then `self * Complex(1)` would evaluate as
41-
// `(1*infinity - 0*0, 0*infinity + 1*0) = (infinity, nan)`, but
42-
// the vector space interpretation produces `(infinity, 0)`. This does
43-
// not matter much, because these are two representations of the same
44-
// semantic value, but note that one requires four multiplies and two
45-
// additions, while the one we use requires only two real multiplications.
25+
/// `self` multiplied by the real value `a`.
26+
///
27+
/// Equivalent to `self * Complex(a)`, but may be computed more efficiently.
28+
@inlinable @inline(__always)
29+
public func multiplied(by a: RealType) -> Complex {
4630
Complex(x*a, y*a)
4731
}
4832

49-
/// `self` unscaled by `a`.
50-
@usableFromInline @_transparent
51-
internal func divided(by a: RealType) -> Complex {
33+
/// `self` divided by the real value `a`.
34+
///
35+
/// More efficient than `self / Complex(a)`. May not produce exactly the
36+
/// same result, but will always be more accurate when they differ.
37+
@inlinable @inline(__always)
38+
public func divided(by a: RealType) -> Complex {
5239
// See implementation notes for `multiplied` above.
5340
Complex(x/a, y/a)
5441
}

Sources/IntegerUtilities/DivideWithRounding.swift

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,29 @@
1212
extension BinaryInteger {
1313
/// `self` divided by `other`, rounding the result according to `rule`.
1414
///
15-
/// The default rounding rule is `.down`, which _is not the same_ as the
16-
/// behavior of the `/` operator from the Swift standard library, but is
17-
/// chosen because it generally produces a more useful remainder. In
18-
/// particular, when `b` is positive, the remainder is always positive.
19-
/// To match the behavior of `/`, use the `.towardZero` rounding mode.
15+
/// By default, this function uses ``RoundingRule/down``, which **is not
16+
/// the same** as the rounding of the `/` operator from the Swift standard
17+
/// library. They agree when the signs of the arguments match, but produce
18+
/// different results when the quotient is negative. This behavior is
19+
/// chosen because it generally produces a more useful remainder; in
20+
/// particular, when the divisor is positive, the remainder is always
21+
/// positive. To match the behavior of `/`, use ``RoundingRule/towardZero``.
2022
///
2123
/// Note that the remainder of division is not always representable in an
22-
/// unsigned type if a rounding rule other than `.down`, `.towardZero`, or
23-
/// `.requireExact` is used. For example:
24-
///
25-
/// let a: UInt = 5
26-
/// let b: UInt = 3
27-
/// let q = a.divided(by: b, rounding: .up) // 2
28-
/// let r = a - b*q // 5 - 3*2 overflows UInt.
29-
///
24+
/// unsigned type if a rounding rule other than ``RoundingRule/down``,
25+
/// ``RoundingRule/towardZero``, or ``RoundingRule/requireExact`` is
26+
/// used. For example:
27+
/// ```swift
28+
/// let a: UInt = 5
29+
/// let b: UInt = 3
30+
/// let q = a.divided(by: b, rounding: .up) // 2
31+
/// let r = a - b*q // 5 - 3*2 overflows UInt.
32+
/// ```
3033
/// For this reason, there is no `remainder(dividingBy:rounding:)`
3134
/// operation defined on `BinaryInteger`. Signed integers do not have
32-
/// this problem, so it is defined on the `SignedInteger` protocol
33-
/// instead, as is an overload of `divided(by:rounding:)` that returns
34-
/// both quotient and remainder.
35+
/// this problem, so the `SignedInteger` protocol is extended with
36+
/// ``SignedInteger/divided(by:rounding:)`` returning both quotient
37+
/// and remainder.
3538
@inlinable
3639
public func divided(
3740
by other: Self,
@@ -52,7 +55,7 @@ extension BinaryInteger {
5255
// rounded toward zero.
5356
//
5457
// If we subtract 1 from q, we add other to r to compensate, because:
55-
//
58+
//
5659
// self = q*other + r
5760
// = (q-1)*other + (r+other)
5861
//
@@ -151,10 +154,10 @@ extension SignedInteger {
151154
/// Divides `self` by `other`, rounding the quotient according to `rule`,
152155
/// and returns the remainder.
153156
///
154-
/// The default rounding rule is `.down`, which _is not the same_ as the
155-
/// behavior of the `%` operator from the Swift standard library, but is
156-
/// chosen because it generally produces a more useful remainder. To
157-
/// match the behavior of `%`, use the `.towardZero` rounding mode.
157+
/// The default rounding rule is ``RoundingRule/down``, which _is not the
158+
/// same_ as the behavior of the `%` operator from the Swift standard
159+
/// library, but is chosen because it generally produces a more useful
160+
/// remainder. To match the behavior of `%`, use ``RoundingRule/towardZero``.
158161
///
159162
/// - Precondition: `other` cannot be zero.
160163
@inlinable
@@ -170,10 +173,10 @@ extension SignedInteger {
170173
/// Divides `self` by `other`, rounding the quotient according to `rule`,
171174
/// and returns both the quotient and remainder.
172175
///
173-
/// The default rounding rule is `.down`, which _is not the same_ as the
174-
/// behavior of the `/` operator from the Swift standard library, but is
175-
/// chosen because it generally produces a more useful remainder. To
176-
/// match the behavior of `/`, use the `.towardZero` rounding mode.
176+
/// The default rounding rule is ``RoundingRule/down``, which _is not the
177+
/// same_ as the behavior of the `%` operator from the Swift standard
178+
/// library, but is chosen because it generally produces a more useful
179+
/// remainder. To match the behavior of `/`, use ``RoundingRule/towardZero``.
177180
///
178181
/// Because the default rounding mode does not match Swift's standard
179182
/// library, this function is a disfavored overload of `divided(by:)`

Sources/IntegerUtilities/SaturatingArithmetic.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ extension FixedWidthInteger {
3131
/// let c = a.addingWithSaturation(b)
3232
/// ```
3333
///
34-
/// Anytime the "normal addition" `self + other` does not trap,
35-
/// `addingWithSaturation` produces the same result.
34+
/// If the "normal addition" `self + other` does not trap,
35+
/// this method produces the same result.
3636
@inlinable
3737
public func addingWithSaturation(_ other: Self) -> Self {
3838
let (wrapped, overflow) = addingReportingOverflow(other)
@@ -54,8 +54,8 @@ extension FixedWidthInteger {
5454
/// `a.subtractingWithSaturation(b)`, because `-b` is not representable
5555
/// if `b` is the minimum value of a signed type.
5656
///
57-
/// Anytime the "normal subtraction" `self - other` does not trap,
58-
/// `subtractingWithSaturation` produces the same result.
57+
/// If the "normal subtraction" `self - other` does not trap,
58+
/// this method produces the same result.
5959
@inlinable
6060
public func subtractingWithSaturation(_ other: Self) -> Self {
6161
let (wrapped, overflow) = subtractingReportingOverflow(other)
@@ -85,8 +85,8 @@ extension FixedWidthInteger {
8585
/// let c = a.multipliedWithSaturation(by: b)
8686
/// ```
8787
///
88-
/// Anytime the "normal multiplication" `self * other` does not trap,
89-
/// `multipliedWithSaturation` produces the same result.
88+
/// If the "normal multiplication" `self * other` does not trap,
89+
/// this method produces the same result.
9090
@inlinable
9191
public func multipliedWithSaturation(by other: Self) -> Self {
9292
let (high, low) = multipliedFullWidth(by: other)
@@ -100,12 +100,12 @@ extension FixedWidthInteger {
100100
/// `self` multiplied by the rational number 2^(`count`), saturated to the
101101
/// range `Self.min ... Self.max`, and rounded according to `rule`.
102102
///
103-
/// See `shifted(rightBy:rounding:)` for more discussion of rounding
104-
/// shifts with examples.
103+
/// See `shifted(rightBy:rounding:)`, defined on `BinaryInteger`, for more
104+
/// discussion of rounding shifts with examples.
105105
///
106106
/// - Parameters:
107-
/// - leftBy count: the number of bits to shift by. If positive, this is a left-shift,
108-
/// and if negative a right shift.
107+
/// - leftBy count: the number of bits to shift by. If positive, this is
108+
/// a left-shift, and if negative a right shift.
109109
/// - rounding rule: the direction in which to round if `count` is negative.
110110
@inlinable
111111
public func shiftedWithSaturation(

Sources/IntegerUtilities/ShiftWithRounding.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
extension BinaryInteger {
1313
/// `self` divided by 2^(`count`), rounding the result according to `rule`.
1414
///
15-
/// The default rounding rule is `.down`, which matches the behavior of
16-
/// the `>>` operator from the standard library.
15+
/// The default rounding rule is ``RoundingRule/down``, which matches the
16+
/// behavior of the `>>` operator from the standard library.
1717
///
1818
/// Some examples of different rounding rules:
1919
///
@@ -138,8 +138,8 @@ extension BinaryInteger {
138138

139139
/// `self` divided by 2^(`count`), rounding the result according to `rule`.
140140
///
141-
/// The default rounding rule is `.down`, which matches the behavior of
142-
/// the `>>` operator from the standard library.
141+
/// The default rounding rule is ``RoundingRule/down``, which matches the
142+
/// behavior of the `>>` operator from the standard library.
143143
///
144144
/// Some examples of different rounding rules:
145145
///

Sources/RealModule/AlgebraicField.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
/// See also `Real`, `SignedNumeric`, `Numeric` and `AdditiveArithmetic`.
4141
///
4242
/// [field]: https://en.wikipedia.org/wiki/Field_(mathematics)
43-
public protocol AlgebraicField: SignedNumeric {
43+
public protocol AlgebraicField: SignedNumeric where Magnitude: AlgebraicField {
4444

4545
/// Replaces a with the (approximate) quotient `a/b`.
4646
static func /=(a: inout Self, b: Self)

Sources/RealModule/AugmentedArithmetic.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12-
/// A namespace for "augmented arithmetic" operations for types conforming to
13-
/// `Real`.
14-
///
15-
/// Augmented arithmetic refers to a family of algorithms that represent
16-
/// the results of floating-point computations using multiple values such that
12+
/// Augmented arithmetic provides a family of algorithms that represent the
13+
/// results of floating-point computations using multiple values such that
1714
/// either the error is minimized or the result is exact.
1815
public enum Augmented { }
1916

Sources/RealModule/ElementaryFunctions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12-
/// A type that has elementary functions available.
12+
/// A type that has elementary functions (`sin`, `cos`, etc.) available.
1313
///
1414
/// An ["elementary function"][elfn] is a function built up from powers, roots,
1515
/// exponentials, logarithms, trigonometric functions (sin, cos, tan) and

0 commit comments

Comments
 (0)