static inline void computeDeltas(int ch) { int a; for (a = 0; a < MAX_VOICES; a++) { if (voices[a].isUsed && voices[a].ch == ch) { findDelta(&voices[a], ch, voices[a].note); } } }
int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "%s <fileA> <fileB>\n", argv[0]); fprintf(stderr, " Figures out at which point fileA and fileB are different.\n"); }; uint8_t bufferA[1024]; uint8_t bufferB[1024]; FILE *fpA = fopen(argv[1], "rb"); FILE *fpB = fopen(argv[2], "rb"); int off = 0; while (1) { bufferA[0] = 1; bufferB[1] = 55; fread(bufferA, 1024, 1, fpA); fread(bufferB, 1024, 1, fpB); int at = findDelta(bufferA, bufferB); if (at == -1) { off += 1024; continue; }; int deltaAt = off + at; fprintf(stderr, "Found delta at %d\n", deltaAt); fclose(fpA); fclose(fpB); return 1; }; fclose(fpA); fclose(fpB); printf("No different between the 2 files.\n"); return 0; };
int FederovOptimize( double *X, /* candidate list of dim Nxk, where k is the number of model terms */ double *B, /* Upper triangle of Space matrix, G=X'X/N, for I or NULL if criterion!=2 */ double *BU, /* Product of G and U or NULL if criterion !=2 */ double *proportions, /* Proportions for approximate theory or NULL if not done */ bool RandomStart, /* if TRUE rows will be filled at random to start */ int Nullify, /* if non-zwro nullify() will be used to fill in rows: if 2, some pts will be random */ int criterion, /* 0 for D, 1 for A, 2 for I */ bool evaluateI, /* Return I as well as D and A */ bool doSpace, int augment, /* number of rows for aumentee design*/ double *D, /* D determinant always returned*/ double *A, /* A always returned */ double *I, /* I eff returned only when criterion==2 or evaluateI is true */ double *G, /* G eff always returned*/ double *U, /* U=XTiTi', Nxk or NULL if criterion!=0 */ double *V, /* V=XTi Nxk */ double *T, /* Design reduced, to T,upper triangular kxk */ double *Ti, /* T^-1 */ double *Tip, /* Ti' rearranged so column i of Ti becomes row i of Tip */ double *W, /* working storage */ double *maxmin, /* maximum and minumum ranges. */ dType *d, /* (d_i=U_i*U_i) */ double *vec, /* Temporary storage */ int *designFlag, /* non-zero if row of X is in design Set to 2 for augmentee points */ /* It is used to mark nullfy induced non support points when doAapprox is true. */ int *ttrows, /* Used only in filloutDesign() to avoid duplicate rows when nullify=1 */ int *rows, /* input/output array of indexes of design row numbers from X */ int *trows, /* working array */ int N, /* Number of rows in X */ int n, /* Number of rows in design */ int k, /* Number of model terms */ int maxIteration, int nRepeats, double DFrac, /* fraction of design points included in search */ double CFrac, /* fraction of candidate points included in search */ int *error ) { double delta=0; double maxd; int maxdi; int xold, xnew; int iter=0; int miter; double crit; // double logDcrit; double Acrit; double Icrit=0; double logdet; bool singular; int countSingular=0; int nRepeatCounts=nRepeats; double bestCrit=criterion?1e8:-1e8; int i; int j; int l; int pp; int ka; double norm=(doApprox)?1:(double)n; bool failure; double alpha; double pf; bool firstPass=true; int nd; int np; if (!doSpace && (criterion==2 || evaluateI)) FillInB(X,B,k,N); /* Recommend KFrac=1, LFrac=0 */ /* Use KFrac=0, LFrac=1 to use the two extreme values only */ Klimit=(int)(DFrac*n); /* uses only d values less than Klimit -- if zero, only the smallest is used. */ Llimit=(int)((1-CFrac)*(N-1)); /* uses only d values greater than Llimit -- if N-1, only the largest is used. */ if (maxIteration==0) maxIteration=100; if (doApprox) { n=k; Nullify=1; /* Never start with a random design, since it will too often by */ /* singular when n is exactly k */ } repeat{ firstPass=true; memset((void *)designFlag,0,N*sizeof(int)); if (augment) { for (i=0;i<augment;i++) { designFlag[rows[i]]=2; /* These rows will never be exchanged */ } } if (Nullify) { if (augment<k) { /* Finds k starting rows */ if (!nullify(X,V,augment,rows,designFlag,N,k)) { *error=13; return(0); } } for (i=0;i<N;i++) { ttrows[i]=designFlag[i]; } ka=maxm(k,augment); if (n>ka) { /* Adds rows to make n by choosing points with max variance */ if (Nullify==1) { filloutDesign(T,Ti,Tip,rows,ttrows,X,maxmin,vec,k,ka,n,N,&singular); if (singular) { countSingular++; break; } for (i=0;i<n;i++) trows[i]=rows[i]; } else { /* Adds rows to make n at random */ j=0; for (i=0;i<N;i++) { if (!designFlag[i]) ttrows[j++]=i; } /* add points at random to fill out the design. */ Permute(ttrows,N-k); j=0; l=0; for (i=0;i<N;i++) { if (designFlag[i]) { pp=rows[j]; trows[j++]=pp; } else { pp=ttrows[l]; trows[l++]=pp; } } /* j=n-k; for (i=0;i<k;i++) { trows[j++]=rows[i]; } */ } } else { for(i=0;i<n;i++) trows[i]=rows[i]; } } else if (RandomStart) { /* Just a random selection of rows */ for (i=0;i<N;i++) { trows[i]=i; } Permute(trows,N); } else { if (augment!=0) { /* Augmenting. Add rows at random to the augment rows */ for (i=0;i<augment;i++) { trows[i]=rows[i]; } if (augment<n) { j=augment; for (i=0;i<N;i++) { if (!designFlag[i]) { trows[j++]=i; } } Permute(trows+augment,N-augment); } } else { /* The user has supplied the rows */ for (i=0;i<n;i++) { trows[i]=rows[i]; } } } for (i=0;i<n;i++) { if (!designFlag[trows[i]]) designFlag[trows[i]]=1; /* designFlag will have 2 for augment rows and 1 for others that may */ /* be exchanged. */ } /* The T from nullify() is not scaled by n; hence remake it here. */ reduceDesign(trows,X,T,maxmin,vec,k,n,true,&singular); if (doApprox) { /* Set the initial proportions to 1/k for rows in the design. */ memset((void *)proportions,0,N*sizeof(double)); pf=1.0/(double)k; for (i=0;i<k;i++) { proportions[rows[i]]=pf; } } restart: if (!singular) { logdet=makeTiAndTipFromT(Tip,T,Ti,maxmin,norm,&singular,k); if (singular) goto sing; crit=evaluateCriteria(Tip,Ti,W,B,criterion,evaluateI,&Acrit,&Icrit,logdet,k,N); makeBXd(X,U,V,Ti,Tip,B,BU,criterion,designFlag,d,&maxd,&maxdi,k,N); miter=0; repeat if (doApprox) { delta=findDeltaAlpha(&alpha,BU,criterion,&xnew,maxd,maxdi,Acrit,Icrit,d,U,N,k,&failure); if (failure || delta<((crit>0)?crit*designTol:designTol) || miter>maxIteration) { break; } R_CheckUserInterrupt(); updateA(xnew,proportions,alpha,T,X,vec,k,N); designFlag[xnew]=0; /* The point is a support point, so remove it from the nullify */ /* induced points */ } else { delta=findDelta(BU,criterion,&xold,&xnew,d,U,V,N,n,k,designFlag,trows,&failure); if (failure || delta<crit*designTol|| miter>maxIteration) { break; } R_CheckUserInterrupt(); update(xold,xnew,trows,designFlag,T,X,vec,k,n); } logdet=makeTiAndTipFromT(Tip,T,Ti,maxmin,norm,&singular,k); if (singular) goto sing; crit=evaluateCriteria(Tip,Ti,W,B,criterion,evaluateI,&Acrit,&Icrit,logdet,k,N); makeBXd(X,U,V,Ti,Tip,B,BU,criterion,designFlag,d,&maxd,&maxdi,k,N); miter+=1; forever; if (miter>iter) iter=miter; if (criterion?crit<bestCrit:crit>bestCrit) { bestCrit=crit; if (!doApprox) { for (i=0;i<n;i++) rows[i]=trows[i]; } *D=exp(logdet/(double)k); *A=Acrit; *G=k/(maxd); if (criterion==2 || evaluateI) *I=Icrit; } if (doApprox && firstPass) { /* Remove nullify induced points and run again */ firstPass=false; np=0; nd=0; for (i=0;i<N;i++) { if (proportions[i]>0) np++; if (designFlag[i]>0) nd++; } n=np-nd; if (n>=k) { /* Are there enough non nullify points to support the model? */ for (i=0;i<N;i++) { if (designFlag[i]>0) proportions[i]=0; } memset((void *)designFlag,0,N*sizeof(int)); /* not used again */ j=0; for (i=0;i<N;i++) { if (proportions[i]>0) { trows[j++]=i; proportions[i]=1/(double)n; } } reduceDesign(trows,X,T,maxmin,vec,k,n,true,&singular); goto restart; } } } else {