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
- Example 1
- Example 2
- Example 3
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]]
use \NDArray as nd;
$a = nd::identity(0);
print_r($a);
[]
use \NDArray as nd;
$a = nd::identity(-1);
print_r($a);
Fatal error: Uncaught Error: negative dimensions are not allowed in /src/test.php:4
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