From f59e64a1aaea668f4818e507a574999caf2a1968 Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Wed, 6 Nov 2019 12:40:12 -0500 Subject: [PATCH] Rename ElementaryFunctions module -> Real No change to the protocols involved, purely a change so that the module that users import shares the name of the feature they are most likely to be using, rather than a somewhat-esoteric implementation detail. --- Package.swift | 12 +- README.md | 2 +- Sources/Complex/Complex.swift | 2 +- Sources/Complex/README.md | 2 +- Sources/Numerics/Numerics.swift | 2 +- .../ElementaryFunctions.swift | 132 ++---------------- .../{ElementaryFunctions => Real}/README.md | 6 +- .../Implementations.swift => Real/Real.swift} | 22 ++- Sources/Real/RealFunctions.swift | 115 +++++++++++++++ .../ScalarConformances.swift | 0 Tests/ComplexTests/ArithmeticTests.swift | 2 +- Tests/ComplexTests/PropertyTests.swift | 2 +- .../RealTests.swift} | 2 +- 13 files changed, 160 insertions(+), 141 deletions(-) rename Sources/{ElementaryFunctions => Real}/ElementaryFunctions.swift (72%) rename Sources/{ElementaryFunctions => Real}/README.md (96%) rename Sources/{ElementaryFunctions/Implementations.swift => Real/Real.swift} (77%) create mode 100644 Sources/Real/RealFunctions.swift rename Sources/{ElementaryFunctions => Real}/ScalarConformances.swift (100%) rename Tests/{ElementaryFunctionTests/ElementaryFunctionTests.swift => RealTests/RealTests.swift} (99%) diff --git a/Package.swift b/Package.swift index e7d6c85..f91f441 100644 --- a/Package.swift +++ b/Package.swift @@ -16,18 +16,18 @@ let package = Package( name: "swift-numerics", products: [ .library(name: "Complex", targets: ["Complex"]), - .library(name: "ElementaryFunctions", targets: ["ElementaryFunctions"]), .library(name: "Numerics", targets: ["Numerics"]), + .library(name: "Real", targets: ["Real"]), ], dependencies: [ ], targets: [ - .target(name: "Complex", dependencies: ["ElementaryFunctions"]), - .target(name: "ElementaryFunctions", dependencies: ["NumericsShims"]), - .target(name: "Numerics", dependencies: ["Complex", "ElementaryFunctions"]), + .target(name: "Complex", dependencies: ["Real"]), + .target(name: "Numerics", dependencies: ["Complex", "Real"]), .target(name: "NumericsShims", dependencies: []), + .target(name: "Real", dependencies: ["NumericsShims"]), - .testTarget(name: "ComplexTests", dependencies: ["Complex"]), - .testTarget(name: "ElementaryFunctionTests", dependencies: ["ElementaryFunctions"]), + .testTarget(name: "ComplexTests", dependencies: ["Complex", "NumericsShims"]), + .testTarget(name: "RealTests", dependencies: ["Real"]), ] ) diff --git a/README.md b/README.md index a03b9c5..1f95eee 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ 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. ## Modules -1. [Elementary Functions](Sources/ElementaryFunctions/README.md) +1. [Real](Sources/Real/README.md) 2. [Complex](Sources/Complex/README.md) ## Future expansion diff --git a/Sources/Complex/Complex.swift b/Sources/Complex/Complex.swift index 3f97e05..5707784 100644 --- a/Sources/Complex/Complex.swift +++ b/Sources/Complex/Complex.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -import ElementaryFunctions +import Real /// A complex number represented by real and imaginary parts. /// diff --git a/Sources/Complex/README.md b/Sources/Complex/README.md index 2f6e274..65195b5 100644 --- a/Sources/Complex/README.md +++ b/Sources/Complex/README.md @@ -10,7 +10,7 @@ This module provides approximate feature parity and memory layout compatibility 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 `Numeric` (hence also `AdditiveArithmetic`). ### Dependencies: -- The `ElementaryFunctions` module. +- The `Real` module. ## Design notes diff --git a/Sources/Numerics/Numerics.swift b/Sources/Numerics/Numerics.swift index 720613e..72e83f1 100644 --- a/Sources/Numerics/Numerics.swift +++ b/Sources/Numerics/Numerics.swift @@ -10,5 +10,5 @@ //===----------------------------------------------------------------------===// // A module that re-exports the complete Swift Numerics public API. -@_exported import ElementaryFunctions +@_exported import Real @_exported import Complex diff --git a/Sources/ElementaryFunctions/ElementaryFunctions.swift b/Sources/Real/ElementaryFunctions.swift similarity index 72% rename from Sources/ElementaryFunctions/ElementaryFunctions.swift rename to Sources/Real/ElementaryFunctions.swift index 3117466..4bba67a 100644 --- a/Sources/ElementaryFunctions/ElementaryFunctions.swift +++ b/Sources/Real/ElementaryFunctions.swift @@ -32,13 +32,18 @@ /// - Power and root functions: /// `pow`, `sqrt`, and `root`. /// -/// There is a second protocol, `RealFunctions`, which refines `ElementaryFunctions` -/// and includes additional operations which are more commonly used specifically with real -/// number types. +/// There are two other protocols that you are more likely to want to use directly: +/// +/// `RealFunctions` refines `ElementaryFunctions` and includes +/// additional functions specific to real number types. +/// +/// `Real` conforms to `RealFunctions` and `FloatingPoint`, and is the +/// protocol that you will want to use most often for generic code. /// /// See Also: /// - /// - `RealFunctions` +/// - `Real` /// /// [elfn]: http://en.wikipedia.org/wiki/Elementary_function public protocol ElementaryFunctions { @@ -311,124 +316,3 @@ public protocol ElementaryFunctions { /// static func root(_ x: Self, _ n: Int) -> Self } - -public protocol RealFunctions: ElementaryFunctions { - /// `atan(y/x)`, with sign selected according to the quadrant of `(x, y)`. - /// - /// See also: - /// - - /// - `atan()` - static func atan2(y: Self, x: Self) -> Self - - /// The error function evaluated at `x`. - /// - /// See also: - /// - - /// - `erfc()` - static func erf(_ x: Self) -> Self - - /// The complimentary error function evaluated at `x`. - /// - /// See also: - /// - - /// - `erf()` - static func erfc(_ x: Self) -> Self - - /// 2^x - /// - /// See also: - /// - - /// - `exp()` - /// - `expMinusOne()` - /// - `exp10()` - /// - `log2()` - /// - `pow()` - static func exp2(_ x: Self) -> Self - - /// 10^x - /// - /// See also: - /// - - /// - `exp()` - /// - `expMinusOne()` - /// - `exp2()` - /// - `log10()` - /// - `pow()` - static func exp10(_ x: Self) -> Self - - /// `sqrt(x*x + y*y)`, computed in a manner that avoids spurious overflow or underflow. - static func hypot(_ x: Self, _ y: Self) -> Self - - /// The gamma function Γ(x). - /// - /// See also: - /// - - /// - `logGamma()` - /// - `signGamma()` - static func gamma(_ x: Self) -> Self - - /// The base-2 logarithm of `x`. - /// - /// See also: - /// - - /// - `exp2()` - /// - `log()` - /// - `log(onePlus:)` - /// - `log10()` - static func log2(_ x: Self) -> Self - - /// The base-10 logarithm of `x`. - /// - /// See also: - /// - - /// - `exp10()` - /// - `log()` - /// - `log(onePlus:)` - /// - `log2()` - static func log10(_ x: Self) -> Self - -#if !os(Windows) - /// The logarithm of the absolute value of the gamma function, log(|Γ(x)|). - /// - /// Not available on Windows targets. - /// - /// See also: - /// - - /// - `gamma()` - /// - `signGamma()` - static func logGamma(_ x: Self) -> Self - - /// The sign of the gamma function, Γ(x). - /// - /// For `x >= 0`, `signGamma(x)` is `.plus`. For negative `x`, `signGamma(x)` is `.plus` - /// when `x` is an integer, and otherwise it is `.minus` whenver `trunc(x)` is even, and `.plus` - /// when `trunc(x)` is odd. - /// - /// This function is used together with `logGamma`, which computes the logarithm of the - /// absolute value of Γ(x), to recover the sign information. - /// - /// Not available on Windows targets. - /// - /// See also: - /// - - /// - `gamma()` - /// - `logGamma()` - static func signGamma(_ x: Self) -> FloatingPointSign -#endif -} - -/// A type that models the real numbers. -/// -/// Types conforming to this protocol provide the arithmetic and utility operations defined by -/// the `FloatingPoint` protocol, and provide all of the math functions defined by the -/// `ElementaryFunctions` and `RealFunctions` protocols. This protocol does not -/// add any additional conformances itself, but is very useful as a protocol against which to -/// write generic code. For example, we can naturally write a generic version of the a sigmoid -/// function: -/// ``` -/// func sigmoid(_ x: T) -> T { -/// return 1/(1 + .exp(-x)) -/// } -/// ``` -public protocol Real: FloatingPoint, RealFunctions { -} diff --git a/Sources/ElementaryFunctions/README.md b/Sources/Real/README.md similarity index 96% rename from Sources/ElementaryFunctions/README.md rename to Sources/Real/README.md index a86d2ef..9d0323d 100644 --- a/Sources/ElementaryFunctions/README.md +++ b/Sources/Real/README.md @@ -1,4 +1,4 @@ -# Elementary Functions +# Real This module implements [SE-0246]. It is implemented in Swift Numerics to work around difficulties that blocked adding these operations to the standard library for 5.1: @@ -11,9 +11,9 @@ It is implemented in Swift Numerics to work around difficulties that blocked add Placing this module in Swift Numerics first gives the community a chance to address some of the compiler limitations that this feature ran into *before* it lands in the standard library (avoiding unnecessary source churn for projects that use these features), while also making it available *now*, including back-deployment to older Apple OS targets. -## Using Elementary Functions +## Using Real -First, either import `ElementaryFunctions` directly or import the `Numerics` umbrella module. +First, either import `Real` directly or import the `Numerics` umbrella module. This makes all the elementary functions available on `Float`, `Double`, and--on platforms that support it--`Float80`: ``` import Numerics diff --git a/Sources/ElementaryFunctions/Implementations.swift b/Sources/Real/Real.swift similarity index 77% rename from Sources/ElementaryFunctions/Implementations.swift rename to Sources/Real/Real.swift index af945ae..e1c246c 100644 --- a/Sources/ElementaryFunctions/Implementations.swift +++ b/Sources/Real/Real.swift @@ -1,4 +1,4 @@ -//===--- Implementations.swift --------------------------------*- swift -*-===// +//===--- Real.swift -------------------------------------------*- swift -*-===// // // This source file is part of the Swift Numerics open source project // @@ -9,6 +9,26 @@ // //===----------------------------------------------------------------------===// +/// A type that models the real numbers. +/// +/// Types conforming to this protocol provide the arithmetic and utility operations defined by +/// the `FloatingPoint` protocol, and provide all of the math functions defined by the +/// `ElementaryFunctions` and `RealFunctions` protocols. This protocol does not +/// add any additional conformances itself, but is very useful as a protocol against which to +/// write generic code. For example, we can naturally write a generic version of the a sigmoid +/// function: +/// ``` +/// func sigmoid(_ x: T) -> T { +/// return 1/(1 + .exp(-x)) +/// } +/// ``` +/// See Also: +/// - +/// - `ElementaryFunctions` +/// - `RealFunctions` +public protocol Real: FloatingPoint, RealFunctions { +} + // While `Real` does not provide any additional customization points, // it does allow us to default the implementation of a few operations, // and also provides `signGamma`. diff --git a/Sources/Real/RealFunctions.swift b/Sources/Real/RealFunctions.swift new file mode 100644 index 0000000..10d4b5c --- /dev/null +++ b/Sources/Real/RealFunctions.swift @@ -0,0 +1,115 @@ +//===--- RealFunctions.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 +// +//===----------------------------------------------------------------------===// + +public protocol RealFunctions: ElementaryFunctions { + /// `atan(y/x)`, with sign selected according to the quadrant of `(x, y)`. + /// + /// See also: + /// - + /// - `atan()` + static func atan2(y: Self, x: Self) -> Self + + /// The error function evaluated at `x`. + /// + /// See also: + /// - + /// - `erfc()` + static func erf(_ x: Self) -> Self + + /// The complimentary error function evaluated at `x`. + /// + /// See also: + /// - + /// - `erf()` + static func erfc(_ x: Self) -> Self + + /// 2^x + /// + /// See also: + /// - + /// - `exp()` + /// - `expMinusOne()` + /// - `exp10()` + /// - `log2()` + /// - `pow()` + static func exp2(_ x: Self) -> Self + + /// 10^x + /// + /// See also: + /// - + /// - `exp()` + /// - `expMinusOne()` + /// - `exp2()` + /// - `log10()` + /// - `pow()` + static func exp10(_ x: Self) -> Self + + /// `sqrt(x*x + y*y)`, computed in a manner that avoids spurious overflow or underflow. + static func hypot(_ x: Self, _ y: Self) -> Self + + /// The gamma function Γ(x). + /// + /// See also: + /// - + /// - `logGamma()` + /// - `signGamma()` + static func gamma(_ x: Self) -> Self + + /// The base-2 logarithm of `x`. + /// + /// See also: + /// - + /// - `exp2()` + /// - `log()` + /// - `log(onePlus:)` + /// - `log10()` + static func log2(_ x: Self) -> Self + + /// The base-10 logarithm of `x`. + /// + /// See also: + /// - + /// - `exp10()` + /// - `log()` + /// - `log(onePlus:)` + /// - `log2()` + static func log10(_ x: Self) -> Self + +#if !os(Windows) + /// The logarithm of the absolute value of the gamma function, log(|Γ(x)|). + /// + /// Not available on Windows targets. + /// + /// See also: + /// - + /// - `gamma()` + /// - `signGamma()` + static func logGamma(_ x: Self) -> Self + + /// The sign of the gamma function, Γ(x). + /// + /// For `x >= 0`, `signGamma(x)` is `.plus`. For negative `x`, `signGamma(x)` is `.plus` + /// when `x` is an integer, and otherwise it is `.minus` whenver `trunc(x)` is even, and `.plus` + /// when `trunc(x)` is odd. + /// + /// This function is used together with `logGamma`, which computes the logarithm of the + /// absolute value of Γ(x), to recover the sign information. + /// + /// Not available on Windows targets. + /// + /// See also: + /// - + /// - `gamma()` + /// - `logGamma()` + static func signGamma(_ x: Self) -> FloatingPointSign +#endif +} diff --git a/Sources/ElementaryFunctions/ScalarConformances.swift b/Sources/Real/ScalarConformances.swift similarity index 100% rename from Sources/ElementaryFunctions/ScalarConformances.swift rename to Sources/Real/ScalarConformances.swift diff --git a/Tests/ComplexTests/ArithmeticTests.swift b/Tests/ComplexTests/ArithmeticTests.swift index 17d49f0..000154d 100644 --- a/Tests/ComplexTests/ArithmeticTests.swift +++ b/Tests/ComplexTests/ArithmeticTests.swift @@ -11,7 +11,7 @@ import XCTest import Complex -import ElementaryFunctions +import Real // TODO: improve this to be a general-purpose complex comparison with tolerance func relativeError(_ a: Complex, _ b: Complex) -> T { diff --git a/Tests/ComplexTests/PropertyTests.swift b/Tests/ComplexTests/PropertyTests.swift index f9a55e3..475fd27 100644 --- a/Tests/ComplexTests/PropertyTests.swift +++ b/Tests/ComplexTests/PropertyTests.swift @@ -11,7 +11,7 @@ import XCTest import Complex -import ElementaryFunctions +import Real final class PropertyTests: XCTestCase { diff --git a/Tests/ElementaryFunctionTests/ElementaryFunctionTests.swift b/Tests/RealTests/RealTests.swift similarity index 99% rename from Tests/ElementaryFunctionTests/ElementaryFunctionTests.swift rename to Tests/RealTests/RealTests.swift index ce753bb..1f618ae 100644 --- a/Tests/ElementaryFunctionTests/ElementaryFunctionTests.swift +++ b/Tests/RealTests/RealTests.swift @@ -10,7 +10,7 @@ //===----------------------------------------------------------------------===// import XCTest -import ElementaryFunctions +import Real #if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android) typealias TestLiteralType = Float80