CacheLocality CacheLocality::readFromSysfs() { return readFromSysfsTree([](std::string name) { std::ifstream xi(name.c_str()); std::string rv; std::getline(xi, rv); return rv; }); }
Real Qg1dLocalVolModel::xi(const Real t, const Real T0, const std::vector<Real> &fixedTimes, const std::vector<Real> &taus, const Handle<YieldTermStructure> &yts, const Real s) const { std::vector<Real> tmp(1, s); return xi(t, T0, fixedTimes, taus, yts, tmp)[0]; }
bool AztecOOSolver::solve() { #ifdef HAVE_AZTECOO assert(m.size == rhs.size); // no output aztec.SetAztecOption(AZ_output, AZ_none); // AZ_all | AZ_warnings | AZ_last | AZ_summary #ifndef COMPLEX // setup the problem aztec.SetUserMatrix(m.mat); aztec.SetRHS(rhs.vec); Epetra_Vector x(*rhs.std_map); aztec.SetLHS(&x); if (pc != NULL) { Epetra_Operator *op = pc->get_obj(); assert(op != NULL); // can work only with Epetra_Operators aztec.SetPrecOperator(op); } // solve it aztec.Iterate(max_iters, tolerance); delete [] sln; sln = new scalar[m.size]; memset(sln, 0, m.size * sizeof(scalar)); // copy the solution into sln vector for (int i = 0; i < m.size; i++) sln[i] = x[i]; #else double c0r = 1.0, c0i = 0.0; double c1r = 0.0, c1i = 1.0; Epetra_Vector xr(*rhs.std_map); Epetra_Vector xi(*rhs.std_map); Komplex_LinearProblem kp(c0r, c0i, *m.mat, c1r, c1i, *m.mat_im, xr, xi, *rhs.vec, *rhs.vec_im); Epetra_LinearProblem *lp = kp.KomplexProblem(); aztec.SetProblem(*lp); // solve it aztec.Iterate(max_iters, tolerance); kp.ExtractSolution(xr, xi); delete [] sln; sln = new scalar[m.size]; memset(sln, 0, m.size * sizeof(scalar)); // copy the solution into sln vector for (int i = 0; i < m.size; i++) sln[i] = scalar(xr[i], xi[i]); #endif return true; #else return false; #endif }
MasterCoord Element::to_master(const Coord &x) const { // Use Newton's method to solve from_master(xi) - x = 0 for xi. // xi -> xi + J^-1 * (x - from_master(xi)) #if DIM == 2 MasterCoord xi(0, 0); #elif DIM == 3 MasterCoord xi(0, 0, 0); #endif Coord dx = x - from_master(xi); int iter = 0; do { // Newton iteration double jac[DIM][DIM]; // use simple matrix repr. for 2x2 for(SpaceIndex i=0; i<DIM; ++i) for(SpaceIndex j=0; j<DIM; ++j) jac[i][j] = jacobian(i, j, xi); #if DIM==2 double dj = jac[0][0]*jac[1][1] - jac[0][1]*jac[1][0]; dj = 1./dj; // xi --> xi + J^-1*(x - from_master(xi)) xi(0) += ( jac[1][1]*dx(0) - jac[0][1]*dx(1))*dj; xi(1) += (-jac[1][0]*dx(0) + jac[0][0]*dx(1))*dj; #elif DIM==3 double invjac[DIM][DIM]; vtkMath::Invert3x3(jac,invjac); // xi --> xi + J^-1*(x - from_master(xi)) xi(0) += ( invjac[0][0]*dx(0) + invjac[0][1]*dx(1) + invjac[0][2]*dx(2) ); xi(1) += ( invjac[1][0]*dx(0) + invjac[1][1]*dx(1) + invjac[1][2]*dx(2) ); xi(2) += ( invjac[2][0]*dx(0) + invjac[2][1]*dx(1) + invjac[2][2]*dx(2) ); #endif // DIM==3 dx = x - from_master(xi); // reevaluate rhs } while((norm2(dx) > tolerancesq) && (++iter < maxiter)); return xi; }
bool Path::PointInside(const Objects & objects, const Vec2 & pt, bool debug) const { vector<Vec2> x_ints; vector<Vec2> y_ints; for (unsigned i = m_start; i <= m_end; ++i) { Bezier bez(objects.beziers[objects.data_indices[i]].ToAbsolute(objects.bounds[i])); vector<Vec2> xi(bez.SolveX(pt.x)); vector<Vec2> yi(bez.SolveY(pt.y)); x_ints.insert(x_ints.end(), xi.begin(), xi.end()); y_ints.insert(y_ints.end(), yi.begin(), yi.end()); } //Debug("Solved for intersections"); unsigned bigger = 0; unsigned smaller = 0; for (unsigned i = 0; i < x_ints.size(); ++i) { if (debug) Debug("X Intersection %u at %f,%f vs %f,%f", i,Double(x_ints[i].x), Double(x_ints[i].y), Double(pt.x), Double(pt.y)); if (x_ints[i].y >= pt.y) { ++bigger; } } smaller = x_ints.size() - bigger; if (debug) { Debug("%u horizontal, %u bigger, %u smaller", x_ints.size(), bigger, smaller); } if (smaller % 2 == 0 || bigger % 2 == 0) return false; bigger = 0; smaller = 0; for (unsigned i = 0; i < y_ints.size(); ++i) { if (debug) Debug("Y Intersection %u at %f,%f vs %f,%f", i,Double(y_ints[i].x), Double(y_ints[i].y), Double(pt.x), Double(pt.y)); if (y_ints[i].x >= pt.x) { ++bigger; } } smaller = y_ints.size() - bigger; if (debug) { Debug("%u vertical, %u bigger, %u smaller", y_ints.size(), bigger, smaller); } if (smaller % 2 == 0 || bigger % 2 == 0) return false; return true; }
XiFunc RealModel::GetXi() { const int n1 = 512; const int n2 = 256; const double r1 = 200; const double r2 = 20000; vector<double> r = QSpline::MakeArray(n1, n2, r1, r2); vector<double> xi(n1+n2); ComputeXiLM(0, 2, pk, n1+n2, &r[0], &xi[0]); return XiFunc(new RealSpaceXi(QSpline(n1, n2, r, xi))); }
void aplus_main(long argc, char** argv) { #if !defined(_INTERPRETER_ONLY) extern void AplusLoop(); #endif I i; /* the number of arguments parsed */ #if !defined(_INTERPRETER_ONLY) dapinit(); #endif initReleaseData(_releaseCode); initVersion(); initDefaultATREE(argv[0]); initCallouts(); i = parseargs(argc, argv); if( !_quiet ) printId(); atmpinit(); envinit(); if(0!=_megsforheap)setk1(_megsforheap); /* must be before mem init! */ ai(_workarea); /* initialize */ if(_enable_coredump) { setSigv(1); setSigb(1); coreLimSet(aplusInfinity); } versSet(_version); releaseCodeSet(_releaseCode); phaseOfReleaseSet(_phaseOfRelease); majorReleaseSet(_majorRelease); minorReleaseSet(_minorRelease); startupSyslog(_version); xi(argv[0]); /* installation */ argvInstall(argc, argv, i); /* set up _argv */ uextInstall(); /* user lib install */ #if !defined(_INTERPRETER_ONLY) AplusLoop(argc, argv, i); #else if (i < argc && argv[i] && *argv[i]) loadafile(argv[i],0); if (Tf) pr(); while(1) getm(); #endif /********************************************************************** These functions are moved to AplusLoop if (i < argc && argv[i] && *argv[i]) loadafile(argv[i],0); / * load script * / if (Tf) pr(); / * initial prompt * / **********************************************************************/ }
int main() { clock_t start, finish; double duration; start = clock(); for (int i = 0; i <= M; ++i) Told[i] = 1 + (sigma - 1) * (xi(i) + L) / (2*L); d[0] = 1; du[0] = 0; d[M] = 1; dl[M-1] = 0; b[0] = 1; b[M] = sigma; for (int i = 1; i < M; ++i) { dl[i-1] = 1/(h*h) - v(alpha, xi(i))/(2*h); du[i] = 1/(h*h) + v(alpha, xi(i))/(2*h); d[i] = -2/(h*h); b[i] = -K*w(beta, Told[i]); } finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; std::cout << "matrix construction time: " << duration << " sec\n"; start = clock(); lapack_int info = LAPACKE_dgtsv(LAPACK_COL_MAJOR, Neq, 1, dl, d, du, b, Neq); if (info != 0) { std::cout << "Error when solving a linear system, info = " << info << std::endl; exit(1); } for (int i = 0; i <= M; ++i) T[i] = b[i]; finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; std::cout << "system solving time: " << duration << " sec\n"; return 0; }
//Initialisierung Eigen::VectorXcd init(double xiMax, double xiMin, double dXi) { int n_max = floor((xiMax - xiMin + 3/2*dXi)/dXi); double summe = 0; double skal = 0; Eigen::VectorXcd xi(n_max); std::complex<double> c; for (int i = 0; i < n_max; i++) { c = Psi((i-floor(n_max/2))*dXi,1,1); xi(i) = c; } //Numerische Normierung des Anfangszustandes auf 1 summe = Norm(xi); skal = sqrt(1./summe); xi = skal * xi; //Anfangszustand in Datei schreiben SchreibeZustand(xi, "A1c"); return xi; }
/// %Test whether \a x is solution virtual bool solution(const SetAssignment& x) const { int max = Gecode::Set::Limits::min - 1; for (int i=0; i<4; i++) { CountableSetRanges xir(x.lub, x[i]); IntSet xi(xir); if (xi.ranges() > 0) { int oldMax = max; max = xi.max(); if (xi.min() <= oldMax) return false; } } return true; }
bool OdeErrMaxabs(void) { bool ok = true; // initial return value CppAD::vector<double> w(2); w[0] = 10.; w[1] = 1.; Method method(w); CppAD::vector<double> xi(2); xi[0] = 1.; xi[1] = 0.; CppAD::vector<double> eabs(2); eabs[0] = 0.; eabs[1] = 0.; CppAD::vector<double> ef(2); CppAD::vector<double> xf(2); CppAD::vector<double> maxabs(2); double ti = 0.; double tf = 1.; double smin = .5; double smax = 1.; double scur = .5; double erel = 1e-4; bool accurate = false; while( ! accurate ) { xf = OdeErrControl(method, ti, tf, xi, smin, smax, scur, eabs, erel, ef, maxabs); accurate = true; size_t i; for(i = 0; i < 2; i++) accurate &= ef[i] <= erel * maxabs[i]; if( ! accurate ) smin = smin / 2; } double x0 = exp(-w[0]*tf); ok &= CppAD::NearEqual(x0, xf[0], erel, 0.); ok &= CppAD::NearEqual(0., ef[0], erel, erel); double x1 = w[0] * (exp(-w[0]*tf) - exp(-w[1]*tf))/(w[1] - w[0]); ok &= CppAD::NearEqual(x1, xf[1], erel, 0.); ok &= CppAD::NearEqual(0., ef[1], erel, erel); return ok; }
virtual void gradient(Vector<Real> &g, const Vector<Real> &x, Real &tol) { g.zero(); // split x const Vector_SimOpt<Real> &xuz = Teuchos::dyn_cast<const Vector_SimOpt<Real> >(x); Teuchos::RCP<const Vector<Real> > xuptr = xuz.get_1(); Teuchos::RCP<const Vector<Real> > xzptr = xuz.get_2(); const SimulatedVector<Real> &pxu = Teuchos::dyn_cast<const SimulatedVector<Real> >(*xuptr); const RiskVector<Real> &rxz = Teuchos::dyn_cast<const RiskVector<Real> >(*xzptr); Real xt = rxz.getStatistic(0); Teuchos::RCP<const Vector<Real> > xz = rxz.getVector(); // split g Vector_SimOpt<Real> &guz = Teuchos::dyn_cast<Vector_SimOpt<Real> >(g); Teuchos::RCP<Vector<Real> > guptr = guz.get_1(); Teuchos::RCP<Vector<Real> > gzptr = guz.get_2(); SimulatedVector<Real> &pgu = Teuchos::dyn_cast<SimulatedVector<Real> >(*guptr); RiskVector<Real> &rgz = Teuchos::dyn_cast<RiskVector<Real> >(*gzptr); Teuchos::RCP<Vector<Real> > gz = rgz.getVector(); std::vector<Real> param; Real weight(0), one(1), sum(0), tmpsum(0), tmpval(0), tmpplus(0); //Teuchos::RCP<Vector<Real> > tmp1 = gzptr->clone(); //Teuchos::RCP<Vector<Real> > tmp2 = gzptr->clone(); Teuchos::RCP<Vector<Real> > tmp1 = gz->clone(); Teuchos::RCP<Vector<Real> > tmp2 = gz->clone(); for (typename std::vector<SimulatedVector<Real> >::size_type i=0; i<pgu.numVectors(); ++i) { param = sampler_->getMyPoint(static_cast<int>(i)); weight = sampler_->getMyWeight(static_cast<int>(i)); pobj_->setParameter(param); //tmpval = pobj_->value(*(pxu.get(i)), *xzptr, tol); tmpval = pobj_->value(*(pxu.get(i)), *xz, tol); tmpplus = pfunc_->evaluate(tmpval-xt, 1); tmpsum += weight*tmpplus; //Vector_SimOpt<Real> xi(Teuchos::rcp_const_cast<Vector<Real> >(pxu.get(i)), Teuchos::rcp_const_cast<Vector<Real> >(xzptr)); Vector_SimOpt<Real> xi(Teuchos::rcp_const_cast<Vector<Real> >(pxu.get(i)), Teuchos::rcp_const_cast<Vector<Real> >(xz)); Vector_SimOpt<Real> gi(pgu.get(i), tmp1); pobj_->gradient(gi, xi, tol); gi.scale(weight*tmpplus); tmp2->plus(*tmp1); pgu.get(i)->scale(one/(one-alpha_)); } //sampler_->sumAll(*tmp2, *gzptr); //gzptr->scale(one/(one-alpha_)); sampler_->sumAll(*tmp2, *gz); gz->scale(one/(one-alpha_)); sampler_->sumAll(&tmpsum, &sum, 1); rgz.setStatistic(one - (one/(one-alpha_))*sum); }
bool OdeErrControl_three(void) { bool ok = true; // initial return value double alpha = 10.; Method_three method(alpha); CppAD::vector<double> xi(2); xi[0] = 1.; xi[1] = 0.; CppAD::vector<double> eabs(2); eabs[0] = 1e-4; eabs[1] = 1e-4; // inputs double ti = 0.; double tf = 1.; double smin = 1e-4; double smax = 1.; double scur = 1.; double erel = 0.; // outputs CppAD::vector<double> ef(2); CppAD::vector<double> xf(2); CppAD::vector<double> maxabs(2); size_t nstep; xf = OdeErrControl(method, ti, tf, xi, smin, smax, scur, eabs, erel, ef, maxabs, nstep); double x0 = exp( alpha * tf * tf ); ok &= CppAD::NearEqual(x0, xf[0], 1e-4, 1e-4); ok &= CppAD::NearEqual(0., ef[0], 1e-4, 1e-4); double root_pi = sqrt( 4. * atan(1.)); double root_alpha = sqrt( alpha ); double x1 = CppAD::erf(alpha * tf) * root_pi / (2 * root_alpha); ok &= CppAD::NearEqual(x1, xf[1], 1e-4, 1e-4); ok &= CppAD::NearEqual(0., ef[1], 1e-4, 1e-4); ok &= method.F.was_negative(); return ok; }
Double80 BigReal::getDouble80NoLimitCheck() const { static const int minExpo2 = 64-16382; static const int maxExpo2 = 0x3fff; DigitPool *pool = getDigitPool(); const BRExpoType ee2 = getExpo2(*this); const BRExpoType expo2 = 64 - ee2; bool e2Overflow; Double80 e2, e2x; BigReal xi(pool); if(expo2 <= minExpo2) { e2 = Double80::pow2(minExpo2); e2Overflow = false; xi.shortProduct(::cut(*this,21), pow2(minExpo2, CONVERSION_POW2DIGITCOUNT), BIGREAL_ESCEXPO); // BigReal multiplication } else if(expo2 >= maxExpo2) { e2 = Double80::pow2(maxExpo2); e2x = Double80::pow2((int)expo2 - maxExpo2); e2Overflow = true; xi.shortProduct(::cut(*this,21), pow2((int)expo2, CONVERSION_POW2DIGITCOUNT), BIGREAL_ESCEXPO); // BigReal multiplication } else { e2 = Double80::pow2((int)expo2); e2Overflow = false; xi = round(xi.shortProduct(::cut(*this,22), pow2((int)expo2, CONVERSION_POW2DIGITCOUNT), -1)); // BigReal multiplication } const Digit *p = xi.m_first; if(p == NULL) { return Double80::zero; } Double80 result = (INT64)p->n; int digitCount = LOG10_BIGREALBASE; for(p = p->next; p && (digitCount < 24); p = p->next, digitCount += LOG10_BIGREALBASE) { result *= BIGREALBASE; result += (INT64)p->n; } const BRExpoType e = xi.m_expo * LOG10_BIGREALBASE - digitCount + LOG10_BIGREALBASE; if(e > 0) { result *= Double80::pow10((int)e); } else if(e < 0) { result /= Double80::pow10(-(int)e); } result /= e2; if(e2Overflow) { result /= e2x; } return isNegative() ? -result : result; }
double ScatteredPointInterpolator::Height(const Vector2d& vPos) { // m: dimension of input vector // 2: the x and y values // size_t m = 2; size_t k = m_vecPoints.size(); double dHeight = 0.0; for (size_t i=0; i<k; i++) { Vector2d xi(m_vecPoints[i].X(), m_vecPoints[i].Y()); double dDist = Distance(vPos, xi); dHeight += m_vecWeights[i] * Gaussian(dDist); } return dHeight; }
Disposable<std::vector<Real> > Qg1dLocalVolModel::phi( const Real t, const std::vector<Real> &s, const Real T0, const std::vector<Real> &fixedTimes, const std::vector<Real> &taus, const Handle<YieldTermStructure> &yts, bool numericalInversion) const { std::vector<Real> x(s.size()); if (numericalInversion) { for (Size i = 0; i < x.size(); ++i) x[i] = sInvX(t, T0, fixedTimes, taus, yts, s[i]); } else { x = xi(t, T0, fixedTimes, taus, yts, s); } Real y = yApprox(t); std::vector<Real> tmp(x.size()); for (Size i = 0; i < tmp.size(); ++i) tmp[i] = dSwapRateDx(T0, t, fixedTimes, taus, x[i], y, yts) * sigma_f(t, t, x[i], y); return tmp; }
XiFunc RealModel::GetXiDeriv(int n) { assert(0 <= n && n < Nbands); const int Nk = 8192; double kmin = bands[n].min; double kmax = bands[n].max; const int n1 = 512; const int n2 = 256; const double r1 = 200; const double r2 = 20000; vector<double> r = QSpline::MakeArray(n1, n2, r1, r2); vector<double> xi(n1+n2); Spline one = ConstantSpline(1, kmin, kmax); ComputeXiLM(0, 2, one, n1+n2, &r[0], &xi[0], Nk, kmin, kmax); return XiFunc(new RealSpaceXi(QSpline(n1, n2, r, xi))); }
bool OdeErrControl_two(void) { bool ok = true; // initial return value CppAD::vector<double> w(2); w[0] = 10.; w[1] = 1.; Method_two method(w); CppAD::vector<double> xi(2); xi[0] = 1.; xi[1] = 0.; CppAD::vector<double> eabs(2); eabs[0] = 1e-4; eabs[1] = 1e-4; // inputs double ti = 0.; double tf = 1.; double smin = 1e-4; double smax = 1.; double scur = .5; double erel = 0.; // outputs CppAD::vector<double> ef(2); CppAD::vector<double> xf(2); CppAD::vector<double> maxabs(2); size_t nstep; xf = OdeErrControl(method, ti, tf, xi, smin, smax, scur, eabs, erel, ef, maxabs, nstep); double x0 = exp(-w[0]*tf); ok &= CppAD::NearEqual(x0, xf[0], 1e-4, 1e-4); ok &= CppAD::NearEqual(0., ef[0], 1e-4, 1e-4); double x1 = w[0] * (exp(-w[0]*tf) - exp(-w[1]*tf))/(w[1] - w[0]); ok &= CppAD::NearEqual(x1, xf[1], 1e-4, 1e-4); ok &= CppAD::NearEqual(0., ef[1], 1e-4, 1e-4); return ok; }
bool OdeGearControl(void) { bool ok = true; // initial return value CPPAD_TEST_VECTOR<double> w(2); w[0] = 10.; w[1] = 1.; Fun F(w); CPPAD_TEST_VECTOR<double> xi(2); xi[0] = 1.; xi[1] = 0.; CPPAD_TEST_VECTOR<double> eabs(2); eabs[0] = 1e-4; eabs[1] = 1e-4; // return values CPPAD_TEST_VECTOR<double> ef(2); CPPAD_TEST_VECTOR<double> maxabs(2); CPPAD_TEST_VECTOR<double> xf(2); size_t nstep; // input values size_t M = 5; double ti = 0.; double tf = 1.; double smin = 1e-8; double smax = 1.; double sini = 1e-10; double erel = 0.; xf = CppAD::OdeGearControl(F, M, ti, tf, xi, smin, smax, sini, eabs, erel, ef, maxabs, nstep); double x0 = exp(-w[0]*tf); ok &= CppAD::NearEqual(x0, xf[0], 1e-4, 1e-4); ok &= CppAD::NearEqual(0., ef[0], 1e-4, 1e-4); double x1 = w[0] * (exp(-w[0]*tf) - exp(-w[1]*tf))/(w[1] - w[0]); ok &= CppAD::NearEqual(x1, xf[1], 1e-4, 1e-4); ok &= CppAD::NearEqual(0., ef[1], 1e-4, 1e-4); return ok; }
int main() { jvec a=jvec_load("time_momentum_prop",T,njacks,2); jvec b=jvec_load("time_momentum_prop",T,njacks,3); a.print_to_file("prop_re.xmg"); b.print_to_file("prop_im.xmg"); jvec pr(T,njacks); jvec pi(T,njacks); for(int t=0;t<T;t++) { double c=cos(0.5*t*2*M_PI/T); double s=sin(0.5*t*2*M_PI/T); pr[t]=a[t]*c-b[t]*s; pi[t]=a[t]*s+b[t]*c; } pr=0.5*(pr-pr.simmetric()); pi=0.5*(pi+pi.simmetric()); pr.print_to_file("prop_re.xmg"); pi.print_to_file("prop_im.xmg"); jvec xr(T,njacks); jvec xi(T,njacks); for(int t=0;t<T;t++) { double c=cos(t*2*M_PI/T); double s=sin(t*2*M_PI/T); xr[t]=pr[t]*c-pi[t]*s; xi[t]=pr[t]*s+pi[t]*c; } xr.print_to_file("prop_x.xmg"); xi.print_to_file("tooth.xmg"); //out<<ph2(t)<<" "<<a[t]<<endl; return 0; }
// Calculate the diamond difference coefficients void calc_dd_coefficients(void) { START_PROFILING; #pragma acc kernels \ present(dd_j[:dd_j_len], dd_k[:dd_k_len], eta[:eta_len], xi[:xi_len]) { dd_i = 2.0 / dx; #pragma acc loop independent for(int a = 0; a < nang; ++a) { dd_j(a) = (2.0/dy)*eta(a); dd_k(a) = (2.0/dz)*xi(a); } } STOP_PROFILING(__func__); }
bool OdeErrControl_one(void) { bool ok = true; // initial return value // Runge45 should yield exact results for x_i (t) = t^(i+1), i < 4 size_t n = 6; // construct method for n component solution Method_one method(n); // inputs to OdeErrControl double ti = 0.; double tf = .9; double smin = 1e-2; double smax = 1.; double scur = .5; double erel = 1e-7; CppAD::vector<double> xi(n); CppAD::vector<double> eabs(n); size_t i; for(i = 0; i < n; i++) { xi[i] = 0.; eabs[i] = 0.; } // outputs from OdeErrControl CppAD::vector<double> ef(n); CppAD::vector<double> xf(n); xf = OdeErrControl(method, ti, tf, xi, smin, smax, scur, eabs, erel, ef); double check = 1.; for(i = 0; i < n; i++) { check *= tf; ok &= CppAD::NearEqual(check, xf[i], erel, 0.); } return ok; }
Type objective_function<Type>::operator() () { DATA_VECTOR(times); DATA_VECTOR(obs); PARAMETER(log_R0); PARAMETER(log_a); PARAMETER(log_theta); PARAMETER(log_sigma); Type sigma=exp(log_sigma); Type theta=exp(log_theta); Type R0=exp(log_R0); Type a=exp(log_a)+Type(1e-4); int n1=times.size(); int n2=2;//mean and variance matrix<Type> xdist(n1,n2); //Ex and Vx Type m=(a-Type(1))*R0/times(n1-1); Type pen; xdist(0,0)=obs[0]; xdist(0,1)=Type(0); Type nll=0; Fun<Type> F; F.setpars(R0, m, theta, sigma); CppAD::vector<Type> xi(n2); xi[1]=Type(0); //Bottinger's code started variance at 0 for all lsoda calls Type ti; Type tf; for(int i=0; i<n1-1; i++) { xi[0] = Type(obs[i]); ti=times[i]; tf=times[i+1]; xdist.row(i+1) = vector<Type>(CppAD::Runge45(F, 1, ti, tf, xi)); xdist(i+1,1)=posfun(xdist(i+1,1), Type(1e-3), pen);//to keep the variance positive nll-= dnorm(obs[i+1], xdist(i+1,0), sqrt(xdist(i+1,1)), true); } nll+=pen; //penalty if the variance is near eps return nll; }
NM_Status SparseLinearSystemNM :: solve(SparseMtrx *A, FloatMatrix &B, FloatMatrix &X) { NM_Status status = NM_None; int ncol = A->giveNumberOfRows(); int nrhs = B.giveNumberOfColumns(); if ( A->giveNumberOfRows() != B.giveNumberOfRows() ) { OOFEM_ERROR("A and B matrix mismatch"); } FloatArray bi(ncol), xi(ncol); X.resize(ncol, nrhs); for ( int i = 1; i <= nrhs; ++i ) { B.copyColumn(bi, i); status &= this->solve(A, & bi, & xi); if ( status & NM_NoSuccess ) { return NM_NoSuccess; } X.setColumn(xi, i); } return status; }
bool OdeErrControl_four(void) { bool ok = true; // initial return value // construct method for n component solution size_t n = 6; Method_four method(n); // inputs to OdeErrControl // special case where scur is converted to ti - tf // (so it is not equal to smin) double ti = 0.; double tf = .9; double smin = .8; double smax = 1.; double scur = smin; double erel = 1e-7; CppAD::vector<double> xi(n); CppAD::vector<double> eabs(n); size_t i; for(i = 0; i < n; i++) { xi[i] = 0.; eabs[i] = 0.; } // outputs from OdeErrControl CppAD::vector<double> ef(n); CppAD::vector<double> xf(n); xf = OdeErrControl(method, ti, tf, xi, smin, smax, scur, eabs, erel, ef); // check that Fun_four always returning nan results in nan for(i = 0; i < n; i++) { ok &= CppAD::isnan(xf[i]); ok &= CppAD::isnan(ef[i]); } return ok; }
bool GenSetBase::generateAll(Matrix& M, ColumnVector& X, double D){ if (Size<=0 || Vdim<=0) { cerr << "***ERROR: GenSetBase::generateAll(Matrix,...) " << "called with size=" << Size << ", vdim=" << Vdim << endl; return false; } if (M.Ncols() != Size || M.Nrows() != Vdim) { cerr << "***ERROR: GenSetBase::generateAll(Matrix,...) " << "dimesion of M expected to be " << Vdim << "-by-" << Size << " but is " << M.Nrows() << "-by-" << M.Ncols() << endl; return false; } ColumnVector xi(Vdim); for (int i=1; i<=Size; i++) { generate(i, D, X, xi); M.Column(i) = xi; } return true; }
MathMatrix<double> f( int n, int j ) { /* internal vector length i.e. polynomial space size. Meaning only powers * * from x**0, x**0, ... x**(np-1) can be represented. If the formula actually * * includes higher power, then the result will be wrong. One can estimate the * * highest power from the analytical integral or you can test if higher np * * change the result. * * Also the result has certain symmetries and simplifications and * * redundancies which can be used to gauge whether it is correct */ const int np = 2*(n+2); /* np-Vector containing all powers of xi from xi**0 to xi**(np-1) */ MathMatrix<double> xi(np,1); if ( j <= 2*n ) { return f1(n,j,np); } else if ( j <= 3*n ) { return f2(n,j,np); } else { return f3(n,j,np); } }
void SqSylvMatrix::multVecKronTrans(KronVector& x, const KronVector& d) const { x.zeros(); if (d.getDepth() == 0) { multaVecTrans(x, d); } else { KronVector aux(x.getM(), x.getN(), x.getDepth()); for (int i = 0; i < x.getM(); i++) { KronVector auxi(aux, i); ConstKronVector di(d, i); multVecKronTrans(auxi, di); } for (int i = 0; i < rows; i++) { KronVector xi(x, i); for (int j = 0; j < cols; j++) { KronVector auxj(aux, j); xi.add(get(j,i), auxj); } } } }
bool runge_45_1(void) { bool ok = true; // initial return value size_t i; // temporary indices size_t n = 5; // number components in X(t) and order of method size_t M = 2; // number of Runge45 steps in [ti, tf] double ti = 0.; // initial time double tf = 2.; // final time // xi = X(0) CppAD::vector<double> xi(n); for(i = 0; i <n; i++) xi[i] = 0.; size_t use_x; for( use_x = 0; use_x < 2; use_x++) { // function object depends on value of use_x Fun F(use_x > 0); // compute Runge45 approximation for X(tf) CppAD::vector<double> xf(n), e(n); xf = CppAD::Runge45(F, M, ti, tf, xi, e); double check = tf; for(i = 0; i < n; i++) { // check that error is always positive ok &= (e[i] >= 0.); // 5th order method is exact for i < 5 if( i < 5 ) ok &= CppAD::NearEqual(xf[i], check, 1e-10, 1e-10); // 4th order method is exact for i < 4 if( i < 4 ) ok &= (e[i] <= 1e-10); // check value for next i check *= tf; } } return ok; }
bool AztecOOSolver<std::complex<double> >::solve() { _F_; assert(m != NULL); assert(rhs != NULL); assert(m->size == rhs->size); Hermes::TimePeriod tmr; // no output aztec.SetAztecOption(AZ_output, AZ_none); // AZ_all | AZ_warnings | AZ_last | AZ_summary double c0r = 1.0, c0i = 0.0; double c1r = 0.0, c1i = 1.0; Epetra_Vector xr(*rhs->std_map); Epetra_Vector xi(*rhs->std_map); Komplex_LinearProblem kp(c0r, c0i, *m->mat, c1r, c1i, *m->mat_im, xr, xi, *rhs->vec, *rhs->vec_im); Epetra_LinearProblem *lp = kp.KomplexProblem(); aztec.SetProblem(*lp); // solve it aztec.Iterate(this->max_iters, this->tolerance); kp.ExtractSolution(xr, xi); delete [] this->sln; this->sln = new std::complex<double>[m->size]; MEM_CHECK(this->sln); memset(this->sln, 0, m->size * sizeof(std::complex<double>)); // copy the solution into sln vector for (unsigned int i = 0; i < m->size; i++) this->sln[i] = std::complex<double>(xr[i], xi[i]); return true; }