You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
4.5 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2009 Gael Guennebaud <g.gael@free.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "main.h"
  10. #include <unsupported/Eigen/AutoDiff>
  11. template<typename Scalar>
  12. EIGEN_DONT_INLINE Scalar foo(const Scalar& x, const Scalar& y)
  13. {
  14. using namespace std;
  15. // return x+std::sin(y);
  16. EIGEN_ASM_COMMENT("mybegin");
  17. return static_cast<Scalar>(x*2 - pow(x,2) + 2*sqrt(y*y) - 4 * sin(x) + 2 * cos(y) - exp(-0.5*x*x));
  18. //return x+2*y*x;//x*2 -std::pow(x,2);//(2*y/x);// - y*2;
  19. EIGEN_ASM_COMMENT("myend");
  20. }
  21. template<typename Vector>
  22. EIGEN_DONT_INLINE typename Vector::Scalar foo(const Vector& p)
  23. {
  24. typedef typename Vector::Scalar Scalar;
  25. return (p-Vector(Scalar(-1),Scalar(1.))).norm() + (p.array() * p.array()).sum() + p.dot(p);
  26. }
  27. template<typename _Scalar, int NX=Dynamic, int NY=Dynamic>
  28. struct TestFunc1
  29. {
  30. typedef _Scalar Scalar;
  31. enum {
  32. InputsAtCompileTime = NX,
  33. ValuesAtCompileTime = NY
  34. };
  35. typedef Matrix<Scalar,InputsAtCompileTime,1> InputType;
  36. typedef Matrix<Scalar,ValuesAtCompileTime,1> ValueType;
  37. typedef Matrix<Scalar,ValuesAtCompileTime,InputsAtCompileTime> JacobianType;
  38. int m_inputs, m_values;
  39. TestFunc1() : m_inputs(InputsAtCompileTime), m_values(ValuesAtCompileTime) {}
  40. TestFunc1(int inputs, int values) : m_inputs(inputs), m_values(values) {}
  41. int inputs() const { return m_inputs; }
  42. int values() const { return m_values; }
  43. template<typename T>
  44. void operator() (const Matrix<T,InputsAtCompileTime,1>& x, Matrix<T,ValuesAtCompileTime,1>* _v) const
  45. {
  46. Matrix<T,ValuesAtCompileTime,1>& v = *_v;
  47. v[0] = 2 * x[0] * x[0] + x[0] * x[1];
  48. v[1] = 3 * x[1] * x[0] + 0.5 * x[1] * x[1];
  49. if(inputs()>2)
  50. {
  51. v[0] += 0.5 * x[2];
  52. v[1] += x[2];
  53. }
  54. if(values()>2)
  55. {
  56. v[2] = 3 * x[1] * x[0] * x[0];
  57. }
  58. if (inputs()>2 && values()>2)
  59. v[2] *= x[2];
  60. }
  61. void operator() (const InputType& x, ValueType* v, JacobianType* _j) const
  62. {
  63. (*this)(x, v);
  64. if(_j)
  65. {
  66. JacobianType& j = *_j;
  67. j(0,0) = 4 * x[0] + x[1];
  68. j(1,0) = 3 * x[1];
  69. j(0,1) = x[0];
  70. j(1,1) = 3 * x[0] + 2 * 0.5 * x[1];
  71. if (inputs()>2)
  72. {
  73. j(0,2) = 0.5;
  74. j(1,2) = 1;
  75. }
  76. if(values()>2)
  77. {
  78. j(2,0) = 3 * x[1] * 2 * x[0];
  79. j(2,1) = 3 * x[0] * x[0];
  80. }
  81. if (inputs()>2 && values()>2)
  82. {
  83. j(2,0) *= x[2];
  84. j(2,1) *= x[2];
  85. j(2,2) = 3 * x[1] * x[0] * x[0];
  86. j(2,2) = 3 * x[1] * x[0] * x[0];
  87. }
  88. }
  89. }
  90. };
  91. template<typename Func> void forward_jacobian(const Func& f)
  92. {
  93. typename Func::InputType x = Func::InputType::Random(f.inputs());
  94. typename Func::ValueType y(f.values()), yref(f.values());
  95. typename Func::JacobianType j(f.values(),f.inputs()), jref(f.values(),f.inputs());
  96. jref.setZero();
  97. yref.setZero();
  98. f(x,&yref,&jref);
  99. // std::cerr << y.transpose() << "\n\n";;
  100. // std::cerr << j << "\n\n";;
  101. j.setZero();
  102. y.setZero();
  103. AutoDiffJacobian<Func> autoj(f);
  104. autoj(x, &y, &j);
  105. // std::cerr << y.transpose() << "\n\n";;
  106. // std::cerr << j << "\n\n";;
  107. VERIFY_IS_APPROX(y, yref);
  108. VERIFY_IS_APPROX(j, jref);
  109. }
  110. void test_autodiff_scalar()
  111. {
  112. std::cerr << foo<float>(1,2) << "\n";
  113. typedef AutoDiffScalar<Vector2f> AD;
  114. AD ax(1,Vector2f::UnitX());
  115. AD ay(2,Vector2f::UnitY());
  116. AD res = foo<AD>(ax,ay);
  117. std::cerr << res.value() << " <> "
  118. << res.derivatives().transpose() << "\n\n";
  119. }
  120. void test_autodiff_vector()
  121. {
  122. std::cerr << foo<Vector2f>(Vector2f(1,2)) << "\n";
  123. typedef AutoDiffScalar<Vector2f> AD;
  124. typedef Matrix<AD,2,1> VectorAD;
  125. VectorAD p(AD(1),AD(-1));
  126. p.x().derivatives() = Vector2f::UnitX();
  127. p.y().derivatives() = Vector2f::UnitY();
  128. AD res = foo<VectorAD>(p);
  129. std::cerr << res.value() << " <> "
  130. << res.derivatives().transpose() << "\n\n";
  131. }
  132. void test_autodiff_jacobian()
  133. {
  134. for(int i = 0; i < g_repeat; i++) {
  135. CALL_SUBTEST(( forward_jacobian(TestFunc1<double,2,2>()) ));
  136. CALL_SUBTEST(( forward_jacobian(TestFunc1<double,2,3>()) ));
  137. CALL_SUBTEST(( forward_jacobian(TestFunc1<double,3,2>()) ));
  138. CALL_SUBTEST(( forward_jacobian(TestFunc1<double,3,3>()) ));
  139. CALL_SUBTEST(( forward_jacobian(TestFunc1<double>(3,3)) ));
  140. }
  141. }
  142. void test_autodiff()
  143. {
  144. test_autodiff_scalar();
  145. test_autodiff_vector();
  146. // test_autodiff_jacobian();
  147. }