SwiftMath is a Swift framework providing some useful math constructs and functions

Related tags

Math SwiftMath
Overview

SwiftMath

Carthage compatible

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.

let v1 = Vector3(x: 1, y: 2, z: 3)
let v2 = Vector3(x: 5, y: 6, z: 7)

// vector sum
let v3 = v1 + v2 // Vector3(x: 6, y: 8, z: 10)

// length
v3.length // equals v3.norm

// zero vector
Vector3.zero() // Vector3(x: 0, y: 0, z: 0)

// unit-length vector
v3.unit() // divides v3 by its length

Vector2

Pretty much like Vector3, but for 2D vectors.

Complex

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
let c1 = 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
let c2 = 5 + 1.i // Complex(5.0, 1.0)

// complex conjugate
c2.conj() // Complex(5.0, -1.0)

// polar form
let c3 = Complex(abs: 2.0, arg: -4.0)

let realComplex = 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
let original = Vector3(x: 3, y: 4, z: 0)
let rotation = Quaternion(axis: Vector3(x: 1, y: 0, z: 0), angle: Double.PI/2.0)
let rotated = 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

let p = 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.

You might also like...
Arbitrary-precision arithmetic in pure Swift
Arbitrary-precision arithmetic in pure Swift

Overview API Documentation License Requirements and Integration Implementation Notes Full-width multiplication and division primitives Why is there no

Swift Matrix Library

Swift Matrix and Machine Learning Library Note: tensorflow/swift and apple/swift-numerics/issues/6 have or will have more complete support for NumPy-l

Swift Custom Operators for Mathematical Notation

Euler Euler uses custom operators in the "Math Symbols" character set to implement functions using traditional mathematical notation. Please keep in m

A cross-platform Swift library for evaluating mathematical expressions at runtime

Introduction What? Why? How? Usage Installation Integration Symbols Variables Operators Functions Arrays Performance Caching Optimization Standard Lib

Numeric facilities for Swift

NumericAnnex NumericAnnex supplements the numeric facilities provided in the Swift standard library. Features The exponentiation operator ** and the c

Swift Matrix Library

Swift Matrix and Machine Learning Library Note: tensorflow/swift and apple/swift-numerics/issues/6 have or will have more complete support for NumPy-l

MRFoundation - A library to complement the Swift Standard Library

MRFoundation MRFoundation is a library to complement the Swift Standard Library.

LemonadeDeclarativeUI framework contains some new functions for UIKit

LemonadeDeclarativeUI framework contains some new functions for UIKit. This library has been developing. If you want to contribute reach me!

Useful functions and extensions for sorting in Swift

SwiftSortUtils Motivation This library takes a shot at making comparing and sorting in Swift more pleasant. It also allows you to reuse your old NSSor

XCode and Swift game based on the generation of random cards and some functions related to the comparison of the results.
XCode and Swift game based on the generation of random cards and some functions related to the comparison of the results.

war-card-game-V1 XCode and Swift game based on the generation of random cards and some functions related to the comparison of the results. Once a card

A Swift package that adds some handy functions to String and NSMutableAttributedString

StringBooster StringBooster is a simple Swift package containing a few useful ex

Simple countdown UILabel with morphing animation, and some useful function.
Simple countdown UILabel with morphing animation, and some useful function.

CountdownLabel Simple countdown UILabel with morphing animation, and some useful function. features Simple creation Easily get status of countdown fro

STDevRxExt contains some extension functions for RxSwift and RxCocoa which makes our live easy.

STDevRxExt Example To run the Example.playground, clone the repo, and run pod install from the Example directory first. Requirements iOS 9.0+ tvOS 9.0

STDevRxExt contains some extension functions for RxSwift and RxCocoa which makes our live easy.

STDevRxExt Example To run the Example.playground, clone the repo, and run pod install from the Example directory first. Requirements iOS 9.0+ tvOS 9.0

Package that extends Combine with some useful APIs

CombineExpanded Package that extends Combine with some useful APIs. New API: then shareReplay Future.deferred then Wait for completion of self, then f

A simple menubar app can give you quick access to some macOS functions
A simple menubar app can give you quick access to some macOS functions

OneClick This simple menubar app can give you quick access to some macOS functio

Beautiful math equation rendering on iOS and MacOS
Beautiful math equation rendering on iOS and MacOS

iosMath iosMath is a library for displaying beautifully rendered math equations in iOS and MacOS applications. It typesets formulae written using the

Multi-dimensional Swift math

Upsurge Upsurge implements multi-dimensional data structures and operations. It brings numpy-like operations to Swift. Upsurge no longer supports DSP

Math expression parser built with Point•Free's swift-parsing package

swift-math-parser Basic math expression parser built with Point•Free's swift-parsing package. NOTE: currently, this uses a fork of that fixes a parsin

Comments
Releases(v0.1.1)
Owner
Matteo Battaglio
Software Engineer @BendingSpoons, co-founder @pragmamark, Swift enthusiast.
Matteo Battaglio
Multi-dimensional Swift math

Upsurge Upsurge implements multi-dimensional data structures and operations. It brings numpy-like operations to Swift. Upsurge no longer supports DSP

Alejandro Isaza 180 Dec 20, 2022
Math expression parser built with Point•Free's swift-parsing package

swift-math-parser Basic math expression parser built with Point•Free's swift-parsing package. NOTE: currently, this uses a fork of that fixes a parsin

Brad Howes 36 Dec 14, 2022
Oovium's math engine

OoviumEngine The function parser and math engine of the iOS / macOS app Oovium, including a command line version: oov. Swift Package Manager It is pos

Joe Charlier 0 Aug 24, 2022
VectorMath is a Swift library for Mac and iOS that implements common 2D and 3D vector and matrix functions

Purpose VectorMath is a Swift library for Mac and iOS that implements common 2D and 3D vector and matrix functions, useful for games or vector-based g

Nick Lockwood 341 Dec 31, 2022
Metron is a comprehensive collection of geometric functions and types that extend the 2D geometric primitives

Metron Geometry, simplified. Metron is a comprehensive collection of geometric functions and types that extend the 2D geometric primitives provided by

Toine Heuvelmans 1k Dec 5, 2022
A collection of functions for statistical calculation written in Swift.

σ (sigma) - statistics library written in Swift This library is a collection of functions that perform statistical calculations in Swift. It can be us

Evgenii Neumerzhitckii 658 Jan 5, 2023
OS X plotting framework

PlotKit Plots made easy. Features 2D line and scatter plots Multiple axes Custom tick marks Usage To start using PlotKit quickly use the plotPoints he

Alejandro Isaza 60 Sep 17, 2021
Overload +-*/ operator for Swift, make it easier to use (and not so strict)

Easy-Cal-Swift Overview This file is an overloading of +-*/ operator for Swift, to make it easier to use (and not so strict) It can make your life wit

Wei Wang 272 Jun 29, 2022
Numpy-like library in swift. (Multi-dimensional Array, ndarray, matrix and vector library)

Matft Matft is Numpy-like library in Swift. Function name and usage is similar to Numpy. Matft Feature & Usage Declaration MfArray MfType Subscription

null 80 Dec 21, 2022
A set of protocols for Arithmetic, Statistics and Logical operations

Arithmosophi - Arithmosoϕ Arithmosophi is a set of missing protocols that simplify arithmetic and statistics on generic objects or functions. As Equat

Eric Marchand 66 Jul 9, 2022