mirror of
https://github.com/apple/swift-numerics
synced 2026-11-27 05:04:26 +00:00
Merge pull request #152 from stephentyrone/complex-trig
First pass over hyperbolics and trig functions for Complex.
This commit is contained in:
2 files changed
+241
-75
No files matched your search
@@ -22,6 +22,10 @@
|
||||
// to do that for all of these functions off the top of my head, and
|
||||
// I don't think that other libraries have tried to do so in general,
|
||||
// so this is a research project. We should not sacrifice 1-4 for it.
|
||||
// Note that multiplication and division don't even provide good
|
||||
// componentwise relative accuracy, so it's _totally OK_ to not get
|
||||
// it for these functions too. But: it's a dynamite long-term research
|
||||
// project.
|
||||
// 6. Give the best performance we can. We should care about performance,
|
||||
// but with lower precedence than the other considerations.
|
||||
|
||||
@@ -31,87 +35,83 @@ import RealModule
|
||||
extension Complex /*: ElementaryFunctions */ {
|
||||
|
||||
// MARK: - exp-like functions
|
||||
/// Checks if x is bounded away overflowing exp(x).
|
||||
///
|
||||
/// This is a conservative (imprecise) check; if it returns `true`, `exp(x)` is definitely safe, but
|
||||
/// it will return `false` even in some cases where `exp(x)` would not overflow.
|
||||
@usableFromInline @inline(__always)
|
||||
internal static func expIsSafe(_ x: RealType) -> Bool {
|
||||
// If x < log(greatestFiniteMagnitude), then exp(x) does not overflow.
|
||||
// To protect ourselves against sketchy log or exp implementations in
|
||||
// an unknown host library, we round down to the nearest integer to get
|
||||
// some margin of safety.
|
||||
return x < RealType.log(.greatestFiniteMagnitude).rounded(.down)
|
||||
}
|
||||
|
||||
/// Computes exp(z) with extra care near the overflow boundary.
|
||||
/// The complex exponential function e^z whose base `e` is the base of the natural logarithm.
|
||||
///
|
||||
/// When x = z.real is large, exp(x) may overflow even when exp(z) is finite,
|
||||
/// because exp(z) = exp(x) * (cos(y) + i sin(y)), and max(cos(y),sin(y)) may
|
||||
/// be as small as 1/sqrt(2).
|
||||
///
|
||||
/// - Parameter z: a complex number with large real part.
|
||||
@usableFromInline
|
||||
internal static func expNearOverflow(_ z: Complex) -> Complex {
|
||||
let xm1 = z.x - 1
|
||||
let y = z.y
|
||||
let r = Complex(.cos(y), .sin(y)).multiplied(by: .exp(1))
|
||||
return r.multiplied(by: .exp(xm1))
|
||||
}
|
||||
|
||||
// exp(x + iy) = exp(x)(cos(y) + i sin(y))
|
||||
/// Mathematically, this operation can be expanded in terms of the `Real` operations `exp`,
|
||||
/// `cos` and `sin` as follows:
|
||||
/// ```
|
||||
/// exp(x + iy) = exp(x) exp(iy)
|
||||
/// = exp(x) cos(y) + i exp(x) sin(y)
|
||||
/// ```
|
||||
/// Note that naive evaluation of this expression in floating-point would be prone to premature
|
||||
/// overflow, since `cos` and `sin` both have magnitude less than 1 for most inputs (i.e.
|
||||
/// `exp(x)` may be infinity when `exp(x) cos(y)` would not be.
|
||||
@inlinable
|
||||
public static func exp(_ z: Complex) -> Complex {
|
||||
// Naively we would let exp(-∞,0) fall out as 0, matching the real
|
||||
// type behavior, but that breaks the single-point-at-infinity
|
||||
// semantics, because exp has an essential singularity at infinity.
|
||||
guard z.x != -.infinity else { return .infinity }
|
||||
guard expIsSafe(z.x) else { return expNearOverflow(z) }
|
||||
guard z.isFinite else { return z }
|
||||
// If x < log(greatestFiniteMagnitude), then exp(x) does not overflow.
|
||||
// To protect ourselves against sketchy log or exp implementations in
|
||||
// an unknown host library, or slight rounding disagreements between
|
||||
// the two, subtract one from the bound for a little safety margin.
|
||||
guard z.x < RealType.log(.greatestFiniteMagnitude) - 1 else {
|
||||
let halfScale = RealType.exp(z.x/2)
|
||||
let phase = Complex(RealType.cos(z.y), RealType.sin(z.y))
|
||||
return phase.multiplied(by: halfScale).multiplied(by: halfScale)
|
||||
}
|
||||
return Complex(.cos(z.y), .sin(z.y)).multiplied(by: .exp(z.x))
|
||||
}
|
||||
|
||||
// exp(x + iy) - 1 = (exp(x) cos(y) - 1) + i exp(x) sin(y)
|
||||
// -------- u --------
|
||||
// Note that the imaginary part is just the usual exp(x) sin(y);
|
||||
// the only trick is computing the real part ("u"):
|
||||
//
|
||||
// u = exp(x) cos(y) - 1
|
||||
// = exp(x) cos(y) - cos(y) + cos(y) - 1
|
||||
// = (exp(x) - 1) cos(y) + (cos(y) - 1)
|
||||
// = expMinusOne(x) cos(y) + cosMinusOne(y)
|
||||
//
|
||||
// Note: most implementations of expm1 for complex (e.g. Julia's)
|
||||
// factor the real part as follows instead:
|
||||
//
|
||||
// exp(x) cosMinuxOne(y) + expMinusOne(y)
|
||||
//
|
||||
// This expression gives good accuracy close to zero, but suffers from
|
||||
// catastrophic cancellation when z.x is large and z.y is near an odd
|
||||
// multiple of π/2. This is _OK_ (the componentwise error is bad, but
|
||||
// the error in a complex norm is acceptable), but we can do better by
|
||||
// factoring on cosine instead of exp.
|
||||
//
|
||||
// The other implementation that is sometimes seen, 2*exp(z/2)*sinh(z/2),
|
||||
// has the same weaknesses.
|
||||
//
|
||||
// The approach used here achieves good componentwise worst-case error
|
||||
// (7e-5 for Float) as well as normwise error (2.9e-7) in structured
|
||||
// and randomized tests. The alternative factorization achieves
|
||||
// comparable normwise error (3.9e-7), but dramatically worse
|
||||
// componentwise errors, e.g. Complex(18, -3π/2) produces (4.0, 6.57e7)
|
||||
// while the reference result would be (-0.22, 6.57e7).
|
||||
@inlinable
|
||||
public static func expMinusOne(_ z: Complex) -> Complex {
|
||||
// Naively we would let exp(-∞,0) fall out as 0, matching the real
|
||||
// type behavior, but that breaks the single-point-at-infinity
|
||||
// semantics, because exp has an essential singularity at infinity.
|
||||
guard z.x != -.infinity else { return .infinity }
|
||||
// exp(x + iy) - 1 = (exp(x) cos(y) - 1) + i exp(x) sin(y)
|
||||
// -------- u --------
|
||||
// Note that the imaginary part is just the usual exp(x) sin(y);
|
||||
// the only trick is computing the real part ("u"):
|
||||
//
|
||||
// u = exp(x) cos(y) - 1
|
||||
// = exp(x) cos(y) - cos(y) + cos(y) - 1
|
||||
// = (exp(x) - 1) cos(y) + (cos(y) - 1)
|
||||
// = expMinusOne(x) cos(y) + cosMinusOne(y)
|
||||
//
|
||||
// Note: most implementations of expm1 for complex (e.g. Julia's)
|
||||
// factor the real part as follows instead:
|
||||
//
|
||||
// exp(x) cosMinuxOne(y) + expMinusOne(x)
|
||||
//
|
||||
// The other implementation that is sometimes seen is:
|
||||
//
|
||||
// expMinusOne(z) = 2*exp(z/2)*sinh(z/2)
|
||||
//
|
||||
// All three of these implementations provide good relative error
|
||||
// bounds _in the complex norm_, but the cosineMinusOne-based
|
||||
// implementation has the best _componentwise_ error characteristics,
|
||||
// which is why we use it here:
|
||||
//
|
||||
// Implementation | Real | Imaginary |
|
||||
// ---------------+--------------------+----------------+
|
||||
// Ours | Hybrid bound | Relative bound |
|
||||
// Standard | No bound | Relative bound |
|
||||
// Half Angle | Hybrid bound | Hybrid bound |
|
||||
//
|
||||
// FUTURE WORK: devise an algorithm that achieves good _relative_ error
|
||||
// in the real component as well. Doing this efficiently is a research
|
||||
// project--exp(x) cos(y) - 1 can be very nearly zero along a curve in
|
||||
// the complex plane, not only at zero. Evaluating it accurately
|
||||
// _without_ depending on arbitrary-precision exp and cos is an
|
||||
// interesting challenge.
|
||||
guard z.isFinite else { return z }
|
||||
// If exp(z) is close to the overflow boundary, we don't need to
|
||||
// worry about the m1 part; we're just computing exp(z). (Even when
|
||||
// z.y is near a multiple of π/2, it can't be close enough to
|
||||
// overcome the scaling from exp(z.x), so the -1 term is _always_
|
||||
// negligable).
|
||||
guard expIsSafe(z.x) else { return expNearOverflow(z) }
|
||||
// worry about the "MinusOne" part of this function; we're just
|
||||
// computing exp(z). (Even when z.y is near a multiple of π/2,
|
||||
// it can't be close enough to overcome the scaling from exp(z.x),
|
||||
// so the -1 term is _always_ negligable). So we simply handle
|
||||
// these cases exactly the same as exp(z).
|
||||
guard z.x < RealType.log(.greatestFiniteMagnitude) - 1 else {
|
||||
let halfScale = RealType.exp(z.x/2)
|
||||
let phase = Complex(RealType.cos(z.y), RealType.sin(z.y))
|
||||
return phase.multiplied(by: halfScale).multiplied(by: halfScale)
|
||||
}
|
||||
// Special cases out of the way, evaluate as discussed above.
|
||||
return Complex(
|
||||
RealType._mulAdd(.cos(z.y), .expMinusOne(z.x), .cosMinusOne(z.y)),
|
||||
@@ -119,16 +119,86 @@ extension Complex /*: ElementaryFunctions */ {
|
||||
)
|
||||
}
|
||||
|
||||
// cosh(x + iy) = cosh(x) cos(y) + i sinh(x) sin(y).
|
||||
//
|
||||
// Like exp, cosh is entire, so we do not need to worry about where
|
||||
// branch cuts fall. Also like exp, cancellation never occurs in the
|
||||
// evaluation of the naive expression, so all we need to be careful
|
||||
// about is the behavior near the overflow boundary.
|
||||
//
|
||||
// Fortunately, if |x| >= -log(ulpOfOne), cosh(x) and sinh(x) are
|
||||
// both just exp(|x|)/2, and we already know how to compute that.
|
||||
//
|
||||
// This function and sinh should stay in sync; if you make a
|
||||
// modification here, you should almost surely make a parallel
|
||||
// modification to sinh below.
|
||||
@inlinable @inline(__always)
|
||||
public static func cosh(_ z: Complex) -> Complex {
|
||||
fatalError()
|
||||
guard z.isFinite else { return z }
|
||||
guard z.x.magnitude < -RealType.log(.ulpOfOne) else {
|
||||
let phase = Complex(RealType.cos(z.y), RealType.sin(z.y))
|
||||
let firstScale = RealType.exp(z.x.magnitude/2)
|
||||
let secondScale = firstScale/2
|
||||
return phase.multiplied(by: firstScale).multiplied(by: secondScale)
|
||||
}
|
||||
// Future optimization opportunity: expm1 is faster than cosh/sinh
|
||||
// on most platforms, and division is now commonly pipelined, so we
|
||||
// might replace the check above with a much more conservative one,
|
||||
// and then evaluate cosh(x) and sinh(x) as
|
||||
//
|
||||
// cosh(x) = 1 + 0.5*expm1(x)*expm1(x) / (1 + expm1(x))
|
||||
// sinh(x) = expm1(x) + 0.5*expm1(x) / (1 + expm1(x))
|
||||
//
|
||||
// This won't be a _big_ win except on platforms with a crappy sinh
|
||||
// and cosh, and for those we should probably just provide our own
|
||||
// implementations of _those_, so for now let's keep it simple and
|
||||
// obviously correct.
|
||||
return Complex(
|
||||
RealType.cosh(z.x) * RealType.cos(z.y),
|
||||
RealType.sinh(z.x) * RealType.sin(z.y)
|
||||
)
|
||||
}
|
||||
|
||||
// sinh(x + iy) = sinh(x) cos(y) + i cosh(x) sinh(y)
|
||||
//
|
||||
// See cosh above for algorithm details.
|
||||
@inlinable @inline(__always)
|
||||
public static func sinh(_ z: Complex) -> Complex {
|
||||
fatalError()
|
||||
guard z.isFinite else { return z }
|
||||
guard z.x.magnitude < -RealType.log(.ulpOfOne) else {
|
||||
let phase = Complex(RealType.cos(z.y), RealType.sin(z.y))
|
||||
let firstScale = RealType.exp(z.x.magnitude/2)
|
||||
let secondScale = RealType(signOf: z.x, magnitudeOf: firstScale/2)
|
||||
return phase.multiplied(by: firstScale).multiplied(by: secondScale)
|
||||
}
|
||||
return Complex(
|
||||
RealType.sinh(z.x) * RealType.cos(z.y),
|
||||
RealType.cosh(z.x) * RealType.sin(z.y)
|
||||
)
|
||||
}
|
||||
|
||||
// tanh(z) = sinh(z) / cosh(z)
|
||||
@inlinable
|
||||
public static func tanh(_ z: Complex) -> Complex {
|
||||
fatalError()
|
||||
guard z.isFinite else { return z }
|
||||
// Note that when |x| is larger than -log(.ulpOfOne),
|
||||
// sinh(x + iy) == ±cosh(x + iy), so tanh(x + iy) is just ±1.
|
||||
guard z.x.magnitude < -RealType.log(.ulpOfOne) else {
|
||||
return Complex(
|
||||
RealType(signOf: z.x, magnitudeOf: 1),
|
||||
RealType(signOf: z.y, magnitudeOf: 0)
|
||||
)
|
||||
}
|
||||
// Now we have z in a vertical strip where exp(x) is reasonable,
|
||||
// and y is finite, so we can simply evaluate sinh(z) and cosh(z).
|
||||
//
|
||||
// TODO: Kahan uses a different expression for evaluation here; it
|
||||
// isn't strictly necessary for numerics reasons--it's to avoid
|
||||
// doing the complex division, but it probably provides better
|
||||
// componentwise error bounds, and is likely more efficient (because
|
||||
// it avoids the complex division, which is painful even when well-
|
||||
// scaled). This suffices to get us up and running.
|
||||
return sinh(z) / cosh(z)
|
||||
}
|
||||
|
||||
// cos(z) = cosh(iz)
|
||||
@@ -149,6 +219,7 @@ extension Complex /*: ElementaryFunctions */ {
|
||||
}
|
||||
|
||||
// MARK: - log-like functions
|
||||
@inlinable
|
||||
public static func log(_ z: Complex) -> Complex {
|
||||
// If z is zero or infinite, the phase is undefined, so the result is
|
||||
// the single exceptional value.
|
||||
@@ -163,8 +234,68 @@ extension Complex /*: ElementaryFunctions */ {
|
||||
return Complex(.log(z.magnitude) + .log(w.lengthSquared)/2, θ)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public static func log(onePlus z: Complex) -> Complex {
|
||||
fatalError()
|
||||
// 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:
|
||||
//
|
||||
// Re(log 1+z) = (log 1+z + log 1+z̅)/2
|
||||
// = log((1+z)(1+z̅)/2
|
||||
// = log(1+z+z̅+zz̅)/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)
|
||||
)
|
||||
}
|
||||
|
||||
public static func acos(_ z: Complex) -> Complex {
|
||||
|
||||
@@ -112,20 +112,55 @@ final class ElementaryFunctionTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testCosh<T: Real & FixedWidthFloatingPoint>(_ type: T.Type) {
|
||||
// cosh(0) = 1
|
||||
XCTAssertEqual(1, Complex<T>.cosh(Complex( 0, 0)))
|
||||
XCTAssertEqual(1, Complex<T>.cosh(Complex(-0, 0)))
|
||||
XCTAssertEqual(1, Complex<T>.cosh(Complex(-0,-0)))
|
||||
XCTAssertEqual(1, Complex<T>.cosh(Complex( 0,-0)))
|
||||
// cosh is the identity at infinity.
|
||||
XCTAssertFalse(Complex<T>.cosh(Complex( .infinity, 0)).isFinite)
|
||||
XCTAssertFalse(Complex<T>.cosh(Complex( .infinity, .infinity)).isFinite)
|
||||
XCTAssertFalse(Complex<T>.cosh(Complex( 0, .infinity)).isFinite)
|
||||
XCTAssertFalse(Complex<T>.cosh(Complex(-.infinity, .infinity)).isFinite)
|
||||
XCTAssertFalse(Complex<T>.cosh(Complex(-.infinity, 0)).isFinite)
|
||||
XCTAssertFalse(Complex<T>.cosh(Complex(-.infinity,-.infinity)).isFinite)
|
||||
XCTAssertFalse(Complex<T>.cosh(Complex( 0,-.infinity)).isFinite)
|
||||
XCTAssertFalse(Complex<T>.cosh(Complex( .infinity,-.infinity)).isFinite)
|
||||
XCTAssertFalse(Complex<T>.cosh(Complex( .nan, .nan)).isFinite)
|
||||
XCTAssertFalse(Complex<T>.cosh(Complex( .infinity, .nan)).isFinite)
|
||||
XCTAssertFalse(Complex<T>.cosh(Complex( .nan, .infinity)).isFinite)
|
||||
XCTAssertFalse(Complex<T>.cosh(Complex(-.infinity, .nan)).isFinite)
|
||||
XCTAssertFalse(Complex<T>.cosh(Complex( .nan,-.infinity)).isFinite)
|
||||
// Near-overflow test, same as exp() above, but it happens later, because
|
||||
// for large x, cosh(x + iy) ~ exp(x + iy)/2.
|
||||
let x = T.log(.greatestFiniteMagnitude) + T.log(18/8)
|
||||
let mag = T.greatestFiniteMagnitude/T.sqrt(2) * (9/8)
|
||||
var huge = Complex<T>.cosh(Complex(x, .pi/4))
|
||||
XCTAssert(huge.real.isApproximatelyEqual(to: mag))
|
||||
XCTAssert(huge.imaginary.isApproximatelyEqual(to: mag))
|
||||
huge = Complex<T>.cosh(Complex(-x, .pi/4))
|
||||
XCTAssert(huge.real.isApproximatelyEqual(to: mag))
|
||||
XCTAssert(huge.imaginary.isApproximatelyEqual(to: mag))
|
||||
}
|
||||
|
||||
func testFloat() {
|
||||
testExp(Float.self)
|
||||
testExpMinusOne(Float.self)
|
||||
testCosh(Float.self)
|
||||
}
|
||||
|
||||
func testDouble() {
|
||||
testExp(Double.self)
|
||||
testExpMinusOne(Double.self)
|
||||
testCosh(Double.self)
|
||||
}
|
||||
|
||||
#if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android)
|
||||
func testFloat80() {
|
||||
testExp(Float80.self)
|
||||
testExpMinusOne(Float80.self)
|
||||
testCosh(Float80.self)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
Reference in new issue
Block a user