PHP Determinant of a Matrix

The Determinant of a Matrix is a real number that can be defined for square matrices only i.e., the number of rows and columns of the matrices must be equal. Moreover, it is helpful in determining the system of the linear equation as well as figuring the inverse of the stated matrix.

Example:

// Determinant of 2*2 matrix

Input: [[1, 2],[2, 3]]

Output:
= (1*3)-(2*2)
= 6-4
= 2

// Determinant of 3*3 matrix
Input: [[1, 3, -2]
[-1, 2, 1]
[1, 0, -2]]

Output:
= 1(-4-0)-3(2-1)+(-2)(0-2)
= -4-3+4
= -3

Note:

  • The determinant of 1*1 matrix is the element itself.
  • The Cofactor of any element of the stated matrix can be calculated by eliminating the row and the column of that element from the matrix stated.

There are several ways to find the determinant of the matrix in PHP which are as follows:

Table of Content

  • Using Recursion
  • Using for loop 

Using Recursion

In this approach, we will implement a recursive function that calls itself again and again until it finds the determinant of the passed matrix. We can change the order of the matrix by changing the value of N, then pass a square matrix of the same order.

Example: The below code example implements recursion in PHP to find determinant of a matrix.

PHP
<?php
// N is the Order of the matrix
$N = 2;

// Recursive function to find determinant
function determinantOfMatrixRecursive($mat, $n) {
    if ($n === 1) {
        return $mat[0][0];
    }
    $det = 0;
    $sign = 1;
    for ($i = 0; $i < $n; $i++) {
        $submatrix = createSubmatrix($mat, $i, $n);
        $det += $sign * $mat[0][$i] 
          * determinantOfMatrixRecursive($submatrix, $n - 1);
        $sign = -$sign;
    }
    return $det;
}

// Function to find sub-matrices of different orders
function createSubmatrix($mat, $colToRemove, $n) {
    $submatrix = [];
    for ($i = 1; $i < $n; $i++) {
        $newRow = [];
        for ($j = 0; $j < $n; $j++) {
            if ($j !== $colToRemove) {
                $newRow[] = $mat[$i][$j];
            }
        }
        $submatrix[] = $newRow;
    }
    return $submatrix;
}

$mat = [
    [0, 4],
    [9, 8]
];
echo "Determinant of the matrix is : "
  . determinantOfMatrixRecursive($mat, $N);
?>

Output
Determinant of the matrix is : -36

Time complexity: O(n!)

Auxiliary Space: O(n2)

Using for loop

This method implements the PHP loops to iterate over the matrix elements and creates sub matrices to find the determinant of the matrix. You can change the value of to change order of the matrix and pass a square matrix of same order.

Example: The below code example is a practical implementation of the iterative approach to find determinant of the matrix.

PHP
<?php

// N is the order of the matrix
const N = 2;
 
function determinantOfMatrixIterative($mat, $n) {
    $det = 1;
    $total = 1;
    $temp = array_fill(0, $n, 0);
    for ($i = 0; $i < $n; $i++) {
        $index = $i;
        while ($index < $n && $mat[$index][$i] === 0) {
            $index++;
        }
        if ($index === $n) {
            continue;
        }
        if ($index !== $i) {
            for ($j = 0; $j < $n; $j++) {
                list($mat[$i][$j], $mat[$index][$j]) = 
                array($mat[$index][$j], $mat[$i][$j]);
            }
            $det *= pow(-1, $index - $i);
        }
        for ($j = 0; $j < $n; $j++) {
            $temp[$j] = $mat[$i][$j];
        }
        for ($j = $i + 1; $j < $n; $j++) {
            $num1 = $temp[$i];
            $num2 = $mat[$j][$i];
            for ($k = 0; $k < $n; $k++) {
                $mat[$j][$k] = 
                    $num1 * $mat[$j][$k] - 
                    $num2 * $temp[$k];
            }
            $total *= $num1;
        }
    }
    for ($i = 0; $i < $n; $i++) {
        $det *= $mat[$i][$i];
    }
    return $det / $total;
}
 
$mat = array(
    array(8, 1),
    array(2, 1),
  
);
echo "Determinant of the matrix is: " 
  . determinantOfMatrixIterative($mat, N);
?>

Output
Determinant of the matrix is: 6

Time Complexity: O(n2)

Auxiliary Space: O(n)