Example shows numeric quadratic function use.
Example shows numeric quadratic function use.
#include "shared-tests/fixture.hh"
#include <iostream>
BOOST_FIXTURE_TEST_SUITE (core, TestSuiteConfiguration)
BOOST_AUTO_TEST_CASE_TEMPLATE (numeric_quadratic_function, T, functionTypes_t)
{
a.coeffRef (0, 0) = 1., a.coeffRef (0, 1) = 0.,
a.coeffRef (0, 2) = 0., a.coeffRef (0, 3) = 0., a.coeffRef (0, 4) = 0.;
a.coeffRef (1, 0) = 0., a.coeffRef (1, 1) = 1.,
a.coeffRef (1, 2) = 0., a.coeffRef (1, 3) = 0., a.coeffRef (1, 4) = 0.;
a.coeffRef (2, 0) = 0., a.coeffRef (2, 1) = 0.,
a.coeffRef (2, 2) = 1., a.coeffRef (2, 3) = 0., a.coeffRef (2, 4) = 0.;
a.coeffRef (3, 0) = 0., a.coeffRef (3, 1) = 0.,
a.coeffRef (3, 2) = 0., a.coeffRef (3, 3) = 1., a.coeffRef (3, 4) = 0.;
a.coeffRef (4, 0) = 0., a.coeffRef (4, 1) = 0.,
a.coeffRef (4, 2) = 0., a.coeffRef (4, 3) = 0., a.coeffRef (4, 4) = 1.;
b[0] = 0.;
b[1] = 0.;
b[2] = 0.;
b[3] = 0.;
b[4] = 0.;
std::cout << f << '\n';
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 5; ++j)
x[j] = std::ceil (rand () % 50);
std::cout << "x = " << x << '\n';
std::cout << "f(x) = " << f (x) << '\n';
std::cout <<
"J(x) = " << f.
jacobian (x) <<
'\n';
std::cout <<
"G(x) = " << f.
gradient (x, 0) <<
'\n';
std::cout <<
"H(x) = " << f.
hessian (x, 0) <<
'\n';
matrix_t J (1, 5);
for (typename matrix_t::Index i = 0; i < 5; ++i)
J.coeffRef (0, i) = 2 * x[i];
for (typename matrix_t::Index i = 0; i < 5; ++i)
{
std::cout << f.
jacobian (x).coeffRef (0, i) <<
'\n';
std::cout << J.coeffRef (0, i) << '\n';
}
BOOST_CHECK (allclose (f.
jacobian (x), J));
BOOST_CHECK (allclose (f.
hessian (x, 0), a));
BOOST_CHECK (checkGradient (f, 0, x));
BOOST_CHECK (checkJacobian (f, x));
}
}
typedef boost::mpl::list< ::roboptim::EigenMatrixSparse> sparseOnly_t;
BOOST_AUTO_TEST_CASE_TEMPLATE (random_gradient_check, T, sparseOnly_t)
{
for (int randomTry = 0; randomTry < 10; ++randomTry)
{
a.setZero ();
b.setZero ();
x.setZero ();
a.insert (i, j) = 0.;
a.coeffRef (i, j) = a.coeffRef (j, i) = static_cast<double> (std::rand () / RAND_MAX);
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 5; ++j)
x[j] = std::ceil (rand () % 50);
BOOST_CHECK (checkGradient (f, 0, x));
BOOST_CHECK (checkJacobian (f, x));
}
}
}
BOOST_AUTO_TEST_SUITE_END ()
DummyDifferentiableSparseSolverLastState::parent_t solver_t
Definition: dummy-d-sparse-laststate.cc:29
defined(EIGEN_RUNTIME_NO_MALLOC) && !defined(ROBOPTIM_DO_NOT_CHECK_ALLOCATION)
Definition: alloc.hh:33
jacobian_t jacobian(const_argument_ref argument) const
Computes the jacobian.
Definition: differentiable-function.hh:131
gradient_t gradient(const_argument_ref argument, size_type functionId=0) const
Computes the gradient.
Definition: differentiable-function.hh:172
Tag type for functions using Eigen dense matrices.
Definition: fwd.hh:59
Tag type for functions using Eigen sparse matrices.
Definition: fwd.hh:61
Build a quadratic function from a matrix and a vector.
Definition: numeric-quadratic-function.hh:39
hessian_t hessian(const_argument_ref argument, size_type functionId=0) const
Compute the hessian at a given point.
Definition: twice-differentiable-function.hh:106
void setZero(hessian_ref symmetric) const
Set a symmetric matrix to zero.
Definition: twice-differentiable-function.hh:176
Dummy solver which always fails.
Definition: dummy.hh:35