void getRange( double *pMx, double *pMn, double *pX, int k ) { int i; for (i=0;i<k;i++) { pMx[i]=maxm(pMx[i],pX[i]); pMn[i]=minm(pMn[i],pX[i]); } }
int CreditCardProcessor::updateCCPay(int &pccpayid, ParameterList &pparams) { if (DEBUG) qDebug("updateCCPay(%d, %d params)", pccpayid, pparams.size()); QString sql; XSqlQuery ccq; bool valid; QVariant param; QString r_error; param = pparams.value("r_error", &valid); if (valid) r_error = param.toString(); if (pccpayid > 0) { sql = "UPDATE ccpay SET" "<? if exists(\"fromcurr\") ?>" " ccpay_amount=ROUND(currToCurr(<? value(\"fromcurr\") ?>," " <? value(\"tocurr\") ?>," " <? value(\"amount\") ?>," " CURRENT_DATE), 2)," " ccpay_curr_id=<? value(\"currid\") ?>," "<? else ?>" " ccpay_amount=ROUND(<? value(\"amount\") ?>, 2)," " ccpay_curr_id=<? value(\"currid\") ?>," "<? endif ?>" " ccpay_auth=<? value(\"auth\") ?>," " ccpay_r_approved=<? value(\"approved\") ?>," " ccpay_r_avs=<? value(\"avs\") ?>," " ccpay_r_code=<? value(\"code\") ?>," " ccpay_r_error=<? value(\"error\") ?>," " ccpay_r_message=<? value(\"message\") ?>," " ccpay_r_ordernum=<? value(\"ordernum\") ?>," " ccpay_r_ref=<? value(\"ref\") ?>," "<? if exists(\"shipping\") ?>" " ccpay_r_shipping=<? value(\"shipping\") ?>," "<? endif ?>" "<? if exists(\"score\") ?>" " ccpay_yp_r_score=<? value(\"score\") ?>," "<? endif ?>" "<? if exists(\"tax\") ?>" " ccpay_r_tax=<? value(\"tax\") ?>," "<? endif ?>" "<? if exists(\"tdate\") ?>" " ccpay_yp_r_tdate=<? value(\"tdate\") ?>," "<? endif ?>" "<? if exists(\"time\") ?>" " ccpay_yp_r_time=<? value(\"time\")?>," "<? endif ?>" " ccpay_status=<? value(\"status\") ?>" " WHERE (ccpay_id=<? value(\"ccpay_id\") ?>);" ; } else { ccq.exec("SELECT NEXTVAL('ccpay_ccpay_id_seq') AS ccpay_id;"); if (ccq.first()) pccpayid = ccq.value("ccpay_id").toInt(); else if (ccq.lastError().type() != QSqlError::None && r_error.isEmpty()) { _errorMsg = errorMsg(4).arg(ccq.lastError().databaseText()); return 1; } else if (ccq.lastError().type() == QSqlError::None && r_error.isEmpty()) { _errorMsg = errorMsg(4).arg(errorMsg(2)); return 2; } else // no rows found and YP reported an error { _errorMsg = errorMsg(-12).arg(r_error); return -12; } if (pparams.inList("ordernum") && pparams.value("ordernum").toInt() > 0) { QString maxs("SELECT MAX(COALESCE(ccpay_order_number_seq, -1)) + 1" " AS next_seq " " FROM ccpay " " WHERE (ccpay_order_number=<? value(\"ordernum\") ?>);"); MetaSQLQuery maxm(maxs); ccq = maxm.toQuery(pparams); if (ccq.first()) pparams.append("next_seq", ccq.value("next_seq")); else pparams.append("next_seq", 0); } else pparams.append("next_seq", 0); sql = "INSERT INTO ccpay (" " ccpay_id, ccpay_ccard_id, ccpay_cust_id," " ccpay_type," " ccpay_amount," " ccpay_curr_id," " ccpay_auth, ccpay_auth_charge," " ccpay_order_number," " ccpay_order_number_seq," " ccpay_r_approved, ccpay_r_avs," " ccpay_r_code, ccpay_r_error," " ccpay_r_message, ccpay_r_ordernum," " ccpay_r_ref," "<? if exists(\"score\") ?> ccpay_yp_r_score, <? endif ?>" "<? if exists(\"shipping\") ?> ccpay_r_shipping, <? endif ?>" "<? if exists(\"tax\") ?> ccpay_r_tax, <? endif ?>" "<? if exists(\"tdate\") ?> ccpay_yp_r_tdate, <? endif ?>" "<? if exists(\"time\") ?> ccpay_yp_r_time, <? endif ?>" " ccpay_status" ") SELECT <? value(\"ccpay_id\") ?>, ccard_id, cust_id," " <? value(\"type\") ?>," "<? if exists(\"fromcurr\") ?>" " ROUND(currToCurr(<? value(\"fromcurr\") ?>," " <? value(\"tocurr\") ?>," " <? value(\"amount\") ?>,CURRENT_DATE), 2)," " <? value(\"tocurr\") ?>," "<? else ?>" " ROUND(<? value(\"amount\") ?>, 2)," " <? value(\"currid\") ?>," "<? endif ?>" " <? value(\"auth\") ?>, <? value(\"auth_charge\") ?>," " <? value(\"ordernum\") ?>," " COALESCE(<? value(\"next_seq\") ?>, 1)," " <? value(\"approved\") ?>, <? value(\"avs\") ?>," " <? value(\"code\") ?>, <? value(\"error\") ?>," " <? value(\"message\") ?>, <? value(\"reforder\") ?>," " <? value(\"ref\") ?>," "<? if exists(\"score\") ?> <? value(\"score\") ?>, <? endif ?>" "<? if exists(\"shipping\") ?> <? value(\"shipping\") ?>,<? endif ?>" "<? if exists(\"tax\") ?> <? value(\"tax\") ?>, <? endif ?>" "<? if exists(\"tdate\") ?> <? value(\"tdate\") ?>, <? endif ?>" "<? if exists(\"time\") ?> <? value(\"time\") ?>, <? endif ?>" " <? value(\"status\") ?>" " FROM ccard, custinfo" " WHERE ((ccard_cust_id=cust_id)" " AND (ccard_id=<? value(\"ccard_id\") ?>));"; } pparams.append("ccpay_id", pccpayid); MetaSQLQuery mql(sql); ccq = mql.toQuery(pparams); if (ccq.lastError().type() != QSqlError::None) { pccpayid = -1; _errorMsg = errorMsg(4).arg(ccq.lastError().databaseText()); return 1; } 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 {