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.

114 lines
2.8 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2009 Thomas Capricelli <orzel@freehackers.org>
  5. #include <stdio.h>
  6. #include "main.h"
  7. #include <unsupported/StormEigen/NumericalDiff>
  8. // Generic functor
  9. template<typename _Scalar, int NX=Dynamic, int NY=Dynamic>
  10. struct Functor
  11. {
  12. typedef _Scalar Scalar;
  13. enum {
  14. InputsAtCompileTime = NX,
  15. ValuesAtCompileTime = NY
  16. };
  17. typedef Matrix<Scalar,InputsAtCompileTime,1> InputType;
  18. typedef Matrix<Scalar,ValuesAtCompileTime,1> ValueType;
  19. typedef Matrix<Scalar,ValuesAtCompileTime,InputsAtCompileTime> JacobianType;
  20. int m_inputs, m_values;
  21. Functor() : m_inputs(InputsAtCompileTime), m_values(ValuesAtCompileTime) {}
  22. Functor(int inputs, int values) : m_inputs(inputs), m_values(values) {}
  23. int inputs() const { return m_inputs; }
  24. int values() const { return m_values; }
  25. };
  26. struct my_functor : Functor<double>
  27. {
  28. my_functor(void): Functor<double>(3,15) {}
  29. int operator()(const VectorXd &x, VectorXd &fvec) const
  30. {
  31. double tmp1, tmp2, tmp3;
  32. double y[15] = {1.4e-1, 1.8e-1, 2.2e-1, 2.5e-1, 2.9e-1, 3.2e-1, 3.5e-1,
  33. 3.9e-1, 3.7e-1, 5.8e-1, 7.3e-1, 9.6e-1, 1.34, 2.1, 4.39};
  34. for (int i = 0; i < values(); i++)
  35. {
  36. tmp1 = i+1;
  37. tmp2 = 16 - i - 1;
  38. tmp3 = (i>=8)? tmp2 : tmp1;
  39. fvec[i] = y[i] - (x[0] + tmp1/(x[1]*tmp2 + x[2]*tmp3));
  40. }
  41. return 0;
  42. }
  43. int actual_df(const VectorXd &x, MatrixXd &fjac) const
  44. {
  45. double tmp1, tmp2, tmp3, tmp4;
  46. for (int i = 0; i < values(); i++)
  47. {
  48. tmp1 = i+1;
  49. tmp2 = 16 - i - 1;
  50. tmp3 = (i>=8)? tmp2 : tmp1;
  51. tmp4 = (x[1]*tmp2 + x[2]*tmp3); tmp4 = tmp4*tmp4;
  52. fjac(i,0) = -1;
  53. fjac(i,1) = tmp1*tmp2/tmp4;
  54. fjac(i,2) = tmp1*tmp3/tmp4;
  55. }
  56. return 0;
  57. }
  58. };
  59. void test_forward()
  60. {
  61. VectorXd x(3);
  62. MatrixXd jac(15,3);
  63. MatrixXd actual_jac(15,3);
  64. my_functor functor;
  65. x << 0.082, 1.13, 2.35;
  66. // real one
  67. functor.actual_df(x, actual_jac);
  68. // std::cout << actual_jac << std::endl << std::endl;
  69. // using NumericalDiff
  70. NumericalDiff<my_functor> numDiff(functor);
  71. numDiff.df(x, jac);
  72. // std::cout << jac << std::endl;
  73. VERIFY_IS_APPROX(jac, actual_jac);
  74. }
  75. void test_central()
  76. {
  77. VectorXd x(3);
  78. MatrixXd jac(15,3);
  79. MatrixXd actual_jac(15,3);
  80. my_functor functor;
  81. x << 0.082, 1.13, 2.35;
  82. // real one
  83. functor.actual_df(x, actual_jac);
  84. // using NumericalDiff
  85. NumericalDiff<my_functor,Central> numDiff(functor);
  86. numDiff.df(x, jac);
  87. VERIFY_IS_APPROX(jac, actual_jac);
  88. }
  89. void test_NumericalDiff()
  90. {
  91. CALL_SUBTEST(test_forward());
  92. CALL_SUBTEST(test_central());
  93. }