Swivl - A set of BLAS-accelerated linerar algebra structures and functions

Overview

Swivl - Swift Vector Library

A set of BLAS-accelerated linerar algebra structures and functions. Includes dense and sparse matrices and vectors, and various solvers. Features:

  • BLAS accelerated operations
  • Sparse and Dense support
  • Cholesky, LU, Eigen- decompositions and more
  • Matlab-style solve function chooses an appropriate solver

Examples

Basic Operations

import Foundation
import Swivl

let v = Vector<Double>([1,2,3,4])
print(Vector.add(v, v))
print(v + v)
print(v + 2)

let v2 = VectorXf([5,6,7,8])
print(v2 + v2)
>>> Vector<Double> 4x1 [2.0, 4.0, 6.0, 8.0]
>>> Vector<Double> 4x1 [2.0, 4.0, 6.0, 8.0]
>>> Vector<Double> 4x1 [3.0, 4.0, 5.0, 6.0]
>>> Vector<Float> 4x1 [10.0, 12.0, 14.0, 16.0]

Matrix Mutliplication

let A = MatrixXd(rows: [
  [1,2,3],
  [4,5,6],
  [7,8,9]
])

let B = MatrixXd(rows: [
  [0,1,0],
  [1,0,0],
  [0,0,1]
])

let x = VectorXd([10,20,30])

print(A*B)
print(A.*B)
print(A*x)
>>> Matrix<Double> 3x3
	[  2,  1,  3 ]
	[  5,  4,  6 ]
	[  8,  7,  9 ]
>>> Matrix<Double> 3x3
	[  0,  2,  0 ]
	[  4,  0,  0 ]
	[  0,  0,  9 ]
>>> Vector<Double> 3x1 [140.0, 320.0, 500.0]

Dense Matrix Creation

let A = MatrixXd(rows: [
  [10, -7, 0],
  [-3,  2, 6],
  [ 5, -1, 5]
])

let B = MatrixXd(flat: [
1.0000,    0.5000,    0.3333,    0.2500,
0.5000,    1.0000,    0.6667,    0.5000,
0.3333,    0.6667,    1.0000,    0.7500,
0.2500,    0.5000,    0.7500,    1.0000
], shape: (4,4))

Matrix Properties

let M = MatrixXd(rows: [
  [1,2,3,4],
  [5,6,7,8],
  [9,0,1,2],
  [3,4,5,6]
])

print(M.det)
print(M.trace)
print(M.T)
print(M.diag())
>>> 1.5987211554602252e-13
>>> 14.0
>>> Matrix<Double> 4x4
	[  1,  5,  9,  3 ]
	[  2,  6,  0,  4 ]
	[  3,  7,  1,  5 ]
	[  4,  8,  2,  6 ]
>>> Vector<Double> 4x1 [1.0, 6.0, 1.0, 6.0]

Slicing

let M = MatrixXd(rows: [
  [1,2,3,4,5],
  [6,7,8,9,0]
])

print(M[0, ...])
print(M[..., [0,2,3]])
print(M[..., 3...])
print(M[...0, 2...4])
>>> Vector<Double> 5x1 [1.0, 2.0, 3.0, 4.0, 5.0]
>>> Matrix<Double> 2x3
	[  1,  3,  4 ]
	[  6,  8,  9 ]
>>> Matrix<Double> 2x2
	[  4,  5 ]
	[  9,  0 ]
>>> Matrix<Double> 1x3
	[  3,  4,  5 ]

Decomposition

var PSD = MatrixXd([
  [1, 0, 1],
  [0, 2, 0],
  [1, 0, 3]
])
print(PSD.isDefinite())
print(try? PSD.chol())

var nonPSD = MatrixXd([
  [1, 1,  1,  1,  1],
  [1, 2,  3,  4,  5],
  [1, 3,  6, 10, 15],
  [1, 4, 10, 20, 35],
  [1, 5, 15, 35, 69]])
print(nonPSD.isDefinite())
print(try? nonPSD.chol())
>>> Optional(Matrix<Double> 3x3
	[  1.000,  0.000,  1.000 ]
	[  0.000,  1.414,  0.000 ]
	[  0.000,  0.000,  1.414 ])
