Tomographer  v2.0
Tomographer C++ Framework Documentation
check_derivatives.h
Go to the documentation of this file.
1 /* This file is part of the Tomographer project, which is distributed under the
2  * terms of the MIT license.
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2015 ETH Zurich, Institute for Theoretical Physics, Philippe Faist
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26 
27 #ifndef CHECK_DERIVATIVES_H
28 #define CHECK_DERIVATIVES_H
29 
30 #include <cstddef>
31 #include <iostream>
32 
33 #include <Eigen/Core>
34 
35 #include <tomographer2/tools/cxxutil.h> // TOMO_STATIC_ASSERT_EXPR
36 
37 
46 namespace Tomographer {
47 namespace MathTools {
48 
98 template<typename Der1, typename Der2, typename fnType, typename ErrorStream>
99 bool check_derivatives(const Eigen::ArrayBase<Der1> & derivatives, const Eigen::MatrixBase<Der2> & point,
100  fnType fn, std::size_t valdims,
101  const typename Eigen::MatrixBase<Der2>::Scalar delta = 1e-6,
102  const typename Eigen::MatrixBase<Der1>::Scalar tol = 1e-6,
103  ErrorStream & error_stream = std::cerr
104  )
105 {
106  bool ok = true;
107 
108  typedef typename Eigen::MatrixBase<Der2>::Scalar XScalar;
109  typedef typename Eigen::MatrixBase<Der1>::Scalar ValScalar;
110 
113 
114  const std::size_t xdims = derivatives.cols();
115  tomographer_assert(point.rows() == (int)xdims);
116  tomographer_assert(point.cols() == (int)1);
117  tomographer_assert(derivatives.rows() == (int)valdims);
118 
121 
122  ValVector val0(valdims);
123  ValVector dval1(valdims);
124  ValVector dvalFromDer(valdims);
125 
126  XVector pt2(point.rows());
127 
128  tomographer_assert(derivatives.allFinite());
129  tomographer_assert(point.allFinite());
130 
131  // calculate the base point
132  fn(val0, point);
133 
134  tomographer_assert(val0.allFinite());
135 
136  std::size_t i;
137  for (i = 0; i < xdims; ++i) {
138  // numerically calculate the differences ...
139 
140  pt2 = point;
141  pt2(i) += delta;
142 
143  fn(dval1, pt2);
144  dval1 -= val0;
145 
146  tomographer_assert(dval1.allFinite());
147 
148  dvalFromDer = delta * derivatives.matrix().col(i);//derivatives.matrix() * (delta*dir);
149 
150  XScalar thediff = (dval1 - dvalFromDer).norm();
151 
152  if (thediff/delta > tol ) {
153  // Error in the derivative
154 
155  // direction in which we probed, for the error_stream
156  XVector dir = XVector::Zero(xdims);
157  dir(i) = 1.0;
158 
159  ok = false;
160  error_stream
161  << "Error in derivative check: Derivative wrong in direction\n"
162  << "dir = " << dir.transpose() << " [basis vector #"<<i<<"]\n"
163  << "\tpoint = \t" << point.transpose() << "\n"
164  << "\tval0 = \t" << val0.transpose() << "\n"
165  << "\tdval1 = \t" << dval1.transpose() << "\n"
166  << "\tdvalFromDer = \t"<<dvalFromDer.transpose() << "\n"
167  << "\tderivative in this direction =\n\t\t\t\t" << derivatives.transpose().block(i,0,1,valdims) << "\n"
168  << "--> difference in p2-points: \t" << thediff << "\n"
169  << "--> difference in derivatives: \t" << thediff/delta << "\n\n";
170  }
171  }
172 
173  return ok;
174 }
175 
176 
177 
178 } // MathTools
179 } // Tomographer
180 
181 
182 
183 #endif
#define TOMO_STATIC_ASSERT_EXPR(...)
Tool for static assertions without message.
Definition: cxxutil.h:59
Base namespace for the Tomographer project.
Definition: densellh.h:44
bool allFinite() const
Some C++ utilities, with a tad of C++11 tricks.
bool check_derivatives(const Eigen::ArrayBase< Der1 > &derivatives, const Eigen::MatrixBase< Der2 > &point, fnType fn, std::size_t valdims, const typename Eigen::MatrixBase< Der2 >::Scalar delta=1e-6, const typename Eigen::MatrixBase< Der1 >::Scalar tol=1e-6, ErrorStream &error_stream=std::cerr)
Check given derivatives against numerically-calculated finite differences.
Eigen::Transpose< Derived > transpose()
MatrixWrapper< Derived > matrix()