Software & Finance





C++ - Matrix Determinant





Determinant is applicable for only square matrix. If we have up to 2 X 2 or 3 X 3 or 4 X 4 square matrix, then it is OK to calculate it manually. But if we have 6 X 6 matrix or 12 X 12 matrix, then definitely we need calculator or computer program to solve this problem.

 

I have written a C++ program to solve the determinant problem that can accept any square matrix even 24 X 24 is possible.

 

I have used recursion and dynamic memory allocation a lot to solve this problem

 


Source Code


#if !defined(MATRIX_H)

#define MATRIX_H

 

#include <stdio.h>

#include <iostream>

#include <tchar.h>

#include <math.h>

 

class CMatrix

{

private:

    int m_rows;

    int m_cols;

    char m_name[128];

 

    CMatrix();

public:

    double **m_pData;

 

    CMatrix(const char *name, int rows, int cols) : m_rows(rows), m_cols(cols)

    {

        strcpy(m_name, name);

        m_pData = new double*[m_rows];

        for(int i = 0; i < m_rows; i++)

            m_pData[i] = new double[m_cols];

 

        for(int i = 0; i < m_rows; i++)

        {

            for(int j = 0; j < m_cols; j++)

            {

                m_pData[i][j] = 0.0;

            }

        }

    }

 

    CMatrix(const CMatrix &other)

    {

        strcpy(m_name, other.m_name);

        m_rows = other.m_rows;

        m_cols = other.m_cols;

 

        m_pData = new double*[m_rows];

        for(int i = 0; i < m_rows; i++)

            m_pData[i] = new double[m_cols];

 

        for(int i = 0; i < m_rows; i++)

        {

            for(int j = 0; j < m_cols; j++)

            {

                m_pData[i][j] = other.m_pData[i][j];

            }

        }

    }

 

    ~CMatrix()

    {

        for(int i = 0; i < m_rows; i++)

            delete [] m_pData[i];

        delete [] m_pData;

        m_rows = m_cols = 0;

    }

 

    void SetName(const char *name) { strcpy(m_name, name); }

    const char* GetName() const { return m_name; }

 

    void GetInput()

    {

        std::cin >> *this;

    }

 

    void FillSimulatedInput()

    {

        static int factor1 = 1, factor2 = 2;

        std::cout << "\n\nEnter Input For Matrix : " << m_name << " Rows: " << m_rows << " Cols: " << m_cols << "\n";

        for(int i = 0; i < m_rows; i++)

        {

            for(int j = 0; j < m_cols; j++)

            {

                std::cout << "Input For Row: " << i + 1 << " Col: " << j + 1 << " = ";

                int data = ((i + 1) * factor1) + (j + 1) * factor2;

                m_pData[i][j] = data / 10.2;

                std::cout << m_pData[i][j] << "\n";

 

                factor1 += (rand() % 4);

                factor2 += (rand() % 3);

            }

            std::cout << "\n";

        }

 

        std::cout << "\n";

    }   

 

    double Determinant()

