MathPHP\LinearAlgebra\MatrixFactory::eye PHP Method

eye() public static method

Diagonal can start at any column. Option to set the diagonal to any number. Example: m = 3; n = 3; k = 1; x = 1 (3x3 matrix with 1s on the kth (1) diagonal) [0 1 0] A = [0 0 1] [0 0 0]
public static eye ( integer $m, integer $n, integer $k, number $x = 1 ) : Matrix
$m integer number of rows
$n integer number of columns
$k integer Diagonal to fill with xs
$x number (optional; default 1)
return Matrix
    public static function eye(int $m, int $n, int $k, $x = 1) : Matrix
    {
        if ($n < 0 || $m < 0 || $k < 0) {
            throw new Exception\OutOfBoundsException("m, n and k must be ≥ 0. m = {$m}, n = {$n}, k = {$k}");
        }
        if ($k >= $n) {
            throw new Exception\OutOfBoundsException("k must be < n. k = {$k}, n = {$n}");
        }
        $R = self::zero($m, $n)->getMatrix();
        for ($i = 0; $i < $m; $i++) {
            if ($k + $i < $n) {
                $R[$i][$k + $i] = $x;
            }
        }
        return self::create($R);
    }