CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
det_by_minor.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_SPEED_DET_BY_MINOR_HPP
2 # define CPPAD_SPEED_DET_BY_MINOR_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_by_minor$$
16 $spell
17  CppAD
18  cppad
19  typedef
20  const
21  hpp
22  Det
23  namespace
24 $$
25 
26 $section Determinant Using Expansion by Minors$$
27 
28 
29 
30 $head Syntax$$
31 $codei%# include <cppad/speed/det_by_minor.hpp>
32 %$$
33 $codei%det_by_minor<%Scalar%> %det%(%n%)
34 %$$
35 $icode%d% = %det%(%a%)
36 %$$
37 
38 $head Inclusion$$
39 The template class $code det_by_minor$$ is defined in the $code CppAD$$
40 namespace by including
41 the file $code cppad/speed/det_by_minor.hpp$$
42 (relative to the CppAD distribution directory).
43 
44 $head Constructor$$
45 The syntax
46 $codei%
47  det_by_minor<%Scalar%> %det%(%n%)
48 %$$
49 constructs the object $icode det$$ which can be used for
50 evaluating the determinant of $icode n$$ by $icode n$$ matrices
51 using expansion by minors.
52 
53 $head Scalar$$
54 The type $icode Scalar$$ must satisfy the same conditions
55 as in the function $cref/det_of_minor/det_of_minor/Scalar/$$.
56 
57 $head n$$
58 The argument $icode n$$ has prototype
59 $codei%
60  size_t %n%
61 %$$
62 
63 $head det$$
64 The syntax
65 $codei%
66  %d% = %det%(%a%)
67 %$$
68 returns the determinant of the matrix $icode A$$ using expansion by minors.
69 
70 $subhead a$$
71 The argument $icode a$$ has prototype
72 $codei%
73  const %Vector% &%a%
74 %$$
75 It must be a $icode Vector$$ with length $latex n * n$$ and with
76 elements of type $icode Scalar$$.
77 The elements of the $latex n \times n$$ matrix $latex A$$ are defined,
78 for $latex i = 0 , \ldots , n-1$$ and $latex j = 0 , \ldots , n-1$$, by
79 $latex \[
80  A_{i,j} = a[ i * m + j]
81 \] $$
82 
83 $subhead d$$
84 The return value $icode d$$ has prototype
85 $codei%
86  %Scalar% %d%
87 %$$
88 It is equal to the determinant of $latex A$$.
89 
90 $head Vector$$
91 If $icode y$$ is a $icode Vector$$ object,
92 it must support the syntax
93 $codei%
94  %y%[%i%]
95 %$$
96 where $icode i$$ has type $code size_t$$ with value less than $latex n * n$$.
97 This must return a $icode Scalar$$ value corresponding to the $th i$$
98 element of the vector $icode y$$.
99 This is the only requirement of the type $icode Vector$$.
100 
101 $children%
102  speed/example/det_by_minor.cpp%
103  omh/det_by_minor_hpp.omh
104 %$$
105 
106 
107 $head Example$$
108 The file
109 $cref det_by_minor.cpp$$
110 contains an example and test of $code det_by_minor.hpp$$.
111 It returns true if it succeeds and false otherwise.
112 
113 $head Source Code$$
114 The file
115 $cref det_by_minor.hpp$$
116 contains the source for this template function.
117 
118 
119 $end
120 ---------------------------------------------------------------------------
121 */
122 // BEGIN C++
124 # include <vector>
125 
126 // BEGIN CppAD namespace
127 namespace CppAD {
128 
129 template <class Scalar>
131 private:
132  size_t m_;
133 
134  // made mutable because modified and then restored
135  mutable std::vector<size_t> r_;
136  mutable std::vector<size_t> c_;
137 
138  // make mutable because its value does not matter
139  mutable std::vector<Scalar> a_;
140 public:
141  det_by_minor(size_t m) : m_(m) , r_(m + 1) , c_(m + 1), a_(m * m)
142  {
143  size_t i;
144 
145  // values for r and c that correspond to entire matrix
146  for(i = 0; i < m; i++)
147  { r_[i] = i+1;
148  c_[i] = i+1;
149  }
150  r_[m] = 0;
151  c_[m] = 0;
152  }
153 
154  template <class Vector>
155  inline Scalar operator()(const Vector &x) const
156  { size_t i = m_ * m_;
157  while(i--)
158  a_[i] = x[i];
159  return det_of_minor(a_, m_, m_, r_, c_);
160  }
161 
162 };
163 
164 } // END CppAD namespace
165 // END C++
166 # endif
std::vector< size_t > r_
std::vector< Scalar > a_
std::vector< size_t > c_
Scalar operator()(const Vector &x) const
Scalar det_of_minor(const std::vector< Scalar > &a, size_t m, size_t n, std::vector< size_t > &r, std::vector< size_t > &c)