# Thread: Need to Calculate the Inverse of A Matrix.

1. ## 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. Do the names Gauss and Jordan mean anything to you?

3. How do i do a function for the gaussian elimination

4. 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. Use Google --- tons of stuff out there!!

6. 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. 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.)