You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 23, 2020. It is now read-only.
SwiftMath is a Swift framework providing some useful math constructs and functions, like complex numbers, vectors, matrices, quaternions, and polynomials.
⚠️SwiftMath is work in progress, in alpha state. Master is currently targeting Swift 2.1.
Requirements
SwiftMath requires iOS 8.0+ / OS X 10.9+.
Installation
SwiftMath can be installed with the dependency manager Carthage.
Add the following line to your project's Cartfile
github "madbat/SwiftMath"
In the terminal, run carthage update
Link your project target(s) with the built frameworks. Application targets should also ensure that the framework gets copied into their application bundle.
Usage
Vector3
Vector3 — as the name suggests — represents a vector in the three-dimensional Euclidean space (aka R×R×R).
Some of the most common uses of 3D vectors consist in encoding physical quantities like position, velocity, acceleration, force, and many others.
Complex numbers extend real numbers in order to solve problems that cannot be solved with real numbers alone.
For example, the roots of a polynomial equation of degree > 1 can always be expressed with complex numbers, but not with real numbers.
// the default constructor for Complex takes the real and imaginary parts as parameters
letc1=Complex(1.0,3.0)
c1.re // 1.0
c1.im // 3.0
// a complex can also be constructed by using the property i defined on Float and Double
letc2=5+1.i // Complex(5.0, 1.0)
// complex conjugate
c2.conj() // Complex(5.0, -1.0)
// polar form
letc3=Complex(abs:2.0, arg:-4.0)letrealComplex=Complex(10.0,0.0)
realComplex.isReal // true
Quaternion
Quaternions extend complex numbers to 4 dimensions.
They're handy to rotate three-dimensional vectors.
// rotating a vector by π/2 around its x axis
letoriginal=Vector3(x:3, y:4, z:0)letrotation=Quaternion(axis:Vector3(x:1, y:0, z:0), angle:Double.PI/2.0)letrotated= original.rotate(rotation) // Vector3(x: 3, y: 0, z: 4.0)
Polynomial
Polynomial lets you represent – and find the roots of – a polynomial expression.
The following snippet shows how to express the polynomial x^2 + 4x + 8
letp=Polynomial(1,4,8)
Use Polynomial's roots() method to calculate its roots, represented as a (multi)set of complex numbers:
p.roots() // returns { (-2 - 2i), (-2 + 2i) }
For polynomials of degree <= 4, roots() defaults to using the analytic method, while for polynomials of higher degrees it uses the the Durand-Kerner method.
It is possible to force the root finding process to use the numeric method also for polynomials
of degree <= 4, using roots(preferClosedFormSolution: false).
Contributing
Contributions in any form (especially pull requests) are very welcome!
License
SwiftMath is released under the MIT License. See the LICENSE file for more info.
About
📐 A math framework for Swift. Includes: vectors, matrices, complex numbers, quaternions and polynomials.