17 return self::transpose(
18 self::getCofactors($matrix)
35 throw new Exception(
'Adjoint can only be calculated for a square matrix');
38 return self::getAdjoint($matrix);
51 $cofactors = self::getMinors($matrix);
52 $dimensions = $matrix->
rows;
55 for (
$i = 0;
$i < $dimensions; ++
$i) {
57 for ($j = 0; $j < $dimensions; ++$j) {
58 $cofactors[
$i][$j] *= $cofs;
64 return new Matrix($cofactors);
78 throw new Exception(
'Cofactors can only be calculated for a square matrix');
81 return self::getCofactors($matrix);
93 $tmpMatrix = $matrix->
toArray();
94 unset($tmpMatrix[
$row]);
97 function (&$row) use ($column) {
102 return self::getDeterminant(
new Matrix($tmpMatrix));
115 $dimensions = $matrix->
rows;
118 switch ($dimensions) {
120 $determinant = $matrix->
getValue(1, 1);
127 for (
$i = 1;
$i <= $dimensions; ++
$i) {
128 $det = $matrix->
getValue(1,
$i) * self::getDeterminantSegment($matrix, 0,
$i - 1);
130 $determinant -= $det;
132 $determinant += $det;
151 throw new Exception(
'Determinant can only be calculated for a square matrix');
154 return self::getDeterminant($matrix);
167 throw new Exception(
'Diagonal can only be extracted from a square matrix');
170 $dimensions = $matrix->
rows;
171 $grid = Builder::createFilledMatrix(0, $dimensions, $dimensions)
174 for (
$i = 0;
$i < $dimensions; ++
$i) {
191 throw new Exception(
'Anti-Diagonal can only be extracted from a square matrix');
194 $dimensions = $matrix->
rows;
195 $grid = Builder::createFilledMatrix(0, $dimensions, $dimensions)
198 for (
$i = 0;
$i < $dimensions; ++
$i) {
217 throw new Exception(
'Identity can only be created for a square matrix');
220 $dimensions = $matrix->
rows;
222 return Builder::createIdentityMatrix($dimensions);
235 throw new Exception(ucfirst(
$type) .
' can only be calculated for a square matrix');
238 $determinant = self::getDeterminant($matrix);
239 if ($determinant == 0.0) {
240 throw new Div0Exception(ucfirst(
$type) .
' can only be calculated for a matrix with a non-zero determinant');
243 if ($matrix->
rows == 1) {
247 return self::getAdjoint($matrix)
248 ->multiply(1 / $determinant);
262 $dimensions = $matrix->
rows;
263 if ($dimensions == 1) {
267 for (
$i = 0;
$i < $dimensions; ++
$i) {
268 for ($j = 0; $j < $dimensions; ++$j) {
269 $minors[
$i][$j] = self::getDeterminantSegment($matrix,
$i, $j);
291 throw new Exception(
'Minors can only be calculated for a square matrix');
294 return new Matrix(self::getMinors($matrix));
309 throw new Exception(
'Trace can only be extracted from a square matrix');
312 $dimensions = $matrix->
rows;
314 for (
$i = 1;
$i <= $dimensions; ++
$i) {
329 $array = array_values(array_merge([null], $matrix->
toArray()));
330 $grid = call_user_func_array(
static antidiagonal(Matrix $matrix)
Return the antidiagonal of this matrix.
static minors(Matrix $matrix)
Return the minors of the matrix The minor of a matrix A is the determinant of some smaller square mat...
static transpose(Matrix $matrix)
Return the transpose of this matrix.
static inverse(Matrix $matrix, string $type='inverse')
Return the inverse of this matrix.
static getCofactors(Matrix $matrix)
Calculate the cofactors of the matrix.
static getMinors(Matrix $matrix)
Calculate the minors of the matrix.
rows()
Returns a Generator that will yield each row of the matrix in turn as a vector matrix or the value of...
static getAdjoint(Matrix $matrix)
Calculate the adjoint of the matrix.
static getDeterminantSegment(Matrix $matrix, $row, $column)
getValue(int $row, int $column)
Return a value from this matrix, from the "cell" identified by the row and column numbers Note that r...
static adjoint(Matrix $matrix)
Return the adjoint of this matrix The adjugate, classical adjoint, or adjunct of a square matrix is t...
static identity(Matrix $matrix)
Return the identity matrix The identity matrix, or sometimes ambiguously called a unit matrix...
static getDeterminant(Matrix $matrix)
Calculate the determinant of the matrix.
static trace(Matrix $matrix)
Return the trace of this matrix The trace is defined as the sum of the elements on the main diagonal ...
static diagonal(Matrix $matrix)
Return the diagonal of this matrix.
toArray()
Return the matrix as a 2-dimensional array.
static determinant(Matrix $matrix)
Return the determinant of this matrix.
static cofactors(Matrix $matrix)
Return the cofactors of this matrix.
Class for the creating "special" Matrices.
isSquare()
Identify if the row and column dimensions of this matrix are equal, i.e.