mirror of
https://github.com/apple/swift-numerics
synced 2026-11-26 04:57:53 +00:00
Moved sampling-based tests out of unit and into an exectuable target.
This commit is contained in:
1 parent
efbe11a734
commit
0b2dc469ed
4 files changed
+127
-76
No files matched your search
@@ -13,23 +13,40 @@
|
||||
import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
|
||||
name: "swift-numerics",
|
||||
products: [
|
||||
.library(name: "ComplexModule", targets: ["ComplexModule"]),
|
||||
.library(name: "Numerics", targets: ["Numerics"]),
|
||||
.library(name: "RealModule", targets: ["RealModule"]),
|
||||
],
|
||||
|
||||
dependencies: [
|
||||
.package(url: "https://github.com/apple/swift-argument-parser", from: "0.1.0"),
|
||||
],
|
||||
|
||||
targets: [
|
||||
// User-facing modules
|
||||
.target(name: "ComplexModule", dependencies: ["RealModule"]),
|
||||
.target(name: "Numerics", dependencies: ["ComplexModule", "RealModule"]),
|
||||
.target(name: "RealModule", dependencies: ["_NumericsShims"]),
|
||||
|
||||
// Implementation details
|
||||
.target(name: "_NumericsShims", dependencies: []),
|
||||
.target(name: "_TestSupport", dependencies: ["Numerics"]),
|
||||
|
||||
// Unit test bundles
|
||||
.testTarget(name: "ComplexTests", dependencies: ["_TestSupport"]),
|
||||
.testTarget(name: "RealTests", dependencies: ["_TestSupport"]),
|
||||
|
||||
// Test executables
|
||||
.target(
|
||||
name: "ComplexElementaryFunctions",
|
||||
dependencies: [
|
||||
"Numerics", "_TestSupport",
|
||||
.product(name: "ArgumentParser", package: "swift-argument-parser"),
|
||||
],
|
||||
path: "Tests/Executable"
|
||||
)
|
||||
]
|
||||
)
|
||||
@@ -112,70 +112,6 @@ final class ElementaryFunctionTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: pull this out into a separate binary target, not run as part of the
|
||||
// normal tests.
|
||||
func testExpMinusOne_FloatVsDouble() {
|
||||
// Walk grid points of the form (n + nπ/16 i) comparing Float and Double,
|
||||
// finding the worst componentwise and normwise errors for Float.
|
||||
let reals = (-100 ... 100).map { Float($0) }
|
||||
let imags = (-100 ... 100).map { Float($0) * .pi / 16 }
|
||||
var componentError = Double(Float.ulpOfOne)
|
||||
var complexError = Double(Float.ulpOfOne)
|
||||
var componentMaxInput = Complex<Float>.zero
|
||||
var complexMaxInput = Complex<Float>.zero
|
||||
for x in reals {
|
||||
for y in imags {
|
||||
let tst = Complex.expMinusOne(Complex(x,y))
|
||||
let ref = Complex.expMinusOne(Complex(Double(x), Double(y)))
|
||||
if tst == Complex<Float>(ref) { continue }
|
||||
let thisError = relativeError(tst, ref)
|
||||
if thisError > complexError {
|
||||
complexMaxInput = Complex(x,y)
|
||||
complexError = thisError
|
||||
}
|
||||
let thisComponentError = max(
|
||||
relativeError(tst.real, ref.real),
|
||||
relativeError(tst.imaginary, ref.imaginary)
|
||||
)
|
||||
if thisComponentError > componentError {
|
||||
componentMaxInput = Complex(x,y)
|
||||
componentError = thisComponentError
|
||||
}
|
||||
}
|
||||
}
|
||||
// Now sample randomly-generated points in an interesting strip along
|
||||
// the real axis.
|
||||
var g = SystemRandomNumberGenerator()
|
||||
for _ in 0 ..< 10_000 {
|
||||
let z = Complex(Float.random(in: -100 ... 100, using: &g),
|
||||
Float.random(in: -2 * .pi ... 2 * .pi, using: &g))
|
||||
let tst = Complex.expMinusOne(z)
|
||||
let ref = Complex.expMinusOne(Complex<Double>(z))
|
||||
if tst == Complex<Float>(ref) { continue }
|
||||
let thisError = relativeError(tst, ref)
|
||||
if thisError > complexError {
|
||||
complexMaxInput = z
|
||||
complexError = thisError
|
||||
}
|
||||
let thisComponentError = max(
|
||||
relativeError(tst.real, ref.real),
|
||||
relativeError(tst.imaginary, ref.imaginary)
|
||||
)
|
||||
if thisComponentError > componentError {
|
||||
componentMaxInput = z
|
||||
componentError = thisComponentError
|
||||
}
|
||||
}
|
||||
print("Worst complex norm error seen for expMinusOne was \(complexError)")
|
||||
print("For input \(complexMaxInput).")
|
||||
print("Reference result: \(Complex.expMinusOne(Complex<Double>(complexMaxInput)))")
|
||||
print(" Observed result: \(Complex.expMinusOne(complexMaxInput))")
|
||||
print("Worst componentwise error seen for expMinusOne was \(componentError)")
|
||||
print("For input \(componentMaxInput).")
|
||||
print("Reference result: \(Complex.expMinusOne(Complex<Double>(componentMaxInput)))")
|
||||
print(" Observed result: \(Complex.expMinusOne(componentMaxInput))")
|
||||
}
|
||||
|
||||
func testFloat() {
|
||||
testExp(Float.self)
|
||||
testExpMinusOne(Float.self)
|
||||
@@ -193,15 +129,3 @@ final class ElementaryFunctionTests: XCTestCase {
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
func relativeError(_ tst: Float, _ ref: Double) -> Double {
|
||||
let scale = max(ref.magnitude, Double(Float.leastNormalMagnitude))
|
||||
let error = (Double(tst) - ref).magnitude
|
||||
return error / scale
|
||||
}
|
||||
|
||||
func relativeError(_ tst: Complex<Float>, _ ref: Complex<Double>) -> Double {
|
||||
let scale = max(ref.magnitude, Double(Float.leastNormalMagnitude))
|
||||
let error = (Complex<Double>(tst) - ref).magnitude
|
||||
return error / scale
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
//===--- Error.swift ------------------------------------------*- swift -*-===//
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2020 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Numerics
|
||||
|
||||
func relativeError(_ tst: Float, _ ref: Double) -> Double {
|
||||
let scale = max(ref.magnitude, Double(Float.leastNormalMagnitude))
|
||||
let error = (Double(tst) - ref).magnitude
|
||||
return error / scale
|
||||
}
|
||||
|
||||
func componentwiseError(_ tst: Complex<Float>, _ ref: Complex<Double>) -> Double {
|
||||
return max(relativeError(tst.real, ref.real),
|
||||
relativeError(tst.imaginary, ref.imaginary))
|
||||
}
|
||||
|
||||
func relativeError(_ tst: Complex<Float>, _ ref: Complex<Double>) -> Double {
|
||||
let scale = max(ref.magnitude, Double(Float.leastNormalMagnitude))
|
||||
let error = (Complex<Double>(tst) - ref).magnitude
|
||||
return error / scale
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
//===--- main.swift -------------------------------------------*- swift -*-===//
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2020 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Numerics
|
||||
import _TestSupport
|
||||
import ArgumentParser
|
||||
import Foundation
|
||||
|
||||
struct ComplexElementaryFunctions: ParsableCommand {
|
||||
func run() throws {
|
||||
// Walk grid points of the form (n + nπ/16 i) comparing Float and Double,
|
||||
// finding the worst componentwise and normwise errors for Float.
|
||||
let reals = (-100 ... 100).map { Float($0) }
|
||||
let imags = (-100 ... 100).map { Float($0) * .pi / 16 }
|
||||
var componentError = Double(Float.ulpOfOne)
|
||||
var complexError = Double(Float.ulpOfOne)
|
||||
var componentMaxInput = Complex<Float>.zero
|
||||
var complexMaxInput = Complex<Float>.zero
|
||||
for x in reals {
|
||||
for y in imags {
|
||||
let z = Complex(x, y)
|
||||
let tst = Complex.expMinusOne(z)
|
||||
let ref = Complex.expMinusOne(Complex<Double>(z))
|
||||
if tst == Complex<Float>(ref) { continue }
|
||||
let thisError = relativeError(tst, ref)
|
||||
if thisError > complexError {
|
||||
complexMaxInput = z
|
||||
complexError = thisError
|
||||
}
|
||||
let thisComponentError = componentwiseError(tst, ref)
|
||||
if thisComponentError > componentError {
|
||||
componentMaxInput = z
|
||||
componentError = thisComponentError
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now sample randomly-generated points in an interesting strip along
|
||||
// the real axis.
|
||||
var g = SystemRandomNumberGenerator()
|
||||
for _ in 0 ..< 10_000 {
|
||||
let z = Complex(Float.random(in: -100 ... 100, using: &g),
|
||||
Float.random(in: -2 * .pi ... 2 * .pi, using: &g))
|
||||
let tst = Complex.expMinusOne(z)
|
||||
let ref = Complex.expMinusOne(Complex<Double>(z))
|
||||
if tst == Complex<Float>(ref) { continue }
|
||||
let thisError = relativeError(tst, ref)
|
||||
if thisError > complexError {
|
||||
complexMaxInput = z
|
||||
complexError = thisError
|
||||
}
|
||||
let thisComponentError = componentwiseError(tst, ref)
|
||||
if thisComponentError > componentError {
|
||||
componentMaxInput = z
|
||||
componentError = thisComponentError
|
||||
}
|
||||
}
|
||||
|
||||
print("Worst complex norm error seen for expMinusOne was \(complexError)")
|
||||
print("For input \(complexMaxInput).")
|
||||
print("Reference result: \(Complex.expMinusOne(Complex<Double>(complexMaxInput)))")
|
||||
print(" Observed result: \(Complex.expMinusOne(complexMaxInput))")
|
||||
|
||||
print("Worst componentwise error seen for expMinusOne was \(componentError)")
|
||||
print("For input \(componentMaxInput).")
|
||||
print("Reference result: \(Complex.expMinusOne(Complex<Double>(componentMaxInput)))")
|
||||
print(" Observed result: \(Complex.expMinusOne(componentMaxInput))")
|
||||
}
|
||||
}
|
||||
|
||||
ComplexElementaryFunctions.main()
|
||||
Reference in new issue
Block a user