Functions
The following functions are available globally.
-
Returns the input (x) corresponding to the local maximum output (y) near
location.This function automates a guess‐and‐check strategy and is useful for inverting otherwise one‐way functions. See the related function
findLocalMaximum(near:bounds:inFunction:)for an example.Warning
Behaviour is undefined when:
locationis at a local minimum. For example:swift // This is undefined: _ = findLocalMaximum(near: 0) { (−10 ..< 10).contains($0) ? $0 ↑ 2 : −($0 ↑ 2) }- two or more adjascent inputs share the maximum output. For example:
swift // This is undefined: _ = findLocalMaximum(near: 0) { (−10 ..< 10).contains($0) ? 1 : −(|$0|) }
Precondition
If
bounds ≠ nil, a local maximum must be known to exist, otherwise execution will get stuck in an infinite loop. For example:// Never do this: _ = findLocalMaximum(near: 0, inFunction: {$0})Declaration
Swift
public func findLocalMaximum<I : OneDimensionalPoint, O : Comparable>(near location: I, within bounds: CountableClosedRange<I>? = nil, inFunction function: (_ input: I) -> O) -> I where I.Vector : IntegerTypeParameters
locationA location () where the slope approaches the searched‐for local maximum.
boundsAn optional domain (for ) to stay within.
functionThe function to analyze.
inputAn input value.
Return Value
The input () that results in the local maximum ().
-
Returns the input (x) corresponding to the local minimum output (y) near
location.This function automates a guess‐and‐check strategy and is useful for inverting otherwise one‐way functions. For example, finding the approximate square root of 120 can be done using only simpler arithmetic like this:
let approximateSquareRootOf120 = findLocalMinimum(near: 10) { (guess: Int) -> Int in // Find the square of the guess. let square = guess × guess // Determine its proximity to 120. return |(square − 120)| } // First iteration (determined by “near: 10”): // 10 → 20 // Second iteration: // 11 → 1 // Decreasing, so continue. // Third iteration: // 12 → 24 // No longer decreasing, so stop. 1 was the local minimum. print(approximateSquareRootOf120) // Prints “11”Warning
Behaviour is undefined when:
locationis at a local maximum. For example:swift // This is undefined: _ = findLocalMinimum(near: 0) { (−10 ..< 10).contains($0) ? −($0 ↑ 2) : $0 ↑ 2 }- two or more adjascent inputs share the minimum output. For example:
swift // This is undefined: _ = findLocalMinimum(near: 0) { (−10 ..< 10).contains($0) ? −1 : |$0| }
Precondition
If
bounds ≠ nil, a local minimum must be known to exist, otherwise execution will get stuck in an infinite loop. For example:// Never do this: _ = findLocalMinimum(near: 0, inFunction: {$0})Declaration
Swift
public func findLocalMinimum<I : OneDimensionalPoint, O : Comparable>(near location: I, within bounds: CountableClosedRange<I>? = nil, inFunction function: (_ input: I) -> O) -> I where I.Vector : IntegerTypeParameters
locationA location () where the slope approaches the searched‐for local minimum.
boundsAn optional domain (for ) to stay within.
functionThe function to analyze.
inputAn input value.
Return Value
The input () that results in the local minimum ().
-
Returns an angle in degrees.
Declaration
Swift
public postfix func ° <N : RealArithmetic>(value: N) -> Angle<N>Parameters
valueThe value in degrees.
-
Returns an angle in minutes.
Declaration
Swift
public postfix func ′ <N : RealArithmetic>(value: N) -> Angle<N>Parameters
valueThe value in minutes.
-
Returns an angle in seconds.
Declaration
Swift
public postfix func ′′ <N : RealArithmetic>(value: N) -> Angle<N>Parameters
valueThe value in seconds.
-
Returns the greatest common divisor of
aandb.Mutating variant
formGreatestCommonDivisor
Declaration
Swift
public func gcd<M : Measurement>(_ a: M, _ b: M) -> MParameters
lhsA value.
rhsAnother value.
-
Returns the least common multiple of
aandb.Mutating variant
formGreatestCommonDivisor
Declaration
Swift
public func lcm<M : Measurement>(_ a: M, _ b: M) -> MParameters
lhsA value.
rhsAnother value.
-
Returns
trueif the left value is ordered before or the same as the right value.Recommended over
<=
Declaration
Swift
public func ≤ <A : Comparable, B : Comparable>(lhs: (A, B), rhs: (A, B)) -> BoolParameters
lhsA value to compare.
rhsAnother value to compare.
-
Returns
trueif the left value is ordered before or the same as the right value.Recommended over
<=
Declaration
Swift
public func ≤ <A : Comparable, B : Comparable, C : Comparable>(lhs: (A, B, C), rhs: (A, B, C)) -> BoolParameters
lhsA value to compare.
rhsAnother value to compare.
-
Returns
trueif the left value is ordered before or the same as the right value.Recommended over
<=
Declaration
Swift
public func ≤ <A : Comparable, B : Comparable, C : Comparable, D : Comparable>(lhs: (A, B, C, D), rhs: (A, B, C, D)) -> BoolParameters
lhsA value to compare.
rhsAnother value to compare.
-
Returns
trueif the left value is ordered before or the same as the right value.Recommended over
<=
Declaration
Swift
public func ≤ <A : Comparable, B : Comparable, C : Comparable, D : Comparable, E : Comparable>(lhs: (A, B, C, D, E), rhs: (A, B, C, D, E)) -> BoolParameters
lhsA value to compare.
rhsAnother value to compare.
-
Returns
trueif the left value is ordered before or the same as the right value.Recommended over
<=
Declaration
Swift
public func ≤ <A : Comparable, B : Comparable, C : Comparable, D : Comparable, E : Comparable, F : Comparable>(lhs: (A, B, C, D, E, F), rhs: (A, B, C, D, E, F)) -> BoolParameters
lhsA value to compare.
rhsAnother value to compare.
-
Returns
trueif the left value is ordered after or the same as the right value.Recommended over
>=
Declaration
Swift
public func ≥ <A : Comparable, B : Comparable>(lhs: (A, B), rhs: (A, B)) -> BoolParameters
lhsA value to compare.
rhsAnother value to compare.
-
Returns
trueif the left value is ordered after or the same as the right value.Recommended over
>=
Declaration
Swift
public func ≥ <A : Comparable, B : Comparable, C : Comparable>(lhs: (A, B, C), rhs: (A, B, C)) -> BoolParameters
lhsA value to compare.
rhsAnother value to compare.
-
Returns
trueif the left value is ordered after or the same as the right value.Recommended over
>=
Declaration
Swift
public func ≥ <A : Comparable, B : Comparable, C : Comparable, D : Comparable>(lhs: (A, B, C, D), rhs: (A, B, C, D)) -> BoolParameters
lhsA value to compare.
rhsAnother value to compare.
-
Returns
trueif the left value is ordered after or the same as the right value.Recommended over
>=
Declaration
Swift
public func ≥ <A : Comparable, B : Comparable, C : Comparable, D : Comparable, E : Comparable>(lhs: (A, B, C, D, E), rhs: (A, B, C, D, E)) -> BoolParameters
lhsA value to compare.
rhsAnother value to compare.
-
Returns
trueif the left value is ordered after or the same as the right value.Recommended over
>=
Declaration
Swift
public func ≥ <A : Comparable, B : Comparable, C : Comparable, D : Comparable, E : Comparable, F : Comparable>(lhs: (A, B, C, D, E, F), rhs: (A, B, C, D, E, F)) -> BoolParameters
lhsA value to compare.
rhsAnother value to compare.
-
Returns the absolute value (in conjuction with postfix
|(_:)).let x = −1 let y = |x| // y == 1Declaration
Swift
public prefix func | <Value>(operand: _PartialAbsoluteValue<Value>) -> Value -
Returns the absolute value (in conjuction with prefix
|(_:)).let x = −1 let y = |x| // y == 1Declaration
Swift
public postfix func | <Value>(operand: Value) -> _PartialAbsoluteValue<Value>
-
An instance of π in the desired return type.
Note
This is an alias forN.πto improve the legibility of code involving mathematical equations.Declaration
Swift
public func π<N : RealArithmetic>() -> N -
An instance of τ in the desired return type.
Note
This is an alias forN.τto improve the legibility of code involving mathematical equations.Declaration
Swift
public func τ<N : RealArithmetic>() -> N -
An instance of e in the desired return type.
Note
This is an alias forN.eto improve the legibility of code involving mathematical equations.Declaration
Swift
public func e<N : RealArithmetic>() -> N -
Returns the base
baselogarithm ofantilogarithm.Precondition
antilogarithm> 0Precondition
base> 0Precondition
base≠ 1Mutating variant
formLogarithm
Declaration
Swift
public func log<N : RealArithmetic>(toBase base: N, of antilogarithm: N) -> NParameters
baseThe base.
antilogarithmThe antilogarithm.
-
Returns the common logarithm of
antilogarithm.Precondition
antilogarithm> 0Mutating variant
formCommonLogarithm
Declaration
Swift
public func log<N : RealArithmetic>(_ antilogarithm: N) -> NParameters
antilogarithmThe antilogarithm.
-
Returns the natural logarithm of
antilogarithm.Precondition
antilogarithm> 0Mutating variant
formNaturalLogarithm
Declaration
Swift
public func ln<N : RealArithmetic>(_ antilogarithm: N) -> NParameters
antilogarithmThe antilogarithm.
-
Returns the sine of an angle.
Declaration
Swift
public func sin<N : RealArithmetic>(_ angle: Angle<N>) -> NParameters
angleThe angle.
-
Returns the cosine of an angle.
Declaration
Swift
public func cos<N : RealArithmetic>(_ angle: Angle<N>) -> NParameters
angleThe angle.
-
Returns the tangent of an angle.
Declaration
Swift
public func tan<N : RealArithmetic>(_ angle: Angle<N>) -> NParameters
angleThe angle.
-
Returns the cosecant of an angle.
Declaration
Swift
public func csc<N : RealArithmetic>(_ angle: Angle<N>) -> NParameters
angleThe angle.
-
Returns the secant of an angle.
Declaration
Swift
public func sec<N : RealArithmetic>(_ angle: Angle<N>) -> NParameters
angleThe angle.
-
Returns the cotangent of an angle.
Declaration
Swift
public func cot<N : RealArithmetic>(_ angle: Angle<N>) -> NParameters
angleThe angle.
-
Returns the arcsine of a value.
The returned angle will be between −90° and 90° inclusive.
Precondition
−1 ≤
sine≤ 1Declaration
Swift
public func arcsin<N : RealArithmetic>(_ sine: N) -> Angle<N>Parameters
sineThe sine.
-
Returns the arccosine of a value.
The returned angle will be between 0° and 180° inclusive.
Precondition
−1 ≤
sine≤ 1Declaration
Swift
public func arccos<N : RealArithmetic>(_ cosine: N) -> Angle<N>Parameters
cosineThe cosine.
-
Returns the arctangent of a value.
The returned angle will be between −90° and 90°.
Declaration
Swift
public func arctan<N : RealArithmetic>(_ tangent: N) -> Angle<N>Parameters
tangentThe tangent.
-
Returns the arccosecant of a value.
The returned angle will be between −90° and 90° inclusive, but never 0°.
Precondition
−1 ≥
cosecant∨cosecant≤ 1Declaration
Swift
public func arccsc<N : RealArithmetic>(_ cosecant: N) -> Angle<N>Parameters
cosecantThe cosecant.
-
Returns the arcsecant of a value.
The returned angle will be between 0° and 180° inclusive, but never 90°.
Precondition
−1≥
secant∨secant≤ 1Declaration
Swift
public func arcsec<N : RealArithmetic>(_ secant: N) -> Angle<N>Parameters
secantThe secant.
-
Returns the arctangent of a value.
The returned angle will be between −90° and 90°.
Declaration
Swift
public func arccot<N : RealArithmetic>(_ cotangent: N) -> Angle<N>Parameters
tangentThe tangent.
View on GitHub
Functions Reference