mirror of
https://github.com/apple/swift-numerics
synced 2026-11-22 04:30:45 +00:00
Compare commits
14
Commits
1.0.2
...
release/1.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
876f439c4a | ||
|
|
96df03d007 | ||
|
|
f412cea212 | ||
|
|
8b28aba874 | ||
|
|
e29c31deb4 | ||
|
|
e0ec0f5f3a | ||
|
|
aa76ee5658 | ||
|
|
d42ab140f7 | ||
|
|
77b98fab75 | ||
|
|
ba43341621 | ||
|
|
6a3ecdb480 | ||
|
|
803aae1ef0 | ||
|
|
db4ef5686a | ||
|
|
a85dd8c2bb |
No files matched your search
@@ -0,0 +1,19 @@
|
||||
name: Pull request
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, reopened, synchronize]
|
||||
|
||||
jobs:
|
||||
tests:
|
||||
name: Test
|
||||
uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@main
|
||||
soundness:
|
||||
name: Soundness
|
||||
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
|
||||
with:
|
||||
license_header_check_project_name: "Swift Numerics"
|
||||
# https://github.com/apple/swift-numerics/issues/303
|
||||
license_header_check_enabled: false
|
||||
# https://github.com/apple/swift-numerics/issues/302
|
||||
format_check_enabled: false
|
||||
@@ -22,9 +22,6 @@ 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}
|
||||
|
||||
+8
-2
@@ -1,4 +1,4 @@
|
||||
// swift-tools-version:5.0
|
||||
// swift-tools-version:5.2
|
||||
//===--- Package.swift ----------------------------------------*- swift -*-===//
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
@@ -25,7 +25,13 @@ let package = Package(
|
||||
// User-facing modules
|
||||
.target(name: "ComplexModule", dependencies: ["RealModule"]),
|
||||
.target(name: "Numerics", dependencies: ["ComplexModule", "RealModule"]),
|
||||
.target(name: "RealModule", dependencies: ["_NumericsShims"]),
|
||||
.target(
|
||||
name: "RealModule",
|
||||
dependencies: ["_NumericsShims"],
|
||||
linkerSettings: [
|
||||
.linkedLibrary("m", .when(platforms: [.linux, .android]))
|
||||
]
|
||||
),
|
||||
|
||||
// Implementation details
|
||||
.target(name: "_NumericsShims", dependencies: []),
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
// swift-tools-version:5.0
|
||||
//===--- Package@swift-5.0.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 PackageDescription
|
||||
|
||||
let package = Package(
|
||||
|
||||
name: "swift-numerics",
|
||||
products: [
|
||||
.library(name: "ComplexModule", targets: ["ComplexModule"]),
|
||||
.library(name: "Numerics", targets: ["Numerics"]),
|
||||
.library(name: "RealModule", targets: ["RealModule"]),
|
||||
],
|
||||
|
||||
targets: [
|
||||
// User-facing modules
|
||||
.target(name: "ComplexModule", dependencies: ["RealModule"]),
|
||||
.target(name: "Numerics", dependencies: ["ComplexModule", "RealModule"]),
|
||||
.target(
|
||||
name: "RealModule",
|
||||
dependencies: ["_NumericsShims"],
|
||||
linkerSettings: [ .linkedLibrary("m", .when(platforms: [.linux])) ]
|
||||
),
|
||||
|
||||
// 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: "ComplexLog", dependencies: ["Numerics", "_TestSupport"], path: "Tests/Executable/ComplexLog"),
|
||||
.target(name: "ComplexLog1p", dependencies: ["Numerics", "_TestSupport"], path: "Tests/Executable/ComplexLog1p")
|
||||
]
|
||||
)
|
||||
@@ -84,7 +84,7 @@ Swift Numerics uses GitHub issues to track bugs and features. We use pull reques
|
||||
1. Raise an issue with the [new module] tag.
|
||||
2. Raise a PR with an implementation sketch.
|
||||
3. Once you have some consensus, ask an admin to create a feature branch against which PRs can be raised.
|
||||
4. When the design has stabilized and is functional enough to be useful, raise a PR to merge the new module to master.
|
||||
4. When the design has stabilized and is functional enough to be useful, raise a PR to merge the new module to main.
|
||||
|
||||
### How to propose a new feature for an existing module
|
||||
|
||||
|
||||
@@ -69,6 +69,10 @@ public struct Complex<RealType> where RealType: Real {
|
||||
}
|
||||
}
|
||||
|
||||
#if swift(>=5.5)
|
||||
extension Complex: Sendable where RealType: Sendable { }
|
||||
#endif
|
||||
|
||||
// MARK: - Basic properties
|
||||
extension Complex {
|
||||
/// The real part of this complex value.
|
||||
|
||||
@@ -413,14 +413,21 @@ extension Complex: ElementaryFunctions {
|
||||
}
|
||||
|
||||
// MARK: - pow-like functions
|
||||
/// `exp(w*log(z))`
|
||||
///
|
||||
/// Edge cases for this function are defined according to the defining
|
||||
/// expression exp(w log(z)), except that we define pow(0, w) to be 0
|
||||
/// instead of infinity when w is in the (strict) right half-plane, so that
|
||||
/// we agree with RealType.pow on the positive real line.
|
||||
@inlinable
|
||||
public static func pow(_ z: Complex, _ w: Complex) -> Complex {
|
||||
if z.isZero { return w.real > 0 ? zero : infinity }
|
||||
return exp(w * log(z))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public static func pow(_ z: Complex, _ n: Int) -> Complex {
|
||||
if z.isZero { return .zero }
|
||||
if z.isZero { return n < 0 ? infinity : n == 0 ? one : 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.
|
||||
|
||||
@@ -150,6 +150,7 @@ extension Double: Real {
|
||||
@_transparent
|
||||
public static func pow(_ x: Double, _ y: Double) -> Double {
|
||||
guard x >= 0 else { return .nan }
|
||||
if x == 0 && y == 0 { return .nan }
|
||||
return libm_pow(x, y)
|
||||
}
|
||||
|
||||
|
||||
@@ -292,20 +292,24 @@ public protocol ElementaryFunctions: AdditiveArithmetic {
|
||||
|
||||
/// exp(y * log(x)) computed with additional internal precision.
|
||||
///
|
||||
/// See also:
|
||||
/// -
|
||||
/// - `sqrt()`
|
||||
/// - `root()`
|
||||
/// The edge-cases of this function are defined based on the behavior of the
|
||||
/// expression `exp(y log x)`, matching IEEE 754's `powr` operation.
|
||||
/// In particular, this means that if `x` and `y` are both zero, `pow(x,y)`
|
||||
/// is `nan` for real types and `infinity` for complex types, rather than 1.
|
||||
///
|
||||
/// There is also a `pow(_:Self,_:Int)` overload, whose behavior is defined
|
||||
/// in terms of repeated multiplication, and hence returns 1 for this case.
|
||||
///
|
||||
/// See also `sqrt()` and `root()`.
|
||||
static func pow(_ x: Self, _ y: Self) -> Self
|
||||
|
||||
/// `x` raised to the nth power.
|
||||
///
|
||||
/// See also:
|
||||
/// -
|
||||
/// - `sqrt()`
|
||||
/// - `root()`
|
||||
/// The edge-cases of this function are defined in terms of repeated
|
||||
/// multiplication or division, rather than exp(n log x). In particular,
|
||||
/// `Float.pow(0, 0)` is 1.
|
||||
///
|
||||
/// See also `sqrt()` and `root()`.
|
||||
static func pow(_ x: Self, _ n: Int) -> Self
|
||||
|
||||
/// The [square root][wiki] of `x`.
|
||||
|
||||
@@ -137,6 +137,7 @@ extension Float: Real {
|
||||
@_transparent
|
||||
public static func pow(_ x: Float, _ y: Float) -> Float {
|
||||
guard x >= 0 else { return .nan }
|
||||
if x == 0 && y == 0 { return .nan }
|
||||
return libm_powf(x, y)
|
||||
}
|
||||
|
||||
|
||||
@@ -132,6 +132,7 @@ extension Float80: Real {
|
||||
@_transparent
|
||||
public static func pow(_ x: Float80, _ y: Float80) -> Float80 {
|
||||
guard x >= 0 else { return .nan }
|
||||
if x == 0 && y == 0 { return .nan }
|
||||
return libm_powl(x, y)
|
||||
}
|
||||
|
||||
|
||||
@@ -73,5 +73,5 @@ Not having this protocol is a significant missing feature for numerical computin
|
||||
|
||||
[ErrorFunction]: https://en.wikipedia.org/wiki/Error_function
|
||||
[GammaFunction]: https://en.wikipedia.org/wiki/Gamma_function
|
||||
[SE-0246]: https://github.com/apple/swift-evolution/blob/master/proposals/0246-mathable.md
|
||||
[SE-0246]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0246-mathable.md
|
||||
[Sigmoid]: https://en.wikipedia.org/wiki/Sigmoid_function
|
||||
@@ -10,5 +10,7 @@ See https://swift.org/LICENSE.txt for license information
|
||||
add_library(_NumericsShims INTERFACE)
|
||||
target_include_directories(_NumericsShims INTERFACE
|
||||
include)
|
||||
target_link_libraries(_NumericsShims INTERFACE
|
||||
$<$<PLATFORM_ID:Linux>:m>)
|
||||
|
||||
set_property(GLOBAL APPEND PROPERTY SWIFT_NUMERICS_EXPORTS _NumericsShims)
|
||||
@@ -9,6 +9,10 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define HEADER_SHIM static inline __attribute__((__always_inline__))
|
||||
|
||||
// This header uses most of the libm functions, but we don't want to end up
|
||||
@@ -397,3 +401,7 @@ HEADER_SHIM double _numerics_muladd(double a, double b, double c) {
|
||||
|
||||
// No long-double muladd operation, because no one has built an FMA for it
|
||||
// (except for Itanium, which Swift doesn't support).
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -10,6 +10,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import RealModule
|
||||
import ComplexModule
|
||||
|
||||
public func relativeError(_ tst: Float, _ ref: Double) -> Double {
|
||||
|
||||
@@ -16,7 +16,7 @@ where Exponent: FixedWidthInteger,
|
||||
RawSignificand: FixedWidthInteger { }
|
||||
|
||||
#if swift(>=5.4) && !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64))
|
||||
@available(macOS 11.0, iOS 14.0, watchOS 14.0, tvOS 7.0, *)
|
||||
@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
|
||||
extension Float16: FixedWidthFloatingPoint { }
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
#[[
|
||||
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,12 +0,0 @@
|
||||
//===--- LinuxMain.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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
fatalError("Use --enable-test-discovery to run Swift Numerics tests on Linux.")
|
||||
@@ -128,13 +128,30 @@ internal extension Real where Self: BinaryFloatingPoint {
|
||||
}
|
||||
}
|
||||
|
||||
extension Real {
|
||||
static func powZeroChecks() {
|
||||
// pow(_:Self,_:Self) is defined by exp(y log(x)) and has edge-cases to
|
||||
// match. In particular, if x is zero, log(x) is -infinity, so pow(0,0)
|
||||
// is exp(0 * -infinity) = exp(nan) = nan.
|
||||
XCTAssertEqual(pow(0, -1 as Self), infinity)
|
||||
XCTAssert(pow(0, 0 as Self).isNaN)
|
||||
XCTAssertEqual(pow(0, 1 as Self), zero)
|
||||
// pow(_:Self,_:Int) is defined by repeated multiplication or division,
|
||||
// and hence pow(0, 0) is 1.
|
||||
XCTAssertEqual(pow(0, -1), infinity)
|
||||
XCTAssertEqual(pow(0, 0), 1)
|
||||
XCTAssertEqual(pow(0, 1), zero)
|
||||
}
|
||||
}
|
||||
|
||||
final class ElementaryFunctionChecks: XCTestCase {
|
||||
|
||||
#if swift(>=5.4) && !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64))
|
||||
func testFloat16() {
|
||||
if #available(macOS 11.0, iOS 14.0, watchOS 14.0, tvOS 7.0, *) {
|
||||
if #available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *) {
|
||||
Float16.elementaryFunctionChecks()
|
||||
Float16.realFunctionChecks()
|
||||
Float16.powZeroChecks()
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -142,17 +159,20 @@ final class ElementaryFunctionChecks: XCTestCase {
|
||||
func testFloat() {
|
||||
Float.elementaryFunctionChecks()
|
||||
Float.realFunctionChecks()
|
||||
Float.powZeroChecks()
|
||||
}
|
||||
|
||||
func testDouble() {
|
||||
Double.elementaryFunctionChecks()
|
||||
Double.realFunctionChecks()
|
||||
Double.powZeroChecks()
|
||||
}
|
||||
|
||||
#if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android)
|
||||
func testFloat80() {
|
||||
Float80.elementaryFunctionChecks()
|
||||
Float80.realFunctionChecks()
|
||||
Float80.powZeroChecks()
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -176,7 +176,7 @@ final class IntegerExponentTests: XCTestCase {
|
||||
|
||||
#if swift(>=5.4) && !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64))
|
||||
func testFloat16() {
|
||||
if #available(macOS 11.0, iOS 14.0, watchOS 14.0, tvOS 7.0, *) {
|
||||
if #available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *) {
|
||||
Float16.testIntegerExponent()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,109 +0,0 @@
|
||||
//===--- 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 ComplexTests.ApproximateEqualityTests {
|
||||
static var all = testCase([
|
||||
("testFloat", ComplexTests.ApproximateEqualityTests.testFloat),
|
||||
("testDouble", ComplexTests.ApproximateEqualityTests.testDouble),
|
||||
])
|
||||
}
|
||||
|
||||
extension RealTests.ApproximateEqualityTests {
|
||||
static var all = testCase([
|
||||
("testFloat", RealTests.ApproximateEqualityTests.testFloat),
|
||||
("testDouble", RealTests.ApproximateEqualityTests.testDouble),
|
||||
])
|
||||
}
|
||||
|
||||
#if swift(>=5.4) && !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64))
|
||||
extension ElementaryFunctionChecks {
|
||||
static var all = testCase([
|
||||
("testFloat16", ElementaryFunctionChecks.testFloat16),
|
||||
("testFloat", ElementaryFunctionChecks.testFloat),
|
||||
("testDouble", ElementaryFunctionChecks.testDouble),
|
||||
])
|
||||
}
|
||||
|
||||
extension IntegerExponentTests {
|
||||
static var all = testCase([
|
||||
("testFloat16", IntegerExponentTests.testFloat16),
|
||||
("testFloat", IntegerExponentTests.testFloat),
|
||||
("testDouble", IntegerExponentTests.testDouble),
|
||||
])
|
||||
}
|
||||
#else
|
||||
extension ElementaryFunctionChecks {
|
||||
static var all = testCase([
|
||||
("testFloat", ElementaryFunctionChecks.testFloat),
|
||||
("testDouble", ElementaryFunctionChecks.testDouble),
|
||||
])
|
||||
}
|
||||
|
||||
extension IntegerExponentTests {
|
||||
static var all = testCase([
|
||||
("testFloat", IntegerExponentTests.testFloat),
|
||||
("testDouble", IntegerExponentTests.testDouble),
|
||||
])
|
||||
}
|
||||
#endif
|
||||
|
||||
extension ArithmeticTests {
|
||||
static var all = testCase([
|
||||
("testPolar", ArithmeticTests.testPolar),
|
||||
("testBaudinSmith", ArithmeticTests.testBaudinSmith),
|
||||
("testDivisionByZero", ArithmeticTests.testDivisionByZero),
|
||||
])
|
||||
}
|
||||
|
||||
extension PropertyTests {
|
||||
static var all = testCase([
|
||||
("testProperties", PropertyTests.testProperties),
|
||||
("testEquatableHashable", PropertyTests.testEquatableHashable),
|
||||
("testCodable", PropertyTests.testCodable),
|
||||
])
|
||||
}
|
||||
|
||||
var testCases = [
|
||||
ComplexTests.ApproximateEqualityTests.all,
|
||||
RealTests.ApproximateEqualityTests.all,
|
||||
ElementaryFunctionChecks.all,
|
||||
IntegerExponentTests.all,
|
||||
ArithmeticTests.all,
|
||||
PropertyTests.all,
|
||||
]
|
||||
|
||||
#if swift(>=5.3) && canImport(_Differentiation)
|
||||
extension DifferentiableTests {
|
||||
static var all = testCase([
|
||||
("testComponentGetter", DifferentiableTests.testComponentGetter),
|
||||
("testInitializer", DifferentiableTests.testInitializer),
|
||||
("testConjugate", DifferentiableTests.testConjugate),
|
||||
("testArithmetics", DifferentiableTests.testArithmetics),
|
||||
])
|
||||
}
|
||||
|
||||
testCases += [
|
||||
DifferentiableTests.all
|
||||
]
|
||||
#endif
|
||||
|
||||
XCTMain(testCases)
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user