Installation:
composer require interop-phpobjects/polite-math
Add to composer.json under require or require-dev depending on your use case.
First Use Case:
Implement a basic NDArray (N-Dimensional Array) for lightweight math operations:
use Interop\Math\NDArray;
// Create a simple 1D array
$array = new class implements NDArray {
private $data = [1, 2, 3];
public function shape(): array { return [3]; }
public function dtype(): int { return 1; /* INT */ }
public function buffer(): Buffer { /* ... */ }
// ... other required methods
};
Key Entry Points:
NDArray for multi-dimensional data structures.Buffer/LinearBuffer for raw data storage (e.g., GPU/CPU memory).OpenCL for hardware-accelerated operations (if needed).NDArray Integration:
NDArray for tensor-like operations (e.g., reshaping, slicing).NDArray:
$ndArray = new YourNDArrayImplementation([1, 2, 3]);
$phpArray = $ndArray->toArray(); // mixed return (scalar/array)
Buffer Management:
Buffer for custom memory handling (e.g., GPU buffers via DeviceBuffer):
use Interop\Math\Buffer;
class GPUBuffer implements Buffer {
public function read(int $offset, int $length): string { /* ... */ }
public function write(int $offset, string $data): void { /* ... */ }
// ... other required methods
}
Linear Algebra:
LinearBuffer for matrix operations (e.g., BLAS-like interfaces):
$matrix = new YourLinearBuffer([1, 2, 3, 4]); // 2x2 matrix
$result = $matrix->gemv($vector); // Generic Matrix-Vector Multiply
Interoperability:
use Interop\Math\OpenCL;
$clDevice = new YourOpenCLDevice();
$buffer = $clDevice->createBuffer($data); // Offload to GPU
Service Providers:
Bind interfaces to implementations in AppServiceProvider:
$this->app->bind(NDArray::class, YourNDArray::class);
Task Scheduling:
Use NDArray for batch processing (e.g., image resizing):
$this->app->command('process-images', function () {
$images = Image::all();
foreach ($images as $image) {
$ndArray = new ImageNDArray($image->pixels());
$processed = $ndArray->applyFilter(); // Custom operation
}
});
API Responses:
Serialize NDArray to JSON for APIs:
return response()->json(['data' => $ndArray->toArray()]);
Type Mismatches:
NDArray::toArray() returns mixed (scalar/array). Validate before use:
$data = $ndArray->toArray();
if (is_array($data)) { /* Handle array */ }
Buffer Ownership:
Buffer implementations may manage external resources (e.g., GPU memory). Ensure proper cleanup:
$buffer->free(); // If supported
OpenCL Limitations:
OpenCL interface is minimal. Use vendor-specific SDKs (e.g., php-opencl) for full functionality.PHP Stability:
minimum-stability in production (check composer.json after 1.0.1).PHPDoc Inconsistencies:
NDArray::buffer()) may not match runtime behavior. Use gettype() for debugging:
$buffer = $ndArray->buffer();
var_dump(gettype($buffer)); // Should match PHPDoc
Shape Validation:
NDArray::shape() must return a flat array. Validate with:
assert(is_array($shape) && !in_array($shape, array_keys($shape)));
Custom Data Types:
NDArray for domain-specific dtypes (e.g., FLOAT64):
const FLOAT64 = 2;
public function dtype(): int { return self::FLOAT64; }
Memory Mapping:
Buffer for memory-mapped files:
class FileBuffer implements Buffer {
private $fileHandle;
public function __construct(string $path) { /* ... */ }
// ... read/write methods
}
Laravel Integration:
NDArray operations:
facade(NDArray::class, YourNDArrayFacade::class);
Then use globally:
$result = NDArray::create([1, 2, 3])->sum();
Testing:
Buffer/NDArray in unit tests:
$mockBuffer = $this->createMock(Buffer::class);
$mockBuffer->method('read')->willReturn('test');
How can I help you explore Laravel packages today?