Dunfey · Hotel WWDC as data, est. 1983
Front desk everything
Years
Topics

2020 Swift

WWDC20 · 15 min · Swift

Explore numerical computing in Swift

Meet Swift Numerics: a new Swift package for computational mathematics. Take a tour of the protocols and types available in the package and find out how you can use them to write generic code. We’ll also show you how and when to use the new Float16 type to improve performance and reduce memory usage. To get the most out of this session, you should have some familiarity with mathematics like logarithmic functions and real and imaginary numbers. You should also be familiar with generic programming in Swift. For more background, watch “Swift Generics (Expanded)” from WWDC18.

Watch at developer.apple.com ↗

Transcript all transcripts

Code shown on screen · 7 snippets

The log-odds function (Double) swift · at 1:05 ↗
import Darwin

/// The log-odds function
///
/// https://en.wikipedia.org/wiki/Logit
///
/// - Parameter p:
///   A probability in the range 0...1.
///
/// - Returns:
///   The log of the odds, 'log(p/(1-p))'.
func logit(_ p: Double) -> Double {
    log(p) - log1p(-p)
}
The log-odds function (Real) swift · at 2:33 ↗
import Numerics

/// The log-odds function
///
/// https://en.wikipedia.org/wiki/Logit
///
/// - Parameter p:
///   A probability in the range 0...1.
///
/// - Returns:
///   The log of the odds, 'log(p/(1-p))'.
func logit<NumberType: Real>(_ p: NumberType) -> NumberType {
    .log(p) - .log(onePlus: -p)
}
The Complex type swift · at 7:10 ↗
import Numerics

let z = Complex(1.0, 2.0) // z = 1 + 2 i
The Complex type: Basic definition swift · at 7:38 ↗
public struct Complex<NumberType> where NumberType: Real {
    /// The real component
    public var real: NumberType
  
    /// The imaginary component
    public var imaginary: NumberType
  
    /// Construct a complex number with specified real and imaginary parts
    public init(_ real: NumberType, _ imaginary: NumberType) {
        self.real = real
        self.imaginary = imaginary
    }
}
The Complex type: Standard arithmetic operations swift · at 8:04 ↗
extension Complex: SignedNumeric {
    /// The sum of 'z' and 'w'
    public static func +(z: Complex, w: Complex) -> Complex {
        return Complex(z.real + w.real, z.imaginary + w.imaginary)
    }

    /// The difference of 'z' and 'w'
    public static func -(z: Complex, w: Complex) -> Complex {
        return Complex(z.real - w.real, z.imaginary - w.imaginary)
    }

    /// The product of 'z' and 'w'
    public static func *(z: Complex, w: Complex) -> Complex {
        return Complex(z.real * w.real - z.imaginary * w.imaginary,
                       z.real * w.imaginary + z.imaginary * w.real)
    }
}
The Complex type: Polar coordinates swift · at 8:19 ↗
extension Complex {
    /// The Euclidean norm (a.k.a. 2-norm) of the number.
    public var length: NumberType {
        return .hypot(real, imaginary)
    }
  
    /// The phase (angle, or "argument").
    ///
    /// Returns the angle (measured above the real axis) in radians.
    public var phase: NumberType {
        return .atan2(y: imaginary, x: real)
    }
  
    /// A complex value with specified polar coordinates.
    public init(length: NumberType, phase: NumberType) {
        self = Complex(.cos(phase), .sin(phase)).multiplied(by: length)
    }
}
Using Accelerate's Basic Linear Algebra Subroutines swift · at 9:16 ↗
import Numerics
import Accelerate

/// Array of 100 random Complex<Double> numbers
let z = (0 ..< 100).map {
    Complex(length: 1.0, phase: Double.random(in: -.pi ... .pi))
}

/// Compute the Euclidean norm of z
let norm = cblas_dznrm2(z.count, &z, 1)

Resources