Vectors

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.

 
<?php

use Rubix\ML\Datasets\Unlabeled;
use 
Rubix\ML\Kernels\Distance\Euclidean;
use 
Rubix\ML\Kernels\Distance\Manhattan;
use 
Rubix\ML\Kernels\Distance\Cosine;
use 
Rubix\ML\Transformers\L1Normalizer;
use 
Rubix\ML\Transformers\L2Normalizer;
use 
Rubix\ML\Transformers\MinMaxNormalizer;

// Create vectors as arrays (RubixML uses arrays for vector representation)
$v1 = [234];
$v2 = [1, -12];

echo 
"Vector 1: " array_to_vector($v1) . "\n";
echo 
"Vector 2: " array_to_vector($v2) ."\n\n";

// VECTOR ADDITION
// Add two vectors using array mapping
$addition array_map(function ($a$b) {
    return 
$a $b;
}, 
$v1$v2);

echo 
"Addition: " array_to_vector($v1) . " + " array_to_vector($v2) . " = " array_to_vector($addition) . "\n";

// VECTOR SUBTRACTION
// Subtract two vectors using array mapping
$subtraction array_map(function ($a$b) {
    return 
$a $b;
}, 
$v1$v2);

echo 
"Subtraction: " array_to_vector($v1) . " - " array_to_vector($v2) . " = " array_to_vector($subtraction) . "\n";

// SCALAR MULTIPLICATION
// Multiply a vector by a scalar
$scalar 3;
$scalarMultiplication array_map(function ($a) use ($scalar) {
    return 
$a $scalar;
}, 
$v1);

echo 
"Scalar Multiplication: $scalar * " array_to_vector($v1) . " = " array_to_vector($scalarMultiplication) . "\n";

// DOT PRODUCT
$dotProduct array_sum(array_map(function ($a$b) {
    return 
$a $b;
}, 
$v1$v2));

echo 
"Dot Product: " array_to_vector($v1) . " · " array_to_vector($v2) . " = $dotProduct\n";

// CROSS PRODUCT (for 3D vectors only)
function crossProduct(array $a, array $b): array
{
    if (
count($a) !== || count($b) !== 3) {
        throw new 
Exception('Cross product requires 3D vectors');
    }

    return [
        
$a[1] * $b[2] - $a[2] * $b[1],
        
$a[2] * $b[0] - $a[0] * $b[2],
        
$a[0] * $b[1] - $a[1] * $b[0]
    ];
}

$crossProduct crossProduct($v1$v2);
echo 
"Cross Product: " array_to_vector($v1) . " × " array_to_vector($v2) . " = " array_to_vector($crossProduct) . "\n";


// MAGNITUDE (L2 NORM)
// Calculate vector magnitude manually
$magnitude1 sqrt(array_sum(array_map(function ($x) {
    return 
$x $x;
}, 
$v1)));
$magnitude2 sqrt(array_sum(array_map(function ($x) {
    return 
$x $x;
}, 
$v2)));

echo 
"Magnitude of Vector1: $magnitude1"\n";
echo 
"Magnitude of Vector2: $magnitude2"\n";

// VECTOR NORMALIZATION
// Create a dataset to utilize RubixML's normalizers
$samples = [$v1$v2];
$dataset = new Unlabeled($samples);

// L1 Normalization
$l1Normalizer = new L1Normalizer();
$l1NormalizedDataset = clone $dataset;
$l1NormalizedDataset->apply($l1Normalizer);
$l1NormalizedSamples $l1NormalizedDataset->samples();

echo 
"L1 Normalized Vector1: " array_to_vector($l1NormalizedSamples[0]) . "\n";
echo 
"L1 Normalized Vector2: " array_to_vector($l1NormalizedSamples[1]) . "\n";

// L2 Normalization (unit vectors)
$l2Normalizer = new L2Normalizer();
$l2NormalizedDataset = clone $dataset;
$l2NormalizedDataset->apply($l2Normalizer);
$l2NormalizedSamples $l2NormalizedDataset->samples();

echo 
"L2 Normalized Vector1: " array_to_vector($l2NormalizedSamples[0]) . "\n";
echo 
"L2 Normalized Vector2: " array_to_vector($l2NormalizedSamples[1]) . "\n";

// VECTOR DISTANCES
// Calculate various distances between vectors using RubixML distance kernels
$euclidean = new Euclidean();
$euclideanDistance $euclidean->compute($v1$v2);
echo 
"Euclidean Distance: $euclideanDistance"\n";

