CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
det_grad_33.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_SPEED_DET_GRAD_33_HPP
2 # define CPPAD_SPEED_DET_GRAD_33_HPP
3 
4 /* --------------------------------------------------------------------------
5 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
6 
7 CppAD is distributed under multiple licenses. This distribution is under
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.
12 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
13 -------------------------------------------------------------------------- */
14 /*
15 $begin det_grad_33$$
16 $spell
17  cppad
18  CppAD
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 
30 $head Syntax$$
31 $codei%# include <cppad/speed/det_grad_33.hpp>
32 %$$
33 $icode%ok% = det_grad_33(%x%, %g%)%$$
34 
35 $head Purpose$$
36 This routine can be used to check a method for computing the
37 gradient of the determinant of a matrix.
38 
39 $head Inclusion$$
40 The template function $code det_grad_33$$ is defined in the $code CppAD$$
41 namespace by including
42 the file $code cppad/speed/det_grad_33.hpp$$
43 (relative to the CppAD distribution directory).
44 
45 $head x$$
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 
55 $head g$$
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 
66 $head Vector$$
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 
77 $head ok$$
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%
86  omh/det_grad_33_hpp.omh
87 %$$
88 
89 $head Source Code$$
90 The file
91 $cref det_grad_33.hpp$$
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