CppAD: A C++ Algorithmic Differentiation Package  20171217
det_33.hpp
Go to the documentation of this file.
3
4 /* --------------------------------------------------------------------------
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 /*
15 \$begin det_33\$\$
16 \$spell
19  det
20  namespace
21  const
22  bool
23  hpp
24 \$\$
25
26 \$section Check Determinant of 3 by 3 matrix\$\$
27 \$mindex det_33 correct\$\$
28
29
32 %\$\$
33 \$icode%ok% = det_33(%x%, %d%)%\$\$
34
36 This routine can be used to check a method for computing
37 the determinant of a matrix.
38
40 The template function \$code det_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 d\$\$ has prototype
57 \$codei%
58  const %Vector% &%d%
59 %\$\$.
60 It is tested to see if \$icode%d%[0]%\$\$ it is equal to \$latex \det ( X )\$\$.
61
63 If \$icode y\$\$ is a \$icode Vector\$\$ object,
64 it must support the syntax
65 \$codei%
66  %y%[%i%]
67 %\$\$
68 where \$icode i\$\$ has type \$code size_t\$\$ with value less than 9.
69 This must return a \$code double\$\$ value corresponding to the \$th i\$\$
70 element of the vector \$icode y\$\$.
71 This is the only requirement of the type \$icode Vector\$\$.
72 (Note that only the first element of the vector \$icode d\$\$ is used.)
73
75 The return value \$icode ok\$\$ has prototype
76 \$codei%
77  bool %ok%
78 %\$\$
79 It is true, if the determinant \$icode%d%[0]%\$\$
80 passes the test and false otherwise.
81
82 \$children%
83  omh/det_33_hpp.omh
84 %\$\$
85
87 The file
88 \$cref det_33.hpp\$\$
89 contains the source code for this template function.
90
91 \$end
92 ------------------------------------------------------------------------------
93 */
94 // BEGIN C++
97 template <class Vector>
98  bool det_33(const Vector &x, const Vector &d)
99  { bool ok = true;
100  double eps99 = 99.0 * std::numeric_limits<double>::epsilon();
101
102  // use expansion by minors to compute the determinant by hand
103  double check = 0.;
104  check += x[0] * ( x[4] * x[8] - x[5] * x[7] );
105  check -= x[1] * ( x[3] * x[8] - x[5] * x[6] );
106  check += x[2] * ( x[3] * x[7] - x[4] * x[6] );
107
108  ok &= CppAD::NearEqual(check, d[0], eps99, eps99);
109
110  return ok;
111  }
112 }
113 // END C++
114 # endif
bool det_33(const Vector &x, const Vector &d)
Definition: det_33.hpp:98
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