mirror of
https://github.com/apple/swift-numerics
synced 2026-11-28 05:14:31 +00:00
Merge pull request #353 from stephentyrone/1.1-backports-from-main
[1.1] backports from main
This commit is contained in:
3 files changed
+32
-6
No files matched your search
@@ -155,9 +155,17 @@ extension Complex: AlgebraicField {
|
||||
///
|
||||
/// Error Bounds:
|
||||
///
|
||||
/// Unlike real types, when working with complex types, multiplying by the
|
||||
/// reciprocal instead of dividing cannot change the result. If the
|
||||
/// reciprocal is non-nil, the two computations are always equivalent.
|
||||
/// Unlike real types, with Complex types, _if no underflow occurs in
|
||||
/// computing the reciprocal_, replacing division with multiplication by
|
||||
/// this value does not perturb the result. Note that this condition is
|
||||
/// not the same as `reciprocal` returning a non-nil value. If the `real`
|
||||
/// and `imaginary` components are poorly scaled with respect to each other,
|
||||
/// one component of `reciprocal` may underflow while the other component
|
||||
/// is normal, resulting in a non-nil `reciprocal`.
|
||||
///
|
||||
/// We return a non-nil reciprocal in this case because it still
|
||||
/// satisfies the accuracy expectations of `reciprocal` in any Complex
|
||||
/// norm (but not componentwise).
|
||||
@inlinable
|
||||
public var reciprocal: Complex? {
|
||||
let recip = 1/self
|
||||
|
||||
@@ -141,7 +141,10 @@ extension Complex: ElementaryFunctions {
|
||||
public static func cosh(_ z: Complex) -> Complex {
|
||||
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 phase = Complex(
|
||||
RealType.cos(z.y),
|
||||
RealType(signOf: z.x, magnitudeOf: 1) * RealType.sin(z.y)
|
||||
)
|
||||
let firstScale = RealType.exp(z.x.magnitude/2)
|
||||
let secondScale = firstScale/2
|
||||
return phase.multiplied(by: firstScale).multiplied(by: secondScale)
|
||||
@@ -171,9 +174,12 @@ extension Complex: ElementaryFunctions {
|
||||
public static func sinh(_ z: Complex) -> Complex {
|
||||
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 phase = Complex(
|
||||
RealType(signOf: z.x, magnitudeOf: 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)
|
||||
let secondScale = firstScale/2
|
||||
return phase.multiplied(by: firstScale).multiplied(by: secondScale)
|
||||
}
|
||||
return Complex(
|
||||
|
||||
@@ -180,7 +180,13 @@ final class ElementaryFunctionTests: XCTestCase {
|
||||
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))
|
||||
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 testSinh<T: Real & FixedWidthFloatingPoint>(_ type: T.Type) {
|
||||
@@ -212,6 +218,12 @@ final class ElementaryFunctionTests: XCTestCase {
|
||||
XCTAssert(huge.imaginary.isApproximatelyEqual(to: mag))
|
||||
huge = Complex<T>.sinh(Complex(-x, .pi/4))
|
||||
XCTAssert(huge.real.isApproximatelyEqual(to: -mag))
|
||||
XCTAssert(huge.imaginary.isApproximatelyEqual(to: mag))
|
||||
huge = Complex<T>.sinh(Complex(-x, -.pi/4))
|
||||
XCTAssert(huge.real.isApproximatelyEqual(to: -mag))
|
||||
XCTAssert(huge.imaginary.isApproximatelyEqual(to: -mag))
|
||||
huge = Complex<T>.sinh(Complex(x, -.pi/4))
|
||||
XCTAssert(huge.real.isApproximatelyEqual(to: mag))
|
||||
XCTAssert(huge.imaginary.isApproximatelyEqual(to: -mag))
|
||||
// For randomly-chosen well-scaled finite values, we expect to have
|
||||
// cosh² - sinh² ≈ 1. Note that this test would break down due to
|
||||
|
||||
Reference in new issue
Block a user