A matrix 2x2 is stored as nested 2-levels PHP arrays.
$matrix =
[[ 1, 0 ],
[ 0, 1 ]];
// OR
$matrix =
array(
array( 1, 0 ),
array( 0, 1 )
);
NumPhp has no restriction about dimensions . You can work with matrices from 0 to n dimensions.
np::ar()
is the entry point. It accepts PHP arrays.
use SciPhp\NumPhp as np;
$matrix = np::ar(
[[ 1, 0, 0 ],
[ 0, 1, 0 ],
[ 0, 0, 1 ]]
);
echo get_class($matrix); // SciPhp\NdArray
np
handles vectors and matrices with a
SciPhp\NdArray type.
To load data from a CSV, please use np::loadtxt() .
When used as string, a NdArray will be printed.
use SciPhp\NumPhp as np;
$matrix = np::ar(
[[ 1, 0, 0 ],
[ 0, 1, 0 ],
[ 0, 0, 1 ]]
);
echo $matrix;
[[1 0 0] [0 1 0] [0 0 1]]
If a matrix is too large, a truncated version will be rendered.
use SciPhp\NumPhp as np;
// Create a matrice 100x5
$matrix = np::arange(500)->reshape(100, 5);
echo $matrix;
[[0 1 2 3 4 ] [5 6 7 8 9 ] [10 11 12 13 14 ] [15 16 17 18 19 ] [20 21 22 23 24 ] [... ... ... ... ...] [475 476 477 478 479] [480 481 482 483 484] [485 486 487 488 489] [490 491 492 493 494] [495 496 497 498 499]]