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.

220 lines
6.3 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/StormEigen/AutoDiff>
  11. template<typename Scalar>
  12. STORMEIGEN_DONT_INLINE Scalar foo(const Scalar& x, const Scalar& y)
  13. {
  14. using namespace std;
  15. // return x+std::sin(y);
  16. STORMEIGEN_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. STORMEIGEN_ASM_COMMENT("myend");
  20. }
  21. template<typename Vector>
  22. STORMEIGEN_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. // TODO also check actual derivatives!
  111. template <int>
  112. void test_autodiff_scalar()
  113. {
  114. Vector2f p = Vector2f::Random();
  115. typedef AutoDiffScalar<Vector2f> AD;
  116. AD ax(p.x(),Vector2f::UnitX());
  117. AD ay(p.y(),Vector2f::UnitY());
  118. AD res = foo<AD>(ax,ay);
  119. VERIFY_IS_APPROX(res.value(), foo(p.x(),p.y()));
  120. }
  121. // TODO also check actual derivatives!
  122. template <int>
  123. void test_autodiff_vector()
  124. {
  125. Vector2f p = Vector2f::Random();
  126. typedef AutoDiffScalar<Vector2f> AD;
  127. typedef Matrix<AD,2,1> VectorAD;
  128. VectorAD ap = p.cast<AD>();
  129. ap.x().derivatives() = Vector2f::UnitX();
  130. ap.y().derivatives() = Vector2f::UnitY();
  131. AD res = foo<VectorAD>(ap);
  132. VERIFY_IS_APPROX(res.value(), foo(p));
  133. }
  134. template <int>
  135. void test_autodiff_jacobian()
  136. {
  137. CALL_SUBTEST(( forward_jacobian(TestFunc1<double,2,2>()) ));
  138. CALL_SUBTEST(( forward_jacobian(TestFunc1<double,2,3>()) ));
  139. CALL_SUBTEST(( forward_jacobian(TestFunc1<double,3,2>()) ));
  140. CALL_SUBTEST(( forward_jacobian(TestFunc1<double,3,3>()) ));
  141. CALL_SUBTEST(( forward_jacobian(TestFunc1<double>(3,3)) ));
  142. }
  143. template <int>
  144. void test_autodiff_hessian()
  145. {
  146. typedef AutoDiffScalar<VectorXd> AD;
  147. typedef Matrix<AD,StormEigen::Dynamic,1> VectorAD;
  148. typedef AutoDiffScalar<VectorAD> ADD;
  149. typedef Matrix<ADD,StormEigen::Dynamic,1> VectorADD;
  150. VectorADD x(2);
  151. double s1 = internal::random<double>(), s2 = internal::random<double>(), s3 = internal::random<double>(), s4 = internal::random<double>();
  152. x(0).value()=s1;
  153. x(1).value()=s2;
  154. //set unit vectors for the derivative directions (partial derivatives of the input vector)
  155. x(0).derivatives().resize(2);
  156. x(0).derivatives().setZero();
  157. x(0).derivatives()(0)= 1;
  158. x(1).derivatives().resize(2);
  159. x(1).derivatives().setZero();
  160. x(1).derivatives()(1)=1;
  161. //repeat partial derivatives for the inner AutoDiffScalar
  162. x(0).value().derivatives() = VectorXd::Unit(2,0);
  163. x(1).value().derivatives() = VectorXd::Unit(2,1);
  164. //set the hessian matrix to zero
  165. for(int idx=0; idx<2; idx++) {
  166. x(0).derivatives()(idx).derivatives() = VectorXd::Zero(2);
  167. x(1).derivatives()(idx).derivatives() = VectorXd::Zero(2);
  168. }
  169. ADD y = sin(AD(s3)*x(0) + AD(s4)*x(1));
  170. VERIFY_IS_APPROX(y.value().derivatives()(0), y.derivatives()(0).value());
  171. VERIFY_IS_APPROX(y.value().derivatives()(1), y.derivatives()(1).value());
  172. VERIFY_IS_APPROX(y.value().derivatives()(0), s3*std::cos(s1*s3+s2*s4));
  173. VERIFY_IS_APPROX(y.value().derivatives()(1), s4*std::cos(s1*s3+s2*s4));
  174. VERIFY_IS_APPROX(y.derivatives()(0).derivatives(), -std::sin(s1*s3+s2*s4)*Vector2d(s3*s3,s4*s3));
  175. VERIFY_IS_APPROX(y.derivatives()(1).derivatives(), -std::sin(s1*s3+s2*s4)*Vector2d(s3*s4,s4*s4));
  176. }
  177. void test_autodiff()
  178. {
  179. for(int i = 0; i < g_repeat; i++) {
  180. CALL_SUBTEST_1( test_autodiff_scalar<1>() );
  181. CALL_SUBTEST_2( test_autodiff_vector<1>() );
  182. CALL_SUBTEST_3( test_autodiff_jacobian<1>() );
  183. CALL_SUBTEST_4( test_autodiff_hessian<1>() );
  184. }
  185. }