#include #include #include #include #include #define MAX 20 //************ complex number handling ******************** typedef struct { double r,i; }doublecomplex; #define COMPLEXMAG(x,y,z) z=sqrt( pow( x,2) + pow(y,2) ); #define COMPLEXMAG2(x,y,z) z= ( pow( x,2) + pow(y,2) ); #define COMPLEXRECIP(ar, ai, xr, xi) \ { \ xr= ar/( pow( ar,2) + pow(ai,2) ); \ xi= -ai/( pow( ar,2) + pow(ai,2) ); \ } \ #define COMPLEXMUL(ar, ai, br, bi,cr,ci) \ { \ cr= ar*br -ai*bi; \ ci= ar*bi +ai*br; \ } \ #define COMPLEXDIV(nr, ni, dr, di,cr,ci) \ { \ cr = nr*dr +ni*di; \ cr/= ( pow( dr,2) + pow(di,2) ); \ ci = ni*dr -nr*di; \ ci/= ( pow( dr,2) + pow(di,2) ); \ } \ //********************************************************* void dataentry(void); void reserve(void); void initialize(void); void inputdim(void); void printresults(void); void erroranalysis(void); void deter(long *nd, doublecomplex w[][MAX], double *ddetr,double *ddeti); double ah; double aq; double aw; doublecomplex aa,az,ba,c,temp; doublecomplex a[MAX][MAX],b[MAX][MAX],x[MAX][MAX]; doublecomplex ac[MAX][MAX],det; long i,ii,ik,imethod; long j,jj,k,kk,n,nv; char filename[5][FILENAME_MAX]; FILE *pFile[5]; // Variables // // n is the dimension of the input matrix. // // a is the input matrix. // // x is initialized as the identity matrix. // It is transformed to the inverse matrix. // // b is the product of matrix a and the inverse matrix x. // It should equal the identity matrix. // void main() { printf("\n\n Inversion of a Complex Matrix via Gaussian Elimination with Partial Pivoting. \n"); printf("\n Version 1.0, May 17, 2002 \n"); printf("\n by Tom Irvine "); printf("\n Email: tomirvine@aol.com \n"); inputdim(); initialize(); dataentry(); reserve(); deter(&n,a,&det.r,&det.i); if((fabs(det.r)<= 1.0e-200) && (fabs(det.i)<= 1.0e-200) ) { printf("\n\n Error: determinant of input matrix = (%12.5e , %12.5e)\n",det.r,det.i); exit(1); } else { printf("\n\n Determinant of input matrix = (%12.5e , %12.5e)\n",det.r,det.i); } /****** begin forward elimination *********/ for(i=0;i=0;i=i-1) { for(k=i+1;k20) { printf("\n Error: maximum dimension = 20 \n"); exit(1); } if(n<2) { printf("\n Error: minimum dimension = 2 \n"); exit(1); } printf("\n\n Select data entry method: 1=keyboard 2=input file \n"); scanf("%ld",&imethod); } void printresults(void) { strcpy(filename[2],"inv_data.out"); pFile[2]=fopen(filename[2],"w"); if(n<=6) { printf("\n\n Inverse Matrix \n\n"); for(i=0; i