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:

    • location is 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 : IntegerType

    Parameters

    location

    A location () where the slope approaches the searched‐for local maximum.

    bounds

    An optional domain (for ) to stay within.

    function

    The function to analyze.

    input

    An 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:

    • location is 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 : IntegerType

    Parameters

    location

    A location () where the slope approaches the searched‐for local minimum.

    bounds

    An optional domain (for ) to stay within.

    function

    The function to analyze.

    input

    An 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

    value

    The value in degrees.

  • Returns an angle in minutes.

    Declaration

    Swift

    public postfix func  <N : RealArithmetic>(value: N) -> Angle<N>

    Parameters

    value

    The value in minutes.

  • Returns an angle in seconds.

    Declaration

    Swift

    public postfix func ′′ <N : RealArithmetic>(value: N) -> Angle<N>

    Parameters

    value

    The value in seconds.

  • Returns the greatest common divisor of a and b.

    Mutating variant

    formGreatestCommonDivisor

    Declaration

    Swift

    public func gcd<M : Measurement>(_ a: M, _ b: M) -> M

    Parameters

    lhs

    A value.

    rhs

    Another value.

  • Returns the least common multiple of a and b.

    Mutating variant

    formGreatestCommonDivisor

    Declaration

    Swift

    public func lcm<M : Measurement>(_ a: M, _ b: M) -> M

    Parameters

    lhs

    A value.

    rhs

    Another value.

  • Returns true if the left value is ordered before or the same as the right value.

    Declaration

    Swift

    public func  <A : Comparable, B : Comparable>(lhs: (A, B), rhs: (A, B)) -> Bool

    Parameters

    lhs

    A value to compare.

    rhs

    Another value to compare.

  • Returns true if the left value is ordered before or the same as the right value.

    Declaration

    Swift

    public func  <A : Comparable, B : Comparable, C : Comparable>(lhs: (A, B, C), rhs: (A, B, C)) -> Bool

    Parameters

    lhs

    A value to compare.

    rhs

    Another value to compare.

  • Returns true if the left value is ordered before or the same as the right value.

    Declaration

    Swift

    public func  <A : Comparable, B : Comparable, C : Comparable, D : Comparable>(lhs: (A, B, C, D), rhs: (A, B, C, D)) -> Bool

    Parameters

    lhs

    A value to compare.

    rhs

    Another value to compare.

  • Returns true if the left value is ordered before or the same as the right value.

    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)) -> Bool

    Parameters

    lhs

    A value to compare.

    rhs

    Another value to compare.

  • Returns true if the left value is ordered before or the same as the right value.

    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)) -> Bool

    Parameters

    lhs

    A value to compare.

    rhs

    Another value to compare.

  • Returns true if the left value is ordered after or the same as the right value.

    Declaration

    Swift

    public func  <A : Comparable, B : Comparable>(lhs: (A, B), rhs: (A, B)) -> Bool

    Parameters

    lhs

    A value to compare.

    rhs

    Another value to compare.

  • Returns true if the left value is ordered after or the same as the right value.

    Declaration

    Swift

    public func  <A : Comparable, B : Comparable, C : Comparable>(lhs: (A, B, C), rhs: (A, B, C)) -> Bool

    Parameters

    lhs

    A value to compare.

    rhs

    Another value to compare.

  • Returns true if the left value is ordered after or the same as the right value.

    Declaration

    Swift

    public func  <A : Comparable, B : Comparable, C : Comparable, D : Comparable>(lhs: (A, B, C, D), rhs: (A, B, C, D)) -> Bool

    Parameters

    lhs

    A value to compare.

    rhs

    Another value to compare.

  • Returns true if the left value is ordered after or the same as the right value.

    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)) -> Bool

    Parameters

    lhs

    A value to compare.

    rhs

    Another value to compare.

  • Returns true if the left value is ordered after or the same as the right value.

    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)) -> Bool

    Parameters

    lhs

    A value to compare.

    rhs

    Another value to compare.

  • Returns the absolute value (in conjuction with postfix |(_:)).

    let x = 1
    let y = |x|
    // y == 1
    

    Declaration

    Swift

    public prefix func | <Value>(operand: _PartialAbsoluteValue<Value>) -> Value
  • Returns the absolute value (in conjuction with prefix |(_:)).

    let x = 1
    let y = |x|
    // y == 1
    

    Declaration

    Swift

    public postfix func | <Value>(operand: Value) -> _PartialAbsoluteValue<Value>
  • An instance of π in the desired return type.

    Note

    This is an alias for N.π 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 for N.τ to improve the legibility of code involving mathematical equations.

    Declaration

    Swift

    public func τ<N : RealArithmetic>() -> N
  • e()

    An instance of e in the desired return type.

    Note

    This is an alias for N.e to improve the legibility of code involving mathematical equations.

    Declaration

    Swift

    public func e<N : RealArithmetic>() -> N
  • Returns the base base logarithm of antilogarithm.

    Precondition

    antilogarithm > 0

    Precondition

    base > 0

    Precondition

    base ≠ 1

    Mutating variant

    formLogarithm

    Declaration

    Swift

    public func log<N : RealArithmetic>(toBase base: N, of antilogarithm: N) -> N

    Parameters

    base

    The base.

    antilogarithm

    The antilogarithm.

  • Returns the common logarithm of antilogarithm.

    Precondition

    antilogarithm > 0

    Mutating variant

    formCommonLogarithm

    Declaration

    Swift

    public func log<N : RealArithmetic>(_ antilogarithm: N) -> N

    Parameters

    antilogarithm

    The antilogarithm.

  • Returns the natural logarithm of antilogarithm.

    Precondition

    antilogarithm > 0

    Mutating variant

    formNaturalLogarithm

    Declaration

    Swift

    public func ln<N : RealArithmetic>(_ antilogarithm: N) -> N

    Parameters

    antilogarithm

    The antilogarithm.

  • Returns the sine of an angle.

    Declaration

    Swift

    public func sin<N : RealArithmetic>(_ angle: Angle<N>) -> N

    Parameters

    angle

    The angle.

  • Returns the cosine of an angle.

    Declaration

    Swift

    public func cos<N : RealArithmetic>(_ angle: Angle<N>) -> N

    Parameters

    angle

    The angle.

  • Returns the tangent of an angle.

    Declaration

    Swift

    public func tan<N : RealArithmetic>(_ angle: Angle<N>) -> N

    Parameters

    angle

    The angle.

  • Returns the cosecant of an angle.

    Declaration

    Swift

    public func csc<N : RealArithmetic>(_ angle: Angle<N>) -> N

    Parameters

    angle

    The angle.

  • Returns the secant of an angle.

    Declaration

    Swift

    public func sec<N : RealArithmetic>(_ angle: Angle<N>) -> N

    Parameters

    angle

    The angle.

  • Returns the cotangent of an angle.

    Declaration

    Swift

    public func cot<N : RealArithmetic>(_ angle: Angle<N>) -> N

    Parameters

    angle

    The angle.

  • Returns the arcsine of a value.

    The returned angle will be between −90° and 90° inclusive.

    Precondition

    −1 ≤ sine ≤ 1

    Declaration

    Swift

    public func arcsin<N : RealArithmetic>(_ sine: N) -> Angle<N>

    Parameters

    sine

    The sine.

  • Returns the arccosine of a value.

    The returned angle will be between 0° and 180° inclusive.

    Precondition

    −1 ≤ sine ≤ 1

    Declaration

    Swift

    public func arccos<N : RealArithmetic>(_ cosine: N) -> Angle<N>

    Parameters

    cosine

    The 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

    tangent

    The tangent.

  • Returns the arccosecant of a value.

    The returned angle will be between −90° and 90° inclusive, but never 0°.

    Precondition

    −1 ≥ cosecantcosecant ≤ 1

    Declaration

    Swift

    public func arccsc<N : RealArithmetic>(_ cosecant: N) -> Angle<N>

    Parameters

    cosecant

    The cosecant.

  • Returns the arcsecant of a value.

    The returned angle will be between 0° and 180° inclusive, but never 90°.

    Precondition

    −1≥ secantsecant ≤ 1

    Declaration

    Swift

    public func arcsec<N : RealArithmetic>(_ secant: N) -> Angle<N>

    Parameters

    secant

    The 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

    tangent

    The tangent.