00001 # ifndef CPPAD_DET_BY_MINOR_INCLUDED 00002 # define CPPAD_DET_BY_MINOR_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_by_minor$$ 00016 $spell 00017 cppad 00018 typedef 00019 const 00020 hpp 00021 Det 00022 CppADvector 00023 namespace 00024 $$ 00025 00026 $section Determinant Using Expansion by Minors$$ 00027 00028 $index determinant, minor expansion$$ 00029 $index minor, expansion determinant$$ 00030 $index expansion, minor determinant$$ 00031 00032 00033 $head Syntax$$ 00034 $syntax%# include <cppad/speed/det_by_minor.hpp> 00035 %$$ 00036 $syntax%det_by_minor<%Scalar%> %det%(%n%) 00037 %$$ 00038 $syntax%%d% = %det%(%matrix%) 00039 %$$ 00040 00041 $head Inclusion$$ 00042 The template class $code det_by_minor$$ is defined in the $code CppAD$$ 00043 namespace by including 00044 the file $code cppad/speed/det_by_minor.hpp$$ 00045 (relative to the CppAD distribution directory). 00046 It is only intended for example and testing purposes, 00047 so it is not automatically included by 00048 $cref/cppad.hpp/cppad/$$. 00049 00050 $head Constructor$$ 00051 The syntax 00052 $syntax% 00053 det_by_minor<%Scalar%> %det%(%n%) 00054 %$$ 00055 constructs the object $italic det$$ which can be used for 00056 evaluating the determinant of $italic n$$ by $italic n$$ matrices 00057 using expansion by minors. 00058 00059 $head Scalar$$ 00060 The type $italic Scalar$$ can be any 00061 $cref/NumericType/$$ 00062 00063 $head n$$ 00064 The argument $italic n$$ has prototype 00065 $syntax% 00066 size_t %n% 00067 %$$ 00068 00069 $head det$$ 00070 The syntax 00071 $syntax% 00072 %d% = %det%(%matrix%) 00073 %$$ 00074 returns the determinant of $italic matrix$$ using expansion by minors. 00075 The argument $italic matrix$$ has prototype 00076 $syntax% 00077 const %Vector% &%matrix% 00078 %$$ 00079 It must be a $italic Vector$$ with length $latex n * n$$ and with 00080 elements of type $italic Scalar$$. 00081 The return value $italic d$$ has prototype 00082 $syntax% 00083 %Scalar% %d% 00084 %$$ 00085 00086 $head Vector$$ 00087 If $italic y$$ is a $italic Vector$$ object, 00088 it must support the syntax 00089 $syntax% 00090 %y%[%i%] 00091 %$$ 00092 where $italic i$$ has type $code size_t$$ with value less than $latex n * n$$. 00093 This must return a $italic Scalar$$ value corresponding to the $th i$$ 00094 element of the vector $italic y$$. 00095 This is the only requirement of the type $italic Vector$$. 00096 00097 $children% 00098 speed/example/det_by_minor.cpp% 00099 omh/det_by_minor_hpp.omh 00100 %$$ 00101 00102 00103 $head Example$$ 00104 The file 00105 $xref/det_by_minor.cpp/$$ 00106 contains an example and test of $code det_by_minor.hpp$$. 00107 It returns true if it succeeds and false otherwise. 00108 00109 $head Source Code$$ 00110 The file 00111 $xref/det_by_minor.hpp/$$ 00112 contains the source for this template function. 00113 00114 00115 $end 00116 --------------------------------------------------------------------------- 00117 */ 00118 // BEGIN PROGRAM 00119 # include <cppad/cppad.hpp> 00120 # include <cppad/speed/det_of_minor.hpp> 00121 00122 // BEGIN CppAD namespace 00123 namespace CppAD { 00124 00125 template <class Scalar> 00126 class det_by_minor { 00127 public: 00128 det_by_minor(size_t m) : m_(m) , r_(m + 1) , c_(m + 1), a_(m * m) 00129 { 00130 size_t i; 00131 00132 // values for r and c that correspond to entire matrix 00133 for(i = 0; i < m; i++) 00134 { r_[i] = i+1; 00135 c_[i] = i+1; 00136 } 00137 r_[m] = 0; 00138 c_[m] = 0; 00139 } 00140 00141 template <class Vector> 00142 inline Scalar operator()(const Vector &x) const 00143 { size_t i = m_ * m_; 00144 while(i--) 00145 a_[i] = x[i]; 00146 return det_of_minor(a_, m_, m_, r_, c_); 00147 } 00148 00149 private: 00150 size_t m_; 00151 00152 // made mutable because modified and then restored 00153 mutable std::vector<size_t> r_; 00154 mutable std::vector<size_t> c_; 00155 // make mutable because its value does not matter 00156 mutable std::vector<Scalar> a_; 00157 }; 00158 00159 } // END CppAD namespace 00160 // END PROGRAM 00161 # endif