ROW OPERATIONS (finding Inverse) (im going crazy)

This is a discussion on ROW OPERATIONS (finding Inverse) (im going crazy) within the C Programming forums, part of the General Programming Boards category; I developed an algorithm to calculate the solution to equations by augmenting a matrix of coefficients with an identity matrix ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    19

    ROW OPERATIONS (finding Inverse) (im going crazy)

    I developed an algorithm to calculate the solution to equations by augmenting a matrix of coefficients with an identity matrix and then finding the inverse. So:

    [A | I ] ---> [I | A-1 ]

    Everything seems to work well at first. I seperate the rightmost coefficients from matrix A. Then i augment A with I. I use pivoting to make the largest number in the pivotal column the pivot element. I then want to use operations to reduce matrix A to an identity (which would cause I to be the inverse). HOWEVER, the operations don't work properly. Instead of being applied to the whole row, the subtraction only seems to work for the pivot column.

    The code for that screwed up function is:

    Code:
    void row_ops(double a_ext[][10], int m, int n, int pivot_row)
    {				
    		int nn, j, i;
    		double factor;
    		
    		nn = pivot_row;
    		
    		pivot(a_ext, m, n, pivot_row);
    		
    		factor = 1 / (a_ext[pivot_row][pivot_row]);
    		
    		printf("\nTEST FACTOR: %f\n", factor);
    				
    		for(j=0; j<(2*n); j++)
    			a_ext[pivot_row][j] = a_ext[pivot_row][j] * factor;
    								
    		for(i=0; i<m; i++)
    		{	 	
    			if(i != nn)
    			{
    				for(j=0; j<(2*n); j++)
    					a_ext[i][j] = a_ext[i][j] - (a_ext[i][pivot_row] * a_ext[pivot_row][j]);
     	 		}
    			
    			else
    				continue;
    		}
    									   	   	   	   	   	   	   	   	   	   	      	   	   	   	   	   	   	   	      	    	   	   
    		mat_out(a_ext, m, (2*n), "a");
    }
    These are the operations i wish to be done. Row OP one is completed, but Row OP 2 is only applied to (in the following example,) number 2, or element [1][0]. LINK ---

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    This is incorrect, as you could see if you printed out the values that it is actually working on.

    Code:
    a_ext[i][j] = a_ext[i][j] - (a_ext[i][pivot_row] * a_ext[pivot_row][j]);
    Consider pivot_row = 0, and i > 0: (so a_ext[i][pivot_row] is a_ext[i][0])

    After the first time through the inner loop (j = 0), a_ext[i][pivot_row] is equal to zero, so successive times through the inner loop do nothing for the remaining elements on that row.

    What is the loop really supposed to do? You might describe it like this:

    Multiply each element of the (normalized) pivot row by "something" and subtract each product from the corresponding element of row number i. Where that "something" is the thing that will result in a_ext[i][pivot_row] == 0.

    You could try something like this:

    Code:
    int mult;
    .
    .
    .   
       for(i = 0; i < m; i++) {
          if(i != nn) {
            mult = a[i][pr];
            for(j = 0; j < 2*n; j++) {
              a[i][j] = a[i][j] - mult * a[pr][j];
            }
          }
        }
    Where I changed the variable names a little, but you get the idea, I hope.


    D

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, 10:00 PM
  2. Need help in Matrix Addition & finding Inverse of a Matrix
    By ssatyan.129 in forum C Programming
    Replies: 6
    Last Post: 05-15-2009, 02:48 PM
  3. Need to Calculate the Inverse of A Matrix.
    By jordan_230 in forum C Programming
    Replies: 6
    Last Post: 11-03-2008, 05:14 PM
  4. Animation not working....
    By aquinn in forum C Programming
    Replies: 7
    Last Post: 02-19-2005, 04:37 AM
  5. Is there a bug in this part of my algorithm for connect 4?
    By Nutshell in forum Game Programming
    Replies: 8
    Last Post: 04-28-2002, 01:58 AM

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