Skip to content

Commit 2b63fc8

Browse files
Merge pull request #78 from stephentyrone/lengthSquared
Rename unsafeLengthSquared -> lengthSquared
2 parents f1d78c4 + f6c2c34 commit 2b63fc8

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

Sources/Complex/Arithmetic.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ extension Complex: Numeric {
7373
// Try the naive expression z/w = z*conj(w) / |w|^2; if we can compute
7474
// this without over/underflow, everything is fine and the result is
7575
// correct. If not, we have to rescale and do the computation carefully.
76-
let lengthSquared = w.unsafeLengthSquared
77-
guard lengthSquared.isNormal else { return rescaledDivide(z, w) }
78-
return z * (w.conjugate.divided(by: lengthSquared))
76+
let lenSq = w.lengthSquared
77+
guard lenSq.isNormal else { return rescaledDivide(z, w) }
78+
return z * (w.conjugate.divided(by: lenSq))
7979
}
8080

8181
@_transparent
@@ -98,7 +98,7 @@ extension Complex: Numeric {
9898
let wScale = w.magnitude
9999
let zNorm = z.divided(by: zScale)
100100
let wNorm = w.divided(by: wScale)
101-
let r = (zNorm * wNorm.conjugate).divided(by: wNorm.unsafeLengthSquared)
101+
let r = (zNorm * wNorm.conjugate).divided(by: wNorm.lengthSquared)
102102
// At this point, the result is (r * zScale)/wScale computed without
103103
// undue overflow or underflow. We know that r is close to unity, so
104104
// the question is simply what order in which to do this computation

Sources/Complex/Complex.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ extension Complex {
208208

209209
/// The ∞-norm of the value (`max(abs(real), abs(imaginary))`).
210210
///
211-
/// If you need the euclidean norm (a.k.a. 2-norm) use the `length` or `unsafeLengthSquared`
211+
/// If you need the euclidean norm (a.k.a. 2-norm) use the `length` or `lengthSquared`
212212
/// properties instead.
213213
///
214214
/// Edge cases:
@@ -220,7 +220,7 @@ extension Complex {
220220
/// See also:
221221
/// -
222222
/// - `.length`
223-
/// - `.unsafeLengthSquared`
223+
/// - `.lengthSquared`
224224
@_transparent
225225
public var magnitude: RealType {
226226
guard isFinite else { return .infinity }
@@ -394,13 +394,13 @@ extension Complex {
394394
/// See also:
395395
/// -
396396
/// - `.magnitude`
397-
/// - `.unsafeLengthSquared`
397+
/// - `.lengthSquared`
398398
/// - `.phase`
399399
/// - `.polar`
400400
/// - `init(r:θ:)`
401401
@_transparent
402402
public var length: RealType {
403-
let naive = unsafeLengthSquared
403+
let naive = lengthSquared
404404
guard naive.isNormal else { return carefulLength }
405405
return .sqrt(naive)
406406
}
@@ -419,7 +419,7 @@ extension Complex {
419419
///
420420
/// This property is more efficient to compute than `length`, but is
421421
/// highly prone to overflow or underflow; for finite values that are
422-
/// not well-scaled, `unsafeLengthSquared` is often either zero or
422+
/// not well-scaled, `lengthSquared` is often either zero or
423423
/// infinity, even when `length` is a finite number. Use this property
424424
/// only when you are certain that this value is well-scaled.
425425
///
@@ -431,10 +431,13 @@ extension Complex {
431431
/// - `.length`
432432
/// - `.magnitude`
433433
@_transparent
434-
public var unsafeLengthSquared: RealType {
434+
public var lengthSquared: RealType {
435435
x*x + y*y
436436
}
437437

438+
@available(*, unavailable, renamed: "lengthSquared")
439+
public var unsafeLengthSquared: RealType { lengthSquared }
440+
438441
/// The phase (angle, or "argument").
439442
///
440443
/// Returns the angle (measured above the real axis) in radians. If
@@ -484,9 +487,9 @@ extension Complex {
484487
/// ```
485488
/// Complex(length: .zero, phase: θ) == .zero
486489
/// ```
487-
/// - For any `θ`, even `.infinity` or `.nan`:
490+
/// - For any `θ`, even `.infinity` or `.nan`, if `r` is infinite then:
488491
/// ```
489-
/// Complex(length: .infinity, phase: θ) == .infinity
492+
/// Complex(length: r, phase: θ) == .infinity
490493
/// ```
491494
/// - Otherwise, `θ` must be finite, or a precondition failure occurs.
492495
///
@@ -501,8 +504,8 @@ extension Complex {
501504
self = Complex(.cos(phase), .sin(phase)).multiplied(by: length)
502505
} else {
503506
precondition(
504-
length == 0 || length == .infinity,
505-
"Either phase must be finite, or length must be zero or infinity."
507+
length.isZero || length.isInfinite,
508+
"Either phase must be finite, or length must be zero or infinite."
506509
)
507510
self = Complex(length)
508511
}

Tests/ComplexTests/ArithmeticTests.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,15 @@ final class ArithmeticTests: XCTestCase {
6767
// In order to support round-tripping from rectangular to polar coordinate
6868
// systems, as a special case phase can be non-finite when length is
6969
// either zero or infinity.
70-
XCTAssertEqual(Complex<T>(length: .zero, phase: .infinity), .zero)
71-
XCTAssertEqual(Complex<T>(length: .zero, phase: -.infinity), .zero)
72-
XCTAssertEqual(Complex<T>(length: .zero, phase: .nan ), .zero)
73-
XCTAssertEqual(Complex<T>(length: .infinity, phase: .infinity), .infinity)
74-
XCTAssertEqual(Complex<T>(length: .infinity, phase: -.infinity), .infinity)
75-
XCTAssertEqual(Complex<T>(length: .infinity, phase: .nan ), .infinity)
70+
XCTAssertEqual(Complex<T>(length: .zero, phase: .infinity), .zero)
71+
XCTAssertEqual(Complex<T>(length: .zero, phase:-.infinity), .zero)
72+
XCTAssertEqual(Complex<T>(length: .zero, phase: .nan ), .zero)
73+
XCTAssertEqual(Complex<T>(length: .infinity, phase: .infinity), .infinity)
74+
XCTAssertEqual(Complex<T>(length: .infinity, phase:-.infinity), .infinity)
75+
XCTAssertEqual(Complex<T>(length: .infinity, phase: .nan ), .infinity)
76+
XCTAssertEqual(Complex<T>(length:-.infinity, phase: .infinity), .infinity)
77+
XCTAssertEqual(Complex<T>(length:-.infinity, phase:-.infinity), .infinity)
78+
XCTAssertEqual(Complex<T>(length:-.infinity, phase: .nan ), .infinity)
7679

7780
let exponentRange =
7881
(T.leastNormalMagnitude.exponent + T.Exponent(T.significandBitCount)) ...
@@ -108,11 +111,11 @@ final class ArithmeticTests: XCTestCase {
108111
XCTFail()
109112
}
110113
XCTAssertEqual(w, -z)
111-
// if length*length is normal, it should be unsafeLengthSquared, up
114+
// if length*length is normal, it should be lengthSquared, up
112115
// to small error.
113116
if (p.length*p.length).isNormal {
114-
if !closeEnough(z.unsafeLengthSquared, p.length*p.length, ulps: 16) {
115-
print("p = \(p)\nz = \(z)\nz.unsafeLengthSquared = \(z.unsafeLengthSquared)")
117+
if !closeEnough(z.lengthSquared, p.length*p.length, ulps: 16) {
118+
print("p = \(p)\nz = \(z)\nz.lengthSquared = \(z.lengthSquared)")
116119
XCTFail()
117120
}
118121
}

0 commit comments

Comments
 (0)