CppAD: A C++ Algorithmic Differentiation Package  20171217
Go to the documentation of this file.
3
4 /* --------------------------------------------------------------------------
5 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
6
8 the terms of the
9  Eclipse Public License Version 1.0.
10
11 A copy of this license is included in the COPYING file of this distribution.
13 -------------------------------------------------------------------------- */
14 /*
16 \$spell
19  det
20  namespace
21  const
22  bool
23  hpp
24 \$\$
25
26 \$section Check Gradient of Determinant of 3 by 3 matrix\$\$
27 \$mindex det_grad_33 correct\$\$
28
29
32 %\$\$
33 \$icode%ok% = det_grad_33(%x%, %g%)%\$\$
34
36 This routine can be used to check a method for computing the
37 gradient of the determinant of a matrix.
38
40 The template function \$code det_grad_33\$\$ is defined in the \$code CppAD\$\$
41 namespace by including
43 (relative to the CppAD distribution directory).
44
46 The argument \$icode x\$\$ has prototype
47 \$codei%
48  const %Vector% &%x%
49 %\$\$.
50 It contains the elements of the matrix \$latex X\$\$ in row major order; i.e.,
51 \$latex \[
52  X_{i,j} = x [ i * 3 + j ]
53 \] \$\$
54
56 The argument \$icode g\$\$ has prototype
57 \$codei%
58  const %Vector% &%g%
59 %\$\$.
60 It contains the elements of the gradient of
61 \$latex \det ( X )\$\$ in row major order; i.e.,
62 \$latex \[
63  \D{\det (X)}{X(i,j)} = g [ i * 3 + j ]
64 \] \$\$
65
67 If \$icode y\$\$ is a \$icode Vector\$\$ object,
68 it must support the syntax
69 \$codei%
70  %y%[%i%]
71 %\$\$
72 where \$icode i\$\$ has type \$code size_t\$\$ with value less than 9.
73 This must return a \$code double\$\$ value corresponding to the \$th i\$\$
74 element of the vector \$icode y\$\$.
75 This is the only requirement of the type \$icode Vector\$\$.
76
78 The return value \$icode ok\$\$ has prototype
79 \$codei%
80  bool %ok%
81 %\$\$
82 It is true, if the gradient \$icode g\$\$
83 passes the test and false otherwise.
84
85 \$children%
87 %\$\$
88
89 \$head Source Code\$\$
90 The file
92 contains the source code for this template function.
93
94 \$end
95 ------------------------------------------------------------------------------
96 */
97 // BEGIN C++
98 # include <limits>
100 namespace CppAD {
101 template <class Vector>
102  bool det_grad_33(const Vector &x, const Vector &g)
103  { bool ok = true;
104  typedef typename Vector::value_type Float;
105  Float eps = 10. * Float( std::numeric_limits<double>::epsilon() );
106
107  // use expansion by minors to compute the derivative by hand
108  double check[9];
109  check[0] = + ( x[4] * x[8] - x[5] * x[7] );
110  check[1] = - ( x[3] * x[8] - x[5] * x[6] );
111  check[2] = + ( x[3] * x[7] - x[4] * x[6] );
112  //
113  check[3] = - ( x[1] * x[8] - x[2] * x[7] );
114  check[4] = + ( x[0] * x[8] - x[2] * x[6] );
115  check[5] = - ( x[0] * x[7] - x[1] * x[6] );
116  //
117  check[6] = + ( x[1] * x[5] - x[2] * x[4] );
118  check[7] = - ( x[0] * x[5] - x[2] * x[3] );
119  check[8] = + ( x[0] * x[4] - x[1] * x[3] );
120  //
121  for(size_t i = 0; i < 3 * 3; i++)
122  ok &= CppAD::NearEqual(check[i], g[i], eps, eps);
123
124  return ok;
125  }
126 }
127 // END C++
128 # endif
bool det_grad_33(const Vector &x, const Vector &g)
Type epsilon(void)
Definition: epsilon.hpp:56
bool NearEqual(const Type &x, const Type &y, const Type &r, const Type &a)
Definition: near_equal.hpp:168
Scalar value_type