void secant_method_test() { data__ delta(std::sqrt(std::numeric_limits<data__>::epsilon())), rhs(0.5), root(0); eli::mutil::nls::secant_method<data__> sm; int stat; sm.set_absolute_tolerance(delta); sm.set_max_iteration(200); sm.set_initial_guesses(eli::constants::math<data__>::pi_by_four(), eli::constants::math<data__>::pi_by_two()); // test using user defined function sm.set_absolute_tolerance(delta); sm.set_max_iteration(200); sm.set_initial_guesses(eli::constants::math<data__>::pi_by_four(), eli::constants::math<data__>::pi_by_two()); stat = sm.find_root(root, std::ptr_fun(my_function<data__>), rhs); TEST_ASSERT(stat==eli::mutil::nls::secant_method<data__>::converged); TEST_ASSERT_DELTA(root, std::acos(rhs), 2*delta); // test using functor sm.set_absolute_tolerance(delta); sm.set_max_iteration(200); sm.set_initial_guesses(eli::constants::math<data__>::pi_by_four(), eli::constants::math<data__>::pi_by_two()); stat = sm.find_root(root, my_functor<data__>(), rhs); TEST_ASSERT(stat==eli::mutil::nls::secant_method<data__>::converged); TEST_ASSERT_DELTA(root, std::acos(rhs), 2*delta); }
void newton_raphson_method_test() { data__ delta(std::sqrt(std::numeric_limits<data__>::epsilon())), rhs(0.5), root(0); eli::mutil::nls::newton_raphson_method<data__> nrm; int stat; nrm.set_absolute_tolerance(delta); nrm.set_max_iteration(200); nrm.set_initial_guess(static_cast<data__>(0.3)*eli::constants::math<data__>::pi_by_four()); // test using user defined functions stat = nrm.find_root(root, std::ptr_fun(my_function<data__>), std::ptr_fun(my_function_derivative<data__>), rhs); TEST_ASSERT(stat==eli::mutil::nls::newton_raphson_method<data__>::converged); TEST_ASSERT_DELTA(root, std::acos(rhs), 2*delta); nrm.set_max_iteration(2); stat = nrm.find_root(root, std::ptr_fun(my_function<data__>), std::ptr_fun(my_function_derivative<data__>), rhs); TEST_ASSERT(stat==eli::mutil::nls::newton_raphson_method<data__>::max_iteration); nrm.set_max_iteration(200); nrm.set_initial_guess(0); stat = nrm.find_root(root, std::ptr_fun(my_function<data__>), std::ptr_fun(my_function_derivative<data__>), rhs); TEST_ASSERT(stat==eli::mutil::nls::newton_raphson_method<data__>::no_root_found); // test using functor nrm.set_absolute_tolerance(delta); nrm.set_max_iteration(200); nrm.set_initial_guess(eli::constants::math<data__>::pi_by_four()); stat = nrm.find_root(root, my_functor<data__>(), my_functor_derivative<data__>(), rhs); TEST_ASSERT(stat==eli::mutil::nls::newton_raphson_method<data__>::converged); TEST_ASSERT_DELTA(root, std::acos(rhs), 2*delta); }
void bisection_method_test() { data__ delta(std::sqrt(std::numeric_limits<data__>::epsilon())), rhs(0.5), root(0); eli::mutil::nls::bisection_method<data__> bm; int stat; bm.set_absolute_tolerance(delta); bm.set_max_iteration(200); bm.set_bounds(0, eli::constants::math<data__>::pi()); // test using user defined function bm.set_absolute_tolerance(delta); bm.set_max_iteration(200); bm.set_bounds(0, eli::constants::math<data__>::pi()); stat = bm.find_root(root, std::ptr_fun(my_function<data__>), rhs); TEST_ASSERT(stat==eli::mutil::nls::bisection_method<data__>::converged); TEST_ASSERT_DELTA(root, std::acos(rhs), 2*delta); // test using functor bm.set_absolute_tolerance(delta); bm.set_max_iteration(200); bm.set_bounds(0, eli::constants::math<data__>::pi()); stat = bm.find_root(root, my_functor<data__>(), rhs); TEST_ASSERT(stat==eli::mutil::nls::bisection_method<data__>::converged); TEST_ASSERT_DELTA(root, std::acos(rhs), 2*delta); }
void TestIbex::check(const Interval& y_actual, const Interval& y_expected) { //cout << "TestIbex::check: " << y_expected << " (expected) " << y_actual << " (actual)"<< endl; if (y_expected.is_empty()) { TEST_ASSERT(y_actual.is_empty()); return; } TEST_ASSERT(!y_actual.is_empty()); TEST_ASSERT(!isnan(y_actual.lb())); TEST_ASSERT(!isnan(y_actual.ub())); TEST_ASSERT_DELTA(y_actual.lb(),y_expected.lb(),ERROR); TEST_ASSERT_DELTA(y_actual.ub(),y_expected.ub(),ERROR); }
void newton_raphson_constrained_method_test() { typedef eli::mutil::nls::newton_raphson_constrained_method<data__> nrcm_type; data__ delta(std::sqrt(std::numeric_limits<data__>::epsilon())), rhs(0.5), root; nrcm_type nrcm; int stat; nrcm.set_absolute_tolerance(delta); nrcm.set_max_iteration(200); nrcm.set_initial_guess(eli::constants::math<data__>::two_pi()+static_cast<data__>(0.3)*eli::constants::math<data__>::pi_by_four()); nrcm.set_lower_condition(eli::constants::math<data__>::two_pi(), nrcm_type::NRC_EXCLUSIVE); nrcm.set_upper_condition(eli::constants::math<data__>::pi()*3, nrcm_type::NRC_EXCLUSIVE); // test using user defined functions stat = nrcm.find_root(root, std::ptr_fun(my_function<data__>), std::ptr_fun(my_function_derivative<data__>), rhs); TEST_ASSERT(stat==nrcm_type::converged); TEST_ASSERT_DELTA(root, eli::constants::math<data__>::two_pi()+std::acos(rhs), 2*delta); nrcm.set_max_iteration(2); stat = nrcm.find_root(root, std::ptr_fun(my_function<data__>), std::ptr_fun(my_function_derivative<data__>), rhs); TEST_ASSERT(stat==nrcm_type::max_iteration); nrcm.set_max_iteration(100); nrcm.set_initial_guess(eli::constants::math<data__>::two_pi()+static_cast<data__>(0.3)*eli::constants::math<data__>::pi_by_four()); nrcm.set_upper_condition(static_cast<data__>(0.1)+eli::constants::math<data__>::two_pi(), nrcm_type::NRC_EXCLUSIVE); stat = nrcm.find_root(root, std::ptr_fun(my_function<data__>), std::ptr_fun(my_function_derivative<data__>), rhs); TEST_ASSERT(stat==nrcm_type::hit_constraint); data__ rhs2(cos(eli::constants::math<data__>::pi()+static_cast<data__>(0.001))); nrcm.set_initial_guess(eli::constants::math<data__>::pi()); nrcm.set_periodic_condition(eli::constants::math<data__>::pi(), eli::constants::math<data__>::pi()*3); stat = nrcm.find_root(root, std::ptr_fun(my_function<data__>), std::ptr_fun(my_function_derivative<data__>), rhs2); TEST_ASSERT(stat==nrcm_type::converged); // test using functor nrcm.set_absolute_tolerance(delta); nrcm.set_max_iteration(200); nrcm.set_lower_condition(eli::constants::math<data__>::two_pi(), nrcm_type::NRC_EXCLUSIVE); nrcm.set_upper_condition(eli::constants::math<data__>::pi()*3, nrcm_type::NRC_EXCLUSIVE); nrcm.set_initial_guess(eli::constants::math<data__>::two_pi()+static_cast<data__>(0.3)*eli::constants::math<data__>::pi_by_four()); stat = nrcm.find_root(root, my_functor<data__>(), my_functor_derivative<data__>(), rhs); TEST_ASSERT(stat==nrcm_type::converged); TEST_ASSERT_DELTA(root, eli::constants::math<data__>::two_pi()+std::acos(rhs), 2*delta); }
void TestIbex::check(double y_actual, double y_expected) { TEST_ASSERT(!isnan(y_expected)); if (y_expected==POS_INFINITY) { TEST_ASSERT(y_actual==POS_INFINITY); } else if (y_expected==NEG_INFINITY) { TEST_ASSERT(y_actual==NEG_INFINITY); } else { TEST_ASSERT(y_actual!=POS_INFINITY); TEST_ASSERT(y_actual!=NEG_INFINITY); TEST_ASSERT_DELTA(y_actual,y_expected,ERROR); } }