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("\n ArrayX[%i][%i] is %f", 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("\n Array is [%i][%i], %f", 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("\n\n Array is [%i][%i], %f", m, k, arrayX[m][k]);
}
/* loop through the rows of the working matrix */
for (n=0; n<rowR; n++)
{
printf("\n Going through 2nd for loop section\n"
;
/*maxx = -1.0;*/
/*for(k=n; k<rowR; k++)*/
/*if ( (fabs (arrayX[k][n])) > maxx )*/
/*{*/
/*maxx = fabs(arrayX[k][n]);*/
/*swap = k;*/
/*}*/
/*printf("mx %f k is %d n is %d\n", arrayX[k][n], k, n);*/
/* check if pivot element is "large enough" */
/*if( maxx <= tol )*/
/*{*/
/*printf(" max = %f tol=%f need tol to be smaller \n", maxx, tol);*/
/*return (FALSE);*/
/*}*/
/*printf(" After the return FALSE statement\n"
;*/
/* 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(" Doing the performing elimintation bit \n"
;*/
/*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("Results = %d", 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("\t"
;
for ( j = 0; j < n; j++ )
printf( "%f\t ", mq[j]);
printf("what is this?"
;
putchar( '\n' );
}
}
[sig][/sig]
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("\n ArrayX[%i][%i] is %f", 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("\n Array is [%i][%i], %f", 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("\n\n Array is [%i][%i], %f", m, k, arrayX[m][k]);
}
/* loop through the rows of the working matrix */
for (n=0; n<rowR; n++)
{
printf("\n Going through 2nd for loop section\n"
/*maxx = -1.0;*/
/*for(k=n; k<rowR; k++)*/
/*if ( (fabs (arrayX[k][n])) > maxx )*/
/*{*/
/*maxx = fabs(arrayX[k][n]);*/
/*swap = k;*/
/*}*/
/*printf("mx %f k is %d n is %d\n", arrayX[k][n], k, n);*/
/* check if pivot element is "large enough" */
/*if( maxx <= tol )*/
/*{*/
/*printf(" max = %f tol=%f need tol to be smaller \n", maxx, tol);*/
/*return (FALSE);*/
/*}*/
/*printf(" After the return FALSE statement\n"
/* 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(" Doing the performing elimintation bit \n"
/*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("Results = %d", 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("\t"
for ( j = 0; j < n; j++ )
printf( "%f\t ", mq[j]);
printf("what is this?"
putchar( '\n' );
}
}
[sig][/sig]