Skip to content

Commit a904f9c

Browse files
committed
Add diagonal.
1 parent 088e28f commit a904f9c

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/Matrix.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,25 @@ public function determinant()
150150
return $this->getLUDecomp()->determinant();
151151
}
152152

153+
/**
154+
* diagonal
155+
*
156+
* Returns the diagonal of this matrix as a vector.
157+
*
158+
* @return self
159+
*/
160+
public function diagonal()
161+
{
162+
$diagonal = [];
163+
$max = min([$this->rows, $this->columns]);
164+
165+
for ($i = 0; $i < $max; $i++) {
166+
$diagonal[] = $this->get($i, $i);
167+
}
168+
169+
return new static([$diagonal]);
170+
}
171+
153172
/**
154173
* equals
155174
*
@@ -658,4 +677,4 @@ protected function isLiteralValid(array $literal)
658677

659678
return true;
660679
}
661-
}
680+
}

0 commit comments

Comments
 (0)