Example shows polynomial function use.
#include <iostream>
#include <boost/mpl/list.hpp>
#include <boost/test/test_case_template.hpp>
#include "shared-tests/fixture.hh"
using namespace roboptim;
BOOST_FIXTURE_TEST_SUITE (core, TestSuiteConfiguration)
BOOST_AUTO_TEST_CASE_TEMPLATE (polynomial_test, T, functionTypes_t)
{
coefficients[0] = 45;
boost::shared_ptr<Polynomial<T> > fct =
boost::make_shared<Polynomial<T> > (coefficients);
std::cout << (*fct) << std::endl;
x[0] = 0;
y[0] = 45;
BOOST_CHECK_EQUAL ((*fct) (x), y);
x[0] = 1;
y[0] = 45;
BOOST_CHECK_EQUAL ((*fct) (x), y);
coefficients.resize (2);
coefficients[0] = 3;
coefficients[1] = 5;
fct = boost::make_shared<Polynomial<T> > (coefficients);
std::cout << (*fct) << std::endl;
x[0] = 0;
y[0] = 3;
BOOST_CHECK_EQUAL ((*fct) (x), y);
x[0] = 1;
y[0] = 8;
BOOST_CHECK_EQUAL((*fct) (x), y);
coefficients.resize (3);
coefficients[0] = 3;
coefficients[1] = 5;
coefficients[2] = 2;
fct = boost::make_shared<Polynomial<T> > (coefficients);
std::cout << (*fct) << std::endl;
x[0] = 0;
y[0] = 3;
BOOST_CHECK_EQUAL ((*fct) (x), y);
x[0] = 1;
y[0] = 10;
BOOST_CHECK_EQUAL ((*fct) (x), y);
x[0] = 3;
y[0] = 36;
BOOST_CHECK_EQUAL ((*fct) (x), y);
coefficients.resize (4);
coefficients[0] = 3;
coefficients[1] = 5;
coefficients[2] = 2;
coefficients[3] = 8;
fct = boost::make_shared<Polynomial<T> > (coefficients);
std::cout << (*fct) << std::endl;
x[0] = 0;
y[0] = 3;
BOOST_CHECK_EQUAL ((*fct) (x), y);
x[0] = 1;
y[0] = 18;
BOOST_CHECK_EQUAL ((*fct) (x), y);
x[0] = 3;
y[0] = 252;
BOOST_CHECK_EQUAL ((*fct) (x), y);
x.fill (1.);
std::cout << std::endl
<< (*fct) << std::endl
<< "Evaluate:" << std::endl
<< (*fct) (x) << std::endl
<< "Gradient:" << std::endl
<< "Jacobian:" << std::endl
<< fct->jacobian (x) << std::endl
<< "Hessian:" << std::endl
<< fct->hessian (x) << std::endl;
coefficients.resize (0);
BOOST_CHECK_THROW (fct = boost::make_shared<
Polynomial<T> > (coefficients),
std::runtime_error);
}
BOOST_AUTO_TEST_SUITE_END ()