    {

        double det = 0;

        double **pd = m_pData;

        switch(m_rows)

        {

       

        case 2:

        {

            det = pd[0][0] * pd[1][1] - pd[0][1] * pd[1][0];

            return det;

        }

        break;

 

        case 3:

        {

            /***

            a b c

            d e f

            g h i

 

            a b c a b c

            d e f d e f

            g h i g h i

 

            // det (A) = aei + bfg + cdh - afh - bdi - ceg.

            ***/

 

            double a = pd[0][0];

            double b = pd[0][1];

            double c = pd[0][2];

 

            double d = pd[1][0];

            double e = pd[1][1];

            double f = pd[1][2];

 

            double g = pd[2][0];

            double h = pd[2][1];

            double i = pd[2][2];

 

            double det = (a*e*i + b*f*g + c*d*h);

            det = det - a*f*h;

            det = det - b*d*i;

            det = det - c*e*g;

           

            return det;

        }

        break;

 

        case 4:

        {

            CMatrix *temp[4];

            for(int i = 0; i < 4; i++)

                temp[i] = new CMatrix("", 3,3);

 

            for(int k = 0; k < 4; k++)

            {

               

                for(int i = 1; i < 4; i++)

                {

                    int j1 = 0;

                    for(int j = 0; j < 4; j++)

                    {

                        if(k == j)

                            continue;

                        temp[k]->m_pData[i-1][j1++] = this->m_pData[i][j];

                    }

                }

            }

            double det = this->m_pData[0][0] * temp[0]->Determinant() -

            this->m_pData[0][1] * temp[1]->Determinant() +

            this->m_pData[0][2] * temp[2]->Determinant() -

            this->m_pData[0][3] * temp[3]->Determinant();

            return det;

        }

        break;

 

        case 5:

        {

            CMatrix *temp[5];

            for(int i = 0; i < 5; i++)

                temp[i] = new CMatrix("", 4,4);

 

            for(int k = 0; k < 5; k++)

            {

               

                for(int i = 1; i < 5; i++)

                {

                    int j1 = 0;

                    for(int j = 0; j < 5; j++)

                    {

                        if(k == j)

                            continue;

                        temp[k]->m_pData[i-1][j1++] = this->m_pData[i][j];

                    }

                }

            }

            double det = this->m_pData[0][0] * temp[0]->Determinant() -

            this->m_pData[0][1] * temp[1]->Determinant() +

            this->m_pData[0][2] * temp[2]->Determinant() -

            this->m_pData[0][3] * temp[3]->Determinant() +

            this->m_pData[0][4] * temp[4]->Determinant();

            return det;

        }

        case 6:

        case 7:

        case 8:

        case 9:

        case 10:

        case 11:

        case 12:

        default:

        {

            int DIM = m_rows;

            CMatrix **temp = new CMatrix*[DIM];

            for(int i = 0; i < DIM; i++)

                temp[i] = new CMatrix("", DIM - 1,DIM - 1);

 

            for(int k = 0; k < DIM; k++)

            {

               

                for(int i = 1; i < DIM; i++)

                {

                    int j1 = 0;

                    for(int j = 0; j < DIM; j++)

                    {

                        if(k == j)

                            continue;

                        temp[k]->m_pData[i-1][j1++] = this->m_pData[i][j];

                    }

                }

            }

 

            double det = 0;

            for(int k = 0; k < DIM; k++)

            {

                if( (k %2) == 0)

                    det = det + (this->m_pData[0][k] * temp[k]->Determinant());

                else

                    det = det - (this->m_pData[0][k] * temp[k]->Determinant());

            }

 

            for(int i = 0; i < DIM; i++)

                delete temp[i];

            delete [] temp;

 

            return det;

        }

        break;

        }

    }

 

    CMatrix& operator = (const CMatrix &other)

    {

        if( this->m_rows != other.m_rows ||

            this->m_cols != other.m_cols)

        {

            std::cout << "WARNING: Assignment is taking place with by changing the number of rows and columns of the matrix";

        }

        for(int i = 0; i < m_rows; i++)

            delete [] m_pData[i];

        delete [] m_pData;

        m_rows = m_cols = 0;

 

        strcpy(m_name, other.m_name);

        m_rows = other.m_rows;

        m_cols = other.m_cols;

 

        m_pData = new double*[m_rows];

        for(int i = 0; i < m_rows; i++)

            m_pData[i] = new double[m_cols];

 

        for(int i = 0; i < m_rows; i++)

        {

            for(int j = 0; j < m_cols; j++)

            {

                m_pData[i][j] = other.m_pData[i][j];

            }

        }

 

        return *this;

    }

 

    friend std::istream& operator >> (std::istream &is, CMatrix &m);

    friend std::ostream& operator << (std::ostream &os, const CMatrix &m);   

};

 

std::istream& operator >> (std::istream &is, CMatrix &m)

