2009-04-01 22:43:37 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-23 02:25:33 +08:00
|
|
|
// for linear algebra.
|
2009-04-01 22:43:37 +08:00
|
|
|
//
|
|
|
|
// Copyright (C) 2009 Gael Guennebaud <g.gael@free.fr>
|
|
|
|
//
|
2012-07-14 02:42:47 +08:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2009-04-01 22:43:37 +08:00
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include <unsupported/Eigen/AutoDiff>
|
|
|
|
|
|
|
|
template<typename Scalar>
|
|
|
|
EIGEN_DONT_INLINE Scalar foo(const Scalar& x, const Scalar& y)
|
|
|
|
{
|
2011-09-05 23:47:58 +08:00
|
|
|
using namespace std;
|
2009-04-01 22:43:37 +08:00
|
|
|
// return x+std::sin(y);
|
2009-04-10 05:29:31 +08:00
|
|
|
EIGEN_ASM_COMMENT("mybegin");
|
2011-09-05 23:47:58 +08:00
|
|
|
return static_cast<Scalar>(x*2 - pow(x,2) + 2*sqrt(y*y) - 4 * sin(x) + 2 * cos(y) - exp(-0.5*x*x));
|
2010-01-05 23:04:05 +08:00
|
|
|
//return x+2*y*x;//x*2 -std::pow(x,2);//(2*y/x);// - y*2;
|
2009-04-10 05:29:31 +08:00
|
|
|
EIGEN_ASM_COMMENT("myend");
|
2009-04-01 22:43:37 +08:00
|
|
|
}
|
|
|
|
|
2011-09-05 23:47:58 +08:00
|
|
|
template<typename Vector>
|
|
|
|
EIGEN_DONT_INLINE typename Vector::Scalar foo(const Vector& p)
|
|
|
|
{
|
|
|
|
typedef typename Vector::Scalar Scalar;
|
2011-09-23 22:28:26 +08:00
|
|
|
return (p-Vector(Scalar(-1),Scalar(1.))).norm() + (p.array() * p.array()).sum() + p.dot(p);
|
2011-09-05 23:47:58 +08:00
|
|
|
}
|
|
|
|
|
2009-04-01 22:43:37 +08:00
|
|
|
template<typename _Scalar, int NX=Dynamic, int NY=Dynamic>
|
|
|
|
struct TestFunc1
|
|
|
|
{
|
|
|
|
typedef _Scalar Scalar;
|
|
|
|
enum {
|
|
|
|
InputsAtCompileTime = NX,
|
|
|
|
ValuesAtCompileTime = NY
|
|
|
|
};
|
|
|
|
typedef Matrix<Scalar,InputsAtCompileTime,1> InputType;
|
|
|
|
typedef Matrix<Scalar,ValuesAtCompileTime,1> ValueType;
|
|
|
|
typedef Matrix<Scalar,ValuesAtCompileTime,InputsAtCompileTime> JacobianType;
|
2009-10-16 00:43:15 +08:00
|
|
|
|
2009-04-01 22:43:37 +08:00
|
|
|
int m_inputs, m_values;
|
2009-10-16 00:43:15 +08:00
|
|
|
|
2009-04-01 22:43:37 +08:00
|
|
|
TestFunc1() : m_inputs(InputsAtCompileTime), m_values(ValuesAtCompileTime) {}
|
|
|
|
TestFunc1(int inputs, int values) : m_inputs(inputs), m_values(values) {}
|
2009-10-16 00:43:15 +08:00
|
|
|
|
2009-04-01 22:43:37 +08:00
|
|
|
int inputs() const { return m_inputs; }
|
|
|
|
int values() const { return m_values; }
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void operator() (const Matrix<T,InputsAtCompileTime,1>& x, Matrix<T,ValuesAtCompileTime,1>* _v) const
|
|
|
|
{
|
|
|
|
Matrix<T,ValuesAtCompileTime,1>& v = *_v;
|
|
|
|
|
|
|
|
v[0] = 2 * x[0] * x[0] + x[0] * x[1];
|
|
|
|
v[1] = 3 * x[1] * x[0] + 0.5 * x[1] * x[1];
|
|
|
|
if(inputs()>2)
|
|
|
|
{
|
|
|
|
v[0] += 0.5 * x[2];
|
|
|
|
v[1] += x[2];
|
|
|
|
}
|
|
|
|
if(values()>2)
|
|
|
|
{
|
|
|
|
v[2] = 3 * x[1] * x[0] * x[0];
|
|
|
|
}
|
|
|
|
if (inputs()>2 && values()>2)
|
|
|
|
v[2] *= x[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator() (const InputType& x, ValueType* v, JacobianType* _j) const
|
|
|
|
{
|
|
|
|
(*this)(x, v);
|
|
|
|
|
|
|
|
if(_j)
|
|
|
|
{
|
|
|
|
JacobianType& j = *_j;
|
|
|
|
|
|
|
|
j(0,0) = 4 * x[0] + x[1];
|
|
|
|
j(1,0) = 3 * x[1];
|
|
|
|
|
|
|
|
j(0,1) = x[0];
|
|
|
|
j(1,1) = 3 * x[0] + 2 * 0.5 * x[1];
|
|
|
|
|
|
|
|
if (inputs()>2)
|
|
|
|
{
|
|
|
|
j(0,2) = 0.5;
|
|
|
|
j(1,2) = 1;
|
|
|
|
}
|
|
|
|
if(values()>2)
|
|
|
|
{
|
|
|
|
j(2,0) = 3 * x[1] * 2 * x[0];
|
|
|
|
j(2,1) = 3 * x[0] * x[0];
|
|
|
|
}
|
|
|
|
if (inputs()>2 && values()>2)
|
|
|
|
{
|
|
|
|
j(2,0) *= x[2];
|
|
|
|
j(2,1) *= x[2];
|
|
|
|
|
|
|
|
j(2,2) = 3 * x[1] * x[0] * x[0];
|
|
|
|
j(2,2) = 3 * x[1] * x[0] * x[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-09-28 05:54:06 +08:00
|
|
|
template<typename Func> void forward_jacobian(const Func& f)
|
2009-04-01 22:43:37 +08:00
|
|
|
{
|
|
|
|
typename Func::InputType x = Func::InputType::Random(f.inputs());
|
|
|
|
typename Func::ValueType y(f.values()), yref(f.values());
|
|
|
|
typename Func::JacobianType j(f.values(),f.inputs()), jref(f.values(),f.inputs());
|
|
|
|
|
|
|
|
jref.setZero();
|
|
|
|
yref.setZero();
|
|
|
|
f(x,&yref,&jref);
|
|
|
|
// std::cerr << y.transpose() << "\n\n";;
|
|
|
|
// std::cerr << j << "\n\n";;
|
|
|
|
|
|
|
|
j.setZero();
|
|
|
|
y.setZero();
|
|
|
|
AutoDiffJacobian<Func> autoj(f);
|
|
|
|
autoj(x, &y, &j);
|
|
|
|
// std::cerr << y.transpose() << "\n\n";;
|
|
|
|
// std::cerr << j << "\n\n";;
|
|
|
|
|
|
|
|
VERIFY_IS_APPROX(y, yref);
|
|
|
|
VERIFY_IS_APPROX(j, jref);
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:44:48 +08:00
|
|
|
|
|
|
|
// TODO also check actual derivatives!
|
2009-09-28 05:54:06 +08:00
|
|
|
void test_autodiff_scalar()
|
2009-04-01 22:43:37 +08:00
|
|
|
{
|
2014-04-01 23:44:48 +08:00
|
|
|
Vector2f p = Vector2f::Random();
|
2009-12-04 19:40:29 +08:00
|
|
|
typedef AutoDiffScalar<Vector2f> AD;
|
2014-04-01 23:44:48 +08:00
|
|
|
AD ax(p.x(),Vector2f::UnitX());
|
|
|
|
AD ay(p.y(),Vector2f::UnitY());
|
2011-09-05 23:47:58 +08:00
|
|
|
AD res = foo<AD>(ax,ay);
|
2014-04-01 23:44:48 +08:00
|
|
|
VERIFY_IS_APPROX(res.value(), foo(p.x(),p.y()));
|
2011-09-05 23:47:58 +08:00
|
|
|
}
|
|
|
|
|
2014-04-01 23:44:48 +08:00
|
|
|
// TODO also check actual derivatives!
|
2011-09-05 23:47:58 +08:00
|
|
|
void test_autodiff_vector()
|
|
|
|
{
|
2014-04-01 23:44:48 +08:00
|
|
|
Vector2f p = Vector2f::Random();
|
2011-09-05 23:47:58 +08:00
|
|
|
typedef AutoDiffScalar<Vector2f> AD;
|
|
|
|
typedef Matrix<AD,2,1> VectorAD;
|
2014-04-01 23:44:48 +08:00
|
|
|
VectorAD ap = p.cast<AD>();
|
|
|
|
ap.x().derivatives() = Vector2f::UnitX();
|
|
|
|
ap.y().derivatives() = Vector2f::UnitY();
|
2011-09-05 23:47:58 +08:00
|
|
|
|
2014-04-01 23:44:48 +08:00
|
|
|
AD res = foo<VectorAD>(ap);
|
|
|
|
VERIFY_IS_APPROX(res.value(), foo(p));
|
2009-09-28 05:54:06 +08:00
|
|
|
}
|
2009-10-16 00:43:15 +08:00
|
|
|
|
2009-09-28 05:54:06 +08:00
|
|
|
void test_autodiff_jacobian()
|
|
|
|
{
|
2014-04-01 23:44:48 +08:00
|
|
|
CALL_SUBTEST(( forward_jacobian(TestFunc1<double,2,2>()) ));
|
|
|
|
CALL_SUBTEST(( forward_jacobian(TestFunc1<double,2,3>()) ));
|
|
|
|
CALL_SUBTEST(( forward_jacobian(TestFunc1<double,3,2>()) ));
|
|
|
|
CALL_SUBTEST(( forward_jacobian(TestFunc1<double,3,3>()) ));
|
|
|
|
CALL_SUBTEST(( forward_jacobian(TestFunc1<double>(3,3)) ));
|
2009-04-01 22:43:37 +08:00
|
|
|
}
|
2009-09-28 05:54:06 +08:00
|
|
|
|
|
|
|
void test_autodiff()
|
|
|
|
{
|
2014-04-01 23:44:48 +08:00
|
|
|
for(int i = 0; i < g_repeat; i++) {
|
|
|
|
CALL_SUBTEST_1( test_autodiff_scalar() );
|
|
|
|
CALL_SUBTEST_2( test_autodiff_vector() );
|
|
|
|
CALL_SUBTEST_3( test_autodiff_jacobian() );
|
|
|
|
}
|
2009-09-28 05:54:06 +08:00
|
|
|
}
|
|
|
|
|