From 2b4c6701f2f077faa4987573da905bb107b2004e Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Wed, 16 Sep 2020 14:33:43 -0400 Subject: [PATCH] log and log(onePlus:) implementations for Complex These significantly improve on the state of the art for arguments close to the circle where the result vanishes, producing good componentwise relative accuracy as well as relative accuracy in the complex norm. Also drops the argument parser dependency, which is desirable to avoid possible circular dependencies in the future with algorithms. --- Package.swift | 14 +- .../ComplexModule/ElementaryFunctions.swift | 177 ++++++++++++------ Sources/RealModule/AugmentedArithmetic.swift | 29 +++ .../_TestSupport}/Error.swift | 11 +- Sources/_TestSupport/Interval.swift | 51 +++++ .../ComplexElementaryFunctions/main.swift | 80 -------- Tests/Executable/ComplexLog/main.swift | 107 +++++++++++ Tests/Executable/ComplexLog1p/main.swift | 107 +++++++++++ 8 files changed, 417 insertions(+), 159 deletions(-) create mode 100644 Sources/RealModule/AugmentedArithmetic.swift rename {Tests/Executable/ComplexElementaryFunctions => Sources/_TestSupport}/Error.swift (70%) create mode 100644 Sources/_TestSupport/Interval.swift delete mode 100644 Tests/Executable/ComplexElementaryFunctions/main.swift create mode 100644 Tests/Executable/ComplexLog/main.swift create mode 100644 Tests/Executable/ComplexLog1p/main.swift diff --git a/Package.swift b/Package.swift index fb6a8d8..565d5d6 100644 --- a/Package.swift +++ b/Package.swift @@ -21,10 +21,6 @@ let package = Package( .library(name: "RealModule", targets: ["RealModule"]), ], - dependencies: [ - .package(url: "https://github.com/apple/swift-argument-parser", from: "0.1.0"), - ], - targets: [ // User-facing modules .target(name: "ComplexModule", dependencies: ["RealModule"]), @@ -40,13 +36,7 @@ let package = Package( .testTarget(name: "RealTests", dependencies: ["_TestSupport"]), // Test executables - .target( - name: "ComplexElementaryFunctions", - dependencies: [ - "Numerics", "_TestSupport", - .product(name: "ArgumentParser", package: "swift-argument-parser"), - ], - path: "Tests/Executable" - ) + .target(name: "ComplexLog", dependencies: ["Numerics", "_TestSupport"], path: "Tests/Executable/ComplexLog"), + .target(name: "ComplexLog1p", dependencies: ["Numerics", "_TestSupport"], path: "Tests/Executable/ComplexLog1p") ] ) diff --git a/Sources/ComplexModule/ElementaryFunctions.swift b/Sources/ComplexModule/ElementaryFunctions.swift index fdeca96..94a3f4c 100644 --- a/Sources/ComplexModule/ElementaryFunctions.swift +++ b/Sources/ComplexModule/ElementaryFunctions.swift @@ -224,78 +224,131 @@ extension Complex /*: ElementaryFunctions */ { // If z is zero or infinite, the phase is undefined, so the result is // the single exceptional value. guard z.isFinite && !z.isZero else { return .infinity } - // Otherwise, try computing lengthSquared; if the result is normal, - // we can just take its log to get the real part of the result. - let r2 = z.lengthSquared + // Having eliminated non-finite values and zero, the imaginary part is + // easy; it's just the phase, which is always computed with good + // relative accuracy via atan2. let θ = z.phase - if r2.isNormal { return Complex(.log(r2)/2, θ) } - // z is finite, but z.lengthSquared is not normal. Rescale and recompute. - let w = z.divided(by: z.magnitude) - return Complex(.log(z.magnitude) + .log(w.lengthSquared)/2, θ) + // The real part of the result is trickier. In exact arithmetic, the + // real part is just log |z|--many implementations of complex functions + // simply use this expression as is. However, there are two problems + // lurking here: + // + // - length can overflow even when log(z) is finite. + // + // - when length is close to 1, catastrophic cancellation is hidden + // in this expression. Consider, e.g. z = 1 + δi for small δ. + // + // Because δ ≪ 1, |z| rounds to 1, and so log |z| produces zero. + // We can expand using Taylor series to see that the result should + // be: + // + // log |z| = log √(1 + δ²) + // = log(1 + δ²)/2 + // = δ²/2 + O(δ⁴) + // + // So naively using log |z| results in a total loss of relative + // accuracy for this case. Note that this is _not_ constrained near + // a single point; it occurs everywhere close to the circle |z| = 1. + // + // Note that this case still _does_ deliver a result with acceptable + // relative accuracy in the complex norm, because + // + // Im(log z) ≈ δ ≫ δ²/2 ≈ Re(log z). + // + // There are a number of ways to try to tackle this problem. I'll begin + // with a simple one that solves the first issue, and _sometimes_ the + // second, then analyze when it doesn't work for the second case. + // + // To handle very large arguments without overflow, the standard + // approach is to _rescale_ the problem. We can do this by finding + // whichever of x and y has greater magnitude, and dividing through + // by it. You can think of this as changing coordinates by reflections + // so that we get a new value w = u + iv with |w| = |z| (and hence + // Re(log w) = Re(log z), and 0 ≤ u, 0 ≤ v ≤ u. + let u = max(z.x.magnitude, z.y.magnitude) + let v = min(z.x.magnitude, z.y.magnitude) + // Now expand out log |w|: + // + // log |w| = log(u² + v²)/2 + // = log u + log(onePlus: (v/u)²)/2 + // + // This looks promising! It handles overflow well, because log(u) is + // finite for every finite u, and we have 0 ≤ v/u ≤ 1, so the second + // term is bounded by 0 ≤ log(1 + (v/u)²)/2 ≤ (log 2)/2. It also + // handles the example I gave above well: we have u = 1, v = δ, and + // + // log(1) + log(onePlus: δ²)/2 = 0 + δ²/2 + // + // as expected. + // + // Unfortunately, it does not handle all points close to the unit + // circle so well; it's easy to see why if we look at the two terms + // that contribute to the result. Cancellation occurs when the result + // is close to zero and the terms have opposing signs. By construction, + // the second term is always positive, so the easiest observation is + // that cancellation is only a problem for u < 1 (because otherwise + // log u is also positive, and there can be no cancellation). + // + // We are not trying for sub-ulp accuracy, just a good relative error + // bound, so for our purposes it suffices to have log u dominate the + // result: + if u >= 1 || u >= RealType._mulAdd(u,u,v*v) { + let r = v / u + return Complex(.log(u) + .log(onePlus: r*r)/2, θ) + } + // Here we're in the tricky case; cancellation is likely to occur. + // Instead of the factorization used above, we will want to evaluate + // log(onePlus: u² + v² - 1)/2. This all boils down to accurately + // evaluating u² + v² - 1. To begin, calculate both squared terms + // as exact head-tail products (u is guaranteed to be well scaled, + // v may underflow, but if it does it doesn't matter, the u term is + // all we need). + let (a,b) = Augmented.twoProdFMA(u, u) + let (c,d) = Augmented.twoProdFMA(v, v) + // It would be nice if we could simply use a - 1, but unfortunately + // we don't have a tight enough bound to guarantee that that expression + // is exact; a may be as small as 1/4, so we could lose a single bit + // to rounding if we did that. + var (s,e) = Augmented.fastTwoSum(-1, a) + // Now we are ready to assemble the result. If cancellation happens, + // then |c| > |e| > |b|, |d|, so this assembly order is safe. It's + // also possible that |c| and |d| are small, but if that happens then + // there is no significant cancellation, and the exact assembly doesn't + // matter. + s = (s + c) + e + b + d + return Complex(.log(onePlus: s)/2, θ) } @inlinable public static func log(onePlus z: Complex) -> Complex { - // Nevin proposed the idea for this implementation on the Swift forums: - // https://forums.swift.org/t/elementaryfunctions-compliance-for-complex/37903/3 - // - // Here's a quick explainer on why it works: in exact arithmetic, - // - // log(1+z) = (log |1+z|, atan2(y, 1+x)) - // - // where x and y are the real and imaginary parts of z, respectively. - // - // The first thing to note is that the expression for the imaginary - // part works fine as is. If cancellation occurs (because x ≈ -1), - // then 1+x is exact, and so we have good componentwise relative - // accuracy. Otherwise, x is bounded away from -1 and 1+x has good - // relative accuracy, and therefore so does atan2(y, 1+x). - // - // So the real part is the hard part (no surprise, just like expPlusOne). - // Nevin's clever idea is simply to take advantage of the expansion: - // - // Re(log 1+z) = (log 1+z + Conj(log 1+z))/2 - // - // Log commutes with conjugation, so this becomes: + // If either |x| or |y| is bounded away from the origin, we don't need + // any extra precision, and can just literally compute log(1+z). Note + // that this includes part of the circle |1+z| = 1 where log(onePlus:) + // vanishes (where x <= -0.5), but on this portion of the circle 1+x + // is always exact by Sterbenz' lemma, so as long as log( ) produces + // a good result, log(1+z) will too. + guard 2*z.x.magnitude < 1 && z.y.magnitude < 1 else { return log(1+z) } + // z is in (±0.5, ±1), so we need to evaluate more carefully. + // The imaginary part is straightforward: + let θ = z.phase + // For the real part, we _could_ use the same approach that we do for + // log( ), but we'd need an extra-precise (1+x)², which can potentially + // be quite painful to calculate. Instead, we can use an approach that + // NevinBR suggested on the Swift forums: // // Re(log 1+z) = (log 1+z + log 1+z̅)/2 // = log((1+z)(1+z̅)/2 // = log(1+z+z̅+zz̅)/2 + // = log((2+x)x + y²)/2 // - // This behaves well close to zero, because the z+z̅ term dominates - // and is computed exactly. Away from zero, cancellation occurs near - // the circle x(x+2) + y^2 = 0, but everywhere along this curve we - // have |Im(log 1+z)| >= π/2, so the relative error in the complex - // norm is well-controlled. We can take advantage of FMA to further - // reduce the cancellation error and recover a good error bound. - // - // The other common implementation choice for log1p is Kahan's trick: - // - // w := 1+z - // return z/(w-1) * log(w) - // - // But this actually doesn't do as well as Nevin's approach does, - // and requires a complex division, which we want to avoid when we - // can do so. - var a = 2*z.x - // We want to add the larger term first (contra usual guidance for - // floating-point error optimization), because we're optimizing for - // the catastrophic cancellation case; when that happens adding the - // larger term via FMA is always exact. When cancellation doesn't - // happen, the simple relative error bound carries through the - // rest of the computation. - let large = max(z.x.magnitude, z.y.magnitude) - let small = min(z.x.magnitude, z.y.magnitude) - a.addProduct(large, large) - a.addProduct(small, small) - // If r2 overflowed, then |z| ≫ 1, and so log(1+z) = log(z). - guard a.isFinite else { return log(z) } - // Unlike log(z), we do not need to worry about what happens if a - // underflows. - return Complex( - RealType.log(onePlus: a)/2, - RealType.atan2(y: z.y, x: 1+z.x) - ) + // So now we need to evaluate (2+x)x + y² accurately. To do this, + // we employ augmented arithmetic; the key observation here is that + // cancellation is only a problem when y² ≈ -(2+x)x + let xp2 = Augmented.fastTwoSum(2, z.x) // Known that 2 > |x|. + let a = Augmented.twoProdFMA(z.x, xp2.head) + let y² = Augmented.twoProdFMA(z.y, z.y) + let s = (a.head + y².head + a.tail + y².tail).addingProduct(z.x, xp2.tail) + return Complex(.log(onePlus: s)/2, θ) } public static func acos(_ z: Complex) -> Complex { diff --git a/Sources/RealModule/AugmentedArithmetic.swift b/Sources/RealModule/AugmentedArithmetic.swift new file mode 100644 index 0000000..c0b7a9a --- /dev/null +++ b/Sources/RealModule/AugmentedArithmetic.swift @@ -0,0 +1,29 @@ +//===--- AugmentedArithmetic.swift ----------------------------*- swift -*-===// +// +// This source file is part of the Swift Numerics open source project +// +// Copyright (c) 2020 Apple Inc. and the Swift Numerics project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +public enum Augmented { } + +extension Augmented { + @_transparent + public static func twoProdFMA(_ a: T, _ b: T) -> (head: T, tail: T) { + let head = a*b + let tail = (-head).addingProduct(a, b) + return (head, tail) + } + + @_transparent + public static func fastTwoSum(_ a: T, _ b: T) -> (head: T, tail: T) { + assert(!(b.magnitude > a.magnitude)) + let head = a + b + let tail = a - head + b + return (head, tail) + } +} diff --git a/Tests/Executable/ComplexElementaryFunctions/Error.swift b/Sources/_TestSupport/Error.swift similarity index 70% rename from Tests/Executable/ComplexElementaryFunctions/Error.swift rename to Sources/_TestSupport/Error.swift index e0b9e4f..93d446c 100644 --- a/Tests/Executable/ComplexElementaryFunctions/Error.swift +++ b/Sources/_TestSupport/Error.swift @@ -10,21 +10,22 @@ // //===----------------------------------------------------------------------===// -import Numerics +import ComplexModule -func relativeError(_ tst: Float, _ ref: Double) -> Double { +public func relativeError(_ tst: Float, _ ref: Double) -> Double { let scale = max(ref.magnitude, Double(Float.leastNormalMagnitude)) let error = (Double(tst) - ref).magnitude return error / scale } -func componentwiseError(_ tst: Complex, _ ref: Complex) -> Double { +public func componentwiseError(_ tst: Complex, _ ref: Complex) -> Double { return max(relativeError(tst.real, ref.real), relativeError(tst.imaginary, ref.imaginary)) } -func relativeError(_ tst: Complex, _ ref: Complex) -> Double { +public func relativeError(_ tst: Complex, _ ref: Complex) -> Double { let scale = max(ref.magnitude, Double(Float.leastNormalMagnitude)) - let error = (Complex(tst) - ref).magnitude + let dtst = Complex(Double(tst.real), Double(tst.imaginary)) + let error = (dtst - ref).magnitude return error / scale } diff --git a/Sources/_TestSupport/Interval.swift b/Sources/_TestSupport/Interval.swift new file mode 100644 index 0000000..6eb9ddb --- /dev/null +++ b/Sources/_TestSupport/Interval.swift @@ -0,0 +1,51 @@ +//===--- Interval.swift ---------------------------------------*- swift -*-===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2020 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +// A not-particularly-clever floating-point iterval that is iterable for the +// purposes of testing. +public struct Interval: Sequence where Element: FloatingPoint { + + let lower: Element + + let upper: Element + + public init(from: Element, through: Element) { + precondition(from <= through) + lower = from + upper = through + } + + public init(from: Element, to: Element) { + self.init(from: from, through: to.nextDown) + } + + public func makeIterator() -> Iterator { + Iterator(self) + } + + public struct Iterator: IteratorProtocol { + let interval: Interval + var nextOutput: Element? + + init(_ interval: Interval) { + self.interval = interval + self.nextOutput = interval.lower + } + + public mutating func next() -> Element? { + let result = nextOutput + if nextOutput == interval.upper { nextOutput = nil } + else { nextOutput = nextOutput?.nextUp } + return result + } + } +} diff --git a/Tests/Executable/ComplexElementaryFunctions/main.swift b/Tests/Executable/ComplexElementaryFunctions/main.swift deleted file mode 100644 index 5926cb2..0000000 --- a/Tests/Executable/ComplexElementaryFunctions/main.swift +++ /dev/null @@ -1,80 +0,0 @@ -//===--- main.swift -------------------------------------------*- swift -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2020 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -import Numerics -import _TestSupport -import ArgumentParser -import Foundation - -struct ComplexElementaryFunctions: ParsableCommand { - func run() throws { - // Walk grid points of the form (n + nπ/16 i) comparing Float and Double, - // finding the worst componentwise and normwise errors for Float. - let reals = (-100 ... 100).map { Float($0) } - let imags = (-100 ... 100).map { Float($0) * .pi / 16 } - var componentError = Double(Float.ulpOfOne) - var complexError = Double(Float.ulpOfOne) - var componentMaxInput = Complex.zero - var complexMaxInput = Complex.zero - for x in reals { - for y in imags { - let z = Complex(x, y) - let tst = Complex.expMinusOne(z) - let ref = Complex.expMinusOne(Complex(z)) - if tst == Complex(ref) { continue } - let thisError = relativeError(tst, ref) - if thisError > complexError { - complexMaxInput = z - complexError = thisError - } - let thisComponentError = componentwiseError(tst, ref) - if thisComponentError > componentError { - componentMaxInput = z - componentError = thisComponentError - } - } - } - - // Now sample randomly-generated points in an interesting strip along - // the real axis. - var g = SystemRandomNumberGenerator() - for _ in 0 ..< 10_000 { - let z = Complex(Float.random(in: -100 ... 100, using: &g), - Float.random(in: -2 * .pi ... 2 * .pi, using: &g)) - let tst = Complex.expMinusOne(z) - let ref = Complex.expMinusOne(Complex(z)) - if tst == Complex(ref) { continue } - let thisError = relativeError(tst, ref) - if thisError > complexError { - complexMaxInput = z - complexError = thisError - } - let thisComponentError = componentwiseError(tst, ref) - if thisComponentError > componentError { - componentMaxInput = z - componentError = thisComponentError - } - } - - print("Worst complex norm error seen for expMinusOne was \(complexError)") - print("For input \(complexMaxInput).") - print("Reference result: \(Complex.expMinusOne(Complex(complexMaxInput)))") - print(" Observed result: \(Complex.expMinusOne(complexMaxInput))") - - print("Worst componentwise error seen for expMinusOne was \(componentError)") - print("For input \(componentMaxInput).") - print("Reference result: \(Complex.expMinusOne(Complex(componentMaxInput)))") - print(" Observed result: \(Complex.expMinusOne(componentMaxInput))") - } -} - -ComplexElementaryFunctions.main() diff --git a/Tests/Executable/ComplexLog/main.swift b/Tests/Executable/ComplexLog/main.swift new file mode 100644 index 0000000..c188033 --- /dev/null +++ b/Tests/Executable/ComplexLog/main.swift @@ -0,0 +1,107 @@ +//===--- main.swift -------------------------------------------*- swift -*-===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2020 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +import Numerics +import _TestSupport + +#if DEBUG +fatalError("Run this test in Release configuration") +#endif + +var componentError = Double(Float.ulpOfOne/2) +var complexError = Double(Float.ulpOfOne/2) +var componentMaxInput = Complex.zero +var complexMaxInput = Complex.zero + +func test(_ z: Complex) { + let tst = Complex.log(z) + // TODO: we _should_ be able to say Complex(z), but that goes through + // the slow, generic path for BinaryFloatingPoint conversion; once we get + // that resolved in the standard library, we can replace the conversion on + // this and the next line. Currently it dominate testing time if we do that. + let ref = Complex.log(Complex(Double(z.real), Double(z.imaginary))) + if tst == Complex(Float(ref.real), Float(ref.imaginary)) { return } + let thisError = relativeError(tst, ref) + if thisError > complexError { + complexMaxInput = z + complexError = thisError + } + let thisComponentError = componentwiseError(tst, ref) + if thisComponentError > componentError { + componentMaxInput = z + componentError = thisComponentError + } +} + +func testWithSymmetries(_ x: Float, _ y: Float) { + test(Complex( x, y)) + test(Complex( y, x)) + test(Complex(-y, x)) + test(Complex(-x, y)) + test(Complex(-x,-y)) + test(Complex(-y,-x)) + test(Complex( y,-x)) + test(Complex( x,-y)) +} + +// The hardest to evaluate cases for log are those close to the unit circle, +// where log(z) is nearly zero. We want to have plausibly dense test coverage +// close to the circle. +let radii: [Float] = [0.9, + 0.95, + 0.99, + 0.999, + 0.9999, + 1 - 10 * .ulpOfOne, + 1, + 1 + 10 * .ulpOfOne, + 1.0001, + 1.001, + 1.01, + 1.05, + 1.1] + +for r in radii { + for x in Interval(from: 1/Float.sqrt(2), to: r) { + // Generate the two y values that put us closest to the circle of radius r + let base = Double.sqrt(Double(r).addingProduct(Double(-x), Double(x))) + let a, b: Float + if Double(Float(base)) < base { a = Float(base); b = a.nextUp } + else { b = Float(base); a = b.nextDown } + testWithSymmetries(x, a) + testWithSymmetries(x, b) + } +} + +// Away from the unit circle is "easy" but we still want to get some coverage. +// Generate 1 million uniform random points inside the circle, and then test +// both z and 1/z. +var g = SystemRandomNumberGenerator() +var count = 0 +while count < 1_000_000 { + let z = Complex(.random(in: -1 ... 1, using: &g), .random(in: -1 ... 1, using: &g)) + if z.length > 1 { continue } + count += 1 + test(z) + test(1/z) +} + +print("Worst complex norm error seen for log was \(complexError)") +print("For input \(complexMaxInput).") +print("Reference result: \(Complex.log(Complex(complexMaxInput)))") +print(" Observed result: \(Complex.log(complexMaxInput))") + +print("Worst componentwise error seen for log was \(componentError)") +print("For input \(componentMaxInput).") +print("Reference result: \(Complex.log(Complex(componentMaxInput)))") +print(" Observed result: \(Complex.log(componentMaxInput))") + diff --git a/Tests/Executable/ComplexLog1p/main.swift b/Tests/Executable/ComplexLog1p/main.swift new file mode 100644 index 0000000..b59981c --- /dev/null +++ b/Tests/Executable/ComplexLog1p/main.swift @@ -0,0 +1,107 @@ +//===--- main.swift -------------------------------------------*- swift -*-===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2020 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +import Numerics +import _TestSupport + +#if DEBUG +fatalError("Run this test in Release configuration") +#endif + +var componentError = Double(Float.ulpOfOne/2) +var complexError = Double(Float.ulpOfOne/2) +var componentMaxInput = Complex.zero +var complexMaxInput = Complex.zero + +func test(_ z: Complex) { + let tst = Complex.log(onePlus: z) + // TODO: we _should_ be able to say Complex(z), but that goes through + // the slow, generic path for BinaryFloatingPoint conversion; once we get + // that resolved in the standard library, we can replace the conversion on + // this and the next line. Currently it dominate testing time if we do that. + let ref = Complex.log(onePlus: Complex(Double(z.real), Double(z.imaginary))) + if tst == Complex(Float(ref.real), Float(ref.imaginary)) { return } + let thisError = relativeError(tst, ref) + if thisError > complexError { + complexMaxInput = z + complexError = thisError + } + let thisComponentError = componentwiseError(tst, ref) + if thisComponentError > componentError { + componentMaxInput = z + componentError = thisComponentError + } +} + +func testWithSymmetry(_ x: Float, _ y: Float) { + test(Complex( x-1, y)) + test(Complex( y-1, x)) + test(Complex(-y-1, x)) + test(Complex(-x-1, y)) + test(Complex(-x-1,-y)) + test(Complex(-y-1,-x)) + test(Complex( y-1,-x)) + test(Complex( x-1,-y)) +} + +// The hardest to evaluate cases for log are those close to the unit circle +// centered at -1, where log(1+z) is nearly zero. We want to have plausibly +// dense test coverage close to the circle. +let radii: [Float] = [0.9, + 0.95, + 0.99, + 0.999, + 0.9999, + 1 - 10 * .ulpOfOne, + 1, + 1 + 10 * .ulpOfOne, + 1.0001, + 1.001, + 1.01, + 1.05, + 1.1] + +for r in radii { + for x in Interval(from: 1/Float.sqrt(2), to: r) { + // Generate the two y values that put us closest to the circle of radius r + let base = Double.sqrt(Double(r).addingProduct(Double(-x), Double(x))) + let a, b: Float + if Double(Float(base)) < base { a = Float(base); b = a.nextUp } + else { b = Float(base); a = b.nextDown } + testWithSymmetry(x, a) + testWithSymmetry(x, b) + } +} + +// Away from the unit circle is "easy" but we still want to get some coverage. +// Generate 1 million uniform random points inside the circle, and then test +// both z and 1/z. +var g = SystemRandomNumberGenerator() +var count = 0 +while count < 1_000_000 { + let z = Complex(.random(in: -1 ... 1, using: &g), .random(in: -1 ... 1, using: &g)) + if z.length > 1 { continue } + count += 1 + test(z - 1) + test(1/z - 1) +} + +print("Worst complex norm error seen for log(onePlus:) was \(complexError)") +print("For input \(complexMaxInput).") +print("Reference result: \(Complex.log(onePlus: Complex(complexMaxInput)))") +print(" Observed result: \(Complex.log(onePlus: complexMaxInput))") + +print("Worst componentwise error seen for log(onePlus:) was \(componentError)") +print("For input \(componentMaxInput).") +print("Reference result: \(Complex.log(onePlus: Complex(componentMaxInput)))") +print(" Observed result: \(Complex.log(onePlus: componentMaxInput))") +