/* GaussFit - A System for Least Squares and Robust Estimation Source Code Copyright (C) 1987 by William H. Jefferys, Michael J. Fitzpatrick and Barbara E. McArthur All Rights Reserved. */ /* Matrix operations to calculate weights and phi vector */ #include #include #include "defines.h" #include "array.h" #include "datum.h" #include "simpledefs.h" #include "prototypes.p" #include "symboltable.h" #include "house.h" #include "files.h" #include "robust.h" #include "alloc.h" int maxdusiz[4] = {0,0,0,0}; int curdusiz[4] = {0,0,0,0}; double *meandu[4]; float *prevdu[4]; char *duwobble[4]; char *wobbleon[4]; extern double envhuber, envtukey, envfair, envtrim; extern int envminsum, envirls, envorm, envprec; extern FILE *fp; extern double tolerance; /* number of residuals */ int iterno=1; matxpose(A,B,p,q) /* transpose p x q matrix A into B */ MATRIX A,B; int p; int q; { int i,j; for(i=0;iMATSIZE) /* max size is MATSIZE */ fatalerror("Matrix Square Root, Order too high\n",""); if(A[0][0] <= 0.0) /* can't do it if diagonal element is <= 0 */ fatalerror("Matrix Square Root Error 0\n",""); A[0][0] = sqrt(A[0][0]); /* 1 x 1 is just square root */ for(i=1;iMATSIZE) fatalerror("Matrix Inverses Order Too Great\n",""); for(i=p-1;i>=0;i--) { for(j=i;j>=0;j--) { sum = deltafn(i,j); /* right hand side is unti matrix */ for(k=j+1;k= MATSIZE) /* if not found and no more room */ fatalerror("No More Room In Matrix Name Table\n","");/*fatal error */ names[*n] = str; /* add name to table */ return (*n)++; /* and return its value */ } covarname(x,y,name) /* concatenate name(x) and name(y) with underscores */ char *x, *y, *name; { strcpy(name,x); /* result is 8 characters long */ strcat(name,"_"); strcat(name,y); } double getsigma(x,y) char *x, *y; { char name[64]; double cov; covarname(x,y,name); /* make the name for the covariance */ if((cov = getdataval(name)) != 0.0) /* look for it in datafile */ { return cov; /* if nonzero, return it */ } if(x!=y) /* if not found, if x!= y then */ { covarname(y,x,name); /* its a covariance, look for it with names in reverse order */ if((cov = getdataval(name)) != 0.0); return cov; } if (iterno == 1) { banner(); iterno = iterno + 1; } return 1.0; /* otherwise its a variance, return 1.0 */ } banner() { fprintf(fp,"\n***********************************"); fprintf(fp,"***********************************\n"); fprintf(fp,"\nNo value given for variance in data table.\n"); fprintf(fp,"Variance assumed equal to 1.0.\n"); fprintf(fp,"Covariance assumed equal to 0.0.\n"); fprintf(fp,"\n***********************************"); fprintf(fp,"***********************************\n\n"); printf("\n***********************************"); printf("***********************************\n"); printf("\nNo value given for variance in data table.\n"); printf("Variance assumed equal to 1.0.\n"); printf("Covariance assumed equal to 0.0.\n"); printf("\n***********************************"); printf("***********************************\n\n"); } int wobblefixon = 0; computewt(m,k,rhs,L) /* compute weight and phi vector */ int m; /* number of conditional equations */ long k[MATSIZE]; /* pointer to equations of condition */ VECTOR rhs; /* phi vector */ MATRIX L; /* weight matrix */ { int i,j,h; MATRIX sigma,fx,sigfxt,U,W,fxt,mx,nx; VECTOR uhat,vhat,temp1,temp2,psizero,duhat,dvhat; MATRIX Q,Qt,R,Rt,D,A,At,B,Bt,C,Ct; char *names[MATSIZE]; int n; long list; extern double SumRho, SumPsi, SumPsiP, SumPsiSq, SumPsiPSq; extern double DeltaV, ScaleFac; double trace[3]; double fxx[MATSIZE][MATSIZE][MATSIZE]; n = 0; for(i=0; i 1.0) /* don't step too far */ duhat[i] = duhat[i]/du; } #endif } else { for(i=0;i*b) return 1; if (*a<*b) return -1; return 0; } initwobblefix() { static int ft = 0; int i, j, k; double ratio; double mediandu; double maxdu; double N, n, X, alf, U, t, xp; double c0, c1, c2, d1, d2, d3; if (ft++) { for (i=0; i<4; ++i) { if (curdusiz[i]) { for (j=0; j= 3) { wobbleon[i][j] = 1; if (!wobblefixon) { printf( "*** Turning ON oscillation damping of residuals for some observations. ***\n"); fprintf(fp, "*** Turning ON oscillation damping of residuals for some observations. ***\n"); wobblefixon = 1; } } } } } } outtahere: curdusiz[0] = 0; curdusiz[1] = 0; curdusiz[2] = 0; curdusiz[3] = 0; } #define MAXDUGROW 50 addmeandu(i, val) int i; double val; { int j,k; double ratio; if (maxdusiz[i] == 0) { maxdusiz[i] = MAXDUGROW; meandu[i] = (double*)MemAlloc("meandu", (long)maxdusiz[i]*sizeof(double)); duwobble[i] = (char*) MemAlloc("duwobble", (long)maxdusiz[i]*sizeof(char)); wobbleon[i] = (char*) MemAlloc("wobbleon", (long)maxdusiz[i]*sizeof(char)); prevdu[i] = (float*) MemAlloc("prevdu", (long)maxdusiz[i]*sizeof(float)); for (k=0; k= maxdusiz[i]) { maxdusiz[i] += MAXDUGROW; meandu[i] = (double*)Reallocate("meandu", (long)maxdusiz[i]*sizeof(double), (long)meandu[i]); duwobble[i] = (char*) Reallocate("duwobble", (long)maxdusiz[i]*sizeof(char), (long)duwobble[i]); wobbleon[i] = (char*) Reallocate("wobbleon", (long)maxdusiz[i]*sizeof(char), (long)wobbleon[i]); prevdu[i] = (float*) Reallocate("prevdu", (long)maxdusiz[i]*sizeof(float), (long)prevdu[i]); for (k=maxdusiz[i]-MAXDUGROW; k -1.1) { duwobble[i][curdusiz[i]]++; /* printf("%1d %3d : ratio1 = %8.2f count = %d\n", i, curdusiz[i], ratio, duwobble[i][curdusiz[i]]); */ } else { duwobble[i][curdusiz[i]] = 0; } } else { duwobble[i][curdusiz[i]] = 0; } prevdu[i][curdusiz[i]] = val; curdusiz[i]++; }