Matrix determinant and Cramers method

The Determinant of a Matrix is a central part in the Matrix Mathematics. It is used to find out whether a Matrix equation can be solved or not and there is a way to solve the Matrix equation by the aid of Determinants. O.k. it’s not the best way to do this :-)


Calculation of the Determinant

The question whether a Matrix Equation has a solution or not leads to the calculation of the Determinant. Starting at a 2x2 Matrix Equation

2x2 Matrix

To solve this for x1 we have to eliminate both x2 part by multiplying equation I by a22 and equation II by a12 and subtracting II from I

2x2 Matrix

And get
2x2 Matrix


This transformed for x1 is


2x2 Matrix

This equation can be solved if the denominator is not 0


2x2 Matrix

And this term is the Determinant for a 2x2 Matrix.


2x2 Matrix


The calculation of the Determinant for a 3x3 Matrix is similar. Starting at the Matrix equation


2x2 Matrix


To solve this equation for X3 we eliminate x1 first and therefore multiply I by a21 and II by a11 and subtract II from I.


2x2 Matrix

And get


2x2 Matrix

Now we have to multiply II by a31 and III by a21 and subtract III from II


2x2 Matrix

This is


2x2 Matrix

If we eliminate the x2 part now we get


2x2 Matrix

And finally


2x2 Matrix


The denominator shall not be 0 to get a solution of this equation.

If we have a closer look onto the terms in the brackets now. It can be seen that they look like Determinants of 2x2 Matrixes. In fact it is


2x2 Matrix

And this is the Determinant of the 3x3 Matrix. Only the sign is negative.If we look at the involved elements in the Matrix:


2x2 Matrix

The building order becomes obvious:We first take a11 and multiply it by the Determinant of the remaining Matrix consisting of the elements except row 1 and column 1. Then we take a12 and multiply it by the Determinant of the Elements except row 1 and column 2. And then a13 is multiplied by the Determinant of the elements except row 1 and column 3. The 3 parts we add together with a switching sign: +..-...+

If we calculate the solution for x1 and x2, we get the same denominator and therefore the same Determinant. If we try to do the same for the 4x4 Matrix it would basically be the same but becomes much more complicate. So we better leave that :-)

The calculation of the Determinant of a (n x n) Matrix is always the same. We take all the elements of one row (or it can as well be one column) and multiply them by the Determinant of the Matrix consisting of rest of the elements except the row and column the element is in. Then we add all this part with together with a switching sign starting at + The definition for the development of the determinant according to Laplace is

2x2 Matrix

For i = element of {1…n}Whereas det Aij’ is the determinant of the Matrix consisting of the remaining elements. Laplace uses the elements of the first column instead of the first row. That’s basically the same.

"Development" means that we are not done by using this formula once. We have to develop the determinant by this formula. The 3 X 3 Matrix we had to dissect into 2 x 2 Matrixes and calculate the determinant of them first. A 4 x 4 Matrix needs to be dissect into 3 x 3 Matrixes which have to be dissect into their 2 x 2 Matrixes to calculate the determinant of them and build the determinants for the 3 x 3 Matrixes with this results and build the determinant of the 4 x 4 Matrix out of this.

It’s quite an effort to develop a determinant for a 4 x 4 Matrix already :-) But if we do this in a programmed routine it becomes quite easy. In a recursive algorithm it could mean we implement a function that dissects a (n x n) Matrix into its n-1 (n-1 x n-1) Matrixes and calls itself n-1 times to proceed each (n-1 x n-1) Matrix. If only 2 x 2 Matrixes remain, it calculates the determinant of each remaining 2 x 2 Matrix and returns the result. This result is multiplied by its top row element and these results of each (n-1 x n-1) Matrix and its top row element are added together.

Put into C# code that looks like this:




double CalcDet(TMatrix m, int order)
{
       int i,j, k;
       double sign = 1.0;
       double dm = 0;
       if (order <= 2)
            if (order <= 2)
            {
                        return m.a[0, 0] * m.a[1, 1] - m.a[1, 0] * m.a[0, 1];
            }
            else
            {
                        TMatrix mm = new TMatrix(order - 1);// temporary matrix
                        for (i = 0; i < order; i++)  
                         
// index for the element of the top row
                        {
                                    for(j=0; j < order-1; j++)
                                     // index that runs down from second row to n
                                    {
                                                for(k=0; k < order-1; k++)
                                                
// index to copy the elements into a sub matrix
                                                {
                                                           if (j < i)
                                                                       mm.a[j,k] = m.a[j, k+1];
                                                           else
                                                                       mm.a[j,k] = m.a[j+1, k+1];
                                                }
                                    }
                                    dm = dm + sign * CalcDet(mm, order - 1) * m.a[i, 0];
                                    sign = -sign;
                        }
                        return dm;
            }
}




With TMatrix as a class



public class TMatrix
{
       public double[,] a;
       public double[] y;
       public double[] x;

       public TMatrix(int size)
       {
             a = new double[size, size];
             y = new double[size];
             x = new double[size];
       }
}





To solve a Matrix equation by determinant. Cramers method

If we have a closer look onto the numerator of the equation for x3 of the 3 x 3 Matrix


2x2 Matrix

This also looks like some determinants of 2 x 2 Matrixes.In fact its


2x2 Matrix

With the involved elements


2x2 Matrix

That’s a determinant as well.

If we switch the sign of both the numerator and the denominator, we get for x3


2x2 Matrix

Similar we get for x1 and x2 for x3


2x2 Matrix

2x2 Matrix

That means to solve the Matrix equation we have to replace the column of the unknown element by the solution vector, calculate the determinant of this matrix and divide it by the determinant of the main Matrix. That's the method of Cramer and for small MAtrix equations this can be an elegant solution. For bigger Matrix equations it consumes quite some calculation time. But still it's worthwhile giving it a trial :-)


Online Matrix determinant solver


I implement an online Matrix determinant solver. This determinant solver solver can be found here :-)



C# Demo Project Determinante

To solve the Matrix equation by using determinants

  • Determinante.zip


  • Java Demo Project Determinante

    To solve the Matrix equation by using determinants

  • MatrixDeterminant.zip