| 27 | |
| 28 | template <typename T> |
| 29 | typename T::matrix_type operator()(const T& x) const |
| 30 | { |
| 31 | // T must be some sort of dlib matrix |
| 32 | COMPILE_TIME_ASSERT(is_matrix<T>::value); |
| 33 | |
| 34 | typename T::matrix_type der(x.size()); |
| 35 | typename T::matrix_type e(x); |
| 36 | for (long i = 0; i < x.size(); ++i) |
| 37 | { |
| 38 | const double old_val = e(i); |
| 39 | |
| 40 | e(i) += eps; |
| 41 | const double delta_plus = f(e); |
| 42 | e(i) = old_val - eps; |
| 43 | const double delta_minus = f(e); |
| 44 | |
| 45 | der(i) = (delta_plus - delta_minus)/((old_val+eps)-(old_val-eps)); |
| 46 | |
| 47 | // and finally restore the old value of this element |
| 48 | e(i) = old_val; |
| 49 | } |
| 50 | |
| 51 | return der; |
| 52 | } |
| 53 | |
| 54 | template <typename T, typename U> |
| 55 | typename U::matrix_type operator()(const T& item, const U& x) const |