17 return self::transpose(
35 throw new Exception(
'Adjoint can only be calculated for a square matrix');
38 return self::getAdjoint(
$matrix);
51 $cofactors = self::getMinors(
$matrix);
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));
118 switch ($dimensions) {
120 $determinant =
$matrix->getValue(1, 1);
123 $determinant =
$matrix->getValue(1, 1) *
$matrix->getValue(2, 2) -
127 for (
$i = 1;
$i <= $dimensions; ++
$i) {
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');
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');
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');
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');
247 return self::getAdjoint(
$matrix)
248 ->multiply(1 / $determinant);
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');
309 throw new Exception(
'Trace can only be extracted from a square matrix');
314 for (
$i = 1;
$i <= $dimensions; ++
$i) {
329 $array = array_values(array_merge([
null],
$matrix->toArray()));
330 $grid = call_user_func_array(
An exception for terminatinating execution or to throw for unit testing.
static inverse(Matrix $matrix, string $type='inverse')
Return the inverse of this matrix.
static getMinors(Matrix $matrix)
Calculate the minors of the matrix.
static getDeterminantSegment(Matrix $matrix, $row, $column)
static adjoint(Matrix $matrix)
Return the adjoint of this matrix The adjugate, classical adjoint, or adjunct of a square matrix is t...
static diagonal(Matrix $matrix)
Return the diagonal of this matrix.
static cofactors(Matrix $matrix)
Return the cofactors of this matrix.
static transpose(Matrix $matrix)
Return the transpose 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 getAdjoint(Matrix $matrix)
Calculate the adjoint of the matrix.
static antidiagonal(Matrix $matrix)
Return the antidiagonal of this matrix.
static identity(Matrix $matrix)
Return the identity matrix The identity matrix, or sometimes ambiguously called a unit 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 determinant(Matrix $matrix)
Return the determinant of this matrix.
static getCofactors(Matrix $matrix)
Calculate the cofactors of the matrix.
static getDeterminant(Matrix $matrix)
Calculate the determinant of the matrix.
Class for the creating "special" Matrices.