bool is_odd(double lb, double ub) const { // put in filtered evaluation Interval lbi= p_(Interval(lb)); Interval ubi= p_(Interval(ub)); bool ret=false; if (is_unknown_sign(lbi) || is_unknown_sign(ubi)){ ret= CGAL::sign(pe_(ENT(lb))) != CGAL::sign(pe_(ENT(ub))) || CGAL::sign(pe_(ENT(lb))) == CGAL::ZERO; } else { ret= CGAL::sign(lbi) != CGAL::sign(ubi) || CGAL::sign(lbi) == CGAL::ZERO; } return ret; }
CPoint2D CPoint2D::translate(double s, double rad) { CPoint2D p(m_x, m_y); CPoint2D p_(m_x + s, m_y); return p_.rotate_about(p, rad); }
SSMProcess* RepeatedStateContractions::contractSpecificStates(int n , int m , SSMProcess tSSMProcess){ assert(n <= this->m_ptrData->getNumStates()); assert(m <= this->m_ptrData->getNumStates()); assert(n != m); rmatrix a( mid( tSSMProcess.getDistributions() ) ); rmatrix t( mid( tSSMProcess.getTransitionMatrix() ) ); //setting up initial values rvector p(tSSMProcess.getStationaryProbabilities()); rmatrix a_( RMatrixUtils::zeros( Lb(a,ROW) , Ub(a,ROW)-1 , Lb(a,COL) , Ub(a,COL) ) ); rmatrix p_( RMatrixUtils::zeros( Lb(t,ROW) , Ub(t,ROW)-1 , Lb(t,COL) , Ub(t,COL)-1 ) ); //contracting state in transitionMatrix for(int i = Lb(a,ROW); i <= Ub(a,ROW); i++){ int buff = (i > n) ? -1 : 0; for(int x = Lb( Row(a,1) ); x <= Ub( Row(a,1) );x++){ real value = 0.0; if(i != m && i != n){ a_[i+buff][x] = a[i][x]; } else if(i == m){ a_[i+buff][x] = (p[i]*a[i][x] + p[n]*a[n][x]) / (p[n]+p[m]); } } } for(int i = 1; i < tSSMProcess.getNumStates(); i++){ int buffI = (i > n) ? -1 : 0; for(int j = 1; j < tSSMProcess.getNumStates();j++){ int buffJ = (j > n) ? -1 : 0; real value = 0.0; if(i != m && i != n && j != n && j != m){ value = t[i][j]; } else if( i != m && i != n && j == m ){ value = t[i][m] + t[i][n]; } else if( i == m && j != m && j != n){ value = (p[m]*t[m][j] + p[n]*t[n][j]) / (p[m] + p[n]); } else if( i == m && j == m){ value = ( (p[m]*(t[m][m]+t[m][n])) + (p[n]*(t[n][m]+t[n][n])) ) / (p[m] + p[n]); } p_[i+buffI][j+buffJ] = value; } } return new SSMProcess( p_ , a_ , false ); }
void NiceConnection::queueData(unsigned int component_id, char* buf, int len){ if (this->checkIceState() == NICE_READY){ boost::mutex::scoped_lock(queueMutex_); if (niceQueue_.size() < 1000 ) { packetPtr p_ (new dataPacket()); memcpy(p_->data, buf, len); p_->comp = component_id; p_->length = len; niceQueue_.push(p_); cond_.notify_one(); } } }
std::vector<Constraint> * conditions( const ConstraintSystem & dt, const ConstraintSystem & df, const std::vector<LinearExpression_ppl> & rel, size_t st, size_t sf, size_t sg ) { std::vector<Constraint> * res = new std::vector<Constraint>(); ConstraintSystem::const_iterator itc_cs; for (itc_cs = dt.begin(); itc_cs != dt.end(); itc_cs++) { LinearExpression_ppl le(0); le += itc_cs->inhomogeneous_term(); for (int i = 0; (i < st) && (i < itc_cs->space_dimension()); i++) { le += itc_cs->coefficient(VariableID(i)) * rel[i]; } for (int i = 0; (i < sg) && (st + i < itc_cs->space_dimension()); i++) { le += itc_cs->coefficient(VariableID(st + i)) * VariableID(sf + i); } if (itc_cs->is_equality()) res->push_back(le == 0); else res->push_back(le >= 0); } Polyhedron p(sf + sg); for (itc_cs = df.begin(); itc_cs != df.end(); itc_cs++) p.refine_with_constraint(*itc_cs); std::vector<Constraint>::iterator it_cs = res->begin(); while (it_cs != res->end()) { if (it_cs->is_tautological()) { it_cs = res->erase(it_cs); break; } if (it_cs->is_inconsistent()) { delete res; return NULL; } Polyhedron p_(p); p_.refine_with_constraint(*it_cs); if (p_.contains(p)) it_cs = res->erase(it_cs); else it_cs++; } return res; }
bool operator () (const Arg1 &a1, const Arg2 &a2) const { ++count_; return p_(a1, a2); }
bool operator () (const Arg &a) const { ++count_; return p_(a); }
SGVector<complex128_t> CCGMShiftedFamilySolver::solve_shifted_weighted( CLinearOperator<SGVector<float64_t>, SGVector<float64_t> >* A, SGVector<float64_t> b, SGVector<complex128_t> shifts, SGVector<complex128_t> weights) { SG_DEBUG("Entering\n"); // sanity check REQUIRE(A, "Operator is NULL!\n"); REQUIRE(A->get_dimension()==b.vlen, "Dimension mismatch! [%d vs %d]\n", A->get_dimension(), b.vlen); REQUIRE(shifts.vector,"Shifts are not initialized!\n"); REQUIRE(weights.vector,"Weights are not initialized!\n"); REQUIRE(shifts.vlen==weights.vlen, "Number of shifts and number of " "weights are not equal! [%d vs %d]\n", shifts.vlen, weights.vlen); // the solution matrix, one column per shift, initial guess 0 for all MatrixXcd x_sh=MatrixXcd::Zero(b.vlen, shifts.vlen); MatrixXcd p_sh=MatrixXcd::Zero(b.vlen, shifts.vlen); // non-shifted direction SGVector<float64_t> p_(b.vlen); // the rest of the part hinges on eigen3 for computing norms Map<VectorXd> b_map(b.vector, b.vlen); Map<VectorXd> p(p_.vector, p_.vlen); // residual r_i=b-Ax_i, here x_0=[0], so r_0=b VectorXd r=b_map; // initial direction is same as residual p=r; p_sh=r.replicate(1, shifts.vlen).cast<complex128_t>(); // non shifted initializers float64_t r_norm2=r.dot(r); float64_t beta_old=1.0; float64_t alpha=1.0; // shifted quantities SGVector<complex128_t> alpha_sh(shifts.vlen); SGVector<complex128_t> beta_sh(shifts.vlen); SGVector<complex128_t> zeta_sh_old(shifts.vlen); SGVector<complex128_t> zeta_sh_cur(shifts.vlen); SGVector<complex128_t> zeta_sh_new(shifts.vlen); // shifted initializers zeta_sh_old.set_const(1.0); zeta_sh_cur.set_const(1.0); // the iterator for this iterative solver IterativeSolverIterator<float64_t> it(r, m_max_iteration_limit, m_relative_tolerence, m_absolute_tolerence); // start the timer CTime time; time.start(); // set the residuals to zero if (m_store_residuals) m_residuals.set_const(0.0); // CG iteration begins for (it.begin(r); !it.end(r); ++it) { SG_DEBUG("CG iteration %d, residual norm %f\n", it.get_iter_info().iteration_count, it.get_iter_info().residual_norm); if (m_store_residuals) { m_residuals[it.get_iter_info().iteration_count] =it.get_iter_info().residual_norm; } // apply linear operator to the direction vector SGVector<float64_t> Ap_=A->apply(p_); Map<VectorXd> Ap(Ap_.vector, Ap_.vlen); // compute p^{T}Ap, if zero, failure float64_t p_dot_Ap=p.dot(Ap); if (p_dot_Ap==0.0) break; // compute the beta parameter of CG_M float64_t beta=-r_norm2/p_dot_Ap; // compute the zeta-shifted parameter of CG_M compute_zeta_sh_new(zeta_sh_old, zeta_sh_cur, shifts, beta_old, beta, alpha, zeta_sh_new); // compute beta-shifted parameter of CG_M compute_beta_sh(zeta_sh_new, zeta_sh_cur, beta, beta_sh); // update the solution vector and residual for (index_t i=0; i<shifts.vlen; ++i) x_sh.col(i)-=beta_sh[i]*p_sh.col(i); // r_{i}=r_{i-1}+\beta_{i}Ap r+=beta*Ap; // compute new ||r||_{2}, if zero, converged float64_t r_norm2_i=r.dot(r); if (r_norm2_i==0.0) break; // compute the alpha parameter of CG_M alpha=r_norm2_i/r_norm2; // update ||r||_{2} r_norm2=r_norm2_i; // update direction p=r+alpha*p; compute_alpha_sh(zeta_sh_new, zeta_sh_cur, beta_sh, beta, alpha, alpha_sh); for (index_t i=0; i<shifts.vlen; ++i) { p_sh.col(i)*=alpha_sh[i]; p_sh.col(i)+=zeta_sh_new[i]*r; } // update parameters for (index_t i=0; i<shifts.vlen; ++i) { zeta_sh_old[i]=zeta_sh_cur[i]; zeta_sh_cur[i]=zeta_sh_new[i]; } beta_old=beta; } float64_t elapsed=time.cur_time_diff(); if (!it.succeeded(r)) SG_WARNING("Did not converge!\n"); SG_INFO("Iteration took %d times, residual norm=%.20lf, time elapsed=%f\n", it.get_iter_info().iteration_count, it.get_iter_info().residual_norm, elapsed); // compute the final result vector multiplied by weights SGVector<complex128_t> result(b.vlen); result.set_const(0.0); Map<VectorXcd> x(result.vector, result.vlen); for (index_t i=0; i<x_sh.cols(); ++i) x+=x_sh.col(i)*weights[i]; SG_DEBUG("Leaving\n"); return result; }
SGVector<float64_t> CConjugateGradientSolver::solve( CLinearOperator<float64_t>* A, SGVector<float64_t> b) { SG_DEBUG("CConjugateGradientSolve::solve(): Entering..\n"); // sanity check REQUIRE(A, "Operator is NULL!\n"); REQUIRE(A->get_dimension()==b.vlen, "Dimension mismatch!\n"); // the final solution vector, initial guess is 0 SGVector<float64_t> result(b.vlen); result.set_const(0.0); // the rest of the part hinges on eigen3 for computing norms Map<VectorXd> x(result.vector, result.vlen); Map<VectorXd> b_map(b.vector, b.vlen); // direction vector SGVector<float64_t> p_(result.vlen); Map<VectorXd> p(p_.vector, p_.vlen); // residual r_i=b-Ax_i, here x_0=[0], so r_0=b VectorXd r=b_map; // initial direction is same as residual p=r; // the iterator for this iterative solver IterativeSolverIterator<float64_t> it(b_map, m_max_iteration_limit, m_relative_tolerence, m_absolute_tolerence); // CG iteration begins float64_t r_norm2=r.dot(r); // start the timer CTime time; time.start(); // set the residuals to zero if (m_store_residuals) m_residuals.set_const(0.0); for (it.begin(r); !it.end(r); ++it) { SG_DEBUG("CG iteration %d, residual norm %f\n", it.get_iter_info().iteration_count, it.get_iter_info().residual_norm); if (m_store_residuals) { m_residuals[it.get_iter_info().iteration_count] =it.get_iter_info().residual_norm; } // apply linear operator to the direction vector SGVector<float64_t> Ap_=A->apply(p_); Map<VectorXd> Ap(Ap_.vector, Ap_.vlen); // compute p^{T}Ap, if zero, failure float64_t p_dot_Ap=p.dot(Ap); if (p_dot_Ap==0.0) break; // compute the alpha parameter of CG float64_t alpha=r_norm2/p_dot_Ap; // update the solution vector and residual // x_{i}=x_{i-1}+\alpha_{i}p x+=alpha*p; // r_{i}=r_{i-1}-\alpha_{i}p r-=alpha*Ap; // compute new ||r||_{2}, if zero, converged float64_t r_norm2_i=r.dot(r); if (r_norm2_i==0.0) break; // compute the beta parameter of CG float64_t beta=r_norm2_i/r_norm2; // update direction, and ||r||_{2} r_norm2=r_norm2_i; p=r+beta*p; } float64_t elapsed=time.cur_time_diff(); if (!it.succeeded(r)) SG_WARNING("Did not converge!\n"); SG_INFO("Iteration took %ld times, residual norm=%.20lf, time elapsed=%lf\n", it.get_iter_info().iteration_count, it.get_iter_info().residual_norm, elapsed); SG_DEBUG("CConjugateGradientSolve::solve(): Leaving..\n"); return result; }
bool no_root(Interval r) const { Interval rv= p_(r); return !is_unknown_sign(rv); }
bool operator () ( const T1 &t1 ) const { return p_ ( *it_, t1 ); }
void Code11Method::code11(void) { double h = 5e-3; double rho = 5300; MatrixXd c = MatrixXd::Zero(6, 6); c(0, 0) = 23.9e10; c(0, 1) = 10.4e10; c(0, 2) = 5e10; c(1, 1) = 24.7e10; c(1, 2) = 5.2e10; c(2, 2) = 13.5e10; c(3, 3) = 6.5e10; c(4, 4) = 6.6e10; c(5, 5) = 7.6e10; double vt = sqrt(c(5, 5) / rho); int n = 90; std::pair<Eigen::MatrixXd, std::vector<Eigen::MatrixXd>> result = UtilityMethods::chebdif(n, 2); MatrixXd x = result.first; x = (x.array() + 1).matrix() * (h / 2); std::vector<Eigen::MatrixXd> dm = result.second; MatrixXd d1 = pow((h / 2), -1) * dm[0]; MatrixXd d2 = pow((h / 2), -2) * dm[1]; MatrixXd o = MatrixXd::Zero(n, n); int kmin = 0; int kmax = 18; const int steps = 4201; MatrixXd k = MatrixXd::Zero(1, steps); { double fixed = ((double)(kmax - kmin)) / (steps - 1); double i; int j; for (i = 0, j = 0; j <= 4.2e3; i += fixed, j++) { k(0, j) = i; } } k = k.cwiseProduct(k.cwiseProduct(k)); MatrixXd W = MatrixXd::Zero(n, k.cols()); // Set up m for loop int m = 0; MatrixXd lp = MatrixXd::Zero(n, n); lp = -pow(k(0, m), 2) * c(4, 4) * MatrixXd::Identity(n, n) + c(5, 5) * d2; MatrixXd l(n, n); l = lp; MatrixXd s = c(5, 5) * d1; l.row(0) = s.row(0); l.row(n - 1) = s.row(n - 1); MatrixXd m2 = MatrixXd::Identity(n, n); m2 *= -rho; m2(0, 0) = 0; m2(n - 1, n - 1) = 0; pair<MatrixXcd, MatrixXcd> eigs = Utility::UtilityMethods::matlab_eig(l, m2); MatrixXd p = eigs.first.real(); MatrixXd e = eigs.second.real(); MatrixXd w = e.cwiseSqrt(); std::sort(w.data(), w.data() + w.size()); MatrixXd p_(n, steps); p_.col(m) = w.col(0); int q = 0; for (int i = 0; i < n; i++) { if (p_(i, m) != 0) { W(q, m) = p_(i, m); q++; } } std::cout << "x:\n"; std::cout << x; }