Skip to content

Instantly share code, notes, and snippets.

@unix1
Last active September 18, 2022 13:09
Show Gist options
  • Save unix1/7510208 to your computer and use it in GitHub Desktop.
Save unix1/7510208 to your computer and use it in GitHub Desktop.
A sample matrix inversion in PHP using Gauss-Jordan elimination. This is implementation is meant for clarity, not for performance, memory usage, or numerical stability. You can optionally turn on the debug flag to output matrix state after every iteration. See https://en.wikipedia.org/wiki/Gauss-Jordan_elimination for more info. Enjoy!
<?php
/**
* Inverts a given matrix
*
* @param array $A matrix to invert
* @param boolean $debug whether to print out debug info
*
* @return array inverted matrix
*/
function invert($A, $debug = FALSE)
{
/// @todo check rows = columns
$n = count($A);
// get and append identity matrix
$I = identity_matrix($n);
for ($i = 0; $i < $n; ++ $i) {
$A[$i] = array_merge($A[$i], $I[$i]);
}
if ($debug) {
echo "\nStarting matrix: ";
print_matrix($A);
}
// forward run
for ($j = 0; $j < $n-1; ++ $j) {
// for all remaining rows (diagonally)
for ($i = $j+1; $i < $n; ++ $i) {
// if the value is not already 0
if ($A[$i][$j] !== 0) {
// adjust scale to pivot row
// subtract pivot row from current
$scalar = $A[$j][$j] / $A[$i][$j];
for ($jj = $j; $jj < $n*2; ++ $jj) {
$A[$i][$jj] *= $scalar;
$A[$i][$jj] -= $A[$j][$jj];
}
}
}
if ($debug) {
echo "\nForward iteration $j: ";
print_matrix($A);
}
}
// reverse run
for ($j = $n-1; $j > 0; -- $j) {
for ($i = $j-1; $i >= 0; -- $i) {
if ($A[$i][$j] !== 0) {
$scalar = $A[$j][$j] / $A[$i][$j];
for ($jj = $i; $jj < $n*2; ++ $jj) {
$A[$i][$jj] *= $scalar;
$A[$i][$jj] -= $A[$j][$jj];
}
}
}
if ($debug) {
echo "\nReverse iteration $j: ";
print_matrix($A);
}
}
// last run to make all diagonal 1s
/// @note this can be done in last iteration (i.e. reverse run) too!
for ($j = 0; $j < $n; ++ $j) {
if ($A[$j][$j] !== 1) {
$scalar = 1 / $A[$j][$j];
for ($jj = $j; $jj < $n*2; ++ $jj) {
$A[$j][$jj] *= $scalar;
}
}
if ($debug) {
echo "\n1-out iteration $j: ";
print_matrix($A);
}
}
// take out the matrix inverse to return
$Inv = array();
for ($i = 0; $i < $n; ++ $i) {
$Inv[$i] = array_slice($A[$i], $n);
}
return $Inv;
}
/**
* Prints matrix
*
* @param array $A matrix
* @param integer $decimals number of decimals
*/
function print_matrix($A, $decimals = 6)
{
foreach ($A as $row) {
echo "\n\t[";
foreach ($row as $i) {
echo "\t" . sprintf("%01.{$decimals}f", round($i, $decimals));
}
echo "\t]";
}
}
/**
* Produces an identity matrix of given size
*
* @param integer $n size of identity matrix
*
* @return array identity matrix
*/
function identity_matrix($n)
{
$I = array();
for ($i = 0; $i < $n; ++ $i) {
for ($j = 0; $j < $n; ++ $j) {
$I[$i][$j] = ($i == $j) ? 1 : 0;
}
}
return $I;
}
$A = array(
array( 10, -15, 30, 6, -8 ),
array( 0, -4, 60, 11, -5 ),
array( 8, 9, 2, 3, 7 ),
array( 25, 10, -9, 9, 0 ),
array( 13, 3, -12, 5, 2 ),
);
echo "\nMatrix:";
print_matrix($A);
echo "\n";
$B = invert($A);
echo "\nInversion result:";
print_matrix($B);
echo "\n\n";
?>
@dvhx
Copy link

dvhx commented Sep 18, 2022

Crashes on division by zero for:
$A = [
[1, 0, 7],
[0, 2, 0],
[0, 0, 3],
];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment