Tensors

Creating Tensors

RubixML’s Tensor library allows you to create matrices and vectors (1-dimensional tensors) easily. Here’s how to create a tensor using various methods:

 
<?php

echo 'Creating Tensors:';
echo 
"\n---------\n";

// Create a scalar (0D tensor)
$scalar = new Tensor([[5]]);
echo 
"\nScalar: ";
print_r($scalar->getData());

// Create a vector (1D tensor)
$vector = new Tensor([1234]);
echo 
"\nVector: ";
print_r($vector->getData());

// Create a matrix (2D tensor)
$matrix = new Tensor([
    [
123],
    [
456],
    [
789],
]);

echo 
"\n\nMatrix: ";
echo 
"\n---------\n";
print_r($matrix->getData());

// Create a 3D tensor
$tensor3D = new Tensor([
    [
        [
12],
        [
34],
    ],
    [
        [
56],
        [
78],
    ],
]);

echo 
"\n\n3D Tensor: ";
echo 
"\n---------\n";
print_r($tensor3D->getData());
Time running: