/SwiftAlgebra

A small standalone package for linear algebra.

Primary LanguageSwiftMIT LicenseMIT

SwiftAlgebra

A small standalone package for linear algebra and Lie groups.

Features

Currently supported features:

  • Vectors are treated as one-dimensional matrices
  • Advanced slicing
  • Arithmetic operations on matrices
  • Linear algebra (inverse and solve using LU decomposition)
  • Lie groups and operations on them

Installation

https://developer.apple.com/documentation/swift_packages/adding_package_dependencies_to_your_app

Requirements

SwiftAlgebra includes only pure Swift code making this package cross-platform. The latest version of Swift is recommended, while Swift 5.5 is the minimum requirement.

Usage

The following code snippets should help you getting the gist of it.

Construct an identity matrix

var I = Matrix(identity: 4)

or a general matrix from a double array

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

We can then do arithmetic operations

I[1...3,0...2] += 0.5 * (A + A.T)

or even matrix multiplication

var B = I[0...1,1...3]A[.all, 1...2]

We can also invert the matrix or solve a system of equations

B[.all, .all] = I[0...1,0...1]
print(try invert(B))
print(try solve(A: B, b: I[0...1,0]))

We can also extract submatrices while keeping the reference alive

var C = I[0...2,0]
print(I)
C *= 2
print(I)

Extra Tips & Tricks

I know, symbols '∙' and '≈' are a bit tedious to type, but! Many editors including Xcode allow snippets which can make the coding very simple while the beautiful syntax remains. Here is a link on how to do it in Xcode.

All code is documented using DocC. The code to the documentation is also available so that if you make any changes, you can simply adopt the docs as well. Since all code is open-source, the documentation on this repository is not yet compiled.

Running examples

Swift Package Manager allows you to include this package in executables or in other packages like this one. Assuming you have Swift and Swift Package Manager installed, follow these steps to create an example command line app:

  1. In Terminal, go to your working directory and create a new folder.
mkdir SomeCalculations
cd SomeCalculations
  1. Create a new package there.
swift package init --type executable --name SomeCalculations
  1. Add dependency on SwiftAlgebra by editing SomeCalculations/Package.swift.
// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "SomeCalculations",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(url: "https://github.com/tomecj/SwiftAlgebra", from: "4.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .executableTarget(
            name: "SomeCalculations",
            dependencies: ["SwiftAlgebra"]),
        .testTarget(
            name: "SomeCalculationsTests",
            dependencies: ["SomeCalculations"]),
    ]
)
  1. Edit source code in SomeCalculations/Sources/SomeCalculations/SomeCalculations.swift
import SwiftAlgebra

@main
public struct SomeCalculations {
    public private(set) var text = "Hello, World!"

    public static func main() {
        print(SomeCalculations().text)
        let A = Matrix(identity: 3)
        let B = Matrix(from: [[1, 2, 3]]).T
        print (A  B)
    }
}

Contribution

Contact me personally.

License

drawing

Resources