Skip to content

Commit

Permalink
Rename ElementaryFunctions module -> Real
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stephentyrone committed Nov 6, 2019
1 parent fe37d98 commit f59e64a
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 141 deletions.
12 changes: 6 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
]
)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/Complex/Complex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//
//===----------------------------------------------------------------------===//

import ElementaryFunctions
import Real

/// A complex number represented by real and imaginary parts.
///
Expand Down
2 changes: 1 addition & 1 deletion Sources/Complex/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Sources/Numerics/Numerics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
//===----------------------------------------------------------------------===//

// A module that re-exports the complete Swift Numerics public API.
@_exported import ElementaryFunctions
@_exported import Real
@_exported import Complex
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<T: Real>(_ x: T) -> T {
/// return 1/(1 + .exp(-x))
/// }
/// ```
public protocol Real: FloatingPoint, RealFunctions {
}
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===--- Implementations.swift --------------------------------*- swift -*-===//
//===--- Real.swift -------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift Numerics open source project
//
Expand All @@ -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<T: Real>(_ 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`.
Expand Down
115 changes: 115 additions & 0 deletions Sources/Real/RealFunctions.swift
Original file line number Diff line number Diff line change
@@ -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
}
File renamed without changes.
2 changes: 1 addition & 1 deletion Tests/ComplexTests/ArithmeticTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(_ a: Complex<T>, _ b: Complex<T>) -> T {
Expand Down
2 changes: 1 addition & 1 deletion Tests/ComplexTests/PropertyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import XCTest
import Complex
import ElementaryFunctions
import Real

final class PropertyTests: XCTestCase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//===----------------------------------------------------------------------===//

import XCTest
import ElementaryFunctions
import Real

#if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android)
typealias TestLiteralType = Float80
Expand Down

0 comments on commit f59e64a

Please sign in to comment.