none
How to Find Determinant of nxn matrix?

    Question

  • How can i find the determinant of an nxn matrix only using iostream library and not using math.h , I have tried alot using 2D Arrays but i'm failing to come up with a general formula for the expansion of the matrix i need urgent assistance kindly help.  Thanx in advance.

    Saturday, March 20, 2010 5:54 PM

Answers

  • 
    The Anubis wrote:
    > How can i find the determinant of an nxn matrix only using iostream library and not using math.h
     
    There's nothing in iostream library, and very little in math.h, that would be of help.
     
    > I have tried alot using 2D
    > Arrays but i'm failing to come up with a general formula for the expansion of the matrix
     
    I'm not sure what you mean by "expansion" here. I'm not familiar with the term. Anyway, see
     
     
    Of these, LU decomposition is probably the most straightforward to implement.
     
    Alternatively, consult your favorite linear algebra textbook.
    --
    Igor Tandetnik
    • Marked as answer by Wesley Yao Friday, March 26, 2010 6:11 AM
    Saturday, March 20, 2010 6:58 PM
  • Hello,

    You may consider Matrix Template Library (http://osl.iu.edu/research/mtl/intro.php3)

    and use the sample bellow:

    #include <mtl/matrix.h>
    #include <mtl/mtl.h>
    #include <mtl/dense1D.h>
    #include <mtl/utils.h>
    #include <mtl/lu.h>

    /*!This function calculates the determinant of a matrix */
    template<class MatrixType>
    double lu_det(const MatrixType& _Matrix){

      int size = _Matrix.nrows();
      MatrixType LU(size,size);
      dense1D<int> pvector(size);

      copy(_Matrix, LU);
      lu_factor(LU, pvector);

      // For every permutation of rows the product of diagonal elements has to be multyplied by -1
      int sign = 1;
      for (int i = 0 ; i < size ; ++i){
        if ( pvector[i] != i+1 )
          sign *= -1;
      }

      typename MatrixType::iterator iter;
      iter = LU.begin();

      double det = 1.0;
      unsigned ind = 0;
      for ( iter = LU.begin(); iter != LU.end(); ++iter ){
        ind = iter.index();
        det *= LU(ind, ind);
      }

      det *= sign;
      return det;
    }

    • Marked as answer by Wesley Yao Friday, March 26, 2010 6:11 AM
    Monday, March 22, 2010 10:24 AM

All replies

  • 
    The Anubis wrote:
    > How can i find the determinant of an nxn matrix only using iostream library and not using math.h
     
    There's nothing in iostream library, and very little in math.h, that would be of help.
     
    > I have tried alot using 2D
    > Arrays but i'm failing to come up with a general formula for the expansion of the matrix
     
    I'm not sure what you mean by "expansion" here. I'm not familiar with the term. Anyway, see
     
     
    Of these, LU decomposition is probably the most straightforward to implement.
     
    Alternatively, consult your favorite linear algebra textbook.
    --
    Igor Tandetnik
    • Marked as answer by Wesley Yao Friday, March 26, 2010 6:11 AM
    Saturday, March 20, 2010 6:58 PM
  • Hello,

    You may consider Matrix Template Library (http://osl.iu.edu/research/mtl/intro.php3)

    and use the sample bellow:

    #include <mtl/matrix.h>
    #include <mtl/mtl.h>
    #include <mtl/dense1D.h>
    #include <mtl/utils.h>
    #include <mtl/lu.h>

    /*!This function calculates the determinant of a matrix */
    template<class MatrixType>
    double lu_det(const MatrixType& _Matrix){

      int size = _Matrix.nrows();
      MatrixType LU(size,size);
      dense1D<int> pvector(size);

      copy(_Matrix, LU);
      lu_factor(LU, pvector);

      // For every permutation of rows the product of diagonal elements has to be multyplied by -1
      int sign = 1;
      for (int i = 0 ; i < size ; ++i){
        if ( pvector[i] != i+1 )
          sign *= -1;
      }

      typename MatrixType::iterator iter;
      iter = LU.begin();

      double det = 1.0;
      unsigned ind = 0;
      for ( iter = LU.begin(); iter != LU.end(); ++iter ){
        ind = iter.index();
        det *= LU(ind, ind);
      }

      det *= sign;
      return det;
    }

    • Marked as answer by Wesley Yao Friday, March 26, 2010 6:11 AM
    Monday, March 22, 2010 10:24 AM
  • I am very much grateful to all of you for your help and I have successfully completed the code using only iostream library

    I have included the code below:


    #include<iostream>
    using namespace std;

    int main()
    {
        int m;        /* Dimension of matrix */
        double **a;    /* Coefficient matrix */
        int i, j, k;        /* Matrix subscripts */
        double factor;    /* Greatest common factor */
        double temp;    /* Temporary variable */
        int counti;    /* Counts number of I operations */

        counti = 0;    /* Initialize the I count */

        cout<<"Please enter matrix (NxN) dimension.\nDimension: 'N' => ";
        cin>>m;

        /* Allocate a one-dimensional array of pointers, one pointer per row */
        a = new double*[m];

        /* Allocate one c-element array of integers for each row */
        for(i = 0; i < m; i++)
        {
            a[i] =new double[m];
        }

        /* Fill matrix */
        cout<<"Please enter matrix contents, a row at a time (SPACE separated).\n";
        for(i = 0; i < m; i++)
        {
            cout<<"=> ";
            for(j = 0; j < m; j++)
            {
                cin>>a[i][j];
            }
        }

        /* Display coefficient matrix */
        cout<<"nMatrix entered:\n";
        for(i = 0; i < m; i++)
        {
            for(j = 0; j < m; j++)
            {
                cout<<"     "<<a[i][j];
            }
            cout<<endl;
        }

        /* Transform matrix into upper triangular */
        for(i = 0; i < m - 1; i++)
        {
            /* Elementary Row Operation I */
            if(a[i][i] == 0)
            {
                for(k = i; k < m; k++)
                {
                    if(a[k][i] != 0)
                    {
                        for(j = 0; j < m; j++)
                        {
                            temp = a[i][j];
                            a[i][j] = a[k][j];
                            a[k][j] = temp;
                        }
                    k = m;
                    }
                }
                counti++;
            }
            /* Elementary Row Operation III */
            if(a[i][i] != 0)
            {
                for(k = i + 1; k < m; k++)
                {
                    factor = -1.0 * a[k][i] /  a[i][i];
                    for(j = i; j < m; j++)
                    {
                        a[k][j] = a[k][j] + (factor * a[i][j]);
                    }
                }
            }
        }

        /* Display upper triangular matrix */
        printf("\nUpper triangular:\n");
        for(i = 0; i < m; i++)
        {
            for(j = 0; j < m; j++)
            {
                cout<<"     "<<a[i][j];
            }
            cout<<endl;
        }

        temp = 1.0;

        /* Calculate determinant */
        for(i = 0; i < m; i++)
        {
            temp *= a[i][i];
        }
        /* Modify determinant */
        cout<<"\nDeterminant:\n";
        if(counti % 2 == 0)
        {
            cout<<endl<<"     "<<temp<<endl<<endl;
        }
        else
        {
            cout<<endl<<"     "<<-1*temp<<endl<<endl;
        }

    }

    Sunday, April 04, 2010 4:38 PM