fmatvec  0.0.0
checkderivative.h
1 #ifndef _FMATVEC_CHECKDERIVATIVE_H_
2 #define _FMATVEC_CHECKDERIVATIVE_H_
3 
4 #include <map>
5 #include <functional>
6 #include <string>
7 #include <iostream>
8 #include <fmatvec/linear_algebra.h>
9 
10 namespace fmatvec {
11 
12  #define FMATVEC_CHECKDERIVATIVE_MEMBERFUNC(func, anaDiff, value, /*indep,*/ ...) \
13  fmatvec::checkDerivative(this, std::string(__FILE__)+":"+std::to_string(__LINE__)+":"+#anaDiff, func, anaDiff, value, /*indep,*/ __VA_ARGS__)
14  #define FMATVEC_CHECKDERIVATIVE_GLOBALFUNC(func, anaDiff, value, /*indep,*/ ...) \
15  fmatvec::checkDerivative(nullptr, std::string(__FILE__)+":"+std::to_string(__LINE__)+":"+#anaDiff, func, anaDiff, value, /*indep,*/ __VA_ARGS__)
16 
17  // Check a analytical derivative against the finite difference.
18  // Any local variable for which a analytical derivative is given can be checked.
19  // - idPtr and idName must, together, be a unique identifier for this call. Usually idPtr is "this" and idName is the name of the variable to be checked.
20  // - func is the callers function with the independent variable as input.
21  // - anaDiff is the analytical derivative which should be checked.
22  // - value is the value for which the analytical derivative should be checked.
23  // - indep is the independent variable with respect to which the derivative is calculated.
24  // - eps is the tolerance. If anaDiff differs more than this value a error is printed.
25  // - delta is the finite difference for the finite derivative calculation.
26  template<class Value>
27  void checkDerivative(const void *idPtr, const std::string &idName, const std::function<void(double)> &func, const Value &anaDiff, const Value &value, double indep, double eps=1e-6, double delta=1e-8) {
28  #ifdef NDEBUG
29  #error "fmatvec::checkDerivative should not be active for release builds"
30  #endif
31 
32  static int distributedCount = 0;
33  static std::map<std::pair<const void*, std::string>, std::pair<bool, Value>> ele;
34 
35  auto &[disturbed, disturbedValue] = ele.emplace(std::make_pair(idPtr, idName), std::make_pair(false, Value())).first->second;
36 
37  if(disturbed) { // this is a distributed call (for the key in "ele") ..
38  // ... save the distributed value (in "ele" map)
39  disturbedValue = value;
40  }
41  else if(distributedCount == 0) { // this is a normal call (not distributed) ...
42  std::array<Value, 2> disturbedValueRightLeft;
43 
44  // .. make a distributed call for the current key in "ele"
45  disturbed = true;
46  distributedCount++;
47 
48  func(indep + delta); // right disturbed
49  // save the distributed value (scalar or fmatvec vec/mat)
50  if constexpr (std::is_same_v<Value, double>)
51  disturbedValueRightLeft[0] = disturbedValue;
52  else
53  disturbedValueRightLeft[0] <<= disturbedValue;
54 
55  func(indep - delta); // left disturbed
56  // save the distributed value (scalar or fmatvec vec/mat)
57  if constexpr (std::is_same_v<Value, double>)
58  disturbedValueRightLeft[1] = disturbedValue;
59  else
60  disturbedValueRightLeft[1] <<= disturbedValue;
61 
62  func(indep); // undisturbed again to reset everything
63 
64  disturbed = false;
65  distributedCount--;
66 
67  // calculate the right and left finite difference and its norm
68  std::array<double, 2> dist;
69  std::array<Value, 2> finiteDiffRightLeft;
70  for(int i=0; i<2; ++i) {
71  finiteDiffRightLeft[i] = (i==0?1.0:-1.0) * (disturbedValueRightLeft[i] - value)/delta;
72 
73  if constexpr (std::is_same_v<Value, double>)
74  dist[i] = std::abs(finiteDiffRightLeft[i] - anaDiff);
75  else if constexpr (Value::isVector)
76  dist[i] = fmatvec::nrmInf(finiteDiffRightLeft[i] - anaDiff);
77  else {
78  dist[i] = 0;
79  for(int c=0; c<anaDiff.cols(); c++)
80  dist[i] = std::max(dist[i], fmatvec::nrm2(finiteDiffRightLeft[i].col(c) - anaDiff.col(c)));
81  }
82  }
83 
84  // if both, the right and the left finite difference, differs from analytically derivative value print a error message
85  double dist0 = dist[0]; // MSVS cannot compile "dist[0] > eps" directly
86  double dist1 = dist[1];
87  if(dist0 > eps && dist1 > eps) {
88  std::stringstream str;
89  str<<"checkDerivative failed in "<<idPtr<<": ID = "<<idName<<":"<<std::endl
90  <<"- anaDiff = "<<anaDiff<<std::endl
91  <<"- finiteDiffRight = "<<finiteDiffRightLeft[0]<<std::endl
92  <<"- finiteDiffLeft = "<<finiteDiffRightLeft[1]<<std::endl
93  <<"- indep = "<<indep<<std::endl
94  <<"- nrmInf (right/left) = "<<dist[0]<<" "<<dist[1];
95  //throw std::runtime_error(str.str());
96  std::cerr<<str.str()<<std::endl;
97  assert(0);
98  throw std::runtime_error(str.str());
99  }
100  }
101  }
102 
103 }
104 
105 #endif
double nrmInf(const Matrix< Symmetric, Ref, Ref, double > &A)
Infinity-norm.
Definition: linear_algebra_double.cc:820
Namespace fmatvec.
Definition: _memory.cc:28