Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inverse matrix using Gauss-Jordan elimination

Status
Not open for further replies.

Uckfield

Programmer
Aug 5, 2000
3
0
0
GB
This if a function that belongs to a large program. This funciton does not work, and we can not work out why.
If anyone could let us know why it doesn't work it would be great...
the problem is when displaying the actual inverse matrix, it displays an empty matrix formed by 0's.

TIA
Dave

It does not work when it is not commented, the comments are just a way of checking what is going on.


/* implementation of inverse matrix using gauss-Jordan elimination with partial pivoting*/

int gauss_jordan (double Result [max][max])
{
double mm, maxx, tol, tempor;
int n,m, nj, id=0, l=0, k=0, swap, gaussi;
float arrayX[max][max], temp[max][max];

tol = 0.0001; /* THIS IS MADE UP */

printf("Which matrix would you like to find the inverse of\n");
scanf("%d", &gaussi);

if (gaussi == 1) {rowR=rowA; colR=colA;}
if (gaussi == 2) {rowR=rowB; colR=colB;}

/* set up working matrix */
nj=2*n;
printf("after rowR bit\n");

for (n=0; n<rowR; n++)
{
for (m=0; m<colR; m++)
{
if (gaussi == 1)
arrayX[n][m] = arrayA[n][m];
if (gaussi == 2)
arrayX[n][m] = arrayB[n][m];
printf(&quot;\n ArrayX[%i][%i] is %f&quot;, n, m, arrayX[n][m]);

}

for (m=colR; m<(2*colR); m++)
{
if ((m-colR) == n)
arrayX[n][m] = 1;
else if ((m-colR) != n)
arrayX[n][m] = 0;
printf(&quot;\n Array is [%i][%i], %f&quot;, n, m, arrayX[n][m]);
}
}


/* Loop through the rows of the working matrix */
for (n=0; n<rowR; n++)
/* Perform elimination */
for (m=0; m<colR; m++)
if (m != n) {
id = arrayX[m][n]/arrayX[n][n];
for(k=n; k<=colR; k++)
arrayX[m][k] -= id*arrayX[n][k];
printf(&quot;\n\n Array is [%i][%i], %f&quot;, m, k, arrayX[m][k]);
}




/* loop through the rows of the working matrix */

for (n=0; n<rowR; n++)
{
printf(&quot;\n Going through 2nd for loop section\n&quot;);
/*maxx = -1.0;*/
/*for(k=n; k<rowR; k++)*/
/*if ( (fabs (arrayX[k][n])) > maxx )*/
/*{*/
/*maxx = fabs(arrayX[k][n]);*/
/*swap = k;*/
/*}*/

/*printf(&quot;mx %f k is %d n is %d\n&quot;, arrayX[k][n], k, n);*/
/* check if pivot element is &quot;large enough&quot; */

/*if( maxx <= tol )*/
/*{*/
/*printf(&quot; max = %f tol=%f need tol to be smaller \n&quot;, maxx, tol);*/
/*return (FALSE);*/
/*}*/


/*printf(&quot; After the return FALSE statement\n&quot;);*/
/* swap rows */
/*if (swap != n)*/
/*for (k=n; k<=rowR; k++)*/
/*{*/
/*tempor = arrayX[n][k];*/
/*arrayX[n][k] = arrayX[swap][k];*/
/*arrayX[swap][k]=tempor;*/
/*}*/


/* perform elimination */


/*printf(&quot; Doing the performing elimintation bit \n&quot;);*/
/*for (m=0; m<n; m++)*/
/*if (m != n)*/
/*{*/
/* mm = arrayX[m][n] / arrayX[n][n];*/
/*for (k=n; k<=rowR; k++)*/
/*arrayX[m][k] -= mm*arrayX[n][k];*/
/* }*/
/*} */

/* compute inverse dividing by diagonal */

for (n=0; n<rowR; n++)
{
for (m=rowR; m< (2*rowR-1); m++)
{
temp[n][m] = arrayX[n][m] / arrayX[n][n];
}
for (k=0; k<rowR; k++)
Result[n][k]= temp[n][m];
}





/*printf(&quot;Results = %d&quot;, Result[0][1]);*/
for (n=0; n<rowR; n++)
for (m=0; m<colR; m++)
arrayR[n][m] = Result[n][m];

t=3;
rowR=rowR+2;
colR=colR+2;
DISPLAY(t, arrayA, arrayB, arrayR);

}


void printmatrix( int mq[max][max], int n, int k )

{
int i, j;
for ( i = 0; i < k; i++ )
{
printf(&quot;\t&quot;);
for ( j = 0; j < n; j++ )
printf( &quot;%f\t &quot;, mq[j]);
printf(&quot;what is this?&quot;);
putchar( '\n' );
}
}
[sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top