ChrisGVE/NumericSwift
Scientific computing library for Swift - distributions, integration, optimization, interpolation
Overview
NumericSwift brings the power of scientific computing to Swift with an API inspired by SciPy and NumPy. It provides probability distributions, numerical integration, optimization algorithms, interpolation methods, special functions, linear algebra, clustering, and more.
Design Philosophy:
- Complex numbers as first-class citizens - Use
DoubleorComplexinterchangeably where mathematically appropriate - SciPy-inspired API - Familiar function names and parameter orders for Python developers
- Pure Swift with vDSP optimizations - Leverages Apple's Accelerate framework for performance
- Production quality - Robust algorithms with proper edge case handling
Installation
Add NumericSwift to your Swift package:
dependencies: [
.package(url: "https://github.com/ChrisGVE/NumericSwift.git", from: "0.1.0")
]Then add it to your target dependencies:
.target(
name: "YourTarget",
dependencies: ["NumericSwift"]
)Quick Start
Complex Numbers
import NumericSwift
let z1 = Complex(re: 3, im: 4)
let z2 = Complex.polar(r: 2, theta: .pi/4)
print(z1.abs) // 5.0 (magnitude)
print(z1.arg) // 0.927... (phase angle)
print(z1 * z2) // Complex multiplication
print(z1.exp) // e^z
print(z1.sqrt) // Principal square rootStatistical Functions
let data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
print(mean(data)) // 5.5
print(median(data)) // 5.5
print(stddev(data)) // 3.028...
print(percentile(data, 75)) // 7.75
// Probability distributions
let norm = NormalDistribution(loc: 0, scale: 1)
print(norm.pdf(0)) // 0.3989... (probability density)
print(norm.cdf(1.96)) // 0.975 (cumulative distribution)
print(norm.ppf(0.975)) // 1.96 (inverse CDF)
print(norm.rvs(5)) // 5 random samplesNumerical Integration
// Adaptive quadrature
let (result, error) = quad({ x in sin(x) }, 0, .pi)
print(result) // 2.0
// Solve ODE: dy/dt = -y, y(0) = 1
let solution = solveIVP(
f: { t, y in [-y[0]] },
tSpan: (0, 5),
y0: [1.0]
)Linear Algebra
let A = Matrix([[4, 3], [6, 3]])
let b = Matrix([[1], [2]])
// Solve Ax = b
let x = solve(A, b)
// Matrix decompositions
let (L, U, P) = A.lu()
let (Q, R) = A.qr()
let eigenvalues = A.eigenvalues()Module Overview
| Module | Description | |--------|-------------| | Complex | Complex number arithmetic with full operator support | | Constants | Mathematical and physical constants (CODATA 2018) | | Distributions | Probability distributions (Normal, T, Chi-squared, F, Gamma, Beta, etc.) | | Integration | Numerical integration (quad, romberg) and ODE solvers (RK4, RK45) | | Interpolation | Cubic spline, PCHIP, Akima interpolation | | Optimization | Root finding (bisect, newton, brent) and minimization (Nelder-Mead) | | NumberTheory | Primes, factorization, GCD, Euler's totient, and more | | Series | Polynomials, Taylor series, power series | | SpecialFunctions | Error functions, Bessel, gamma, beta, elliptic integrals | | Statistics | Descriptive statistics, correlation, statistical tests | | LinAlg | Matrix operations, decompositions (LU, QR, SVD, Cholesky), solvers | | Cluster | K-means, DBSCAN, hierarchical clustering | | Spatial | KDTree, Voronoi diagrams, Delaunay triangulation, distance metrics | | Geometry | 2D/3D geometry with SIMD vectors, coordinate transforms | | Utilities | vDSP-optimized array operations | | MathExpr | Mathematical expression parsing and evaluation | | Regression | Linear/nonlinear regression, ARIMA time series (statsmodels-inspired) |
Ecosystem
NumericSwift is part of a suite of Swift scientific computing libraries:
- ArraySwift (optional) - N-dimensional array support with NumPy-style API
- PlotSwift (planned) - Data visualization and plotting
ArraySwift Integration
When compiled with ArraySwift, NumericSwift provides seamless interoperability between LinAlg.Matrix and NDArray:
NUMERICSWIFT_INCLUDE_ARRAYSWIFT=1 swift buildThis enables:
- Matrix ↔ NDArray conversion:
matrix.toNDArray(),LinAlg.Matrix(ndarray: arr) - Linear algebra on NDArray:
arr.solve(b),arr.inv(),arr.det(),arr.lu(),arr.qr(),arr.svd(),arr.eig() - Statistics functions for NDArray:
sum(arr),mean(arr),stddev(arr),percentile(arr, p) - Math functions for NDArray:
sinArray(arr),expArray(arr),sqrtArray(arr), etc.
import NumericSwift
import ArraySwift
// Convert between types
let matrix = LinAlg.Matrix([[1.0, 2.0], [3.0, 4.0]])
let arr = matrix.toNDArray() // NDArray with shape [2, 2]
// Linear algebra directly on NDArray
let A = NDArray([[3.0, 1.0], [1.0, 2.0]])
let b = NDArray([9.0, 8.0])
let x = A.solve(b) // Solve Ax = b
// Decompositions
let (Q, R) = A.qr()!
let (U, s, Vt) = A.svd()!Documentation
Full API documentation is available via DocC. Build locally with:
swift package generate-documentation --target NumericSwiftRequirements
- Swift 5.9+
- iOS 15+ / macOS 12+
- Accelerate framework (included with Apple platforms)
License
NumericSwift is released under the MIT License. See LICENSE for details
Package Metadata
Repository: ChrisGVE/NumericSwift
Stars: 1
Forks: 0
Open issues: 0
Default branch: main
Primary language: swift
License: MIT
README: README.md