* return the number of evaluation of functor */
| 62 | * return the number of evaluation of functor |
| 63 | */ |
| 64 | int df(const InputType& _x, JacobianType &jac) const |
| 65 | { |
| 66 | using std::sqrt; |
| 67 | using std::abs; |
| 68 | /* Local variables */ |
| 69 | Scalar h; |
| 70 | int nfev=0; |
| 71 | const typename InputType::Index n = _x.size(); |
| 72 | const Scalar eps = sqrt(((std::max)(epsfcn,NumTraits<Scalar>::epsilon() ))); |
| 73 | ValueType val1, val2; |
| 74 | InputType x = _x; |
| 75 | // TODO : we should do this only if the size is not already known |
| 76 | val1.resize(Functor::values()); |
| 77 | val2.resize(Functor::values()); |
| 78 | |
| 79 | // initialization |
| 80 | switch(mode) { |
| 81 | case Forward: |
| 82 | // compute f(x) |
| 83 | Functor::operator()(x, val1); nfev++; |
| 84 | break; |
| 85 | case Central: |
| 86 | // do nothing |
| 87 | break; |
| 88 | default: |
| 89 | eigen_assert(false); |
| 90 | }; |
| 91 | |
| 92 | // Function Body |
| 93 | for (int j = 0; j < n; ++j) { |
| 94 | h = eps * abs(x[j]); |
| 95 | if (h == 0.) { |
| 96 | h = eps; |
| 97 | } |
| 98 | switch(mode) { |
| 99 | case Forward: |
| 100 | x[j] += h; |
| 101 | Functor::operator()(x, val2); |
| 102 | nfev++; |
| 103 | x[j] = _x[j]; |
| 104 | jac.col(j) = (val2-val1)/h; |
| 105 | break; |
| 106 | case Central: |
| 107 | x[j] += h; |
| 108 | Functor::operator()(x, val2); nfev++; |
| 109 | x[j] -= 2*h; |
| 110 | Functor::operator()(x, val1); nfev++; |
| 111 | x[j] = _x[j]; |
| 112 | jac.col(j) = (val2-val1)/(2*h); |
| 113 | break; |
| 114 | default: |
| 115 | eigen_assert(false); |
| 116 | }; |
| 117 | } |
| 118 | return nfev; |
| 119 | } |
| 120 | private: |
| 121 | Scalar epsfcn; |
no test coverage detected