Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Inverse of a 6x6 Matrix?

$
0
0
Hi all;

I am a bit new to C++. I am making a C++ program for calculating the inverse of a 6x6 matrix, using the Minor and Co-factor (Analytical) method. I have made a code for calculating the determinant of the matrix and it works for any valued matrix. Now I want to make a code for calculating the inverse which uses the determinant function I developed, but the problem arises when I try to use the Co factors calculated in the determinant function because the determinant function returns the value of determinant and does not return a 6x6 Co-factor array. Even when I try to access the Co-factor matrix using a globally declared pointer variable (pointed to the first element of Co-factor in each row i.e. Cofactor[row][0]) the first row of Co factor is correctly accessed in the Inverse function but gives garbage values for the next rows !Any help would be greatly appreciated. I have also attached my determinant function code.

[/
 #include "stdlib.h"
    #include <iostream>
    #include <math.h>
    using namespace std;


    double determinant(double Gl[6][6],int m);
    double* inverse(double Gl[6][6],int m);
    double  *ptr;//to access the Cofactor matrix

    void main(void)
    {
     double G[6][6]= {111,112,113,114,115,116,     
     117,118,119,10,11,12,
     13,14,15,16,17,18,
     19,20,21,22,23,24,
     25,26,27,28,29,30,
     31,32,33,34,35,36};    
     int i;int j;
     int n=6;
     for( i=0;i<=n-1;i++)
      {for( j=0;j<=n-1;j++)
        {cout<<"\n";cout<<"row no="<<i<<endl;
         cout<<"col no="<<j<<endl;
         cout<<"G["<<i<<"]"<<"G["<<j<<"]="<<G[i][j]<<endl;
        }
      }  

     printf("\n");
     cout<<"determinant is="<<determinant(G,n)<<endl;
     printf("\n");
     cout<<"inverse is="<<inverse(G,n)<<endl;
    }//end main

    double determinant(double Gl[6][6],int m)

     {double new_mat[6][6];double  Cofactor[6][6];
      double pivot[6][6];double det=0;
      if (m==2)
       {det=0;
        det=(Gl[0][0]*Gl[1][1])-(Gl[0][1]*Gl[1][0]);
        return det;
       }//end if(m==2)
      else //if m!=2
       {for (int row=0;row<=(m-1);row++)
        {for (int col=0;col<=(m-1);col++)
         { pivot[row][col]=Gl[row][col];
           if(row==0)
            {
             int r=0;
             for(int i=row+1;i<=(m-1);i++)
              {int s=0;
               if(col==0)
                {for(int j=col+1;j<=(m-1);j++)
                  {new_mat[r][s]=Gl[i][j];
                   s++;
                  }
                }
               else
                {for(int j=0;j<=(m-1);j++)
                  {if(j==col)
                    break;
                   else
                    {new_mat[r][s]=Gl[i][j];
                     s++;
                    }
                  }
                 }
                r++;
               }
              }
           if(row!=0)
            {int r=0;
             for (int i=0;i<=(m-1);i++)
              {
               if (i!=row)
                {int s=0;
                if(col==0)
                 {for(int j=col+1;j<=(m-1);j++)
                   {new_mat[r][s]=Gl[i][j];
                    s++;
                   }
                 }
                if(col!=0)
                 {for(int j=0;j<=(m-1);j++)
                   {
                    if(j!=col)
                     {new_mat[r][s]=Gl[i][j];
                      s++;
                     }
                    }
                  }
                 r++;
                }
               }
              }
          Cofactor[row][col]=pow(-1,row+col+2)*determinant(new_mat,m-1);
          ptr=&Cofactor[row][0];
           }

       double det=0;
       for(int coll=0;coll<=(m-1);coll++)
        {
         det=det+Cofactor[row][coll]*pivot[row][coll];
        }
       return det;
      }
     }
    }//end determinant function

   void inverse(double Gl[6][6],int m)
    {double adj[6][6];double inv[6][6];double cof[6][6],Cofactor[6][6];
     double det=determinant(Gl,m);
     if (m==2)//for second order matrices
      {adj[0][0]=Gl[1][1];
       adj[0][1]=-Gl[0][1];
       adj[1][0]=-Gl[1][0];
       adj[1][1]=Gl[0][0];}//end if
     else //for higher order matrices
      {
       int index=0;
       for (int row=0;row<=(m-1);row++)
        {for (int col=0;col<=(m-1);col++)
          {   
           cof[row][col]=*(ptr+index);
           //transpose of cofactor matrix for adjoint matrix
           adj[row][col]=cof[col][row];      
           index++;
          }
         }
       }//end else
       for (int row=0;row<=(m-1);row++)//now using adjoint values for inverse
        {for (int col=0;col<=(m-1);col++)
          {
           inv[row][col]=adj[row][col]*1/det;

           cout<<"cofactor["<<row<<"]["<<col<<"] is="<<cof[row][col]<<"\t";
           cout<<"inverse["<<row<<"]["<<col<<"] is="<<inv[row][col]<<endl;
          }
         printf("\n");
        }
    }//end inverse function
]

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>