Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dastrobu/acceleratearray

Swift Array Extensions for the Apple Accelerate Framework
https://github.com/dastrobu/acceleratearray

accelerate-framework blas lapack linear-algebra swift

Last synced: 2 months ago
JSON representation

Swift Array Extensions for the Apple Accelerate Framework

Awesome Lists containing this project

README

        

# AccelerateArray

[![Swift Version](https://img.shields.io/badge/swift-5.9-blue.svg)](https://swift.org)
![Platform](https://img.shields.io/badge/platform-macOS-lightgray.svg)
![Build](https://github.com/dastrobu/AccelerateArray/actions/workflows/ci.yaml/badge.svg)

Swift Array Extensions for the Apple Accelerate Framework.

The goal of this package is to provide slightly easier access to the [BLAS](http://www.netlib.org/blas/),
[LAPACK](http://www.netlib.org/lapack/) and [vDSP](https://developer.apple.com/documentation/accelerate/vdsp) functions
of the [Accelerate](https://developer.apple.com/documentation/accelerate) framework,
to apply these functions to Float and Double swift arrays.

Out of scope of this package are more convenient wrappers to handle arrays as matrices, which
would include storing strides, shapes and order (row/column major). This would require to add
additional types, which can be easily built on top of this package.

## Table of Contents

- [Installation](#installation)
- [Swift Package Manager](#swift-package-manager)
- [Cocoa Pods](#cocoa-pods)
- [Dependencies](#dependencies)
- [Examples](#examples)
- [BLAS (ATLAS)](#blas-atlas)
- [Scale Vector (sscal, dscal)](#scale-vector-sscal-dscal)
- [Set Vector to Constant (sset, dset)](#set-vector-to-constant-sset-dset)
- [LAPACK](#lapack)
- [LU Factorization](#lu-factorization)
- [Inverse](#inverse)
- [Docs](#docs)


## Installation

### Swift Package Manager

```swift
let package = Package(
dependencies: [
.package(url: "https://github.com/dastrobu/AccelerateArray.git", from: "0.5.0"),
]
)
```

### Cocoa Pods

Make sure a valid deployment target is setup in the Podfile and add

pod 'AccelerateArray', '~> 0'

### Dependencies

There are no dependencies on macOS apart from the Accelerate framework, which is installed by default.
Since Accelerate is also included in iOS and other Apple Platforms, this package should run on all Apple plattforms.

## Examples

### BLAS (ATLAS)

#### Scale Vector (sscal, dscal)

Vector can be scaled by
```swift
a = [1, 2, 3]
a.scal(2) // [2, 4, 6]
```
or by providing all parameters for example with a stride of two
```swift
a = [1, 2, 3]
a.scal(n: Int32(a.count), alpha: 2, incX: 2) // [2, 2, 6]
```

#### Set Vector to Constant (sset, dset)

Set all elements of a vector to a single constant.
```swift
a = [1, 2, 3]
a.set(9) // [9, 9, 9]
```
or by providing all parameters for example with a stride of two
```swift
a = [1, 2, 3]
a.set(n: Int32(a.count), alpha: 2, incX: 2) // [9, 2, 9]
```

### LAPACK

#### LU Factorization

Compute LU factorization.

```swift
// A in row major
let A: [Float] = [
1.0, 2.0,
3.0, 4.0,
5.0, 6.0,
7.0, 8.0
]
// convert A to col major (for fortran)
var At = A.mtrans(m: 2, n: 4)
// compute factorization

let ipiv = try At.getrf(m: 4, n: 2)

// convert solution to row major
let X = At.mtrans(m: 4, n: 2)

// L in row major (pic lower triangular matrix)
let L: [Float] = [
1.0, 0.0,
X[2], 1.0,
X[4], X[5],
X[6], X[7],
]

// U in row major (pic upper triangular matrix)
let U: [Float] = [
X[0], X[1],
0.0, X[3],
]

// note, the indices in ipiv are one base (fortran)
// construct the permutation vector
// see: https://math.stackexchange.com/a/3112224/91477
var p = [0, 1, 2, 3]
for i in 0..