The Swift Numerics library, which became open source at the end of 2019, focuses on all kinds of general calculations of floating-point numbers. Here’s a quick look at what’s available.

Project address: github.com/apple/swift…

An overview of the

Swift Numerics abstracts two data types: Real number and Complex number, which are represented by Real protocol and Complex structure respectively, while Complex is defined based on Real protocol.

Real deal

Real is a good example of using the Swift protocol and shows how existing types can be extended and combined to create more powerful capabilities.

The Real protocol defines a set of commonly used calculation methods for Real floating-point numbers. These methods are embedded in the system at the bottom, but they are not general enough to support the Swift paradigm, not friendly enough to support new floating-point types, and are not written to Swift. These are the main problems solved by the design of Real protocol.

Agreement chain

Here’s a quick look at the relationship chain of the Real protocol and how it is constructed:

The Real protocol combines three protocols: FloatingPoint, RealFunctions and AlgebraicField.

FloatingPoint and its descendants, SignedNumeric and AdditiveArithmetic, are built-in protocols in the standard library, allowing Real to directly follow existing floating-point features.

AlgebraicField, RealFunctions and ElementaryFunctions protocols added in this project are more perfect encapsulation of the concept of Real numbers by Real protocol.

Finally, the Real protocol is implemented for Double, Float, Float80 and Float16 in the project. These commonly used floating-point types can seamlessly support the Real number features brought by Real.

The set of real Numbers

Real encapsulates the concept of Real numbers in the following two aspects:

  • Definition of algebraic number field: The primary feature of algebraic number field in mathematics is to support four operations. AlgebraicField protocol mainly defines four operations, and it adds division calculation on the basis of SignedNumeric protocol.
  • Exponent, trigonometry, logarithm, square root and other floating point operations: these methods are the most used floating point numbers, in graphics, animation, AR and other fields have a lot of applications. The ElementaryFunctions protocol supports the most common of these methods, and the RealFunctions protocol further extends some methods to form the most complete set of floating-point methods.

The Real protocol comes with methods

The Real protocol itself does not provide many methods. Most of the methods are in the other three protocols. The methods are as follows:

  • Exp10 (_:) is an exponent based on 10
  • CosMinusOne (_:) cos(x) -1 is another calculation method that preserves accuracy.
  • SignGamma (_:) gamma function symbol (positive and negative)
  • mulAdd(::Compute a*b + c
  • SQRT (_:) square root calculation

ElementaryFunctions agreement

ElementaryFunctions protocol provides exponents, trigonometric functions, logarithms, square roots and other operations, which are conventional calculations in floating point numbers. Meanwhile, it is based on AdditiveArithmetic protocol, so it also supports addition and subtraction operations. It supports the following methods:

  • Exponential exp(_:), based on the natural logarithm.
  • ExpMinusOne (_:) exp(x) -1, provides better accuracy than exp(x).
  • Cosh (_:) hyperbolic cosine
  • Sinh (_:) hyperbolic sine
  • Tanh (_:) hyperbolic tangent function
  • Cosine of _: a cosine
  • Sine of _: sine
  • Tangent of _:)
  • Log (_:) natural logarithm calculation
  • Log (onePlus:_) log(1 + x), which provides better accuracy than log(x).
  • Acosh (_:) inverse hyperbolic cosine
  • Asinh (_:) inverse hyperbolic sine function
  • Atanh (_:) inverse hyperbolic tangent function
  • A cosine of OS (_:)
  • A sine of theta (_:) arcsine
  • Atan (_:) arctangent function
  • pow(:🙂 another way of calculating exponentials, equivalent to exp(y * log(x)), but with different precision.
  • pow(:: Int) power operation
  • SQRT (_:) square root calculation
  • root(:🙂 compute the quadratic root

RealFunctions agreement

The RealFunctions protocol extends some of the less common methods on top of ElementaryFunctions:

atan2(y:x:)
erf(_:)
erfc(_:)
exp2(_:)
exp10(_:)
hypot(_:_:)
gamma(_:)
log2(_:)
log10(_:)
logGamma(_:)
signGamma(_:)
_mulAdd(_:_:_:)
Copy the code

Complex structure

Complex defines some basic properties and methods for mathematical Complex numbers, using two values x and y that comply with the Real protocol to represent the Real and imaginary parts, respectively.

The warehouse used

If Xcode 11 or above is used, directly add a package to the project Settings of the project, address: github.com/apple/swift… , select version 0.0.8.

The module reference

Currently, the project offers two major modules:

  • RealModule
  • ComplexModule

A unified package of both modules is provided for Numerics, which can be used separately or uniformly:

import RealModule
import ComplexModule
import Numerics // There is no need to import the above two modules
Copy the code

conclusion

The Swift open source library is now in full bloom, and the usage scenarios are getting richer. The library shows us elegant ways to use the protocol, which can guide us to write more authentic Swift.