-
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.Ressources
Vidéos connexes
WWDC21
WWDC20
-
Rechercher dans cette vidéo…
-
-
1:05 - The log-odds function (Double)
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) } -
2:33 - The log-odds function (Real)
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) } -
7:10 - The Complex type
import Numerics let z = Complex(1.0, 2.0) // z = 1 + 2 i -
7:38 - The Complex type: Basic definition
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 } } -
8:04 - The Complex type: Standard arithmetic operations
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) } } -
8:19 - The Complex type: Polar coordinates
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) } } -
9:16 - Using Accelerate's Basic Linear Algebra Subroutines
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)
-