Vectors with PHP
Vector Operations with Rubix
Rubix ML offers powerful tools for vector operations, making it easy to perform mathematical computations in machine learning applications. The library provides a Vector class that supports element-wise arithmetic, dot products, norms, and statistical functions like mean and variance. These operations are essential for feature scaling, distance calculations, and optimizing machine learning models.
Example of Use:
Vector Operations with Rubix/Tensor
The RubixML/Tensor library provides efficient vector operations for numerical computing in PHP. With its Vector class, developers can perform element-wise arithmetic, dot products, norms, and statistical calculations like mean and variance. These operations are crucial for tasks such as feature scaling, distance measurement, and optimizing machine learning models. Designed for high performance, Tensor enables seamless vector computations without external dependencies.
Example of Use:
Vector Operations with MathPHP
MathPHP is a PHP library designed for advanced mathematical operations, including vector operations. Below is an overview of how you can use MathPHP to perform vector operations such as addition, subtraction, dot product, cross product, and magnitude.
Example of Use:
Vector Operations with Pure PHP
In PHP it can be written as a class Vector with implementation of a set of vector operations.
This class is a PHP implementation of vector operations commonly used in linear algebra and, by extension, in various AI and machine learning algorithms. It provides a robust set of methods for performing vectors calculations, making it a valuable tool for developers working on AI projects in PHP.
Example of Use:
use Apphp\MLKit\Math\Linear\Vector;
// Define vectors
$v1 = new Vector([2, 3]);
$v2 = new Vector([1, -1]);
$v3 = new Vector([2, -1]);
$v4 = new Vector([1, 2]);
$v5 = new Vector([3, 4]);
// Addition (from previous example)
$sum = $v1->add($v2);
echo "Addition: $v1 + $v2 = $sum\n";
// Subtraction (from previous example)
$difference = $v1->subtract($v2);
echo "Subtraction: $v1 - $v2 = $difference\n";
// Scalar Multiplication
$scalar = 3;
$scalarProduct = $v3->scalarMultiply($scalar);
echo "Scalar Multiplication: $scalar * $v3 = $scalarProduct\n";
// Dot Product
$dotProduct = $v4->dotProduct($v5);
echo "Dot Product: $v4 · $v5 = $dotProduct\n";
// Cross Product
$v6 = new Vector([1, 0, 0]);
$v7 = new Vector([0, 1, 0]);
$crossProduct = $v6->crossProduct($v7);
echo "Cross Product: $v6 × $v7 = $crossProduct\n";
// Magnitude (length) of a vector
$magnitude = $v5->magnitude();
echo "Magnitude of $v5 = $magnitude\n";
// Normalize a vector (create unit vector)
$normalized = $v5->normalize();
echo "Normalized $v5 = $normalized\n";
echo 'Magnitude of normalized vector = ' . $normalized->magnitude() . "\n";
// Get vector dimension
$dimension = $v5->getDimension();
echo "Dimension of $v5 = $dimension\n";
// Get vector components
$components = $v5->getComponents();
echo "Components of $v5 = [" . implode(', ', $components) . "]\n";
// Get specific component (0-based index)
$component = $v5->getComponent(1); // Get second component
echo "Second component of $v5 = $component\n";
// Calculate angle between vectors (in radians)
$angle = $v4->angleBetween($v5);
echo "Angle between $v4 and $v5 = " . number_format($angle, 4) . " radians\n";
echo 'Angle in degrees = ' . number_format(rad2deg($angle), 2) . "°\n";
// Check if vectors are parallel
$v8 = new Vector([2, 4]);
$v9 = new Vector([1, 2]); // Parallel to v8 (same direction)
$isParallel = $v8->isParallelTo($v9);
echo "Are $v8 and $v9 parallel? " . ($isParallel ? 'Yes' : 'No') . "\n";
// Create a zero vector
$zeroVector = Vector::zero(3);
echo "3D zero vector = $zeroVector\n";
// Vector projection
$v10 = new Vector([3, 2]);
$v11 = new Vector([4, 0]);
$projection = $v10->projectOnto($v11);
echo "Projection of $v10 onto $v11 = $projection\n";
// Hadamard Product (element-wise multiplication)
$hadamard = $v4->hadamardProduct($v5);
echo "Hadamard Product: $v4 ∘ $v5 = $hadamard\n";
// Euclidean Distance
$euclidean = $v4->euclideanDistance($v5);
echo "Euclidean Distance between $v4 and $v5 = $euclidean\n";
// Manhattan Distance
$manhattan = $v4->manhattanDistance($v5);
echo "Manhattan Distance between $v4 and $v5 = $manhattan\n";
// Angle in Degrees
$angleDeg = $v4->angleBetweenDegrees($v5);
echo "Angle between $v4 and $v5 = " . number_format($angleDeg, 2) . "°\n";
// Orthogonality check
$v12 = new Vector([1, 0]);
$v13 = new Vector([0, 5]);
$isOrthogonal = $v12->isOrthogonalTo($v13);
echo "Are $v12 and $v13 orthogonal? " . ($isOrthogonal ? 'Yes' : 'No') . "\n";
Last updated