/* inverseg.c Gausian elimination version that keeps going when singular */ /* this may be called the general inverse, that can return */ /* rows and columns of all zeros */ #include #include #include #define abs(a) ((a)<0?(-(a)):(a)) void inverseg(int n, double A[], double AA[]) { int *ROW, *COL; double *TEMP; int HOLD , I_pivot , J_pivot; double pivot, abs_pivot; int i, j, k, ip, degree; degree = 0; /* degree of singularity */ ROW = (int *)calloc(n, sizeof(int)); COL = (int *)calloc(n, sizeof(int)); TEMP = (double *)calloc(n, sizeof(double)); memcpy(AA,A,n*n*sizeof(double)); /* set up row and column interchange vectors */ for (k=0; k abs_pivot ) { I_pivot = i ; J_pivot = j ; pivot = AA[ROW[i]*n+COL[j]] ; } } } HOLD = ROW[k]; /* could be null interchange, yet may be needed */ ROW[k]= ROW[I_pivot]; ROW[I_pivot] = HOLD ; HOLD = COL[k]; COL[k]= COL[J_pivot]; COL[J_pivot] = HOLD ; if(abs(pivot) < 1.0E-24) /* may be set smaller */ { degree++; for(ip=0; ip0) printf("warning inverseg detected singularity degree %d of %d\n", degree, n); free(ROW); free(COL); free(TEMP); } /* end inverse */