int PSLQ::getZeroComponent(const BigRealVector &y) const { if(m_verbose&VERBOSE_Y) { tcout << _T("y:") << dparam(8) << y << endl; } BigReal minimum, maximum; maximum = minimum = fabs(y[0]); int result = 0; for(UINT i = 1; i < y.getDimension(); i++) { const BigReal tmp = fabs(y[i]); if(tmp < minimum) { minimum = tmp; result = i; } else if(tmp > maximum) { maximum = tmp; } } const BigReal q = rQuot(minimum,maximum,10); if(m_verbose&VERBOSE_DATA) { tcout << _T("min:") << dparam(8) << minimum << _T(" |min/max|:") << dparam(8) << q << _T(" Min Bound:") << dparam(8) << getMinBound() << endl; } if(q < 1e-7) { return result; } return -1; }
static void findIntegerPolynomial(const BigReal &r, int digits, int verbose) { const int xdigits = (int)r.getDecimalDigits()+1; bool solutionFound = false; for(int degree = 2; !solutionFound && (degree <= 30); degree++) { BigRealVector x(degree+1); x[0] = BIGREAL_1; for(int i = 1; i <= degree; i++) { x[i] = rProd(r,x[i-1],xdigits); } if(verbose&VERBOSE_DATA) { tcout << _T("Trying degree ") << iparam(2) << degree << endl; tcout << _T("x:") << dparam(xdigits) << x << endl; } PSLQ pslq(x,digits,verbose); if(pslq.solve(6)) { tcout << _T("Found integer polynomial of degree ") << iparam(2) << degree << _T(".") << _T(" c(0)..c(") << degree << _T("):") << iparam(1) << pslq.getSolution() << endl; solutionFound = true; } else { if(verbose) { tcout << _T("No solution of degree ") << iparam(1) << degree << endl; } } } if(!solutionFound) { tcout << _T("No solution of degree [2..30] found.") << endl; } }
/* This routine integrates the physical values */ void Integrate_CNAM(int Je, double dt, Element_List *U, Element_List *Uf, double **uf) { register int i; int nq; double theta = dparam("THETA"); nq = U->htot*U->nz; dcopy(nq, Uf->base_h, 1, uf[0], 1); /* multiply u^n by theta factor */ dscal(nq, 1.0/(1-theta),U->base_h,1); for(i = 0; i < Je; ++i) daxpy(nq, Beta_Int[i]*dt, uf[i], 1, U->base_h,1); reshuffle(uf,Je); }
static double searchGeom (Point a, Point p, Geometry *g) { Vector ap; double tol = dparam("TOLCURV"), s[3], f[3]; register int ip; /* start the search at the closest point */ ap = setVector (a, p); s[0] = g -> arclen[ip = closest (p, g)]; s[1] = g -> arclen[ip + 1]; bracket (s, f, g, a, ap); if (fabs(f[1]) > tol) brent (s, g, a, ap, tol); return s[1]; }
bool PSLQ::solve(UINT maxDigits) { const BigReal maxNorm = rSqrt(sqr(e(1,maxDigits+1)-1)*m_x.getDimension(),10); const BigRealMatrix H = createH(); m_A = createHermiteReducingMatrix(H); BigRealMatrix AHQ = m_A * H; // N x (N-1)-matrix tcout << _T("Digits in calculation :") << iparam(3) << m_digits << endl << _T("Max number of digits in solution:") << iparam(3) << maxDigits << endl << _T("Max bound :") << dparam(5) << maxNorm << endl; for(;;) { m_minBound = rQuot(BIGREAL_1, getMaxDiagElement(AHQ), 10); if(m_minBound > maxNorm) { return false; // we are out of digits } const int r = findPivotRow(AHQ); // Step 1: Exchange. r = [0..N-2] if(m_verbose&VERBOSE_PIVOT) { tcout << _T("Pivotrow:") << r << endl; } m_A.swapRows(r, r+1); AHQ.swapRows(r, r+1); if(r != m_n - 2) { // Step 2: Corner #ifdef TEST_ROTATION BigRealMatrix AHQTest = AHQ; AHQTest *= RotationMatrix1(AHQTest, r); #endif // TEST_ROTATION AHQ *= RotationMatrix(AHQ, r); } // Step 3: Reduction #ifdef TEST_REDUCEEXACT if(m_verbose&VERBOSE_MATRIX) { const BigRealMatrix D0 = createHermiteReducingMatrix0(AHQ); const BigRealMatrix D0AHQ = D0 * AHQ; tcout << _T("Hermite reducing Matrix D0:") << endl << dparam(8) << D0; tcout << _T("D0*AHQ:") << endl << dparam(8) << D0AHQ; } #endif const BigRealMatrix D = createHermiteReducingMatrix(AHQ); if(m_verbose&VERBOSE_MATRIX) { tcout << _T("Hermite reducing Matrix D:") << endl << iparam(6) << D; } m_A = D * m_A; AHQ = D * AHQ; if(m_verbose&VERBOSE_MATRIX) { tcout << _T("AHQ:") << endl << dparam(8) << AHQ; tcout << _T("A:" ) << endl << iparam(16) << m_A; } // Step 4: check Termination try { const BigRealMatrix Ainv = round(inverse(m_A)); const BigRealVector y = m_x * Ainv; const int z = getZeroComponent(y); if(z >= 0) { if(m_verbose&VERBOSE_INVA) { tcout << _T("inv(A):") << endl << iparam(12) << Ainv; } if(m_verbose&VERBOSE_DATA) { tcout << _T("column:") << z << endl; } m_solution = Ainv.getColumn(z); return true; } } catch(Exception e) { return false; // Occurs when m_A is singular (Exception comes from inverse(m_A)) } } }
static void _php_imap_add_body(Object &ret, BODY *body, bool do_multipart) { if (body->type <= TYPEMAX) { ret.o_set("type", body->type); } if (body->encoding <= ENCMAX) { ret.o_set("encoding", body->encoding); } if (body->subtype) { ret.o_set("ifsubtype", 1); ret.o_set("subtype", String((const char*)body->subtype, CopyString)); } else { ret.o_set("ifsubtype", 0); } if (body->description) { ret.o_set("ifdescription", 1); ret.o_set("description", String((const char*)body->description, CopyString)); } else { ret.o_set("ifdescription", 0); } if (body->id) { ret.o_set("ifid", 1); ret.o_set("id", String((const char*)body->id, CopyString)); } else { ret.o_set("ifid", 0); } if (body->size.lines) { ret.o_set("lines", (int64_t)body->size.lines); } if (body->size.bytes) { ret.o_set("bytes", (int64_t)body->size.bytes); } if (body->disposition.type) { ret.o_set("ifdisposition", 1); ret.o_set("disposition", String((const char*)body->disposition.type, CopyString)); } else { ret.o_set("ifdisposition", 0); } if (body->disposition.parameter) { PARAMETER *dpar; dpar = body->disposition.parameter; ret.o_set("ifdparameters", 1); Array dparametres(Array::Create()); do { Object dparam(SystemLib::AllocStdClassObject()); dparam.o_set("attribute", String((const char*)dpar->attribute, CopyString)); dparam.o_set("value", String((const char*)dpar->value, CopyString)); dparametres.append(dparam); } while ((dpar = dpar->next)); ret.o_set("dparameters", dparametres); } else { ret.o_set("ifdparameters", 0); } PARAMETER *par; Array parametres(Array::Create()); if ((par = body->parameter)) { ret.o_set("ifparameters", 1); do { Object param(SystemLib::AllocStdClassObject()); OBJ_SET_ENTRY(param, par, "attribute", attribute); OBJ_SET_ENTRY(param, par, "value", value); parametres.append(param); } while ((par = par->next)); ret.o_set("parameters", parametres); } else { ret.o_set("ifparameters", 0); } if (do_multipart) { /* multipart message ? */ if (body->type == TYPEMULTIPART) { parametres.clear(); PART *part; for (part = body->nested.part; part; part = part->next) { Object param(SystemLib::AllocStdClassObject()); _php_imap_add_body(param, &part->body, do_multipart); parametres.append(param); } ret.o_set("parts", parametres); } /* encapsulated message ? */ if ((body->type == TYPEMESSAGE) && (!strcasecmp(body->subtype, "rfc822"))) { body = body->nested.msg->body; parametres.clear(); Object param(SystemLib::AllocStdClassObject()); _php_imap_add_body(param, body, do_multipart); parametres.append(param); ret.o_set("parts", parametres); } } }
static void Bsolve_Stokes_PCR(Element_List **V, Bsystem **B, double *p){ int eDIM = V[0]->flist[0]->dim(); const int nsolve = B[eDIM]->nsolve; const int nglobal = B[eDIM]->nglobal; int iter = 0; double tolcg, alpha, beta, eps, Aps, epsfac; static double *u = (double*)0; static double *s = (double*)0; static double *r1 = (double*)0; static double *r2 = (double*)0; static double *Ap = (double*)0; static double *Ar = (double*)0; static double *z = (double*)0, **wk; static int nsol = 0, nglob = 0; if(nsolve > nsol){ if(nsol){ free(u); free(s); free(r1); free(z); free_dmatrix(wk,0,0); } /* Temporary arrays */ u = dvector(0,nsolve-1); /* Solution */ s = dvector(0,nsolve-1); r1 = dvector(0,nsolve-1); /* residual */ z = dvector(0,nsolve-1); /* precondition solution */ if(eDIM == 2) wk = dmatrix(0,1,0,eDIM*4*LGmax); else wk = dmatrix(0,1,0,eDIM*6*LGmax*LGmax); nsol = nsolve; } if(nglobal > nglob){ if(nglob){ free(Ar); free(Ap); free(r2); } Ar = dvector(0,nglobal-1); /* A*r Search direction */ Ap = dvector(0,nglobal-1); /* A*p Search direction */ r2 = dvector(0,nglobal-1); /* residual */ nglob = nglobal; } dzero (B[eDIM]->nglobal, Ap, 1); dzero (B[eDIM]->nglobal, Ar, 1); dzero (B[eDIM]->nglobal, r2, 1); dzero (nsolve, u, 1); dcopy (nsolve, p, 1, r1, 1); tolcg = dparam("TOLCG"); epsfac = 1.0; eps = sqrt(ddot(nsolve,r1,1,r1,1))*epsfac; if (option("verbose") > 1) printf("\t %3d iterations, error = %#14.6g %lg %lg\n", iter, eps/epsfac, epsfac, tolcg); /* =========================================================== * * ---- Conjugate Gradient Iteration ---- * * =========================================================== */ while (eps > tolcg && iter++ < MAX_ITERATIONS ){ if (iter > 1) { /* Update search direction */ A_Stokes(V,B,r2,Ar,wk); beta = -ddot(nsolve,Ar,1,s,1)/Aps; dsvtvp (nsolve,beta,p,1,r2,1,p,1); dsvtvp (nsolve,beta,Ap,1,Ar,1,Ap,1); } else { Precon_Stokes(V[0],B[eDIM],r1,r2); dcopy (nsolve, r2, 1, p, 1); A_Stokes(V,B,p,Ap,wk); } Precon_Stokes(V[0],B[eDIM],Ap,s); Aps = ddot(nsolve,s,1,Ap,1); alpha = ddot(nsolve,r2,1,Ap,1)/Aps; daxpy(nsolve, alpha, p , 1, u, 1); /* Update solution... */ daxpy(nsolve,-alpha, Ap, 1, r1, 1); /* ...and residual */ daxpy(nsolve,-alpha, s , 1, r2, 1); /* ...and residual */ eps = sqrt(ddot(nsolve, r1, 1, r1, 1))*epsfac; /* Compute new L2-error */ fprintf(stdout,"%d %lg %lg\n",iter,eps,sqrt(ddot(nsolve,r2,1,r2,1))); } /* =========================================================== * * End of Loop * * =========================================================== */ /* Save solution and clean up */ dcopy(nsolve,u,1,p,1); if (iter > MAX_ITERATIONS){ error_msg (Bsolve_Stokes_CG failed to converge); } else if (option("verbose") > 1) printf("\t %3d iterations, error = %#14.6g %lg %lg\n", iter, eps/epsfac, epsfac, tolcg); return; }
static void Bsolve_Stokes_PCG(Element_List **V, Bsystem **B, double *p){ int eDIM = V[0]->flist[0]->dim(); const int nsolve = B[eDIM]->nsolve; int iter = 0; double tolcg, alpha, beta, eps, rtz, rtz_old, epsfac; static double *u0 = (double*)0; static double *u = (double*)0; static double *r = (double*)0; static double *w = (double*)0; static double *z = (double*)0, **wk; static int nsol = 0, nglob = 0; if(nsolve > nsol){ if(nsol){ free(u0); free(u); free(r); free(z); free_dmatrix(wk,0,0); } /* Temporary arrays */ u0 = dvector(0,B[eDIM]->nglobal-1);/* intial divergence-free solution */ u = dvector(0,nsolve-1); /* Solution */ r = dvector(0,nsolve-1); /* residual */ z = dvector(0,nsolve-1); /* precondition solution */ if(eDIM == 2) wk = dmatrix(0,1,0,eDIM*4*LGmax); else wk = dmatrix(0,1,0,eDIM*6*LGmax*LGmax); nsol = nsolve; } if(B[eDIM]->nglobal > nglob){ if(nglob) free(w); w = dvector(0,B[eDIM]->nglobal-1); /* A*Search direction */ nglob = B[eDIM]->nglobal; } divergence_free_init(V,u0,p,B,wk); dzero (B[eDIM]->nglobal, w, 1); dzero (nsolve, u, 1); dcopy (nsolve, p, 1, r, 1); tolcg = dparam("TOLCG"); epsfac = 1.0; eps = sqrt(ddot(nsolve,r,1,r,1))*epsfac; if (option("verbose") > 1) printf("\t %3d iterations, error = %#14.6g %lg %lg\n", iter, eps/epsfac, epsfac, tolcg); rtz = eps; /* =========================================================== * * ---- Conjugate Gradient Iteration ---- * * =========================================================== */ while (eps > tolcg && iter++ < MAX_ITERATIONS ){ /* while (sqrt(rtz) > tolcg && iter++ < MAX_ITERATIONS ){*/ Precon_Stokes(V[0],B[eDIM],r,z); rtz = ddot (nsolve, r, 1, z, 1); if (iter > 1) { /* Update search direction */ beta = rtz / rtz_old; dsvtvp(nsolve, beta, p, 1, z, 1, p, 1); } else dcopy(nsolve, z, 1, p, 1); A_Stokes(V,B,p,w,wk); alpha = rtz/ddot(nsolve, p, 1, w, 1); daxpy(nsolve, alpha, p, 1, u, 1); /* Update solution... */ daxpy(nsolve,-alpha, w, 1, r, 1); /* ...and residual */ rtz_old = rtz; eps = sqrt(ddot(nsolve, r, 1, r, 1))*epsfac; /* Compute new L2-error */ fprintf(stdout,"%d %lg %lg\n",iter,eps,sqrt(rtz)); } /* =========================================================== * * End of Loop * * =========================================================== */ /* Save solution and clean up */ dcopy(nsolve,u,1,p,1); /* add back u0 */ dvadd(nsolve,u0,1,p,1,p,1); if (iter > MAX_ITERATIONS){ error_msg (Bsolve_Stokes_CG failed to converge); } else if (option("verbose") > 1) printf("\t %3d iterations, error = %#14.6g %lg %lg\n", iter, eps/epsfac, epsfac, tolcg); return; }