Swift Matrix Library

Overview

Swift Matrix and Machine Learning Library

Note: tensorflow/swift and apple/swift-numerics/issues/6 have or will have more complete support for NumPy-like ndarrays and autodiff. Fast AI has a good overview: https://www.fast.ai/2019/01/10/swift-numerics/

An alternate and much mature library is https://github.com/AlexanderTar/LASwift

Apple's Swift is a high level language that's asking for some numerical library to perform computation fast or at the very least easily. This is a bare-bones wrapper for that library.

A way to have iOS run high-level code similar to Python or Matlab is something I've been waiting for, and am incredibly excited to see the results. This will make porting complex signal processing algorithms to C much easier. Porting from Python/MATLAB to C was (and is) a pain in the butt, and this library aims to make the conversion between a Python/Matlab algorithm and a mobile app simple.

In most cases, this library calls Accelerate or OpenCV. If you want to speed up some function or add add another feature in those libraries, feel free to file an issue or submit a pull request (preferred!).

Currently, this library gives you

  • operators and various functions (sin, etc) that operate on entire arrays
  • helper function (reshape, reverse, delete, repeat, etc)
  • easy initializers for 1D and 2D arrays
  • complex math (dot product, matrix inversion, eigenvalues, etc)
  • machine learning algorithms (SVM, kNN, SVD/PCA, more to come)
  • one dimensional Fourier transforms
  • speed optimization using Accelerate and OpenCV

When I was crafting this library, I primarily followed the footsteps and example set by NumPy. For the more complex mathematical functions (e.g., SVD) I tested it against NumPy. Matlab, at least for the SVD, returns slightly different output.

Additionally, I followed NumPy's syntax whenever possible. For example, NumPy and Matlab differ in their initializer called ones by ones((M,N)) and ones(M, N) respectively. If in doubt or getting weird compiler bugs, look at NumPy for Matlab users or the section on possible swix bugs that may pop up during the Install or other Bugs you may find.

Documentation

Details on how to install can be found in Install. The swix documentation includes details on each individual function and possible bugs.

Third Party Frameworks/Libraries

To be integrated

Uses

FAQ

Why does this library exist?

Not only should you be able to do simple math in arrays like in Surge, Swift makes it possible to call high level mathematical functions just like in Python/Matlab.

How does this library compare to Python/Matlab?

Complete speed results can be found in Speed

