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.

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