Install the package:
composer require rindow/rindow-math-matrix
Verify installation (check service level):
vendor/bin/rindow-math-matrix
Service Level: Accelerated (if drivers are properly configured).Basic usage (create and manipulate matrices):
use Rindow\Math\Matrix\MatrixOperator;
$mo = new MatrixOperator();
$matrix = $mo->array([[1, 2], [3, 4]]);
echo $mo->toString($matrix); // Output: [[1,2],[3,4]]
$a = $mo->array([[1, 2], [3, 4]]);
$b = $mo->array([[5, 6], [7, 8]]);
$c = $mo->matmul($a, $b); // Matrix multiplication
echo $mo->toString($c); // Output: [[19,22],[43,50]]
MatrixOperator: Core class for all operations.NDArray: N-dimensional array object (e.g., $mo->array()).$mo->gemm(), $mo->svd(), etc.$mo->laAccelerated(['deviceType' => 'GPU']).$mo = new MatrixOperator();
// Create matrices
$A = $mo->array([[1, 2], [3, 4]], dtype: 'float64');
$B = $mo->array([[5, 6], [7, 8]], dtype: 'float64');
// Basic operations
$sum = $mo->add($A, $B); // Element-wise addition
$product = $mo->matmul($A, $B); // Matrix multiplication
$transpose = $mo->transpose($A); // Transpose
// Broadcasting (auto-handled)
$vector = $mo->array([1, 2, 3]);
$broadcasted = $mo->add($A, $vector); // Adds [1,2,3] to each row of A
// Solve linear system: Ax = b
$A = $mo->array([[1, 2], [3, 4]], dtype: 'float64');
$b = $mo->array([5, 6], dtype: 'float64');
$x = $mo->solve($A, $b); // Returns solution vector
// Singular Value Decomposition (SVD)
$U = $mo->zeros(2, 2);
$S = $mo->zeros(2);
$V = $mo->zeros(2, 2);
$mo->svd($A, $U, $S, $V); // In-place decomposition
// Enable GPU (OpenCL) for acceleration
$mo->laAccelerated(['deviceType' => 'GPU']);
// Perform operation on GPU
$A_gpu = $mo->toGPU($A); // Transfer matrix to GPU
$B_gpu = $mo->toGPU($B);
$C_gpu = $mo->matmul($A_gpu, $B_gpu); // Compute on GPU
$C_cpu = $mo->toCPU($C_gpu); // Transfer back to CPU
// Batch normalization
$mean = $mo->mean($A, axes: [0, 1]); // Global mean
$var = $mo->var($A, axes: [0, 1]); // Global variance
$normalized = $mo->batchNorm($A, $mean, $var);
// Activation functions
$relu = $mo->relu($A);
$sigmoid = $mo->sigmoid($A);
// Check loaded drivers
$status = $mo->getStatus();
echo "BLAS Driver: " . $status['BLAS Driver'];
// Switch drivers dynamically (e.g., for testing)
$mo->setDriver('Rindow\OpenBLAS\FFI\Blas');
Default "Basic" Mode: Without FFI drivers, operations run in pure PHP (slow). Always check service level:
vendor/bin/rindow-math-matrix
rindow/rindow-math-matrix-matlibffi and pre-built binaries (OpenBLAS/OpenCL).Data Type Mismatches: Explicitly specify dtype (e.g., 'float32', 'float64') to avoid silent precision loss.
$A = $mo->array([[1, 2]], dtype: 'float64'); // Force 64-bit
vendor/bin/rindow-math-matrix -v
$mo->laAccelerated(['device' => '0,1']); // Platform 0, Device 1
$temp = $mo->zeros(1000, 1000); // Large buffer
// ... use $temp ...
$temp = null; // Explicitly free
| Error | Cause | Solution |
|---|---|---|
Service Level: Basic |
Missing FFI drivers | Install rindow-math-matrix-matlibffi |
Invalid argument for axis |
Wrong range style | Use R(0,5) for v2 style or set RANGE_STYLE_1 |
Unsupported dtype |
Complex numbers not enabled | Use 'complex64' or check BLAS support |
OpenCL initialization failed |
Missing OpenCL runtime | Install Intel/AMD GPU drivers |
$mo->setDriver('MyCustom\BlasDriver');
NDArray::serialize()/unserialize() for inter-process communication.topk() or gathernd() (may change in future releases).[0,5) (exclusive end). Force v1 style with:
$mo->array([[1, 2]], rangeStyle: NDArray::RANGE_STYLE_1);
gemm). Use 'complex64' dtype.$mo->getOpenCLInfo();
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(MatrixOperator::class, function () {
return new MatrixOperator();
});
}
dispatch(new MatrixOperationJob($matrixData));
$cacheKey = 'model_weights_v1';
$weights = Cache::remember($cacheKey, now()->addHours(1), function () {
return $mo->load('weights.npy');
});
How can I help you explore Laravel packages today?