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