00001 # ifndef CPPAD_DET_GRAD_33_INCLUDED 00002 # define CPPAD_DET_GRAD_33_INCLUDED 00003 00004 /* -------------------------------------------------------------------------- 00005 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-07 Bradley M. Bell 00006 00007 CppAD is distributed under multiple licenses. This distribution is under 00008 the terms of the 00009 Common Public License Version 1.0. 00010 00011 A copy of this license is included in the COPYING file of this distribution. 00012 Please visit http://www.coin-or.org/CppAD/ for information on other licenses. 00013 -------------------------------------------------------------------------- */ 00014 /* 00015 $begin det_grad_33$$ 00016 $spell 00017 cppad 00018 CppAD 00019 det 00020 cppad.hpp 00021 namespace 00022 const 00023 bool 00024 $$ 00025 00026 $section Check Gradient of Determinant of 3 by 3 matrix$$ 00027 00028 $index det_grad_33$$ 00029 $index determinant, check correct$$ 00030 $index correct, determinant check$$ 00031 $index check, determinant correct$$ 00032 00033 $head Syntax$$ 00034 $syntax%# include <cppad/speed/det_grad_33.hpp> 00035 %$$ 00036 $syntax%%ok% = det_grad_33(%x%, %g%)%$$ 00037 00038 $head Purpose$$ 00039 This routine can be used to check a method for computing the 00040 gradient of the determinant of a matrix. 00041 00042 $head Inclusion$$ 00043 The template function $code det_grad_33$$ is defined in the $code CppAD$$ 00044 namespace by including 00045 the file $code cppad/speed/det_grad_33.hpp$$ 00046 (relative to the CppAD distribution directory). 00047 It is only intended for example and testing purposes, 00048 so it is not automatically included by 00049 $cref/cppad.hpp/cppad/$$. 00050 00051 $head x$$ 00052 The argument $italic x$$ has prototype 00053 $syntax% 00054 const %Vector% &%x% 00055 %$$. 00056 It contains the elements of the matrix $latex X$$ in row major order; i.e., 00057 $latex \[ 00058 X_{i,j} = x [ i * 3 + j ] 00059 \] $$ 00060 00061 $head g$$ 00062 The argument $italic g$$ has prototype 00063 $syntax% 00064 const %Vector% &%g% 00065 %$$. 00066 It contains the elements of the gradient of 00067 $latex \det ( X )$$ in row major order; i.e., 00068 $latex \[ 00069 \D{\det (X)}{X(i,j)} = g [ i * 3 + j ] 00070 \] $$ 00071 00072 $head Vector$$ 00073 If $italic y$$ is a $italic Vector$$ object, 00074 it must support the syntax 00075 $syntax% 00076 %y%[%i%] 00077 %$$ 00078 where $italic i$$ has type $code size_t$$ with value less than 9. 00079 This must return a $code double$$ value corresponding to the $th i$$ 00080 element of the vector $italic y$$. 00081 This is the only requirement of the type $italic Vector$$. 00082 00083 $head ok$$ 00084 The return value $italic ok$$ has prototype 00085 $syntax% 00086 bool %ok% 00087 %$$ 00088 It is true, if the gradient $italic g$$ 00089 passes the test and false otherwise. 00090 00091 $children% 00092 omh/det_grad_33_hpp.omh 00093 %$$ 00094 00095 $head Source Code$$ 00096 The file 00097 $cref/det_grad_33.hpp/$$ 00098 constraints the source code for this template function. 00099 00100 $end 00101 ------------------------------------------------------------------------------ 00102 */ 00103 // BEGIN PROGRAM 00104 namespace CppAD { 00105 template <class Vector> 00106 bool det_grad_33(const Vector &x, const Vector &g) 00107 { bool ok = true; 00108 00109 // use expansion by minors to compute the derivative by hand 00110 double check[9]; 00111 check[0] = + ( x[4] * x[8] - x[5] * x[7] ); 00112 check[1] = - ( x[3] * x[8] - x[5] * x[6] ); 00113 check[2] = + ( x[3] * x[7] - x[4] * x[6] ); 00114 // 00115 check[3] = - ( x[1] * x[8] - x[2] * x[7] ); 00116 check[4] = + ( x[0] * x[8] - x[2] * x[6] ); 00117 check[5] = - ( x[0] * x[7] - x[1] * x[6] ); 00118 // 00119 check[6] = + ( x[1] * x[5] - x[2] * x[4] ); 00120 check[7] = - ( x[0] * x[5] - x[2] * x[3] ); 00121 check[8] = + ( x[0] * x[4] - x[1] * x[3] ); 00122 // 00123 size_t i; 00124 for(i = 0; i < 3 * 3; i++) 00125 ok &= CppAD::NearEqual(check[i], g[i], 1e-10, 1e-10); 00126 00127 return ok; 00128 } 00129 } 00130 // END PROGRAM 00131 # endif