# Thread: ROW OPERATIONS (finding Inverse) (im going crazy)

1. ## 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. 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