diff --git a/README.md b/README.md index 1f166c0..ca3b717 100644 --- a/README.md +++ b/README.md @@ -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[1](#foot1) 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 -[1](#back1) 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 diff --git a/Sources/ComplexModule/Differentiable.swift b/Sources/ComplexModule/Differentiable.swift index 75883f7..2c266db 100644 --- a/Sources/ComplexModule/Differentiable.swift +++ b/Sources/ComplexModule/Differentiable.swift @@ -15,6 +15,11 @@ import _Differentiation extension Complex: Differentiable where RealType: Differentiable, RealType.TangentVector == RealType { public typealias TangentVector = Self + + @inlinable + public var zeroTangentVectorInitializer: () -> Self { + { Complex.zero } + } } extension Complex diff --git a/Sources/ComplexModule/README.md b/Sources/ComplexModule/README.md index a8b3370..dd758d3 100644 --- a/Sources/ComplexModule/README.md +++ b/Sources/ComplexModule/README.md @@ -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. diff --git a/Tests/ComplexTests/ArithmeticBenchmarkTests.swift b/Tests/ComplexTests/ArithmeticBenchmarkTests.swift deleted file mode 100644 index 6b2d94d..0000000 --- a/Tests/ComplexTests/ArithmeticBenchmarkTests.swift +++ /dev/null @@ -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.self) - } - @_transparent - var ctype: CComplex { - unsafeBitCast(self, to: CComplex.self) - } -} - -let wellScaledDoubles: [Complex] = (0 ..< 1024).map { _ in - Complex(length: Double.random(in: 0.5 ..< 2.0), - phase: Double.random(in: -.pi ..< .pi)) -} - -let poorlyScaledDoubles: [Complex] = (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.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.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.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.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.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.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.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.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.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.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.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) - } -} diff --git a/Tests/ComplexTests/DifferentiableTests.swift b/Tests/ComplexTests/DifferentiableTests.swift index 0dc09df..2e44935 100644 --- a/Tests/ComplexTests/DifferentiableTests.swift +++ b/Tests/ComplexTests/DifferentiableTests.swift @@ -69,6 +69,10 @@ final class DifferentiableTests: XCTestCase { XCTAssertEqual(φDivide(Complex(1, 0)), Complex(0.25, -0.25)) XCTAssertEqual(φDivide(Complex(0, 1)), Complex(0.25, 0.25)) } + + func testZeroTangentVectorInitializer() { + XCTAssertEqual(Complex(-5, 5).zeroTangentVector, Complex(0, 0)) + } } #endif