N-dimensional array library for Swift with NumPy-inspired API and Accelerate optimization.
ArraySwift provides a powerful NDArray type for numerical computing in Swift. Designed with a familiar NumPy-style API, it leverages Apple's Accelerate framework for high-performance SIMD operations.
- NumPy-inspired API - Function names and behaviors mirror NumPy for a familiar experience
- vDSP acceleration - Leverages Apple's Accelerate framework for optimized vector operations
- Complex number support - First-class
complex128dtype with split real/imaginary storage for optimal vDSP vectorization - Comprehensive operations - Array creation, manipulation, math functions, arithmetic, reductions, and linear algebra
- Pure Swift - No external dependencies except Darwin/Accelerate
Add ArraySwift to your Swift package:
dependencies: [
.package(url: "https://github.com/ChrisGVE/ArraySwift.git", from: "0.1.0")
]Then add it as a dependency to your target:
.target(
name: "YourTarget",
dependencies: ["ArraySwift"]
)import ArraySwift
// Create arrays
let a = NDArray.arange(start: 0, stop: 6).reshape([2, 3])
let b = NDArray.ones([2, 3])
// Element-wise operations
let c = a + b
let d = a * 2.0
// Reductions
let total = a.sum() // Sum all elements
let rowMeans = a.mean(axis: 1) // Mean along rows
// Linear algebra
let matrix = NDArray([[1.0, 2.0], [3.0, 4.0]])
let vector = NDArray([1.0, 0.0])
let result = matrix.dot(vector)
// Complex numbers
let complex = NDArray.complexArray(
shape: [2],
real: [1.0, 2.0],
imag: [3.0, 4.0]
)
let magnitude = complex.abs()| Module | Purpose |
|---|---|
NDArray |
Core N-dimensional array type with shape, dtype, and data storage |
| Creation | zeros, ones, full, eye, arange, linspace, random, randn |
| Manipulation | reshape, transpose, flatten, concatenate, stack, split, flip, roll |
| Math | sin, cos, tan, exp, log, sqrt, abs, power, floor, ceil, round |
| Arithmetic | Element-wise +, -, *, /, broadcasting, compound assignment |
| Reductions | sum, mean, min, max, std, variance, prod, argmin, argmax |
| Linear Algebra | dot, matmul, cross, inner, outer |
| Comparison | equal, less, greater, where, logical operations |
ArraySwift supports two data types:
| Type | Description |
|---|---|
float64 |
64-bit floating point (default) |
complex128 |
Complex numbers with 64-bit real and imaginary parts |
Complex arrays use split storage (separate real/imag arrays) for optimal vDSP vectorization performance.
// From Swift arrays
let arr1D = NDArray([1.0, 2.0, 3.0])
let arr2D = NDArray([[1.0, 2.0], [3.0, 4.0]])
// Factory functions
let zeros = NDArray.zeros([3, 4])
let ones = NDArray.ones([2, 2])
let eye = NDArray.eye(3)
let range = NDArray.arange(start: 0, stop: 10, step: 2)
let linear = NDArray.linspace(start: 0, stop: 1, num: 5)let arr = NDArray.arange(start: 0, stop: 12)
let matrix = arr.reshape([3, 4])
let transposed = matrix.T
let flat = matrix.flatten()let arr = NDArray([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0]])
arr.sum() // 21.0
arr.sum(axis: 0) // [5, 7, 9]
arr.sum(axis: 1) // [6, 15]
arr.mean() // 3.5
arr.std() // Standard deviation// Dot product
let a = NDArray([1.0, 2.0, 3.0])
let b = NDArray([4.0, 5.0, 6.0])
let dot = a.dot(b) // 32.0
// Matrix multiplication
let A = NDArray([[1.0, 2.0], [3.0, 4.0]])
let B = NDArray([[5.0, 6.0], [7.0, 8.0]])
let C = A.matmul(B)
// Cross product
let x = NDArray([1.0, 0.0, 0.0])
let y = NDArray([0.0, 1.0, 0.0])
let z = x.cross(y) // [0, 0, 1]let complex = NDArray.complexArray(
shape: [3],
real: [1.0, 2.0, 3.0],
imag: [4.0, 5.0, 6.0]
)
complex.dtype // .complex128
complex.conjugate() // Complex conjugate
complex.abs() // Magnitude
complex.angle() // Phase angle- Swift 5.9+
- iOS 15+ / macOS 12+
- Xcode 15+
ArraySwift is part of a suite of Swift scientific computing libraries:
- NumericSwift - Scientific computing (SciPy-inspired)
- PlotSwift - Data visualization
ArraySwift is available under the MIT License. See the LICENSE file for details.