Matft

Matft is Numpy-like library in Swift. Function name and usage is similar to Numpy.
📖 Documentation: https://jjjkkkjjj.github.io/Matft/
(Guide · NumPy Mapping · API Reference)
import Matft
let a = Matft.arange(start: 0, to: 27, by: 1, shape: [3, 3, 3])
let b = a[1~<3, Matft.all, 0] // a[1:3, :, 0] in Numpy
let c = Matft.math.sin(a) + a.T // broadcasting & element-wise math
a[a > 20] = MfArray([0]) // boolean indexing
Features
MfArray — an n-dimensional array like numpy.ndarray, with many types and pretty print
- Indexing — positive / negative indexing, slicing (
~<), newaxis, boolean and fancy indexing, views sharing memory
- Broadcasting, transpose, reshape,
astype and universal function reduction
- Math, statistics, linear algebra and FFT backed by the Accelerate framework (performance)
- Complex numbers
- Image processing compatible with OpenCV, and preprocessing compatible with PIL / Hugging Face transformers (visual check against OpenCV)
- Audio features (STFT, mel spectrogram, Whisper log-mel) compatible with librosa
- MLX interop — zero-copy conversion between
MfArray and MLXArray via MatftMLX
See the NumPy Mapping for all functions.
Matft and MLX
Matft is a complement to mlx-swift, not a replacement.
In short: Matft plays the role of NumPy / SciPy / OpenCV / librosa, and MLX plays the role of PyTorch.
Use Matft for exact, CPU-side pre / post processing and numerical work, and MLX for neural networks on the GPU.
import Matft
import MLX
import MatftMLX
let pixelValues = Matft.image.clip_preprocess(rgb) // (1, 3, 224, 224), same as CLIPImageProcessor
let output = model(pixelValues.toMLXArray()) // inference with MLX (GPU)
let result = MfArray(mlx: output) // back to Matft without copy (float32 / float64)
See Using Matft with MLX for the comparison table with mlx-swift and the details of MatftMLX.
Installation
Add Matft with Swift Package Manager:
dependencies: [
.package(url: "https://github.com/jjjkkkjjj/Matft", from: "1.0.1"),
],
In Xcode: File > Add Package Dependencies… and enter https://github.com/jjjkkkjjj/Matft.
Carthage and CocoaPods are outdated; see Installation.
Requirements: Swift 6.1 or later, macOS 10.13+ / iOS 12+. WebAssembly is also supported.
Development
./scripts/build-and-test-ios.sh # build & test on iOS/macOS
./scripts/build-and-test-wasm.sh # build & test on WebAssembly
./scripts/build-and-test-mlx.sh # build & test MatftMLX (Xcode with the Metal Toolchain)
./scripts/build-docs.sh # build the API reference
See Contributing for the details. The documentation site is in website/.
Feel free to ask this project or anything via [email protected]
评论