Thread: Matrix Inverse Problem

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    Matrix Inverse Problem

    Hello,
    I made the program to inverse a 3*3 matrix and here's the code to do that

    Code:
    #include <iostream.h>
    
    int** adjoint(int **matrix,int r,int c)
    {
     int i,j,l,m,t,temp[4],**ad_mat;
     ad_mat=new int *[r];
    
     for(i=0;i<r;i++)
     {
    	ad_mat[i]=new int[c];
     }
    
     for(l=0;l<r;l++)
     {
    	for(m=0;m<c;m++)
    	{
    	  t=0;
    		for(i=0;i<r;i++)
    		{
    			for(j=0;j<c;j++)
    			{
    				if(i!=l && j!=m)
    				{
    				  temp[t]=matrix[i][j];
    				  t++;
    				}
    			}
    		}
    		ad_mat[l][m]=(temp[0]*temp[3])-(temp[1]*temp[2]);
    	}
     }
      return ad_mat;
    }
    
    int main(void)
    {
     clrscr();
     int **matrix,i,j,**ad_mat;
     float inverse[3][3],det=0;
     matrix=new int *[3];
     for(i=0;i<3;i++)
     {
    	matrix[i]=new int [3];
     }
     cout<<"\n\nEnter 9 Elements :\n";
     for(i=0;i<3;i++)
     {
    	for(j=0;j<3;j++)
    	{
    		cin>>matrix[i][j];
    	}
     }
     ad_mat=adjoint(matrix,3,3);
    
     cout<<"\n\nAdjoint Of Matrix Is :\n\n";
     for(i=0;i<3;i++)
     {
    	for(j=0;j<3;j++)
    	{
    		cout.width(4);
    		cout<<ad_mat[i][j];
    	}
    		cout<<endl;
     }
     cout<<"\n\nValues Of Matrix :\n";
     for(i=0;i<3;i++)
     {
    	for(j=0;j<3;j++)
    	{
    		if(i+j==1 || i+j==3)
    		{
    			matrix[i][j]*=-1;
    		}
    		cout.width(4);
    		cout<<matrix[i][j];
    	}
    	cout<<endl;
     }
     for(j=0;j<3;j++)
     {
    	det=det+ad_mat[0][j]*matrix[0][j];
     }
     if(det!=0)
     {
    	cout<<"\n\nThe Determinant Is :"<<det;
    
    	cout<<"\n\nThe Inverse Of Matrix Is :\n";
    	for(i=0;i<3;i++)
    	{
    		for(j=0;j<3;j++)
    		{
    			inverse[i][j]=ad_mat[i][j]/det;
    			cout.width(8);
    			cout.precision(3);
    			cout<<inverse[i][j];
    		}
    		cout<<endl;
    
    	 }
     }
     else
    	cout<<"\n\nInverse Of Matix Not Possible";
     return 0;
    }

    Now what i want to know is that is the logic of the program correct??
    This is just a 3 *3 matrix. Can a N*N matrix be inverted??If yes then kindly tell me what changes do i need to make in the above program to make it work for a N*N matrix?
    i am not sure whether a N*N matrix be inverted or not??!

    anybody please tell me how to find adjoint of a N*N matrix or of 4*4 matrix so that i can get a rough idea and try to implement that program in C++.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Find a linear algebra textbook. Square matrices can be inverted if and only if their determinant is nonzero.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    i have already stated that condition in the program .You may check it out

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What I'm saying is that is the condition for any N*N matrix to be invertible. Of course, computing a determinant gets harder.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need to Calculate the Inverse of A Matrix.
    By jordan_230 in forum C Programming
    Replies: 6
    Last Post: 11-03-2008, 06:14 PM
  2. Music Programming - Serial Matrix Display (Help needed)
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-13-2007, 04:28 PM
  3. Music Programming - Serial Matrix Display
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 04:16 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. OpenGL - Getting a Grip on Perspectives
    By Tonto in forum Game Programming
    Replies: 14
    Last Post: 11-10-2006, 12:00 AM