Need to Calculate the Inverse of A Matrix.

This is a discussion on Need to Calculate the Inverse of A Matrix. within the C Programming forums, part of the General Programming Boards category; I need to calculate the Inverse of A matrix. An nxn matrix using different row operations: Row Interchange: Row i ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    4

    Need to Calculate the Inverse of A Matrix.

    I need to calculate the Inverse of A matrix.
    An nxn matrix using different row operations:

    Row Interchange: Row i is interchanged with Row j in matrix A. We write Ri ↔ Rj .
    Row Scaling: Row i in matrix A is multiplied by a nonzero scaling factor c. We write
    Ri ← cRi.
    Row Addition: Row i in matrix A is replaced by the sum of Row i and a multiple m of
    some row Row j. We write Ri ← Ri + mRj .

    I need help setting up the algorithm..
    So i make a matrix B the Identity Matrix
    And i have to make A into the identity matrix using various row operations.
    and whatever i do to A i have to do to B to get the inverse

    So far I have :
    Code:
    #include<stdio.h>
    
    void printMatrix (char name, double **matrix, int n)
    {
        printf("%c =\n", name);
        int i,j;
        for (i=0; i<n; i++)
        {
            for (j=0; j<n; j++)
            {
                if (matrix[i][j] == -0.0)
                    matrix[i][j] = 0.0;
                printf("  %11.4e", matrix[i][j]);
            }
            printf("\n");
        }
        return;
    }
    
    int main()
    {
    
     
     	int dimension;
    	printf("Enter the dimension of square matrix: \n");
    	scanf("%d",&dimension);
    	printf("Enter the %d entries of the matrix:", dimension*dimension);
     
            scanf("%d %d", &dimension, &dimension);
            // allocate the first dimension (rows)
            int **matrix = (int **)malloc(dimension*sizeof(int *));
    
            // allocate the second dimension (columns)
            int i;
            for (i=0; i<dimension; i++)
            {
                matrix[i] = (int *)malloc(dimension*sizeof(int));
            }
    
    	
    	int k,l;
              for (k=0; k<M; k++)
              {
                  for (l=0; l<N; l++)
                  {
                      scanf("%lf",&table[k][l]);
                  }
              }
    	  
    	  //Set up the identity Matrix 
    	
    	
    	int **matrixB = (int **)malloc(dimension*sizeof(int *));
    	int j;
            for (j=0; j<dimension; j++)
            {
                matrixB[i] = (int *)malloc(dimension*sizeof(int));
    
           }
    	   //Make B the identity Matirix.
    	int m,n;
    	for(m=0;m<=dimension;m++)
    	{
    			for(n=0;n<=dimension;n++)
    			{
    			if(m==n)
    			matrixB[m][m]=1;
    			else
    			matrixB[m][n]=0;
    			}
    	}
    	
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do the names Gauss and Jordan mean anything to you?

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    4
    How do i do a function for the gaussian elimination

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You start at the beginning, and keep going until you get to the end. What do you mean, how do you do a function?

  5. #5
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Use Google --- tons of stuff out there!!

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    4
    What do you mean start at the beginning and finish at the end? I dont know how to write the function. if i knew i wud not ask you tabstop.
    Thanks Kcpilot i looked on google but was very confused..

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quoting the algorithm from Wikipedia:
    Code:
    i := 1
    j := 1
    while (i ≤ m and j ≤ n) do
      Find pivot in column j, starting in row i:
      maxi := i
      for k := i+1 to m do
        if abs(A[k,j]) > abs(A[maxi,j]) then
          maxi := k
        end if
      end for
      if A[maxi,j] ≠ 0 then
        swap rows i and maxi, but do not change the value of i
        Now A[i,j] will contain the old value of A[maxi,j].
        divide each entry in row i by A[i,j]
        Now A[i,j] will have the value 1.
        for u := i+1 to m do
          subtract A[u,j] * row i from row u
          Now A[u,j] will be 0, since A[u,j] - A[i,j] * A[u,j] = A[u,j] - 1 * A[u,j] = 0.
        end for
        i := i + 1
      end if
      j := j + 1
    end while
    You start at the beginning of the algorithm and you go to the end. (Actually, this is Gaussian elimination, not Gauss-Jordan, but it should get you started.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program - inverse of a matrix
    By chaugh in forum C Programming
    Replies: 4
    Last Post: 01-18-2010, 11:00 PM
  2. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 02:30 PM
  3. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  4. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-14-2004, 12:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21