>>> true

>>> nil
>>> false

Sparse Matrix Creation

let rows: [Int] = [0, 0, 1, 1, 2, 2, 2]
let cols: [Int] = [1, 3, 1, 4, 0, 1, 3]
let vals: [Double] = [1, 2, 5, 7, 8, 9, 4]
let A = SparseMatrix(rows, cols, vals)

let M = MatrixXd([
  [0, 1, 0, 2, 0],
  [0, 5, 0, 0, 7],
  [8, 9, 0, 4, 0]
])
let B = SparseMatrix(M)

let C = SparseMatrix<Double>(.eye(4))

let D = SparseMatrix<Double>.eye(4)
You might also like...
Differific - a fast and convenient diffing framework.
Differific - a fast and convenient diffing framework.

Differific Description Differific is a diffing tool that helps you compare Hashable objects using the Paul Heckel's diffing algorithm. Creating a chan

💻 A fast and flexible O(n) difference algorithm framework for Swift collection.
💻 A fast and flexible O(n) difference algorithm framework for Swift collection.

A fast and flexible O(n) difference algorithm framework for Swift collection. The algorithm is optimized based on the Paul Heckel's algorithm. Made wi

Extension of Diffable API which allow not duplicate code and use less models. Included example for SideBar.
Extension of Diffable API which allow not duplicate code and use less models. Included example for SideBar.

SPDiffable Apple's diffable API requerid models for each object type. If you want use it in many place, you pass many time to implemenet and get over

Accelerated tensor operations and dynamic neural networks based on reverse mode automatic differentiation for every device that can run Swift - from watchOS to Linux
Accelerated tensor operations and dynamic neural networks based on reverse mode automatic differentiation for every device that can run Swift - from watchOS to Linux

DL4S provides a high-level API for many accelerated operations common in neural networks and deep learning. It furthermore has automatic differentiati

GPUImage 2 is a BSD-licensed Swift framework for GPU-accelerated video and image processing.
GPUImage 2 is a BSD-licensed Swift framework for GPU-accelerated video and image processing.

GPUImage 2 Brad Larson http://www.sunsetlakesoftware.com @bradlarson [email protected] Overview GPUImage 2 is the second generation of th

GPUImage 3 is a BSD-licensed Swift framework for GPU-accelerated video and image processing using Metal.
GPUImage 3 is a BSD-licensed Swift framework for GPU-accelerated video and image processing using Metal.

GPUImage 3 Janie Clayton http://redqueengraphics.com @RedQueenCoder Brad Larson http://www.sunsetlakesoftware.com @bradlarson contact@sunsetlakesoftwa

A GPU accelerated image and video processing framework built on Metal.
A GPU accelerated image and video processing framework built on Metal.

MetalPetal An image processing framework based on Metal. Design Overview Goals Core Components MTIContext MTIImage MTIFilter MTIKernel Optimizations C

DL4S provides a high-level API for many accelerated operations common in neural networks and deep learning.
DL4S provides a high-level API for many accelerated operations common in neural networks and deep learning.

DL4S provides a high-level API for many accelerated operations common in neural networks and deep learning. It furthermore has automatic differentiati

A set of helper classes and functions in Swift

SwiftTools This is a set of tools written in Swift that add some sugar and some small functionality. Mainly meant for small projects and scripts, as a

Swift-friendly API for a set of powerful Objective C runtime functions.
Swift-friendly API for a set of powerful Objective C runtime functions.

ObjectiveKit ObjectiveKit provides a Swift friendly API for a set of powerful Objective C runtime functions. Usage To use ObjectiveKit: Import Objecti

A custom UIControl which functions like UISlider where you can set multiple intervals with different step values for each interval.
A custom UIControl which functions like UISlider where you can set multiple intervals with different step values for each interval.

MultiStepSlider A custom UIControl which functions like UISlider where you can set multiple intervals with different step values for each interval. Th

A beautiful set of predefined colors and a set of color methods to make your iOS/OSX development life easier.
A beautiful set of predefined colors and a set of color methods to make your iOS/OSX development life easier.

