Blazing Fast
Zero-copy views, Rust-powered operations, and FFI integration deliver near-native performance for numerical computing.

<?php
use PhpMlKit\NDArray\NDArray;
// Create arrays
$a = NDArray::array([[1, 2], [3, 4]]);
$b = NDArray::ones([2, 2]);
// Mathematical operations (method calls, NOT operators)
$c = $a->add($b); // Element-wise addition
$d = $a->matmul($b); // Matrix multiplication
$e = $a->sum(); // Sum all elements
// Slicing with zero-copy views
$first_row = $a[0]; // View: no data copied!
$sub_matrix = $a->slice(['0:1', '0:1']);
// Linear algebra
$dot = $a->dot($b);
$trace = $a->trace();Traditional PHP arrays are powerful but slow for numerical work. NDArray changes the game:
// Work with a 1000x1000 matrix subset instantly
$data = NDArray::random([1000, 1000]); // 8MB array
$batch = $data->slice(['0:32']); // View: 0 bytes copied!
$batch->set([0, 0], 5); // Modifies $data!
$mean = $batch->mean(); // Calculate on viewComing from NumPy? You'll feel right at home (with PHP syntax):
| NumPy | NDArray PHP |
|---|---|
np.array([1, 2, 3]) | NDArray::array([1, 2, 3]) |
arr.shape | $arr->shape() |
arr.sum() | $arr->sum() |
a + b | $a->add($b) |
a[0, 0] | $a['0,0'] |
a[0:5] | $a->slice(['0:5']) |
PHP Syntax Differences
PHP does NOT support operator overloading or multi-dimensional indexing with commas. Always use method calls:
// ❌ This doesn't work
$c = $a + $b;
$value = $matrix[0, 0];
// ✅ Use method calls
$c = $a->add($b);
$value = $matrix['0,0'];
// or
$value = $matrix->get(0, 0);composer require phpmlkit/ndarrayRequirements: PHP 8.1+ with FFI extension enabled