Matrix math fundamental to machine learning implementations

matrixMultiplyWe are building our first Swift app for the iPhone / iPad with machine learning “at the edge” – i.e. on the device.

As anyone who has tried to implement even the most basic Machine Learning algorithms knows, you need a good library for reading in data to matrices, saving data, manipulating matrices and doing basic stuff such as transpose and multiplication, to even get off the starting block with your sanity intact.

When starting out we could not find  an easy (and free) to use Swift library to help us do Matrix manipulations, so we wrote our own. You can download the Swift file here:

SMatric Class - Swift Matrix manipulation library (195 downloads)


The library is implemented as a Swift Class, each instance of the class is a Matrix. Most methods are non-destructive, i.e. they return a new object rather than manipulating the old, while some are destructive for performance reasons. The class links to the Accelerate framework which means that heavy calculations make use of the Vector support of the CPU. This speeds stuff up nicely.  Because we wrote the SMatrix class with a view to running it on iPhones and iPads, we did not make use of Graphics Card libraries such as CUDA or OPEN-CL. If rewriting for the desktop, adding that support would make good sense.

Feel free to download and use the library in any way you see fit. We have made it available under the Lesser GNU General Public Licence, so you can include it in projects, modify it and distribute it.  If you want support or would like us to make additions or changes, we are happy to provide that under commercial conditions. Get in touch on:

Happy Coding!

BT, December 13, 2015