Main MRPT website > C++ reference for MRPT 1.9.9
num_jacobian.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/core/exceptions.h>
12 #include <functional>
13 
14 namespace mrpt
15 {
16 namespace math
17 {
18 /** Estimate the Jacobian of a multi-dimensional function around a point "x",
19  * using finite differences of a given size in each input dimension.
20  * The template argument USERPARAM is for the data can be passed to the
21  * functor.
22  * If it is not required, set to "int" or any other basic type.
23  *
24  * This is a generic template which works with:
25  * VECTORLIKE: vector_float, CVectorDouble, CArrayNumeric<>, double [N],
26  * ...
27  * MATRIXLIKE: CMatrixTemplateNumeric, CMatrixFixedNumeric
28  */
29 template <class VECTORLIKE, class VECTORLIKE2, class VECTORLIKE3,
30  class MATRIXLIKE, class USERPARAM>
32  const VECTORLIKE& x,
33  const std::function<void(
34  const VECTORLIKE& x, const USERPARAM& y, VECTORLIKE3& out)>& functor,
35  const VECTORLIKE2& increments, const USERPARAM& userParam,
36  MATRIXLIKE& out_Jacobian)
37 {
39  ASSERT_(x.size() > 0 && increments.size() == x.size());
40 
41  size_t m = 0; // will determine automatically on the first call to "f":
42  const size_t n = x.size();
43 
44  for (size_t j = 0; j < n; j++)
45  {
46  ASSERT_(increments[j] > 0);
47  } // Who knows...
48 
49  VECTORLIKE3 f_minus, f_plus;
50  VECTORLIKE x_mod(x);
51 
52  // Evaluate the function "i" with increments in the "j" input x variable:
53  for (size_t j = 0; j < n; j++)
54  {
55  // Create the modified "x" vector:
56  x_mod[j] = x[j] + increments[j];
57  functor(x_mod, userParam, f_plus);
58 
59  x_mod[j] = x[j] - increments[j];
60  functor(x_mod, userParam, f_minus);
61 
62  x_mod[j] = x[j]; // Leave as original
63  const double Ax_2_inv = 0.5 / increments[j];
64 
65  // The first time?
66  if (j == 0)
67  {
68  m = f_plus.size();
69  out_Jacobian.setSize(m, n);
70  }
71 
72  for (size_t i = 0; i < m; i++)
73  out_Jacobian.get_unsafe(i, j) = Ax_2_inv * (f_plus[i] - f_minus[i]);
74 
75  } // end for j
76 
77  MRPT_END
78 }
79 
80 } // End of MATH namespace
81 } // End of namespace
n
GLenum GLsizei n
Definition: glext.h:5074
exceptions.h
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
MRPT_START
#define MRPT_START
Definition: exceptions.h:262
MRPT_END
#define MRPT_END
Definition: exceptions.h:266
mrpt::math::estimateJacobian
void estimateJacobian(const VECTORLIKE &x, const std::function< void(const VECTORLIKE &x, const USERPARAM &y, VECTORLIKE3 &out)> &functor, const VECTORLIKE2 &increments, const USERPARAM &userParam, MATRIXLIKE &out_Jacobian)
Estimate the Jacobian of a multi-dimensional function around a point "x", using finite differences of...
Definition: num_jacobian.h:31
y
GLenum GLint GLint y
Definition: glext.h:3538
x
GLenum GLint x
Definition: glext.h:3538



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST