Basics

Matrix representation

A matrix 2x2 is stored as nested 2-levels PHP arrays.

$matrix 
[[ 
1],
 [ 
0]];

// OR
$matrix 
array(
  array( 
1),
  array( 
0)
);

NumPhp has no restriction about dimensions . You can work with matrices from 0 to n dimensions.

NdArray type

np::ar()is the entry point. It accepts PHP arrays.

use SciPhp\NumPhp as np;

$matrix np::ar(
  [[ 
10],
   [ 
01],
   [ 
00]]
);

echo 
get_class($matrix); // SciPhp\NdArray

nphandles vectors and matrices with a SciPhp\NdArray type.

To load data from a CSV, please use np::loadtxt() .

Print matrices

When used as string, a NdArray will be printed.

use SciPhp\NumPhp as np;

$matrix np::ar(
  [[ 
10],
   [ 
01],
   [ 
00]]
);

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(1005);

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]]

See Also