mirror of
https://github.com/apple/swift-numerics
synced 2026-11-23 04:34:25 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
110beefe0b | ||
|
|
697e9351e8 | ||
|
|
f30f73ed10 | ||
|
|
c634b5ddce | ||
|
|
86f0b33c65 | ||
|
|
593946d4aa | ||
|
|
5f09b7ef22 | ||
|
|
6d75786ee0 | ||
|
|
4297fbee6a | ||
|
|
a410fd7029 | ||
|
|
c111cd001e | ||
|
|
52fa265ac9 | ||
|
|
750170404a | ||
|
|
5ae8cd103a | ||
|
|
488e250259 | ||
|
|
6e77726ac9 | ||
|
|
c14381db8c | ||
|
|
6dc8bb4d17 | ||
|
|
ff6a0ff2a5 | ||
|
|
4db6faa3e7 | ||
|
|
fc9ff1401d | ||
|
|
33d10c02cd |
No files matched your search
@@ -4,3 +4,4 @@
|
||||
/Packages
|
||||
/*.xcodeproj
|
||||
/.swiftpm
|
||||
.*.sw?
|
||||
@@ -0,0 +1,33 @@
|
||||
#[[
|
||||
This source file is part of the Swift Numerics open source project
|
||||
|
||||
Copyright (c) 2019 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
|
||||
#]]
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(swift-numerics
|
||||
LANGUAGES Swift)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules)
|
||||
|
||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
|
||||
|
||||
include(CTest)
|
||||
include(SwiftSupport)
|
||||
|
||||
add_subdirectory(Sources)
|
||||
if(BUILD_TESTING)
|
||||
add_subdirectory(Tests)
|
||||
endif()
|
||||
|
||||
get_property(SWIFT_NUMERICS_EXPORTS GLOBAL PROPERTY SWIFT_NUMERICS_EXPORTS)
|
||||
export(TARGETS ${SWIFT_NUMERICS_EXPORTS}
|
||||
NAMESPACE SwiftNumerics::
|
||||
FILE swift-numerics-config.cmake
|
||||
EXPORT_LINK_INTERFACE_LIBRARIES)
|
||||
@@ -9,7 +9,7 @@ These modules fall broadly into two categories:
|
||||
|
||||
There is some overlap between these two categories, and API that begins in the first category may migrate to the second as it matures and new uses are discovered.
|
||||
|
||||
Swift Numerics modules are fine-grained; if you need support for Complex numbers, you can import ComplexModule<sup><a name="back1">[1](#foot1)</a></sup> without pulling in everything else in the library as well:
|
||||
Swift Numerics modules are fine-grained; if you need support for Complex numbers, you can import ComplexModule¹ without pulling in everything else in the library as well:
|
||||
```swift
|
||||
import ComplexModule
|
||||
|
||||
@@ -28,7 +28,21 @@ Future expansion may assume the availability of other standard interfaces such a
|
||||
|
||||
Because we intend to make it possible to adopt Swift Numerics modules in the standard library at some future point, Swift Numerics uses the same license and contribution guidelines as the Swift project.
|
||||
|
||||
## Process
|
||||
## Using Swift Numerics in your project
|
||||
To use Swift Numerics in a SwiftPM project, add the following line to the dependencies in your `Package.swift` file:
|
||||
```swift
|
||||
.package(url: "https://github.com/apple/swift-numerics", from: "0.0.5"),
|
||||
```
|
||||
add `Numerics` as a dependency for your target:
|
||||
```swift
|
||||
.target(name: "MyTarget", dependencies: [
|
||||
.product(name: "Numerics", package: "swift-numerics"),
|
||||
"AnotherModule"
|
||||
]),
|
||||
```
|
||||
and finally, add `import Numerics` in your source code.
|
||||
|
||||
## Contributing to Swift Numerics
|
||||
Swift Numerics is a standalone library separate from the core Swift project.
|
||||
In practice, it will act as a staging ground for some APIs that may eventually be incorporated into the Swift Standard Library, and when that happens such changes will be proposed to the Swift Standard Library using the established evolution process of the Swift project.
|
||||
|
||||
@@ -62,7 +76,7 @@ Questions about how to use Swift Numerics modules, or issues that are not clearl
|
||||
5. [Decimal Floating-point](https://github.com/apple/swift-numerics/issues/7)
|
||||
|
||||
## Notes
|
||||
<sup><a name="foot1">[1](#back1)</a></sup> Swift is currently unable to use the fully-qualified name for types when a type and module have the same name (discussion here: https://forums.swift.org/t/pitch-fully-qualified-name-syntax/28482).
|
||||
¹ Swift is currently unable to use the fully-qualified name for types when a type and module have the same name (discussion here: https://forums.swift.org/t/pitch-fully-qualified-name-syntax/28482).
|
||||
This would prevent users of Swift Numerics who don't need generic types from doing things like:
|
||||
```swift
|
||||
import Complex
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
#[[
|
||||
This source file is part of the Swift Numerics open source project
|
||||
|
||||
Copyright (c) 2019 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
|
||||
#]]
|
||||
|
||||
add_subdirectory(_NumericsShims)
|
||||
add_subdirectory(ComplexModule)
|
||||
add_subdirectory(Numerics)
|
||||
add_subdirectory(RealModule)
|
||||
@@ -0,0 +1,21 @@
|
||||
#[[
|
||||
This source file is part of the Swift Numerics open source project
|
||||
|
||||
Copyright (c) 2019 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
|
||||
#]]
|
||||
|
||||
add_library(ComplexModule
|
||||
Arithmetic.swift
|
||||
Complex.swift
|
||||
Differentiable.swift)
|
||||
set_target_properties(ComplexModule PROPERTIES
|
||||
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
|
||||
target_link_libraries(ComplexModule PUBLIC
|
||||
RealModule)
|
||||
|
||||
|
||||
_install_target(ComplexModule)
|
||||
set_property(GLOBAL APPEND PROPERTY SWIFT_NUMERICS_EXPORTS ComplexModule)
|
||||
@@ -0,0 +1,89 @@
|
||||
//===--- Differentiable.swift ---------------------------------*- swift -*-===//
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2019 - 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if canImport(_Differentiation)
|
||||
import _Differentiation
|
||||
|
||||
extension Complex: Differentiable
|
||||
where RealType: Differentiable, RealType.TangentVector == RealType {
|
||||
public typealias TangentVector = Self
|
||||
|
||||
@inlinable
|
||||
public var zeroTangentVectorInitializer: () -> Self {
|
||||
{ Complex.zero }
|
||||
}
|
||||
}
|
||||
|
||||
extension Complex
|
||||
where RealType: Differentiable, RealType.TangentVector == RealType {
|
||||
|
||||
@derivative(of: real)
|
||||
@usableFromInline
|
||||
func _derivativeReal() -> (value: RealType, pullback: (RealType) -> Complex) {
|
||||
(value: real, pullback: { v in
|
||||
Complex(v, .zero)
|
||||
})
|
||||
}
|
||||
|
||||
@derivative(of: imaginary)
|
||||
@usableFromInline
|
||||
func _derivativeImaginary() -> (
|
||||
value: RealType,
|
||||
pullback: (RealType) -> Complex
|
||||
) {
|
||||
(value: real, pullback: { v in
|
||||
Complex(.zero, v)
|
||||
})
|
||||
}
|
||||
|
||||
@derivative(of: +)
|
||||
@usableFromInline
|
||||
static func _derivativeAdd(lhs: Complex, rhs: Complex)
|
||||
-> (value: Complex, pullback: (Complex) -> (Complex, Complex))
|
||||
{
|
||||
(lhs + rhs, { v in (v, v) })
|
||||
}
|
||||
|
||||
@derivative(of: -)
|
||||
@usableFromInline
|
||||
static func _derivativeSubtract(lhs: Complex, rhs: Complex)
|
||||
-> (value: Complex, pullback: (Complex) -> (Complex, Complex))
|
||||
{
|
||||
(lhs - rhs, { v in (v, -v) })
|
||||
}
|
||||
|
||||
@derivative(of: *)
|
||||
@usableFromInline
|
||||
static func _derivativeMultiply(lhs: Complex, rhs: Complex)
|
||||
-> (value: Complex, pullback: (Complex) -> (Complex, Complex))
|
||||
{
|
||||
(lhs * rhs, { v in (rhs * v, lhs * v) })
|
||||
}
|
||||
|
||||
@derivative(of: /)
|
||||
@usableFromInline
|
||||
static func _derivativeDivide(lhs: Complex, rhs: Complex)
|
||||
-> (value: Complex, pullback: (Complex) -> (Complex, Complex))
|
||||
{
|
||||
(lhs / rhs, { v in (v / rhs, -lhs / (rhs * rhs) * v) })
|
||||
}
|
||||
|
||||
@derivative(of: conjugate)
|
||||
@usableFromInline
|
||||
func _derivativeConjugate() -> (
|
||||
value: Complex,
|
||||
pullback: (Complex) -> Complex
|
||||
) {
|
||||
(conjugate, { v in v.conjugate })
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -60,7 +60,7 @@ Because of this, the ∞-norm is the obvious alternative; it gives the nicest AP
|
||||
- The ∞-norm is heavily used in other computational libraries; for example, it is used by the `izamax` and `icamax` functions in BLAS.
|
||||
|
||||
The 2-norm still needs to be available, of course, because sometimes you need it.
|
||||
This functionality is accessed via the `.length` and `.unsafeLengthSquared` properties.
|
||||
This functionality is accessed via the `.length` and `.lengthSquared` properties.
|
||||
|
||||
### Accuracy of division and multiplication
|
||||
This library attempts to provide robust division and multiplication operations, with small relative error in a complex norm. It is a non-goal to deliver small componentwise errors.
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
#[[
|
||||
This source file is part of the Swift Numerics open source project
|
||||
|
||||
Copyright (c) 2019 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
|
||||
#]]
|
||||
|
||||
add_library(Numerics
|
||||
Numerics.swift)
|
||||
set_target_properties(Numerics PROPERTIES
|
||||
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
|
||||
target_link_libraries(Numerics PUBLIC
|
||||
ComplexModule
|
||||
RealModule)
|
||||
|
||||
|
||||
_install_target(Numerics)
|
||||
set_property(GLOBAL APPEND PROPERTY SWIFT_NUMERICS_EXPORTS Numerics)
|
||||
@@ -0,0 +1,25 @@
|
||||
#[[
|
||||
This source file is part of the Swift Numerics open source project
|
||||
|
||||
Copyright (c) 2019 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
|
||||
#]]
|
||||
|
||||
add_library(RealModule
|
||||
AlgebraicField.swift
|
||||
Double+Real.swift
|
||||
ElementaryFunctions.swift
|
||||
Float+Real.swift
|
||||
Float80+Real.swift
|
||||
Real.swift
|
||||
RealFunctions.swift)
|
||||
set_target_properties(RealModule PROPERTIES
|
||||
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
|
||||
target_link_libraries(RealModule PUBLIC
|
||||
_NumericsShims)
|
||||
|
||||
|
||||
_install_target(RealModule)
|
||||
set_property(GLOBAL APPEND PROPERTY SWIFT_NUMERICS_EXPORTS RealModule)
|
||||
@@ -158,16 +158,40 @@ extension Double: Real {
|
||||
// If n is exactly representable as Double, we can just call pow:
|
||||
// Note that all calls on a 32b platform go down this path.
|
||||
if let y = Double(exactly: n) { return libm_pow(x, y) }
|
||||
// Otherwise, n is too large to losslessly represent as Double, so we
|
||||
// just split it into two parts, high and low. This is always exact,
|
||||
// so the only source of error is pow itself and the multiplication.
|
||||
// n is not representable in Double, so we will split it into two parts,
|
||||
// low and high, such that (high + low) = n, and use the identity:
|
||||
//
|
||||
// mask constant is spelled in this funny way because if we just anded
|
||||
// with the hex value, we'd get a compile error on 32b platforms, even
|
||||
// though this whole branch is dead code on 32b.
|
||||
let mask = Int(truncatingIfNeeded: 0x1f_ffff_ffff_ffff as UInt64)
|
||||
let low = n & mask
|
||||
let high = n - low
|
||||
// x**(high + low) = x**high * x**low.
|
||||
//
|
||||
// We put the high-order 32 bits into high, and the remaining 32 bits
|
||||
// in low.
|
||||
//
|
||||
// The exact split isn't important; all we need is that both pieces get
|
||||
// less than 53 bits (so that they are exact) and that they both have
|
||||
// the same sign as n.
|
||||
//
|
||||
// This second point is a little bit subtle--why is
|
||||
// it necessary? Consider what would happen if we took x = 2 and
|
||||
// n = Int.min + Int(UInt32.max), and simply naively split n without
|
||||
// taking care with the sign. We would end up computing:
|
||||
//
|
||||
// 2**n = 2**Int.min * 2**UInt32.max
|
||||
//
|
||||
// The first exponent is negative, the second positive, so the first term
|
||||
// underflows to zero, and the second overflows to infinity, so the final
|
||||
// result is NaN, when it should be zero. In order to avoid this
|
||||
// situation, we make sure that high contains n rounded *towards zero*,
|
||||
// rather than using simple two's-complement truncation (which rounds
|
||||
// down).
|
||||
let mask = Int(truncatingIfNeeded: UInt32.max)
|
||||
let round = n < 0 ? mask : 0
|
||||
// The addition and subtraction below cannot actually overflow (proof:
|
||||
// round is positive if n is negative, and zero otherwise, so n + round
|
||||
// is guaranteed to be representable, and n and high have the same sign,
|
||||
// so n - high is also representable), but it's hard to tell the compiler
|
||||
// that, so I'm using wrapping operations instead.
|
||||
let high = (n &+ round) & ~mask
|
||||
let low = n &- high
|
||||
return libm_pow(x, Double(low)) * libm_pow(x, Double(high))
|
||||
}
|
||||
|
||||
|
||||
@@ -150,12 +150,21 @@ extension Float: Real {
|
||||
// The range of "interesting" n is -1488522191 ... 1744361944; outside
|
||||
// of this range, all x != 1 overflow or underflow, so only the parity
|
||||
// of x matters. We don't really care about the specific range at all,
|
||||
// only that the bounds fit exactly into two Floats. Mask the low 24
|
||||
// bits of n, get pow with that exponent (this contains the parity),
|
||||
// then get pow with the rest (this may round, but if it does we've
|
||||
// saturated anyway, so it doesn't matter).
|
||||
let low = n & 0xffffff
|
||||
let high = n - low
|
||||
// only that the bounds fit exactly into two Floats.
|
||||
//
|
||||
// We do, however, need to be careful that high and low both have the
|
||||
// same sign as n (consult the Double implementation for details of why
|
||||
// this matters), so we need to be a little bit careful constructing
|
||||
// them.
|
||||
//
|
||||
// Unlike the Double implementation, when n is very large, high will
|
||||
// get rounded here; that's OK because it does not change the sign or
|
||||
// parity, which are the only two bits that matter for such large
|
||||
// exponents in Float.
|
||||
let mask = Int(truncatingIfNeeded: 0xffffff)
|
||||
let round = n < 0 ? mask : 0
|
||||
let high = (n &+ round) & ~mask
|
||||
let low = n &- high
|
||||
return libm_powf(x, Float(low)) * libm_powf(x, Float(high))
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
#[[
|
||||
This source file is part of the Swift Numerics open source project
|
||||
|
||||
Copyright (c) 2019 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
|
||||
#]]
|
||||
|
||||
add_library(_NumericsShims INTERFACE)
|
||||
target_include_directories(_NumericsShims INTERFACE
|
||||
include)
|
||||
|
||||
set_property(GLOBAL APPEND PROPERTY SWIFT_NUMERICS_EXPORTS _NumericsShims)
|
||||
@@ -383,6 +383,10 @@ HEADER_SHIM long double libm_lgammal(long double x, int *signp) {
|
||||
#endif
|
||||
|
||||
// MARK: - shims to import C complex operations for timing purposes
|
||||
#if !defined _WIN32
|
||||
// Clang doesn't have support for complex arithmetic on Windows, so
|
||||
// it doesn't make sense to benchmark against it (and these will
|
||||
// fail to link if used there).
|
||||
|
||||
typedef struct { double real; double imag; } CComplex;
|
||||
|
||||
@@ -399,3 +403,5 @@ HEADER_SHIM CComplex libm_cmul(CComplex z, CComplex w) {
|
||||
double _Complex c = a*b;
|
||||
return (CComplex){ __real__ c, __imag__ c };
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,3 @@
|
||||
module _NumericsShims {
|
||||
header "_NumericsShims.h"
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#[[
|
||||
This source file is part of the Swift Numerics open source project
|
||||
|
||||
Copyright (c) 2019 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
|
||||
#]]
|
||||
|
||||
find_package(dispatch CONFIG QUIET)
|
||||
find_package(Foundation CONFIG QUIET)
|
||||
find_package(XCTest CONFIG QUIET)
|
||||
|
||||
add_subdirectory(ComplexTests)
|
||||
add_subdirectory(RealTests)
|
||||
|
||||
add_executable(SwiftNumericsTestRunner
|
||||
WindowsMain.swift)
|
||||
target_link_libraries(SwiftNumericsTestRunner PRIVATE
|
||||
ComplexTests
|
||||
RealTests)
|
||||
|
||||
add_test(NAME SwiftNumericsTestRunner
|
||||
COMMAND SwiftNumericsTestRunner)
|
||||
@@ -1,180 +0,0 @@
|
||||
//===--- ArithmeticBenchmarkTests.swift -----------------------*- swift -*-===//
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2019 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import XCTest
|
||||
import ComplexModule
|
||||
|
||||
// For CComplex and shims
|
||||
import _NumericsShims
|
||||
|
||||
extension Complex where RealType == Double {
|
||||
@_transparent
|
||||
init(_ other: CComplex) {
|
||||
self = unsafeBitCast(other, to: Complex<Double>.self)
|
||||
}
|
||||
@_transparent
|
||||
var ctype: CComplex {
|
||||
unsafeBitCast(self, to: CComplex.self)
|
||||
}
|
||||
}
|
||||
|
||||
let wellScaledDoubles: [Complex<Double>] = (0 ..< 1024).map { _ in
|
||||
Complex(length: Double.random(in: 0.5 ..< 2.0),
|
||||
phase: Double.random(in: -.pi ..< .pi))
|
||||
}
|
||||
|
||||
let poorlyScaledDoubles: [Complex<Double>] = (0 ..< 1024).map { _ in
|
||||
Complex(
|
||||
length: Double(sign: .plus,
|
||||
exponent: .random(in: -970 ... 1023),
|
||||
significand: .random(in: 1 ..< 2)),
|
||||
phase: Double.random(in: -.pi ..< .pi)
|
||||
)
|
||||
}
|
||||
|
||||
final class ArithmeticBenchmarkTests: XCTestCase {
|
||||
|
||||
let iters = 1000
|
||||
|
||||
func testDivisionByConstant() {
|
||||
let divisor = wellScaledDoubles[0]
|
||||
var sum = Complex<Double>.zero
|
||||
measure {
|
||||
for _ in 0 ..< iters {
|
||||
sum = wellScaledDoubles.reduce(into: sum) { $0 += $1 / divisor }
|
||||
}
|
||||
}
|
||||
print(sum)
|
||||
}
|
||||
|
||||
func testReciprocal() {
|
||||
let recip = wellScaledDoubles[0].reciprocal!
|
||||
var sum = Complex<Double>.zero
|
||||
measure {
|
||||
for _ in 0 ..< iters {
|
||||
sum = wellScaledDoubles.reduce(into: sum) { $0 += $1 * recip }
|
||||
}
|
||||
}
|
||||
print(sum)
|
||||
}
|
||||
|
||||
func testDivisionByConstantC() {
|
||||
let divisor = wellScaledDoubles[0].ctype
|
||||
var sum = Complex<Double>.zero
|
||||
measure {
|
||||
for _ in 0 ..< iters {
|
||||
sum = wellScaledDoubles.reduce(into: sum) {
|
||||
$0 += Complex(libm_cdiv($1.ctype, divisor))
|
||||
}
|
||||
}
|
||||
}
|
||||
print(sum)
|
||||
}
|
||||
|
||||
func testMultiplication() {
|
||||
let multiplicand = wellScaledDoubles[0]
|
||||
var sum = Complex<Double>.zero
|
||||
measure {
|
||||
for _ in 0 ..< iters {
|
||||
sum = wellScaledDoubles.reduce(into: sum) { $0 += $1 * multiplicand }
|
||||
}
|
||||
}
|
||||
print(sum)
|
||||
}
|
||||
|
||||
func testMultiplicationC() {
|
||||
let multiplicand = wellScaledDoubles[0].ctype
|
||||
var sum = Complex<Double>.zero
|
||||
measure {
|
||||
for _ in 0 ..< iters {
|
||||
sum = wellScaledDoubles.reduce(into: sum) {
|
||||
$0 += Complex(libm_cmul($1.ctype, multiplicand))
|
||||
}
|
||||
}
|
||||
}
|
||||
print(sum)
|
||||
}
|
||||
|
||||
func testDivision() {
|
||||
var sum = Complex<Double>.zero
|
||||
measure {
|
||||
for o in 0 ..< iters {
|
||||
for i in 0 ..< 1024 {
|
||||
sum += wellScaledDoubles[i] / wellScaledDoubles[(i &- o) & 1023]
|
||||
}
|
||||
}
|
||||
}
|
||||
print(sum)
|
||||
}
|
||||
|
||||
func testDivisionC() {
|
||||
var sum = Complex<Double>.zero
|
||||
measure {
|
||||
for o in 0 ..< iters {
|
||||
for i in 0 ..< 1024 {
|
||||
sum += Complex(libm_cdiv(wellScaledDoubles[i].ctype,
|
||||
wellScaledDoubles[(i &- o) & 1023].ctype))
|
||||
}
|
||||
}
|
||||
}
|
||||
print(sum)
|
||||
}
|
||||
|
||||
func testDivisionPoorScaling() {
|
||||
var sum = Complex<Double>.zero
|
||||
measure {
|
||||
for o in 0 ..< iters {
|
||||
for i in 0 ..< 1024 {
|
||||
sum += poorlyScaledDoubles[i] / poorlyScaledDoubles[(i &- o) & 1023]
|
||||
}
|
||||
}
|
||||
}
|
||||
print(sum)
|
||||
}
|
||||
|
||||
func testDivisionPoorScalingC() {
|
||||
var sum = Complex<Double>.zero
|
||||
measure {
|
||||
for o in 0 ..< iters {
|
||||
for i in 0 ..< 1024 {
|
||||
sum += Complex(libm_cdiv(poorlyScaledDoubles[i].ctype,
|
||||
poorlyScaledDoubles[(i &- o) & 1023].ctype))
|
||||
}
|
||||
}
|
||||
}
|
||||
print(sum)
|
||||
}
|
||||
|
||||
func testMultiplicationPoorScaling() {
|
||||
var sum = Complex<Double>.zero
|
||||
measure {
|
||||
for o in 0 ..< iters {
|
||||
for i in 0 ..< 1024 {
|
||||
sum += poorlyScaledDoubles[i] * poorlyScaledDoubles[(i &- o) & 1023]
|
||||
}
|
||||
}
|
||||
}
|
||||
print(sum)
|
||||
}
|
||||
|
||||
func testMultiplicationPoorScalingC() {
|
||||
var sum = Complex<Double>.zero
|
||||
measure {
|
||||
for o in 0 ..< iters {
|
||||
for i in 0 ..< 1024 {
|
||||
sum += Complex(libm_cmul(poorlyScaledDoubles[i].ctype,
|
||||
poorlyScaledDoubles[(i &- o) & 1023].ctype))
|
||||
}
|
||||
}
|
||||
}
|
||||
print(sum)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#[[
|
||||
This source file is part of the Swift Numerics open source project
|
||||
|
||||
Copyright (c) 2019 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
|
||||
#]]
|
||||
|
||||
add_library(ComplexTests
|
||||
ArithmeticTests.swift
|
||||
DifferentiableTests.swift
|
||||
PropertyTests.swift)
|
||||
set_target_properties(ComplexTests PROPERTIES
|
||||
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
|
||||
target_compile_options(ComplexTests PRIVATE
|
||||
-enable-testing)
|
||||
target_link_libraries(ComplexTests PUBLIC
|
||||
$<$<NOT:$<PLATFORM_ID:Darwin>>:Foundation>
|
||||
ComplexModule
|
||||
XCTest)
|
||||
@@ -0,0 +1,66 @@
|
||||
//===--- DifferentiableTests.swift ----------------------------*- swift -*-===//
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2019 - 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if canImport(_Differentiation)
|
||||
|
||||
import XCTest
|
||||
import ComplexModule
|
||||
import _Differentiation
|
||||
|
||||
final class DifferentiableTests: XCTestCase {
|
||||
func testComponentGetter() {
|
||||
XCTAssertEqual(
|
||||
gradient(at: Complex<Float>(5, 5)) { $0.real * 2 },
|
||||
Complex(2, 0))
|
||||
XCTAssertEqual(
|
||||
gradient(at: Complex<Float>(5, 5)) { $0.imaginary * 2 },
|
||||
Complex(0, 2))
|
||||
XCTAssertEqual(
|
||||
gradient(at: Complex<Float>(5, 5)) { $0.real * 5 + $0.imaginary * 2 },
|
||||
Complex(5, 2))
|
||||
}
|
||||
|
||||
func testConjugate() {
|
||||
let φ = pullback(at: Complex<Float>(20, -4)) { x in x.conjugate }
|
||||
XCTAssertEqual(φ(Complex(1, 0)), Complex(1, 0))
|
||||
XCTAssertEqual(φ(Complex(0, 1)), Complex(0, -1))
|
||||
XCTAssertEqual(φ(Complex(-1, 1)), Complex(-1, -1))
|
||||
}
|
||||
|
||||
func testArithmetics() {
|
||||
let φAdd = pullback(at: Complex<Float>(2, 3)) { x in
|
||||
x + Complex(5, 6)
|
||||
}
|
||||
XCTAssertEqual(φAdd(Complex(1, 1)), Complex(1, 1))
|
||||
|
||||
let φSubtract = pullback(at: Complex<Float>(2, 3)) { x in
|
||||
Complex(5, 6) - x
|
||||
}
|
||||
XCTAssertEqual(φSubtract(Complex(1, 1)), Complex(-1, -1))
|
||||
|
||||
let φMultiply = pullback(at: Complex<Float>(2, 3)) { x in x * x }
|
||||
XCTAssertEqual(φMultiply(Complex(1, 0)), Complex(4, 6))
|
||||
XCTAssertEqual(φMultiply(Complex(0, 1)), Complex(-6, 4))
|
||||
XCTAssertEqual(φMultiply(Complex(1, 1)), Complex(-2, 10))
|
||||
|
||||
let φDivide = pullback(at: Complex<Float>(20, -4)) { x in
|
||||
x / Complex(2, 2)
|
||||
}
|
||||
XCTAssertEqual(φDivide(Complex(1, 0)), Complex(0.25, -0.25))
|
||||
XCTAssertEqual(φDivide(Complex(0, 1)), Complex(0.25, 0.25))
|
||||
}
|
||||
|
||||
func testZeroTangentVectorInitializer() {
|
||||
XCTAssertEqual(Complex<Float>(-5, 5).zeroTangentVector, Complex(0, 0))
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,19 @@
|
||||
#[[
|
||||
This source file is part of the Swift Numerics open source project
|
||||
|
||||
Copyright (c) 2019 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
|
||||
#]]
|
||||
|
||||
add_library(RealTests
|
||||
IntegerExponentTests.swift
|
||||
RealTests.swift
|
||||
RealTestSupport.swift)
|
||||
target_compile_options(RealTests PRIVATE
|
||||
-enable-testing)
|
||||
target_link_libraries(RealTests PUBLIC
|
||||
$<$<NOT:$<PLATFORM_ID:Darwin>>:Foundation>
|
||||
RealModule
|
||||
XCTest)
|
||||
@@ -12,7 +12,8 @@
|
||||
import XCTest
|
||||
import RealModule
|
||||
|
||||
internal extension Real where Self: BinaryFloatingPoint {
|
||||
internal extension Real where Self: BinaryFloatingPoint,
|
||||
Self.RawSignificand: FixedWidthInteger {
|
||||
static func testIntegerExponentCommon() {
|
||||
// If x is -1, then the result is ±1 with sign chosen by parity of n.
|
||||
// Simply converting n to Real will flip parity when n is large, so
|
||||
@@ -27,12 +28,55 @@ internal extension Real where Self: BinaryFloatingPoint {
|
||||
XCTAssertEqual(Self.pow(-1, Int.max), -1)
|
||||
XCTAssertEqual(Self.pow(-1, -Int.max), -1)
|
||||
XCTAssertEqual(Self.pow(-1, Int.min), 1)
|
||||
// Generate some random test values that definitely overflow or
|
||||
// underflow; we want to be sure that we get the right ±0 or ±∞
|
||||
// result.
|
||||
for _ in 0 ..< 10 {
|
||||
let x = Self.random(in: 2 ..< 4)
|
||||
let n = Int.random(in: 1 - Int(Self.leastNonzeroMagnitude.exponent) ..< .max)
|
||||
let even = n & -2
|
||||
let odd = even | 1
|
||||
assertClose( .infinity, Self.pow(x, even))
|
||||
assertClose( .infinity, Self.pow(x, odd))
|
||||
assertClose( 0.0, Self.pow(1/x, even))
|
||||
assertClose( 0.0, Self.pow(1/x, odd))
|
||||
assertClose( .infinity, Self.pow(-x, even))
|
||||
assertClose(-.infinity, Self.pow(-x, odd))
|
||||
assertClose( 0.0, Self.pow(-1/x, even))
|
||||
assertClose(-0.0, Self.pow(-1/x, odd))
|
||||
assertClose( 0.0, Self.pow(x, -even))
|
||||
assertClose( 0.0, Self.pow(x, -odd))
|
||||
assertClose( .infinity, Self.pow(1/x, -even))
|
||||
assertClose( .infinity, Self.pow(1/x, -odd))
|
||||
assertClose( 0.0, Self.pow(-x, -even))
|
||||
assertClose(-0.0, Self.pow(-x, -odd))
|
||||
assertClose( .infinity, Self.pow(-1/x, -even))
|
||||
assertClose(-.infinity, Self.pow(-1/x, -odd))
|
||||
}
|
||||
}
|
||||
|
||||
static func testIntegerExponentDoubleAndSmaller() {
|
||||
// max/min exponents, these always saturate, but this will reveal
|
||||
// errors in some implementations that one could try.
|
||||
let u = Self(1).nextUp
|
||||
let d = Self(1).nextDown
|
||||
assertClose( .infinity, Self.pow(-u, Int.max - 1))
|
||||
assertClose( 0.0, Self.pow(-d, Int.max - 1))
|
||||
assertClose( 0.0, Self.pow(-u, -Int.max + 1))
|
||||
assertClose( .infinity, Self.pow(-d, -Int.max + 1))
|
||||
assertClose(-.infinity, Self.pow(-u, Int.max))
|
||||
assertClose(-0.0, Self.pow(-d, Int.max))
|
||||
assertClose(-0.0, Self.pow(-u, -Int.max))
|
||||
assertClose(-.infinity, Self.pow(-d, -Int.max))
|
||||
assertClose( 0.0, Self.pow(-u, Int.min))
|
||||
assertClose( .infinity, Self.pow(-d, Int.min))
|
||||
}
|
||||
}
|
||||
|
||||
extension Float {
|
||||
static func testIntegerExponent() {
|
||||
testIntegerExponentCommon()
|
||||
testIntegerExponentDoubleAndSmaller()
|
||||
let u = Float(1).nextUp
|
||||
let d = Float(1).nextDown
|
||||
// Smallest exponents not exactly representable as Float.
|
||||
@@ -52,24 +96,13 @@ extension Float {
|
||||
assertClose(-7.0064924138100205091278464932003585e-46, Float.pow(-d, 1744361943))
|
||||
assertClose( 7.0064919961905290625123586120258840e-46, Float.pow(-d, 1744361944))
|
||||
assertClose(-7.0064915785710625079583096856510544e-46, Float.pow(-d, 1744361945))
|
||||
// max/min exponents, these always saturate, but this will reveal
|
||||
// errors in some implementations that one could try.
|
||||
assertClose( .infinity, Self.pow(-u, Int.max - 1))
|
||||
assertClose( 0.0, Self.pow(-d, Int.max - 1))
|
||||
assertClose( 0.0, Self.pow(-u, -Int.max + 1))
|
||||
assertClose( .infinity, Self.pow(-d, -Int.max + 1))
|
||||
assertClose(-.infinity, Self.pow(-u, Int.max))
|
||||
assertClose(-0.0, Self.pow(-d, Int.max))
|
||||
assertClose(-0.0, Self.pow(-u, -Int.max))
|
||||
assertClose(-.infinity, Self.pow(-d, -Int.max))
|
||||
assertClose( 0.0, Self.pow(-u, Int.min))
|
||||
assertClose( .infinity, Self.pow(-d, Int.min))
|
||||
}
|
||||
}
|
||||
|
||||
extension Double {
|
||||
static func testIntegerExponent() {
|
||||
testIntegerExponentCommon()
|
||||
testIntegerExponentDoubleAndSmaller()
|
||||
// Following tests only make sense (and are only necessary) on 64b platforms.
|
||||
#if arch(arm64) || arch(x86_64)
|
||||
let u: Double = 1.nextUp
|
||||
@@ -93,18 +126,6 @@ extension Double {
|
||||
assertClose(-2.4703282292062332640976590913373022e-324, Double.pow(-d, 6711563375777760775))
|
||||
assertClose( 2.4703282292062329898361312467121758e-324, Double.pow(-d, 6711563375777760776))
|
||||
assertClose(-2.4703282292062327155746034020870799e-324, Double.pow(-d, 6711563375777760777))
|
||||
// max/min exponents, these always saturate, but this will reveal
|
||||
// errors in some implementations that one could try.
|
||||
assertClose( .infinity, Self.pow(-u, Int.max - 1))
|
||||
assertClose( 0.0, Self.pow(-d, Int.max - 1))
|
||||
assertClose( 0.0, Self.pow(-u, -Int.max + 1))
|
||||
assertClose( .infinity, Self.pow(-d, -Int.max + 1))
|
||||
assertClose(-.infinity, Self.pow(-u, Int.max))
|
||||
assertClose(-0.0, Self.pow(-d, Int.max))
|
||||
assertClose(-0.0, Self.pow(-u, -Int.max))
|
||||
assertClose(-.infinity, Self.pow(-d, -Int.max))
|
||||
assertClose( 0.0, Self.pow(-u, Int.min))
|
||||
assertClose( .infinity, Self.pow(-d, Int.min))
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
//===--- WindowsMain.swift ------------------------------------*- swift -*-===//
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2019 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if os(Windows)
|
||||
import XCTest
|
||||
|
||||
@testable
|
||||
import RealTests
|
||||
|
||||
@testable
|
||||
import ComplexTests
|
||||
|
||||
extension ElementaryFunctionChecks {
|
||||
static var all = testCase([
|
||||
("testFloat", ElementaryFunctionChecks.testFloat),
|
||||
("testDouble", ElementaryFunctionChecks.testDouble),
|
||||
])
|
||||
}
|
||||
|
||||
extension IntegerExponentTests {
|
||||
static var all = testCase([
|
||||
("testFloat", IntegerExponentTests.testFloat),
|
||||
("testDouble", IntegerExponentTests.testDouble),
|
||||
])
|
||||
}
|
||||
|
||||
extension ArithmeticTests {
|
||||
static var all = testCase([
|
||||
("testPolar", ArithmeticTests.testPolar),
|
||||
("testBaudinSmith", ArithmeticTests.testBaudinSmith),
|
||||
("testDivisionByZero", ArithmeticTests.testDivisionByZero),
|
||||
])
|
||||
}
|
||||
|
||||
#if canImport(_Differentiation)
|
||||
extension DifferentiableTests {
|
||||
static var all = testCase([
|
||||
("testComponentGetter", DifferentiableTests.testComponentGetter),
|
||||
("testConjugate", DifferentiableTests.testConjugate),
|
||||
("testArithmetics", DifferentiableTests.testArithmetics),
|
||||
])
|
||||
}
|
||||
#endif
|
||||
|
||||
extension PropertyTests {
|
||||
static var all = testCase([
|
||||
("testProperties", PropertyTests.testProperties),
|
||||
("testEquatableHashable", PropertyTests.testEquatableHashable),
|
||||
("testCodable", PropertyTests.testCodable),
|
||||
])
|
||||
}
|
||||
|
||||
var testCases = [
|
||||
ElementaryFunctionChecks.all,
|
||||
IntegerExponentTests.all,
|
||||
ArithmeticTests.all,
|
||||
PropertyTests.all,
|
||||
]
|
||||
|
||||
#if canImport(_Differentiation)
|
||||
testCases += [
|
||||
DifferentiableTests.all
|
||||
]
|
||||
#endif
|
||||
|
||||
XCTMain(testCases)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,84 @@
|
||||
#[[
|
||||
This source file is part of the Swift Numerics open source project
|
||||
|
||||
Copyright (c) 2019 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
|
||||
#]]
|
||||
|
||||
# Returns the architecture name in a variable
|
||||
#
|
||||
# Usage:
|
||||
# get_swift_host_arch(result_var_name)
|
||||
#
|
||||
# Sets ${result_var_name} with the converted architecture name derived from
|
||||
# CMAKE_SYSTEM_PROCESSOR.
|
||||
function(get_swift_host_arch result_var_name)
|
||||
if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
|
||||
set("${result_var_name}" "x86_64" PARENT_SCOPE)
|
||||
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64")
|
||||
set("${result_var_name}" "aarch64" PARENT_SCOPE)
|
||||
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64")
|
||||
set("${result_var_name}" "powerpc64" PARENT_SCOPE)
|
||||
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le")
|
||||
set("${result_var_name}" "powerpc64le" PARENT_SCOPE)
|
||||
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "s390x")
|
||||
set("${result_var_name}" "s390x" PARENT_SCOPE)
|
||||
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv6l")
|
||||
set("${result_var_name}" "armv6" PARENT_SCOPE)
|
||||
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7l")
|
||||
set("${result_var_name}" "armv7" PARENT_SCOPE)
|
||||
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7-a")
|
||||
set("${result_var_name}" "armv7" PARENT_SCOPE)
|
||||
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AMD64")
|
||||
set("${result_var_name}" "x86_64" PARENT_SCOPE)
|
||||
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "IA64")
|
||||
set("${result_var_name}" "itanium" PARENT_SCOPE)
|
||||
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86")
|
||||
set("${result_var_name}" "i686" PARENT_SCOPE)
|
||||
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i686")
|
||||
set("${result_var_name}" "i686" PARENT_SCOPE)
|
||||
else()
|
||||
message(FATAL_ERROR "Unrecognized architecture on host system: ${CMAKE_SYSTEM_PROCESSOR}")
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
# Returns the os name in a variable
|
||||
#
|
||||
# Usage:
|
||||
# get_swift_host_os(result_var_name)
|
||||
#
|
||||
#
|
||||
# Sets ${result_var_name} with the converted OS name derived from
|
||||
# CMAKE_SYSTEM_NAME.
|
||||
function(get_swift_host_os result_var_name)
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
|
||||
set(${result_var_name} macosx PARENT_SCOPE)
|
||||
else()
|
||||
string(TOLOWER ${CMAKE_SYSTEM_NAME} cmake_system_name_lc)
|
||||
set(${result_var_name} ${cmake_system_name_lc} PARENT_SCOPE)
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
function(_install_target module)
|
||||
get_swift_host_arch(swift_arch)
|
||||
get_swift_host_os(swift_os)
|
||||
install(TARGETS ${module}
|
||||
ARCHIVE DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${swift_os}
|
||||
LIBRARY DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${swift_os}
|
||||
RUNTIME DESTINATION bin)
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
|
||||
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module}.swiftdoc
|
||||
DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${swift_os}/${mmodule}.swiftmodule
|
||||
RENAME ${swift_arch}.swiftdoc)
|
||||
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module}.swiftmodule
|
||||
DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${swift_os}/${mmodule}.swiftmodule
|
||||
RENAME ${swift_arch}.swiftmodule)
|
||||
else()
|
||||
install(FILES
|
||||
$<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module}.swiftdoc
|
||||
$<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module}.swiftmodule
|
||||
DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${swift_os}/${swift_arch})
|
||||
endif()
|
||||
endfunction()
|
||||
Reference in new issue
Block a user