NumPhp::eye

Construct a 2-D array with ones on the diagonal and zeros elsewhere.

Description

SciPhp\NdArray NumPhp::eye( int $rows [, int $cols = 0 ] [, int $k = 0 ])

Parameters

$rows
A positive integer. Represents number of rows.
$cols
A positive integer. Represents number of columns.
$k
Default value is 0. It's an offset.

Return Values

A SciPhp\NdArray

Examples

Example #1: Constructing a 4x4 identity matrix

use SciPhp\NumPhp as np;

$m np::eye(4);

print 
$m;

The above example will output:

[[1  0  0  0]
 [0  1  0  0]
 [0  0  1  0]
 [0  0  0  1]]

Example #2: Constructing a 3x5 matrix

use SciPhp\NumPhp as np;

$m np::eye(35);

print 
$m;

The above example will output:

[[1  0  0  0  0]
 [0  1  0  0  0]
 [0  0  1  0  0]]

Example #3: Constructing a 3x5 matrix, offset=2

use SciPhp\NumPhp as np;

$m np::eye(352);

print 
$m;

The above example will output:

[[0  0  1  0  0]
 [0  0  0  1  0]
 [0  0  0  0  1]]

See Also