{

    std::cout << "\n\nEnter Input For Matrix : " << m.m_name << " Rows: " << m.m_rows << " Cols: " << m.m_cols << "\n";

    for(int i = 0; i < m.m_rows; i++)

    {

        for(int j = 0; j < m.m_cols; j++)

        {

            std::cout << "Input For Row: " << i + 1 << " Col: " << j + 1 << " = ";

            is >> m.m_pData[i][j];

        }

        std::cout << "\n";

    }

    std::cout << "\n";

    return is;

}

 

std::ostream& operator << (std::ostream &os,const CMatrix &m)

{

    os << "\n\nMatrix : " << m.m_name << " Rows: " << m.m_rows << " Cols: " << m.m_cols << "\n\n";

    for(int i = 0; i < m.m_rows; i++)

    {

        os << " | ";

        for(int j = 0; j < m.m_cols; j++)

        {

            char buf[32];

            double data = m.m_pData[i][j];

            if( m.m_pData[i][j] > -0.00001 &&

                m.m_pData[i][j] < 0.00001)

                data = 0;

            sprintf(buf, "%10.2lf ", data);

            os <<  buf;

        }

        os << "|\n";

    }

    os << "\n\n";

    return os;

}

 

 

#endif

 

int main()

{  

 

    CMatrix a("A", 6,6);

    a.FillSimulatedInput();

 

    double det = a.Determinant();

    std::cout << a  << "\n Determinant : ";

    std::cout << det << "\n";

}

Output


Enter Input For Matrix : A Rows: 6 Cols: 6

Input For Row: 1 Col: 1 = 0.294118

Input For Row: 1 Col: 2 = 0.980392

Input For Row: 1 Col: 3 = 1.86275

Input For Row: 1 Col: 4 = 2.84314

Input For Row: 1 Col: 5 = 3.62745

Input For Row: 1 Col: 6 = 5.58824

 

Input For Row: 2 Col: 1 = 2.94118

Input For Row: 2 Col: 2 = 4.11765

Input For Row: 2 Col: 3 = 5.88235

Input For Row: 2 Col: 4 = 8.43137

Input For Row: 2 Col: 5 = 10.3922

Input For Row: 2 Col: 6 = 12.3529

 

Input For Row: 3 Col: 1 = 8.13725

Input For Row: 3 Col: 2 = 9.70588

Input For Row: 3 Col: 3 = 12.0588

Input For Row: 3 Col: 4 = 15.098

Input For Row: 3 Col: 5 = 17.8431

Input For Row: 3 Col: 6 = 20.5882

 

Input For Row: 4 Col: 1 = 14.902

Input For Row: 4 Col: 2 = 18.2353

Input For Row: 4 Col: 3 = 21.4706

Input For Row: 4 Col: 4 = 24.7059

Input For Row: 4 Col: 5 = 27.549

Input For Row: 4 Col: 6 = 31.1765

 

Input For Row: 5 Col: 1 = 24.902

Input For Row: 5 Col: 2 = 27.9412

Input For Row: 5 Col: 3 = 32.451

Input For Row: 5 Col: 4 = 36.0784

Input For Row: 5 Col: 5 = 39.7059

Input For Row: 5 Col: 6 = 43.9216

 

Input For Row: 6 Col: 1 = 36.3725

Input For Row: 6 Col: 2 = 39.6078

Input For Row: 6 Col: 3 = 43.8235

Input For Row: 6 Col: 4 = 47.2549

Input For Row: 6 Col: 5 = 51.3725

Input For Row: 6 Col: 6 = 55.2941

 

 

 

 

Matrix : A Rows: 6 Cols: 6

 

 |       0.29     0.98     1.86     2.84     3.63     5.59 |

 |       2.94     4.12     5.88     8.43    10.39    12.35 |

 |       8.14     9.71    12.06    15.10    17.84    20.59 |

 |      14.90    18.24    21.47    24.71    27.55    31.18 |

 |      24.90    27.94    32.45    36.08    39.71    43.92 |

 |      36.37    39.61    43.82    47.25    51.37    55.29 |

 

 

 

Determinant : -11.9339