diff --git a/Sources/QuaternionModule/ElementaryFunctions.swift b/Sources/QuaternionModule/ElementaryFunctions.swift index 928ca15..63e087c 100644 --- a/Sources/QuaternionModule/ElementaryFunctions.swift +++ b/Sources/QuaternionModule/ElementaryFunctions.swift @@ -21,23 +21,16 @@ extension Quaternion/*: ElementaryFunctions */ { // MARK: - exp-like functions - // Mathematically, this operation can be expanded in terms of the `Real` - // operations `exp`, `cos` and `sin` as follows: - // ``` // exp(r + xi + yj + zk) = exp(r + v) = exp(r) exp(v) // = exp(r) (cos(||v||) + (v/||v||) sin(||v||)) - // ``` - // 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(r)` may be infinity when - // `exp(r) cos(arg)` would not be). + // + // See exp on complex numbers for algorithm details. @inlinable public static func exp(_ q: Quaternion) -> Quaternion { guard q.isFinite else { return q } // For real quaternions we can skip phase and axis calculations - // TODO: Replace q.imaginary == .zero with `q.isReal` - let argument = q.imaginary == .zero ? .zero : q.imaginary.length - let axis = q.imaginary == .zero ? .zero : (q.imaginary / argument) + let argument = q.isReal ? .zero : q.imaginary.length + let axis = q.isReal ? .zero : (q.imaginary / argument) // If real < log(greatestFiniteMagnitude), then exp(q.real) does not overflow. // To protect ourselves against sketchy log or exp implementations in // an unknown host library, or slight rounding disagreements between @@ -55,11 +48,10 @@ extension Quaternion/*: ElementaryFunctions */ { // Note that the imaginary part is just the usual exp(r) sin(argument); // the only trick is computing the real part, which allows us to borrow // the derivative of real part for this function from complex numbers. - // See `expMinusOne` in the ComplexModule for implementation details. + // See `expMinusOne` in the ComplexModule for implementation details. guard q.isFinite else { return q } - // TODO: Replace q.imaginary == .zero with `q.isReal` - let argument = q.imaginary == .zero ? .zero : q.imaginary.length - let axis = q.imaginary == .zero ? .zero : (q.imaginary / argument) + let argument = q.isReal ? .zero : q.imaginary.length + let axis = q.isReal ? .zero : (q.imaginary / argument) // If exp(q) is close to the overflow boundary, we don't need to // worry about the "MinusOne" part of this function; we're just // computing exp(q). (Even when q.y is near a multiple of π/2, @@ -78,28 +70,26 @@ extension Quaternion/*: ElementaryFunctions */ { } // cosh(r + xi + yj + zk) = cosh(r + v) - // cosh(r + v) = cosh(r) cos(||v||) + (v/||v||) sinh(r) sin(||v||). + // = cosh(r) cos(||v||) + (v/||v||) sinh(r) sin(||v||) // // See cosh on complex numbers for algorithm details. @inlinable public static func cosh(_ q: Quaternion) -> Quaternion { guard q.isFinite else { return q } - // TODO: Replace q.imaginary == .zero with `q.isReal` - let argument = q.imaginary == .zero ? .zero : q.imaginary.length - let axis = q.imaginary == .zero ? .zero : (q.imaginary / argument) + let argument = q.isReal ? .zero : q.imaginary.length + let axis = q.isReal ? .zero : (q.imaginary / argument) return cosh(q.real, argument, axis: axis) } // sinh(r + xi + yj + zk) = sinh(r + v) - // sinh(r + v) = sinh(r) cos(||v||) + (v/||v||) cosh(r) sin(||v||) + // = sinh(r) cos(||v||) + (v/||v||) cosh(r) sin(||v||) // - // See cosh on complex numbers for algorithm details. + // See sinh on complex numbers for algorithm details. @inlinable public static func sinh(_ q: Quaternion) -> Quaternion { guard q.isFinite else { return q } - // TODO: Replace q.imaginary == .zero with `q.isReal` - let argument = q.imaginary == .zero ? .zero : q.imaginary.length - let axis = q.imaginary == .zero ? .zero : (q.imaginary / argument) + let argument = q.isReal ? .zero : q.imaginary.length + let axis = q.isReal ? .zero : (q.imaginary / argument) guard q.real.magnitude < -RealType.log(.ulpOfOne) else { let rotation = Quaternion(halfAngle: argument, unitAxis: axis) let firstScale = RealType.exp(q.real.magnitude/2) @@ -133,25 +123,26 @@ extension Quaternion/*: ElementaryFunctions */ { } // cos(r + xi + yj + zk) = cos(r + v) - // cos(r + v) = cos(r) cosh(||v||) - (v/||v||) sin(r) sinh(||v||). + // = cos(r) cosh(||v||) - (v/||v||) sin(r) sinh(||v||) // // See cosh for algorithm details. @inlinable public static func cos(_ q: Quaternion) -> Quaternion { guard q.isFinite else { return q } - // TODO: Replace q.imaginary == .zero with `q.isReal` - let argument = q.imaginary == .zero ? .zero : q.imaginary.length - let axis = q.imaginary == .zero ? .zero : (q.imaginary / argument) + let argument = q.isReal ? .zero : q.imaginary.length + let axis = q.isReal ? .zero : (q.imaginary / argument) return cosh(-argument, q.real, axis: axis) } - // See sinh on complex numbers for algorithm details. + // sin(r + xi + yj + zk) = sin(r + v) + // = sin(r) cosh(-||v||) - (v/||v||) cos(r) sinh(-||v||) + // + // See sinh for algorithm details. @inlinable public static func sin(_ q: Quaternion) -> Quaternion { guard q.isFinite else { return q } - // TODO: Replace q.imaginary == .zero with `q.isReal` - let argument = q.imaginary == .zero ? .zero : q.imaginary.length - let axis = q.imaginary == .zero ? .zero : (q.imaginary / argument) + let argument = q.isReal ? .zero : q.imaginary.length + let axis = q.isReal ? .zero : (q.imaginary / argument) let (x, y) = sinh(-argument, q.real) return Quaternion(real: y, imaginary: axis * -x) } @@ -182,42 +173,36 @@ extension Quaternion/*: ElementaryFunctions */ { } // MARK: - pow-like functions + + // pow(q, p) = exp(log(pow(q, p))) = exp(p * log(q)) + // + // See pow on complex numbers for algorithm details. @inlinable public static func pow(_ q: Quaternion, _ p: Quaternion) -> Quaternion { - // pow(q, p) = exp(log(q^p)) = exp(p * log(q)) return exp(p * log(q)) } + // pow(q, n) = exp(log(q) * n) + // + // See pow on complex numbers for algorithm details. @inlinable public static func pow(_ q: Quaternion, _ n: Int) -> Quaternion { if q.isZero { return .zero } - // TODO: this implementation is not quite correct, because n may be - // rounded in conversion to RealType. This only effects very extreme - // cases, so we'll leave it alone for now. - // - // Note that this does not have the same problems that a similar - // implementation for a real type would have, because there's no - // parity/sign interaction in the complex plane. return exp(log(q).multiplied(by: RealType(n))) } @inlinable public static func sqrt(_ q: Quaternion) -> Quaternion { if q.isZero { return .zero } - // TODO: This is not the fastest implementation available return exp(log(q).divided(by: 2)) } + // root(q, n) = exp(log(q) / n) + // + // See root on complex numbers for algorithm details. @inlinable public static func root(_ q: Quaternion, _ n: Int) -> Quaternion { if q.isZero { return .zero } - // TODO: this implementation is not quite correct, because n may be - // rounded in conversion to RealType. This only effects very extreme - // cases, so we'll leave it alone for now. - // - // Note that this does not have the same problems that a similar - // implementation for a real type would have, because there's no - // parity/sign interaction in the complex plane. return exp(log(q).divided(by: RealType(n))) } } @@ -244,7 +229,7 @@ extension Quaternion { ) } - // See sinh of complex numbers for algorithm details. + // See sinh of complex numbers for algorithm details. @usableFromInline @_transparent internal static func sinh( _ x: RealType, diff --git a/Tests/QuaternionTests/ElementaryFunctionTests.swift b/Tests/QuaternionTests/ElementaryFunctionTests.swift index bf35dd2..fc5620e 100644 --- a/Tests/QuaternionTests/ElementaryFunctionTests.swift +++ b/Tests/QuaternionTests/ElementaryFunctionTests.swift @@ -17,6 +17,8 @@ import _TestSupport final class ElementaryFunctionTests: XCTestCase { + // MARK: - exp-like functions + func testExp(_ type: T.Type) { // exp(0) = 1 XCTAssertEqual(1, Quaternion.exp(Quaternion(real: 0, imaginary: 0, 0, 0))) @@ -209,24 +211,6 @@ final class ElementaryFunctionTests: XCTestCase { } } - func testLog(_ type: T.Type) { - // log(0) = undefined/infinity - XCTAssertFalse(Quaternion.log(Quaternion(real: 0, imaginary: 0, 0, 0)).isFinite) - XCTAssertFalse(Quaternion.log(Quaternion(real:-0, imaginary: 0, 0, 0)).isFinite) - XCTAssertFalse(Quaternion.log(Quaternion(real:-0, imaginary:-0,-0,-0)).isFinite) - XCTAssertFalse(Quaternion.log(Quaternion(real: 0, imaginary:-0,-0,-0)).isFinite) - - var g = SystemRandomNumberGenerator() - let values: [Quaternion] = (0..<100).map { _ in - Quaternion( - real: T.random(in: -1 ... 1, using: &g), - imaginary: SIMD3(repeating: T.random(in: -.pi ... .pi, using: &g) / 3)) - } - for q in values { - XCTAssertTrue(q.isApproximatelyEqual(to: .log(.exp(q)))) - } - } - func testCos(_ type: T.Type) { var g = SystemRandomNumberGenerator() let values: [Quaternion] = (0..<1000).map { _ in @@ -264,7 +248,7 @@ final class ElementaryFunctionTests: XCTestCase { let s = Quaternion.sin(q) // For randomly-chosen well-scaled finite values, we expect to have - // cos ≈ (e^(q*||v||)+e^(-q*||v||)) / 2 + // sin ≈ (e^(q*||v||)+e^(-q*||v||)) / 2 let p = Quaternion(imaginary: q.imaginary / q.imaginary.length) let e = (.exp(p * q) - .exp(-p * q)) / (p * 2) XCTAssert(s.isApproximatelyEqual(to: e)) @@ -276,6 +260,26 @@ final class ElementaryFunctionTests: XCTestCase { } } + // MARK: - log-like functions + + func testLog(_ type: T.Type) { + // log(0) = undefined/infinity + XCTAssertFalse(Quaternion.log(Quaternion(real: 0, imaginary: 0, 0, 0)).isFinite) + XCTAssertFalse(Quaternion.log(Quaternion(real:-0, imaginary: 0, 0, 0)).isFinite) + XCTAssertFalse(Quaternion.log(Quaternion(real:-0, imaginary:-0,-0,-0)).isFinite) + XCTAssertFalse(Quaternion.log(Quaternion(real: 0, imaginary:-0,-0,-0)).isFinite) + + var g = SystemRandomNumberGenerator() + let values: [Quaternion] = (0..<100).map { _ in + Quaternion( + real: T.random(in: -1 ... 1, using: &g), + imaginary: SIMD3(repeating: T.random(in: -.pi ... .pi, using: &g) / 3)) + } + for q in values { + XCTAssertTrue(q.isApproximatelyEqual(to: .log(.exp(q)))) + } + } + func testFloat() { testExp(Float32.self) testExpMinusOne(Float32.self)