home *** CD-ROM | disk | FTP | other *** search
- # include <math.h>
-
- void gjswap(float *s1,float *s2)
- {
- float temp;
-
- temp = (*s1);
- (*s1) = (*s2);
- (*s2) = temp;
- }
-
- void GaussJordan( float *coefary,float *constary,
- int numcol,float *solcoef, float *invary,
- float *det)
- {
- int pivlst[50][2];
- char pivchk[50];
- int i;
- int j;
- int k;
- int l;
- int lerow;
- int lecol;
- int l1;
- float piv;
- float t;
- float leval;
-
- (*det) = 1;
- for ( i = 0; i <= numcol-1; ++i ) {
- pivchk[i] = 0;
- for ( j = 0; j <= numcol-1; ++j ) {
- invary[i*numcol+j] = coefary[i*numcol+j];
- }
- }
- for ( i = 0; i <= numcol-1; ++i ) {
- leval = 0.0;
- for ( j = 0; j <= numcol-1; ++j ) {
- if ( ! (pivchk[j]) ) {
- for ( k = 0; k <= numcol-1; ++k ) {
- if ( ! (pivchk[k]) ) {
- if ( fabs(invary[j*numcol+k]) > leval ) {
- lerow = j;
- lecol = k;
- leval = fabs(invary[j*numcol+k]);
- }
- }
- }
- }
- }
- pivchk[lecol] = 1;
- pivlst[i][0] = lerow;
- pivlst[i][1] = lecol;
- if ( lerow != lecol ) {
- (*det) = -(*det);
- for ( l = 0; l <= numcol-1; ++l ) {
- gjswap(&invary[lerow*numcol+l],&invary[lecol*numcol+l]);
- }
- gjswap(&constary[lerow],&constary[lecol]);
- }
- piv = invary[lecol*numcol+lecol];
- (*det) = (*det) * piv;
- if ( (*det) > 1.0e+20 ) {
- (*det) = 1.0e+30;
- }
- invary[lecol*numcol+lecol] = 1.0;
- for ( l = 0; l <= numcol-1; ++l ) {
- invary[lecol*numcol+l] = invary[lecol*numcol+l] / piv;
- }
- constary[lecol] = constary[lecol] / piv;
- for ( l1 = 0; l1 <= numcol-1; ++l1 ) {
- if ( l1 != lecol ) {
- t = invary[l1*numcol+lecol];
- invary[l1*numcol+lecol] = 0.0;
- for ( l = 0; l <= numcol-1; ++l ) {
- invary[l1*numcol+l] = invary[l1*numcol+l] -
- invary[lecol*numcol+l] * t;
- }
- constary[l1] = constary[l1] - constary[lecol] * t;
- }
- }
- }
- for ( i = 0; i <= numcol-1; ++i ) {
- l = numcol - i - 1;
- if ( pivlst[l][0] != pivlst[l][1] ) {
- lerow = pivlst[l][0];
- lecol = pivlst[l][1];
- for ( k = 0; k <= numcol-1; ++k ) {
- gjswap(&invary[k*numcol+lerow],&invary[k*numcol+lecol]);
- }
- }
- }
- for ( i = 0; i <= numcol-1; ++i ) {
- solcoef[i] = constary[i];
- }
- }