Comments
  • Suggestion

    Suggestion

    This is a very cool project. I'm wondering if there is any interest in adding a shape attribute of ndarrays? I see there is some interest (for good reason) in being similar to numpy/matlab, and the program I'm writing makes its own shape variables for ndarrays. I'd make a pull request if project maintainers believe it would be a desirable and straightforward change.

    opened by seewalker 14
  • Enhance read csv function

    Enhance read csv function

    The function name is read_csv2. It takes two arguments: filename and whether it contains header (i.e. first row does not contain data, but variable names). I made several changes:

    1. As mentioned above, many times the csv files use first row as variable names, so by setting header=true we will ignore first row. The "header" just follows R's routine.
    2. Many times the csv files are created under Windows platform, so the line break is actually "\r\n" instead of "\n". I check if there is "\r" and delete all of them.
    3. The data in the csv file may not only be numeric. It might be categorical, e.g. "M" and "F", "Y" and "N". The original read_csv will ignore these categorical variables. I use different approach: I create a dictionary [String:Int], the key is the categorical value (e.g. "M") while the value is Int (e.g. 0). All the keys are stored in a set. A set means identical values will be considered as one element. Once we encounter a categorical value, first we check if it is in the set. If yes, we look up the dictionary and add its value to the data; Otherwise we create a key-value pair and insert the key into the set. So if the column is M,F,F,M, it will be 0,1,1,0 in the final data.
    opened by arondes 13
  • Detailed steps for installing swix on a new project

    Detailed steps for installing swix on a new project

    Please give a detailed and working steps for installing swix on a new project. I tried several times to follow the given steps. First I found that OpenCV must be added as Framework in order to compile the project. In this way the project compiles but the functions cannot be called in any swift file, how to make swix to work?

    Many thanks

    question 
    opened by sniis84 12
  • Framework

    Framework

    Hi Scott, I have done a lot of rework, and it's not complete yet. However, I like you to have a look at it. There are now two frameworks for pure OO and one with additional wrappers for functional access (not complete). There's also the test suite (adapted). Biggest changes:

    • two different frameworks (OO and F)
    • vector and matrix as classes are now capitalized.
    • init stuff is not in a functional manner but using class inits with different signatures
    • functional access is decoupled (and will then be available through the F-framework)
    • quite some minor changes
    • using internally just the OO-methods

    So, why? Swift it not a functional language and should not be programmed that way. You are a mathematician like my wife and I know that you have quite a functional thinking (as can well be seen from your programming style). E.g instead of

    arange(n)
    

    it's now

    Vector(arange:n)
    

    Looks like more typing, but for outer access there will be the F-wrapper which allows still for the first form. So for people who are somehow used to that, there will be no difference.

    There are still some issues with scoping I need to fix (as already said: Matrix allows to modify the flat vector without any check which is no good). Since the compiler allows to forbid those invalid accesses it shall be implemented. Quite some more bits and pieces I currently can't recall.

    opened by thomaskilian 9
  • Performance no acceptable

    Performance no acceptable

    Running this little Python snippet on my machine takes 3.5 seconds:

    import numpy as np
    import time
    
    t = time.time()
    for i in range(1000):
        a = np.arange(100000)
        a += 1
        a = a * a
        a = a / a
        b = np.ones(100000)
        if not np.allclose(a, b): print "?"
    print time.time() - t
    

    The corresponding Swix-variant takes 33.7 seconds (factor 10 slower than NumPy).

    opened by thomaskilian 4
  • issue about: linker command failed with exit code 1

    issue about: linker command failed with exit code 1

    Everything works fine on the Simulation, as soon as I plug any device I cannot install the App. The following error appars.

    ld: '/mypath/myproject/swix/objc/opencv2.framework/opencv2(knearest.o)' does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. for architecture armv7
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    

    It seems a clang problem any idea how to solve it?

    question 
    opened by sniis84 4
  • Plotting

    Plotting

    Opinions on including some plotting functionality? It would probably not be a random window popping up like in MATLAB, but maybe it could output to a PNG, or a UIView / NSView, which could then be embedded directly in the given app's view hierarchy?

    opened by donald-pinckney 4
  • Add Swix to the Swift Source Compatibility test suite

    Add Swix to the Swift Source Compatibility test suite

    Here it is.

    These tests should be good enough. I think.

    Edit

    Ah, I see. You're working now on the package-manager branch. Take your time... :) Maybe I'll drop a PR to help you implementing/porting/testing stuff ^^

    opened by felix91gr 3
  • Migration to newer constructors

    Migration to newer constructors

    With Swift still changing, factor methods are being replaced by constructors. Not sure what timeline is best for this, but at some point code such as:

    var x = String.stringWithContentsOfFile(prefix+"../"+filename, encoding: NSUTF8StringEncoding, error: nil)
    

    needs to be changed to:

    var x = String(contentsOfFile: prefix+"../"+filename, encoding: NSUTF8StringEncoding, error: nil)
    

    to continue building (thats necessary as of Xcode 6.1 beta 2)

    opened by donald-pinckney 3
  • Ignore user .DS_Stores

    Ignore user .DS_Stores

    First removed .DS_Store from Git tracking. Then added .DS_Store to .gitignore.

    The file has nothing to do with the code that is being tracked in git. It contains custom attributes set on the containing folder in the Finder. Unless those custom attributes are relevant to the code, that file is just noise. Additionally, if the file is not ignored and gets checked in unintentionally and later two devs each decide to put say, pretty custom icons on the folder (for whatever reason), then you could end up with a merge conflict on a binary file.

    opened by Jxrgxn 2
  • Regression

    Regression

    linear and quadratic (polynomial) regression added. To illustrate regression, I implemented a RegressionView - subclass of UIView - allowing us to draw hypothetic data (x, y). Also the degree of the hypothesis can be adjusted with a UIStepper above.

    opened by erkekin 2
  • Unresolved merge conflicts

    Unresolved merge conflicts

    In the swift speed benchmark, introduced in commit e0cd9df

    https://github.com/stsievert/swix/blob/522cb65f076a96ca89a591bc96510d98f7d24006/swix/speed/speed.swift#L16

    https://github.com/stsievert/swix/blob/522cb65f076a96ca89a591bc96510d98f7d24006/swix/speed/speed.swift#L19

    https://github.com/stsievert/swix/blob/522cb65f076a96ca89a591bc96510d98f7d24006/swix/speed/speed.swift#L22

    https://github.com/stsievert/swix/blob/522cb65f076a96ca89a591bc96510d98f7d24006/swix/speed/speed.swift#L41

    https://github.com/stsievert/swix/blob/522cb65f076a96ca89a591bc96510d98f7d24006/swix/speed/speed.swift#L46

    https://github.com/stsievert/swix/blob/522cb65f076a96ca89a591bc96510d98f7d24006/swix/speed/speed.swift#L52

    opened by DarthPumpkin 1
  • Ambiguous use of operator '/'

    Ambiguous use of operator '/'

    Hi!

    I am trying to install swix in this project https://github.com/hollance/Forge/tree/master/Examples/YOLO. After following the swix manual install instructions of adding the swix folder to the project and adding swix-master/swix/swix/swix to the objective c bridging header and running, I get this error Ambiguous use of operator '/', pointing to these line of code as canidates. func / (lhs: Int, rhs: Int) -> Double{ return lhs.double / rhs.double} func / (lhs: Double, rhs: Int) -> Double{ return lhs / rhs.double}

    I also get an error Use of unresolved identifier 'Process', pointing to this line of code

    Process.launchedProcess(launchPath: "cd", arguments: [S2_PREFIX+";", PYTHON_PATH, "imshow.py", filename, "(save)", "(show)"]) print("savefig: Removing CSV FILE " + S2_PREFIX + "temp.csv") Process.launchedProcess(launchPath: "rm", arguments: [S2_PREFIX+"temp.csv"])

    This is on xcode version 8.3.3. What is the cause of these issues? Any advice on fixing?

    Thank you so much! Jordan

    opened by jordanhart 0
  • Wrong FFT implementation

    Wrong FFT implementation

    the fft and ifft implementations in complex-math.swift are wrong:

    • the number of elements inside the input (real) array must be a power of 2 for the FFT to give correct results
    • as stated at https://developer.apple.com/library/ios/documentation/Performance/Conceptual/vDSP_Programming_Guide/UsingFourierTransforms/UsingFourierTransforms.html the output of Accelerate fft functions must be re-scaled: you're already dividing by 2 the output of forward real transforms, while you should divide by n (not 2048) the output of inverse real transforms.
    opened by ezamagni 1
