Skip to main content

NDArray::identity

public static function identity(int $size): NDArray;

This function returns a square array, where the main diagonal consists of ones and all other elements are zeros. It takes a parameter $size which determines the number of rows and columns in the output array.


Parameters

$size

Type long

  • Number of rows and columns of the new square array of size ($size, $size)

Return

Type - NDArray

  • Return a new square array of size ($size, $size)

Examples

use \NDArray as nd;

$a = nd::identity(10);
print_r($a);
[[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1]]

Exceptions

If $size is less than 0 a Fatal error will be raised

Fatal error: Uncaught Error: negative dimensions are not allowed in /src/test.php:4