// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2009 Gael Guennebaud // // Eigen is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 3 of the License, or (at your option) any later version. // // Alternatively, you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of // the License, or (at your option) any later version. // // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the // GNU General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License and a copy of the GNU General Public License along with // Eigen. If not, see . #include "main.h" #include template EIGEN_DONT_INLINE Scalar foo(const Scalar& x, const Scalar& y) { using namespace std; // return x+std::sin(y); EIGEN_ASM_COMMENT("mybegin"); return static_cast(x*2 - pow(x,2) + 2*sqrt(y*y) - 4 * sin(x) + 2 * cos(y) - exp(-0.5*x*x)); //return x+2*y*x;//x*2 -std::pow(x,2);//(2*y/x);// - y*2; EIGEN_ASM_COMMENT("myend"); } template EIGEN_DONT_INLINE typename Vector::Scalar foo(const Vector& p) { typedef typename Vector::Scalar Scalar; return (p-Vector(Scalar(-1),Scalar(1.))).norm() + (p.array() * p.array()).sum() + p.dot(p); } template struct TestFunc1 { typedef _Scalar Scalar; enum { InputsAtCompileTime = NX, ValuesAtCompileTime = NY }; typedef Matrix InputType; typedef Matrix ValueType; typedef Matrix JacobianType; int m_inputs, m_values; TestFunc1() : m_inputs(InputsAtCompileTime), m_values(ValuesAtCompileTime) {} TestFunc1(int inputs, int values) : m_inputs(inputs), m_values(values) {} int inputs() const { return m_inputs; } int values() const { return m_values; } template void operator() (const Matrix& x, Matrix* _v) const { Matrix& 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]; } } } }; template void forward_jacobian(const Func& f) { 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 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); } void test_autodiff_scalar() { std::cerr << foo(1,2) << "\n"; typedef AutoDiffScalar AD; AD ax(1,Vector2f::UnitX()); AD ay(2,Vector2f::UnitY()); AD res = foo(ax,ay); std::cerr << res.value() << " <> " << res.derivatives().transpose() << "\n\n"; } void test_autodiff_vector() { std::cerr << foo(Vector2f(1,2)) << "\n"; typedef AutoDiffScalar AD; typedef Matrix VectorAD; VectorAD p(AD(1),AD(-1)); p.x().derivatives() = Vector2f::UnitX(); p.y().derivatives() = Vector2f::UnitY(); AD res = foo(p); std::cerr << res.value() << " <> " << res.derivatives().transpose() << "\n\n"; } void test_autodiff_jacobian() { for(int i = 0; i < g_repeat; i++) { CALL_SUBTEST(( forward_jacobian(TestFunc1()) )); CALL_SUBTEST(( forward_jacobian(TestFunc1()) )); CALL_SUBTEST(( forward_jacobian(TestFunc1()) )); CALL_SUBTEST(( forward_jacobian(TestFunc1()) )); CALL_SUBTEST(( forward_jacobian(TestFunc1(3,3)) )); } } void test_autodiff() { test_autodiff_scalar(); test_autodiff_vector(); // test_autodiff_jacobian(); }