Releases(v0.31)
  • v0.31(Jun 2, 2016)

    Renames "ndarray" to "vector". If you want to update to this version, renaming all "ndarray" instances to "vector" should work (and file an issue if it doesn't).

    Source code(tar.gz)
    Source code(zip)
  • v0.30(Sep 21, 2015)

    This release runs and compiles under the release version of Xcode 7, downloaded from the App Store.

    Further modifications were made after integrating swix into another project. This release cleans up the code and removes or adds certain functions/operators. Specifically, it

    • gets rid of the *! operator for scoping issues. Instead of A *! x, use A.dot(x).
    • removes function find. The argwhere function is more clear what find/argwhere does.
    • allows changing the seed for rand, etc
    • removes swixUseCases and implements swix_ios_app
    • updates image functions for Swift 2
    • adds matrix.dot(ndarray).
    Source code(tar.gz)
    Source code(zip)
  • v0.21(Jul 24, 2015)

  • 0.2(Oct 6, 2014)

    Significant work has been done to further emulate NumPy. Added functions for stuff I think everyone would find useful, not just me. This library is not under heavy development anymore.

    Source code(tar.gz)
    Source code(zip)
  • v0.1(Jul 22, 2014)

    A much cleaner and faster library. It calls the Accelerate library in most cases (all operators, some simple functions and all complex functions) and is nicely structured (matrix depends on ndarray depends on [Double]).

    In addition, I have implemented a complex signal processing algorithm from Matlab to iOS with this framework. The conversion was almost line for line. For example, here's a real-world example of my Matlab-iOS conversion:

    swix

    var i = abs(S) < mu/2
    S[argwhere(i)] = zeros(i.n)
    i = argwhere(1 - i) // equivalent to notting every element
    S[i] = S[i] - sign(S[i]) * mu/2;
    var S0 = zeros((sm, sn))
    S0["diag"] = S
    var L_new = U *! (S0 *! V)
    

    matlab

    i = abs(S) > mu/2;
    S(~i) = 0; 
    S(i) = S(i) - sign(S(i))*mu/2; 
    S = diag(S);
    S0(1:min_snm, 1:min_snm) = S;
    L_new = U * S0 * V';
    
    Source code(tar.gz)
    Source code(zip)
  • v0.0.1beta(Jul 9, 2014)

    A very preliminary release that details how to integrate OpenCV/Accelerate and includes ScalarArithmetic and swift-complex. This release relies on some very rough behaviors, such as calling a matrix2d an Array of Arrays. I intend to rewrap matrix and matrix2d into structs and integrate the rest of the code to reflect that.

    Since this release is beta, the API may change.

    Source code(tar.gz)
    Source code(zip)
Owner
Scott Sievert
Graduate student at UW–Madison studying machine learning, systems and optimization.
Scott Sievert
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
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

Scott Sievert 591 Sep 5, 2022
MRFoundation - A library to complement the Swift Standard Library

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

Roman Mogutnov 2 Feb 12, 2022
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

Nick Lockwood 738 Jan 7, 2023
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

null 707 Dec 19, 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
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
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
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

Mattt 1.1k Jan 4, 2023
SwiftMath is a Swift framework providing some useful math constructs and functions

SwiftMath is a Swift framework providing some useful math constructs and functions, like complex numbers, vectors, matrices, quaternions, and polynomials.

Matteo Battaglio 175 Dec 2, 2022
Numeric facilities for Swift

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

Xiaodi Wu 69 Nov 3, 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
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
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

Scott Sievert 591 Sep 5, 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
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

Scott Sievert 591 Sep 5, 2022
Seaglass is a truly native macOS client for Matrix. It is written in Swift and uses the Cocoa user interface framework.

Seaglass is a truly native macOS client for Matrix. It is written in Swift and uses the Cocoa user interface framework.

null 1 Jan 17, 2022
A glossy Matrix collaboration client for iOS

Element iOS Element iOS is an iOS Matrix client provided by Element. It is based on MatrixKit and MatrixSDK. Beta testing You can try last beta build

Element (formerly New Vector) 1.5k Jan 1, 2023
A glossy Matrix collaboration client for iOS

Element iOS Element iOS is an iOS Matrix client provided by Element. It is based on MatrixKit and MatrixSDK. Beta testing You can try last beta build

Element 1.5k Jan 1, 2023
The Matrix SDK for iOS

Matrix iOS SDK This open-source library allows you to build iOS apps compatible with Matrix (http://www.matrix.org), an open standard for interoperabl

matrix.org 390 Dec 20, 2022