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.

143 lines
3.7 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008 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 <Eigen/Dense>
  11. #define NUMBER_DIRECTIONS 16
  12. #include <unsupported/Eigen/AdolcForward>
  13. int adtl::ADOLC_numDir;
  14. template<typename Vector>
  15. EIGEN_DONT_INLINE typename Vector::Scalar foo(const Vector& p)
  16. {
  17. typedef typename Vector::Scalar Scalar;
  18. return (p-Vector(Scalar(-1),Scalar(1.))).norm() + (p.array().sqrt().abs() * p.array().sin()).sum() + p.dot(p);
  19. }
  20. template<typename _Scalar, int NX=Dynamic, int NY=Dynamic>
  21. struct TestFunc1
  22. {
  23. typedef _Scalar Scalar;
  24. enum {
  25. InputsAtCompileTime = NX,
  26. ValuesAtCompileTime = NY
  27. };
  28. typedef Matrix<Scalar,InputsAtCompileTime,1> InputType;
  29. typedef Matrix<Scalar,ValuesAtCompileTime,1> ValueType;
  30. typedef Matrix<Scalar,ValuesAtCompileTime,InputsAtCompileTime> JacobianType;
  31. int m_inputs, m_values;
  32. TestFunc1() : m_inputs(InputsAtCompileTime), m_values(ValuesAtCompileTime) {}
  33. TestFunc1(int inputs, int values) : m_inputs(inputs), m_values(values) {}
  34. int inputs() const { return m_inputs; }
  35. int values() const { return m_values; }
  36. template<typename T>
  37. void operator() (const Matrix<T,InputsAtCompileTime,1>& x, Matrix<T,ValuesAtCompileTime,1>* _v) const
  38. {
  39. Matrix<T,ValuesAtCompileTime,1>& v = *_v;
  40. v[0] = 2 * x[0] * x[0] + x[0] * x[1];
  41. v[1] = 3 * x[1] * x[0] + 0.5 * x[1] * x[1];
  42. if(inputs()>2)
  43. {
  44. v[0] += 0.5 * x[2];
  45. v[1] += x[2];
  46. }
  47. if(values()>2)
  48. {
  49. v[2] = 3 * x[1] * x[0] * x[0];
  50. }
  51. if (inputs()>2 && values()>2)
  52. v[2] *= x[2];
  53. }
  54. void operator() (const InputType& x, ValueType* v, JacobianType* _j) const
  55. {
  56. (*this)(x, v);
  57. if(_j)
  58. {
  59. JacobianType& j = *_j;
  60. j(0,0) = 4 * x[0] + x[1];
  61. j(1,0) = 3 * x[1];
  62. j(0,1) = x[0];
  63. j(1,1) = 3 * x[0] + 2 * 0.5 * x[1];
  64. if (inputs()>2)
  65. {
  66. j(0,2) = 0.5;
  67. j(1,2) = 1;
  68. }
  69. if(values()>2)
  70. {
  71. j(2,0) = 3 * x[1] * 2 * x[0];
  72. j(2,1) = 3 * x[0] * x[0];
  73. }
  74. if (inputs()>2 && values()>2)
  75. {
  76. j(2,0) *= x[2];
  77. j(2,1) *= x[2];
  78. j(2,2) = 3 * x[1] * x[0] * x[0];
  79. j(2,2) = 3 * x[1] * x[0] * x[0];
  80. }
  81. }
  82. }
  83. };
  84. template<typename Func> void adolc_forward_jacobian(const Func& f)
  85. {
  86. typename Func::InputType x = Func::InputType::Random(f.inputs());
  87. typename Func::ValueType y(f.values()), yref(f.values());
  88. typename Func::JacobianType j(f.values(),f.inputs()), jref(f.values(),f.inputs());
  89. jref.setZero();
  90. yref.setZero();
  91. f(x,&yref,&jref);
  92. // std::cerr << y.transpose() << "\n\n";;
  93. // std::cerr << j << "\n\n";;
  94. j.setZero();
  95. y.setZero();
  96. AdolcForwardJacobian<Func> autoj(f);
  97. autoj(x, &y, &j);
  98. // std::cerr << y.transpose() << "\n\n";;
  99. // std::cerr << j << "\n\n";;
  100. VERIFY_IS_APPROX(y, yref);
  101. VERIFY_IS_APPROX(j, jref);
  102. }
  103. void test_forward_adolc()
  104. {
  105. adtl::ADOLC_numDir = NUMBER_DIRECTIONS;
  106. for(int i = 0; i < g_repeat; i++) {
  107. CALL_SUBTEST(( adolc_forward_jacobian(TestFunc1<double,2,2>()) ));
  108. CALL_SUBTEST(( adolc_forward_jacobian(TestFunc1<double,2,3>()) ));
  109. CALL_SUBTEST(( adolc_forward_jacobian(TestFunc1<double,3,2>()) ));
  110. CALL_SUBTEST(( adolc_forward_jacobian(TestFunc1<double,3,3>()) ));
  111. CALL_SUBTEST(( adolc_forward_jacobian(TestFunc1<double>(3,3)) ));
  112. }
  113. {
  114. // simple instanciation tests
  115. Matrix<adtl::adouble,2,1> x;
  116. foo(x);
  117. Matrix<adtl::adouble,Dynamic,Dynamic> A(4,4);;
  118. A.selfadjointView<Lower>().eigenvalues();
  119. }
  120. }