Installation Drag the included Colours.h and Colours.m files into your project. They are located in the top-level directory. You can see a demo of how

Artificial intelligence/machine learning data structures and Swift algorithms for future iOS development. bayes theorem, neural networks, and more AI.

Swift Brain The first neural network / machine learning library written in Swift. This is a project for AI algorithms in Swift for iOS and OS X develo

Classes-and-structures-in-swift - This source files show what is the difference between class and structure

This source files show what is the difference between class and structure You ca

Algorithms and data structures in Swift, with explanations!
Algorithms and data structures in Swift, with explanations!

Welcome to the Swift Algorithm Club! Here you'll find implementations of popular algorithms and data structures in everyone's favorite new language Sw

Argo is a library that lets you extract models from JSON or similar structures in a way that's concise, type-safe, and easy to extend
Argo is a library that lets you extract models from JSON or similar structures in a way that's concise, type-safe, and easy to extend

Argo is a library that lets you extract models from JSON or similar structures in a way that's concise, type-safe, and easy to extend. Using Argo

List tree data souce to display hierachical data structures in lists-like way. It's UI agnostic, just like view-model and doesn't depend on UI framework
List tree data souce to display hierachical data structures in lists-like way. It's UI agnostic, just like view-model and doesn't depend on UI framework

SwiftListTreeDataSource List tree data souce to display hierachical data structures in lists-like way. It's UI agnostic, just like view-model, so can

A collection of tools for debugging, diffing, and testing your application's data structures.

Custom Dump A collection of tools for debugging, diffing, and testing your application's data structures. Motivation customDump diff XCTAssertNoDiffer

Examples of commonly used data structures and algorithms in Swift.

Swift Structures This project provides a framework for commonly used data structures and algorithms written in a new iOS development language called S

Owner
PhD Student at DGP, University of Toronto
null
Algorithms and data structures in Swift, with explanations!

Welcome to the Swift Algorithm Club! Here you'll find implementations of popular algorithms and data structures in everyone's favorite new language Sw

raywenderlich 27.3k Jan 8, 2023
Commonly used data structures for Swift

Swift Collections is an open-source package of data structure implementations for the Swift programming language.

Apple 2.7k Jan 5, 2023
EKAlgorithms contains some well known CS algorithms & data structures.

EKAlgorithms EKAlgorithms is a set of computer exercises implemented in Objective-C. Data structures, well known algorithms, CS curiosities, you name

Evgeny Karkan 2.4k Jan 4, 2023
Simple implementations of various dynamic data structures in Swift.

SwiftDataStructures Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Installatio

Hector Delgado 0 Oct 21, 2021
KeyPathKit is a library that provides the standard functions to manipulate data along with a call-syntax that relies on typed keypaths to make the call sites as short and clean as possible.

KeyPathKit Context Swift 4 has introduced a new type called KeyPath, with allows to access the properties of an object with a very nice syntax. For in

Vincent Pradeilles 406 Dec 25, 2022
RandMyMod base on your own struct or class create one or a set of instance, which the variable's value in the instance is automatic randomized.

RandMyMod is an IOS Native Framework helps you generate one or a set of variable base on your own model. No matter your model is Class / Struct. Insta

郭介騵 17 Sep 24, 2022
:droplet: A generic view model for both basic and complex scenarios

Brick Description Brick is a generic view model for both basic and complex scenarios. Mapping a basic table view cells is as easy as pie, if you have

HyperRedink 59 Jul 31, 2021
Swift μ-framework for efficient array diffs and datasource adapters.

Buffer Swift μ-framework for efficient array diffs, collection observation and data source implementation. C++11 port here Installation cd {PROJECT_RO

Alex Usbergo 348 Aug 2, 2022
Swift library to generate differences and patches between collections.

Differ Differ generates the differences between Collection instances (this includes Strings!). It uses a fast algorithm (O((N+M)*D)) to do this. Featu

Tony Arnold 628 Dec 29, 2022
A Swift probability and statistics library

Probably Probably is a set of Swift structures for computing the probability and cumulative distributions of different probablistic functions. Right n

Harlan Haskins 270 Dec 2, 2022