mirror of
https://github.com/apple/swift-numerics
synced 2026-11-22 04:30:45 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5dfc460876 | ||
|
|
014ce562a4 | ||
|
|
9dba38cc1a | ||
|
|
0fa2cd458e | ||
|
|
be3d09ab26 | ||
|
|
ba4469d3fd | ||
|
|
6951647e4c | ||
|
|
a7c141e818 | ||
|
|
76954c06a7 | ||
|
|
4352c3a6a1 | ||
|
|
43052f0819 | ||
|
|
119377cc49 | ||
|
|
016cd6a2fc |
No files matched your search
+8
-8
@@ -15,19 +15,19 @@ import PackageDescription
|
||||
let package = Package(
|
||||
name: "swift-numerics",
|
||||
products: [
|
||||
.library(name: "Complex", targets: ["Complex"]),
|
||||
.library(name: "ComplexModule", targets: ["ComplexModule"]),
|
||||
.library(name: "Numerics", targets: ["Numerics"]),
|
||||
.library(name: "Real", targets: ["Real"]),
|
||||
.library(name: "RealModule", targets: ["RealModule"]),
|
||||
],
|
||||
dependencies: [
|
||||
],
|
||||
targets: [
|
||||
.target(name: "Complex", dependencies: ["Real"]),
|
||||
.target(name: "Numerics", dependencies: ["Complex", "Real"]),
|
||||
.target(name: "NumericsShims", dependencies: []),
|
||||
.target(name: "Real", dependencies: ["NumericsShims"]),
|
||||
.target(name: "ComplexModule", dependencies: ["RealModule"]),
|
||||
.target(name: "Numerics", dependencies: ["ComplexModule", "RealModule"]),
|
||||
.target(name: "_NumericsShims", dependencies: []),
|
||||
.target(name: "RealModule", dependencies: ["_NumericsShims"]),
|
||||
|
||||
.testTarget(name: "ComplexTests", dependencies: ["Complex", "NumericsShims"]),
|
||||
.testTarget(name: "RealTests", dependencies: ["Real"]),
|
||||
.testTarget(name: "ComplexTests", dependencies: ["ComplexModule", "_NumericsShims"]),
|
||||
.testTarget(name: "RealTests", dependencies: ["RealModule"]),
|
||||
]
|
||||
)
|
||||
@@ -9,9 +9,9 @@ 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 the Complex module 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<sup><a name="back1">[1](#foot1)</a></sup> without pulling in everything else in the library as well:
|
||||
```swift
|
||||
import Complex
|
||||
import ComplexModule
|
||||
|
||||
let z = Complex<Double>.i
|
||||
```
|
||||
@@ -51,8 +51,8 @@ To fix a bug, or make smaller improvements:
|
||||
Questions about how to use Swift Numerics modules, or issues that are not clearly bugs can be discussed in the ["Swift Numerics" section of the Swift forums.](https://forums.swift.org/c/related-projects/swift-numerics)
|
||||
|
||||
## Modules
|
||||
1. [Real](Sources/Real/README.md)
|
||||
2. [Complex](Sources/Complex/README.md)
|
||||
1. [RealModule](Sources/RealModule/README.md)
|
||||
2. [ComplexModule](Sources/ComplexModule/README.md)
|
||||
|
||||
## Future expansion
|
||||
1. [Approximate Equality](https://github.com/apple/swift-numerics/issues/3)
|
||||
@@ -60,4 +60,23 @@ Questions about how to use Swift Numerics modules, or issues that are not clearl
|
||||
3. [Arbitrary-Precision Integers](https://github.com/apple/swift-numerics/issues/5)
|
||||
4. [Shaped Arrays](https://github.com/apple/swift-numerics/issues/6)
|
||||
5. [Decimal Floating-point](https://github.com/apple/swift-numerics/issues/7)
|
||||
6. [Float16](https://github.com/apple/swift-numerics/issues/8)
|
||||
|
||||
## 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).
|
||||
This would prevent users of Swift Numerics who don't need generic types from doing things like:
|
||||
```swift
|
||||
import Complex
|
||||
// I know I only ever want Complex<Double>, so I shouldn't need the generic parameter.
|
||||
typealias Complex = Complex.Complex<Double> // doesn't work, because name lookup fails.
|
||||
```
|
||||
For this reason, modules that would have this ambiguity are suffixed with `Module` within Swift Numerics:
|
||||
```swift
|
||||
import ComplexModule
|
||||
// I know I only ever want Complex<Double>, so I shouldn't need the generic parameter.
|
||||
typealias Complex = ComplexModule.Complex<Double>
|
||||
// But I can still refer to the generic type by qualifying the name if I need it occasionally:
|
||||
let a = ComplexModule.Complex<Float>
|
||||
```
|
||||
The `Real` module does not contain a `Real` type, but does contain a `Real` protocol, and users may want to define their own `Real` type (and possibly re-export the `Real` module), so the suffix is also applied there.
|
||||
New modules have to evaluate this decision carefully, but can err on the side of adding the suffix.
|
||||
It's expected that most users will simply `import Numerics`, so this isn't an issue for them.
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Real
|
||||
import RealModule
|
||||
|
||||
// MARK: - Additive structure
|
||||
extension Complex: AdditiveArithmetic {
|
||||
@@ -165,5 +165,3 @@ extension Complex: AlgebraicField {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
extension Complex: SignedNumeric {}
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Real
|
||||
import RealModule
|
||||
|
||||
/// A complex number represented by real and imaginary parts.
|
||||
///
|
||||
@@ -2,15 +2,15 @@
|
||||
|
||||
This module provides a `Complex` number type generic over an underlying `RealType`:
|
||||
```swift
|
||||
1> import Complex
|
||||
1> import ComplexModule
|
||||
2> let z = Complex(1,1) // z = 1 + i
|
||||
```
|
||||
This module provides approximate feature parity and memory layout compatibility with C, Fortran, and C++ complex types (although the importer cannot map the types for you, buffers may be reinterpreted to shim API defined in other languages).
|
||||
|
||||
The usual arithmetic operators are provided for Complex numbers, as well as conversion to and from polar coordinates and many useful properties, plus conformances to the obvious usual protocols: `Equatable`, `Hashable`, `Codable` (if the underlying `RealType` is), and `AlgebraicField` (hence also `AdditiveArithmetic` and `Numeric`).
|
||||
The usual arithmetic operators are provided for Complex numbers, as well as conversion to and from polar coordinates and many useful properties, plus conformances to the obvious usual protocols: `Equatable`, `Hashable`, `Codable` (if the underlying `RealType` is), and `AlgebraicField` (hence also `AdditiveArithmetic` and `SignedNumeric`).
|
||||
|
||||
### Dependencies:
|
||||
- The `Real` module.
|
||||
- `RealModule`.
|
||||
|
||||
## Design notes
|
||||
|
||||
@@ -10,5 +10,5 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// A module that re-exports the complete Swift Numerics public API.
|
||||
@_exported import Real
|
||||
@_exported import Complex
|
||||
@_exported import RealModule
|
||||
@_exported import ComplexModule
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
/// A type modeling an algebraic [field]. Refines the `Numeric` protocol,
|
||||
/// A type modeling an algebraic [field]. Refines the `SignedNumeric` protocol,
|
||||
/// adding division.
|
||||
///
|
||||
/// A field is a set on which addition, subtraction, multiplication, and
|
||||
@@ -40,11 +40,12 @@
|
||||
/// See Also:
|
||||
/// -
|
||||
/// - Real
|
||||
/// - SignedNumeric
|
||||
/// - Numeric
|
||||
/// - AdditiveArithmetic
|
||||
///
|
||||
/// [field]: https://en.wikipedia.org/wiki/Field_(mathematics)
|
||||
public protocol AlgebraicField: Numeric {
|
||||
public protocol AlgebraicField: SignedNumeric {
|
||||
|
||||
static func /=(a: inout Self, b: Self)
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import NumericsShims
|
||||
import _NumericsShims
|
||||
|
||||
extension Double: Real {
|
||||
@_transparent
|
||||
File renamed without changes.
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import NumericsShims
|
||||
import _NumericsShims
|
||||
|
||||
extension Float: Real {
|
||||
@_transparent
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import NumericsShims
|
||||
import _NumericsShims
|
||||
|
||||
// Restrict extension to platforms for which Float80 exists.
|
||||
#if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android)
|
||||
@@ -1,8 +1,8 @@
|
||||
# Real
|
||||
# Real Module
|
||||
|
||||
[SE-0246] proposed an API for "basic math functions" that would make operations like sine and logarithm available in generic contexts.
|
||||
It was accepted, but because of limitations in the compiler, the API could not be added to the standard library in a source-stable manner.
|
||||
The `Real` module provides that API as a separate module so that you can use it right away to get access to the improved API for these operations in your projects.
|
||||
`RealModule` provides that API as a separate module so that you can use it right away to get access to the improved API for these operations in your projects.
|
||||
|
||||
## Protocols and Methods
|
||||
|
||||
@@ -28,12 +28,13 @@ The `RealFunctions` protocol refines `ElementaryFunctions`, and adds operations
|
||||
The protocol that you will use most often is `Real`, which describes a floating-point type equipped with the full set of basic math functions.
|
||||
This is a great protocol to use in writing generic code, because it has all the basics that you need to implement most numeric functions.
|
||||
|
||||
The fourth protocol is `AlgebraicField`, which `Real` also refines. This protocol is a very small refinement of `Numeric`, adding the `/` and `/=` operators and a `reciprocal` property.
|
||||
The fourth protocol is `AlgebraicField`, which `Real` also refines.
|
||||
This protocol is a very small refinement of `SignedNumeric`, adding the `/` and `/=` operators and a `reciprocal` property.
|
||||
The primary use of this protocol is for writing code that is generic over real and complex types.
|
||||
|
||||
## Using Real
|
||||
|
||||
First, either import `Real` directly or import the `Numerics` umbrella module.
|
||||
First, either import `RealModule` directly or import the `Numerics` umbrella module.
|
||||
|
||||
Suppose we were experimenting with some basic machine learning, and needed a generic [sigmoid function][Sigmoid] activation function:
|
||||
|
||||
@@ -68,7 +69,7 @@ When new basic floating-point types are added to Swift, like `Float16` or `Float
|
||||
Not having this protocol is a significant missing feature for numerical computing in Swift, and I'm really looking forward to seeing what people do with it.
|
||||
|
||||
### Dependencies:
|
||||
- The C standard math library (`libm`) via the `NumericShims` target.
|
||||
- The C standard math library (`libm`) via the `_NumericsShims` target.
|
||||
|
||||
[ErrorFunction]: https://en.wikipedia.org/wiki/Error_function
|
||||
[GammaFunction]: https://en.wikipedia.org/wiki/Gamma_function
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -16,4 +16,4 @@
|
||||
// If any shims are added that are not pure header inlines, whatever runtime
|
||||
// support they require can be added to this file.
|
||||
|
||||
#include "NumericsShims.h"
|
||||
#include "_NumericsShims.h"
|
||||
File renamed without changes.
@@ -10,10 +10,10 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import XCTest
|
||||
import Complex
|
||||
import ComplexModule
|
||||
|
||||
// For CComplex and shims
|
||||
import NumericsShims
|
||||
import _NumericsShims
|
||||
|
||||
extension Complex where RealType == Double {
|
||||
@_transparent
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import XCTest
|
||||
import Complex
|
||||
import Real
|
||||
import ComplexModule
|
||||
import RealModule
|
||||
|
||||
// TODO: improve this to be a general-purpose complex comparison with tolerance
|
||||
func relativeError<T>(_ a: Complex<T>, _ b: Complex<T>) -> T {
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import XCTest
|
||||
import Complex
|
||||
import Real
|
||||
import ComplexModule
|
||||
import RealModule
|
||||
|
||||
final class PropertyTests: XCTestCase {
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import XCTest
|
||||
import Real
|
||||
import RealModule
|
||||
|
||||
internal extension Real where Self: BinaryFloatingPoint {
|
||||
static func testIntegerExponentCommon() {
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import XCTest
|
||||
import Real
|
||||
import RealModule
|
||||
|
||||
#if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android)
|
||||
typealias TestLiteralType = Float80
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import XCTest
|
||||
import Real
|
||||
import RealModule
|
||||
|
||||
internal extension ElementaryFunctions where Self: BinaryFloatingPoint {
|
||||
static func elementaryFunctionChecks() {
|
||||
|
||||
Reference in new issue
Block a user