mirror of
https://github.com/apple/swift-numerics
synced 2026-11-26 04:57:53 +00:00
Remove log and pow functions
This commit is contained in:
1 parent
bc944e79f0
commit
aa376839bf
2 files changed
-169
No files matched your search
@@ -202,136 +202,6 @@ extension Quaternion/*: ElementaryFunctions */ {
|
||||
let p = Quaternion(imaginary: â)
|
||||
return -p * tanh(q * p)
|
||||
}
|
||||
|
||||
// MARK: - log-like functions
|
||||
@inlinable
|
||||
public static func log(_ q: Quaternion) -> Quaternion {
|
||||
// If q is zero or infinite, the phase is undefined, so the result is
|
||||
// the single exceptional value.
|
||||
guard q.isFinite && !q.isZero else { return .infinity }
|
||||
|
||||
let argument = q.imaginary.length
|
||||
let axis = q.imaginary / argument
|
||||
|
||||
// We deliberatly choose log(length) over the (faster)
|
||||
// log(lengthSquared) / 2 which is used for complex numbers; as
|
||||
// the squared length of quaternions is more prone to overflows than the
|
||||
// squared length of complex numbers.
|
||||
return Quaternion(real: .log(q.length), imaginary: axis * q.halfAngle)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public static func log(onePlus q: Quaternion) -> Quaternion {
|
||||
// If either |r| or ||v||₁ 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 sphere |1+q| = 1 where log(onePlus:)
|
||||
// vanishes (where r <= -0.5), but on this portion of the sphere 1+r
|
||||
// is always exact by Sterbenz' lemma, so as long as log( ) produces
|
||||
// a good result, log(1+q) will too.
|
||||
guard 2*q.real.magnitude < 1 && q.imaginary.oneNorm < 1 else {
|
||||
return log(.one + q)
|
||||
}
|
||||
// q is in (±0.5, ±1), so we need to evaluate more carefully.
|
||||
// The imaginary part is straightforward:
|
||||
let argument = (.one + q).halfAngle
|
||||
let (â,_) = q.imaginary.unitAxisAndLength
|
||||
let imaginary = â * argument
|
||||
// For the real part, we _could_ use the same approach that we do for
|
||||
// log( ), but we'd need an extra-precise (1+r)², which can potentially
|
||||
// be quite painful to calculate. Instead, we can use an approach that
|
||||
// NevinBR suggested on the Swift forums for complex numbers:
|
||||
//
|
||||
// Re(log 1+q) = (log 1+q + log 1+q̅)/2
|
||||
// = log((1+q)(1+q̅)/2
|
||||
// = log(1 + q + q̅ + qq̅)/2
|
||||
// = log1p((2+r)r + x² + y² + z²)/2
|
||||
//
|
||||
// So now we need to evaluate (2+r)r + x² + y² + z² accurately. To do this,
|
||||
// we employ augmented arithmetic;
|
||||
// (2+r)r + x² + y² + z²
|
||||
// --↓--
|
||||
let rp2 = Augmented.fastTwoSum(2, q.real) // Known that 2 > |r|
|
||||
var (head, δ) = Augmented.twoProdFMA(q.real, rp2.head)
|
||||
var tail = δ
|
||||
// head + x² + y² + z²
|
||||
// ----↓----
|
||||
let x² = Augmented.twoProdFMA(q.imaginary.x, q.imaginary.x)
|
||||
(head, δ) = Augmented.twoSum(head, x².head)
|
||||
tail += (δ + x².tail)
|
||||
// head + y² + z²
|
||||
// ----↓----
|
||||
let y² = Augmented.twoProdFMA(q.imaginary.y, q.imaginary.y)
|
||||
(head, δ) = Augmented.twoSum(head, y².head)
|
||||
tail += (δ + y².tail)
|
||||
// head + z²
|
||||
// ----↓----
|
||||
let z² = Augmented.twoProdFMA(q.imaginary.z, q.imaginary.z)
|
||||
(head, δ) = Augmented.twoSum(head, z².head)
|
||||
tail += (δ + z².tail)
|
||||
|
||||
let s = (head + tail).addingProduct(q.real, rp2.tail)
|
||||
return Quaternion(real: .log(onePlus: s)/2, imaginary: imaginary)
|
||||
}
|
||||
|
||||
//
|
||||
// MARK: - pow-like functions
|
||||
|
||||
@inlinable
|
||||
public static func pow(_ q: Quaternion, _ p: Quaternion) -> Quaternion {
|
||||
// Mathematically, this operation can be expanded in terms of the
|
||||
// quaternionic `exp` and `log` operations as follows:
|
||||
//
|
||||
// ```
|
||||
// pow(q, p) = exp(log(pow(q, p)))
|
||||
// = exp(p * log(q))
|
||||
// ```
|
||||
exp(p * log(q))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public static func pow(_ q: Quaternion, _ n: Int) -> Quaternion {
|
||||
// Mathematically, this operation can be expanded in terms of the
|
||||
// quaternionic `exp` and `log` operations as follows:
|
||||
//
|
||||
// ```
|
||||
// pow(q, n) = exp(log(pow(q, n)))
|
||||
// = exp(log(q) * n)
|
||||
// ```
|
||||
guard !q.isZero else { 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.
|
||||
return exp(log(q).multiplied(by: RealType(n)))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public static func sqrt(_ q: Quaternion) -> Quaternion<RealType> {
|
||||
// Mathematically, this operation can be expanded in terms of the
|
||||
// quaternionic `exp` and `log` operations as follows:
|
||||
//
|
||||
// ```
|
||||
// sqrt(q) = q^(1/2) = exp(log(q^(1/2)))
|
||||
// = exp(log(q) * (1/2))
|
||||
// ```
|
||||
guard !q.isZero else { return .zero }
|
||||
return exp(log(q).divided(by: 2))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public static func root(_ q: Quaternion, _ n: Int) -> Quaternion {
|
||||
// Mathematically, this operation can be expanded in terms of the
|
||||
// quaternionic `exp` and `log` operations as follows:
|
||||
//
|
||||
// ```
|
||||
// root(q, n) = exp(log(root(q, n)))
|
||||
// = exp(log(q) / n)
|
||||
// ```
|
||||
guard !q.isZero else { 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.
|
||||
return exp(log(q).divided(by: RealType(n)))
|
||||
}
|
||||
}
|
||||
|
||||
extension SIMD3 where Scalar: FloatingPoint {
|
||||
@@ -349,18 +219,3 @@ extension SIMD3 where Scalar: FloatingPoint {
|
||||
return (self/length, length)
|
||||
}
|
||||
}
|
||||
|
||||
extension Augmented {
|
||||
|
||||
// TODO: Move to Augmented.swift
|
||||
@usableFromInline @_transparent
|
||||
internal static func twoSum<T:Real>(_ a: T, _ b: T) -> (head: T, tail: T) {
|
||||
let head = a + b
|
||||
let x = head - b
|
||||
let y = head - x
|
||||
let ax = a - x
|
||||
let by = b - y
|
||||
let tail = ax + by
|
||||
return (head, tail)
|
||||
}
|
||||
}
|
||||
@@ -234,34 +234,12 @@ final class ElementaryFunctionTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - log-like functions
|
||||
|
||||
func testLog<T: Real & FixedWidthFloatingPoint & SIMDScalar>(_ type: T.Type) {
|
||||
// log(0) = undefined/infinity
|
||||
XCTAssertFalse(Quaternion<T>.log(Quaternion(real: 0, imaginary: 0, 0, 0)).isFinite)
|
||||
XCTAssertFalse(Quaternion<T>.log(Quaternion(real:-0, imaginary: 0, 0, 0)).isFinite)
|
||||
XCTAssertFalse(Quaternion<T>.log(Quaternion(real:-0, imaginary:-0,-0,-0)).isFinite)
|
||||
XCTAssertFalse(Quaternion<T>.log(Quaternion(real: 0, imaginary:-0,-0,-0)).isFinite)
|
||||
|
||||
var g = SystemRandomNumberGenerator()
|
||||
let values: [Quaternion<T>] = (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)
|
||||
testCosh(Float32.self)
|
||||
testSinh(Float32.self)
|
||||
testCosSin(Float32.self)
|
||||
|
||||
testLog(Float32.self)
|
||||
}
|
||||
|
||||
func testDouble() {
|
||||
@@ -270,7 +248,5 @@ final class ElementaryFunctionTests: XCTestCase {
|
||||
testCosh(Float64.self)
|
||||
testSinh(Float64.self)
|
||||
testCosSin(Float64.self)
|
||||
|
||||
testLog(Float64.self)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user