int main(int argc, const char * argv[]) { // the integral of 1/sqrt(1-x^2) is arcsin(x) double exact = asin(0.99) - asin(0); double a = 0.0; double b = 0.99; std::vector<double> nodes; nodes.push_back(101); nodes.push_back(201); nodes.push_back(1001); nodes.push_back(2001); CompTrapRule ctp(&f, &fDoublePrime, a, b, nodes); std::vector<double> results = ctp.getResult(); std::vector<double> hSteps = ctp.getHStep(); std::vector<double> errorBounds = ctp.getErrorBound(); std::cout << "Exact Value: " << exact << std::endl; for(unsigned i = 0; i < results.size(); i++){ std::cout << "Composite Trapezoidal Rule approximation: " << std::setprecision(9) << results[i] << " with h step of " << hSteps[i] << ", an absolute erorr of " << absoluteError(exact, results[i]) << ", and error bound of " << errorBounds[i] << '.' << std::endl; } std::vector<double> extrp = extrapolation(results); for(unsigned i = 0; i < extrp.size(); i++){ std::cout << "Richardson's Extrapolation approximation: " << std::setprecision(9) << extrp[i] << " with an absolute error of " << absoluteError(exact, extrp[i]) << std::endl; } return 0; }
int AcontainsB(MapNode *pA, MapNode *pB){ CheckTreeProc ctp(pB); int i; ProcTree(&ctp, pA); i = ctp.contains(); return i; }
thrust_rewriter::result_type thrust_rewriter::map_rewrite( const bind& n) { //The rhs must be an apply assert(detail::isinstance<apply>(n.rhs())); const apply& rhs = boost::get<const apply&>(n.rhs()); //The rhs must apply a "map" assert(rhs.fn().id().substr(0, 3) == string("map")); const tuple& ap_args = rhs.args(); //Map must have arguments assert(ap_args.begin() != ap_args.end()); auto init = ap_args.begin(); shared_ptr<const ctype::type_t> fn_t; if (detail::isinstance<apply>(*init)) { //Function instantiation const apply& fn_inst = boost::get<const apply&>( *init); if (detail::isinstance<templated_name>(fn_inst.fn())) { const templated_name& tn = boost::get<const templated_name&>(fn_inst.fn()); const ctype::tuple_t& tt = tn.template_types(); vector<shared_ptr<const ctype::type_t> > ttc; for(auto i = tt.begin(); i != tt.end(); i++) { ttc.push_back(i->ptr()); } shared_ptr<const ctype::monotype_t> base = make_shared<const ctype::monotype_t>(tn.id()); fn_t = make_shared<const ctype::polytype_t>( std::move(ttc), base); } else { assert(detail::isinstance<name>(fn_inst.fn())); string fn_id = fn_inst.fn().id(); fn_t = make_shared<const ctype::monotype_t>(fn_id); } } else { //We must be dealing with a closure assert(detail::isinstance<closure>(*init)); const closure& close = boost::get<const closure&>( *init); stringstream ss; ss << "closure"; string closure_t_name = ss.str(); shared_ptr<const ctype::monotype_t> closure_mt = make_shared<const ctype::monotype_t>(closure_t_name); vector<shared_ptr<const ctype::type_t> > cts; //By this point, the body of the closure is an //instantiated functor (which must be an apply node) assert(detail::isinstance<apply>(close.body())); const apply& inst_fctor = boost::get<const apply&>(close.body()); stringstream os; //It's either a plain name or a templated name if (detail::isinstance<templated_name>(inst_fctor.fn())) { const templated_name& tn = boost::get<const templated_name&>(inst_fctor.fn()); os << tn.id() << "<"; const ctype::tuple_t& template_types = tn.template_types(); ctype::ctype_printer ctp(m_target, os); for(auto i = template_types.begin(); i != template_types.end(); i++) { boost::apply_visitor(ctp, *i); if (std::next(i) != template_types.end()) { os << ", "; } } os << " > "; } else { assert(detail::isinstance<name>(inst_fctor.fn())); const name& fnn = boost::get<const name&>(inst_fctor.fn()); os << fnn.id(); } cts.push_back( make_shared<const ctype::monotype_t>( os.str())); vector<shared_ptr<const ctype::type_t> > tuple_sub_cts; for(auto i = close.args().begin(); i != close.args().end(); i++) { //Can only close over names assert(detail::isinstance<name>(*i)); const name& arg_i_name = boost::get<const name&>(*i); tuple_sub_cts.push_back( make_shared<const ctype::monotype_t>( detail::typify(arg_i_name.id()))); } cts.push_back( make_shared<const ctype::tuple_t>( std::move(tuple_sub_cts))); fn_t = make_shared<const ctype::polytype_t>( std::move(cts), closure_mt); } vector<shared_ptr<const ctype::type_t> > arg_types; for(auto i = init+1; i != ap_args.end(); i++) { //Assert we're looking at a name assert(detail::isinstance<name>(*i)); arg_types.push_back( make_shared<const ctype::monotype_t>( detail::typify(boost::get<const name&>(*i).id()))); } shared_ptr<const ctype::tuple_t> thrust_tupled = make_shared<const ctype::tuple_t>( std::move(arg_types)); shared_ptr<const ctype::polytype_t> transform_t = make_shared<const ctype::polytype_t>( make_vector<shared_ptr<const ctype::type_t> > (fn_t)(thrust_tupled), make_shared<const ctype::monotype_t>("transformed_sequence")); shared_ptr<const apply> n_rhs = static_pointer_cast<const apply>(n.rhs().ptr()); //Can only handle names on the LHS assert(detail::isinstance<name>(n.lhs())); const name& lhs = boost::get<const name&>(n.lhs()); shared_ptr<const name> n_lhs = make_shared<const name>(lhs.id(), lhs.type().ptr(), transform_t); auto result = make_shared<const bind>(n_lhs, n_rhs); return result; }