$manhattan = new Manhattan();
$manhattanDistance $manhattan->compute($v1$v2);
echo 
"Manhattan Distance: $manhattanDistance"\n";

$cosine = new Cosine();
$cosineDistance $cosine->compute($v1$v2);
$cosineSimilarity $cosineDistance;
echo 
"Cosine Distance: $cosineDistance"\n";
echo 
"Cosine Similarity: $cosineSimilarity"\n";

// ELEMENT-WISE OPERATIONS
// Element-wise multiplication
$elementWiseMultiplication array_map(function ($a$b) {
    return 
$a $b;
}, 
$v1$v2);
echo 
"Element-wise Multiplication: " array_to_vector($elementWiseMultiplication) . "\n";

// Element-wise division
$elementWiseDivision array_map(function ($a$b) {
    return 
$b != $a $b 'undefined';
}, 
$v1$v2);
echo 
"Element-wise Division: " array_to_vector($elementWiseDivision) . "\n";

// STATISTICAL OPERATIONS
// Calculate mean of vectors
$mean1 array_sum($v1) / count($v1);
$mean2 array_sum($v2) / count($v2);
echo 
"Mean of Vector1: $mean1\n";
echo 
"Mean of Vector2: $mean2\n";

// Calculate variance of vectors
$variance1 array_sum(array_map(function ($x) use ($mean1) {
        return 
pow($x $mean12);
    }, 
$v1)) / count($v1);

$variance2 array_sum(array_map(function ($x) use ($mean2) {
        return 
pow($x $mean22);
    }, 
$v2)) / count($v2);

echo 
"Variance of Vector1: $variance1\n";
echo 
"Variance of Vector2: $variance2\n";

// Calculate standard deviation of vectors
$std1 sqrt($variance1);
$std2 sqrt($variance2);
echo 
"Standard Deviation of Vector1: $std1\n";
echo 
"Standard Deviation of Vector2: $std2\n";

// ANGLE BETWEEN VECTORS
$angleRadians acos($cosineSimilarity);
$angleDegrees rad2deg($angleRadians);
echo 
"Angle between vectors (radians): $angleRadians\n";
echo 
"Angle between vectors (degrees): $angleDegrees\n";

// PROJECTION OF VECTOR1 ONTO VECTOR2
$projectionScalar $dotProduct / ($magnitude2 $magnitude2);
$projection array_map(function ($element) use ($projectionScalar) {
    return 
$element $projectionScalar;
}, 
$v2);

echo 
"Projection of Vector1 onto Vector2: " array_to_vector($projection) . "\n";
Result: Memory: 0.161 Mb Time running: 0.004 sec.
Vector 1: [2, 3, 4]
Vector 2: [1, -1, 2]

Addition: [2, 3, 4] + [1, -1, 2] = [3, 2, 6]
Subtraction: [2, 3, 4] - [1, -1, 2] = [1, 4, 2]
Scalar Multiplication: 3 * [2, 3, 4] = [6, 9, 12]
Dot Product: [2, 3, 4] · [1, -1, 2] = 7
Cross Product: [2, 3, 4] × [1, -1, 2] = [10, 0, -5]
Magnitude of Vector1: 5.3851648071345
Magnitude of Vector2: 2.4494897427832
L1 Normalized Vector1: [0.22222222222222, 0.33333333333333, 0.44444444444444]
L1 Normalized Vector2: [0.25, -0.25, 0.5]
L2 Normalized Vector1: [0.3713906763541, 0.55708601453116, 0.74278135270821]
L2 Normalized Vector2: [0.40824829046386, -0.40824829046386, 0.81649658092773]
Euclidean Distance: 4.5825756949558
Manhattan Distance: 7
Cosine Distance: 0.46933136949477
Cosine Similarity: 0.53066863050523
Element-wise Multiplication: [2, -3, 8]
Element-wise Division: [2, -3, 2]
Mean of Vector1: 3
Mean of Vector2: 0.66666666666667
Variance of Vector1: 0.66666666666667
Variance of Vector2: 1.5555555555556
Standard Deviation of Vector1: 0.81649658092773
Standard Deviation of Vector2: 1.2472191289246
Angle between vectors (radians): 1.0114070854294
Angle between vectors (degrees): 57.949357364731
Projection of Vector1 onto Vector2: [1.1666666666667, -1.1666666666667, 2.3333333333333]