CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
det_by_lu.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_SPEED_DET_BY_LU_HPP
2 # define CPPAD_SPEED_DET_BY_LU_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_lu$$
16 $spell
17  CppAD
18  cppad
19  lu
20  hpp
21  typedef
22  const
23  hpp
24  Det
25  CPPAD_TESTVECTOR
26  namespace
27 $$
28 
29 $section Determinant Using Expansion by Lu Factorization$$
30 $mindex det_by_lu factor$$
31 
32 
33 $head Syntax$$
34 $codei%# include <cppad/speed/det_by_lu.hpp>
35 %$$
36 $codei%det_by_lu<%Scalar%> %det%(%n%)
37 %$$
38 $icode%d% = %det%(%a%)
39 %$$
40 
41 $head Inclusion$$
42 The template class $code det_by_lu$$ is defined in the $code CppAD$$
43 namespace by including
44 the file $code cppad/speed/det_by_lu.hpp$$
45 (relative to the CppAD distribution directory).
46 
47 $head Constructor$$
48 The syntax
49 $codei%
50  det_by_lu<%Scalar%> %det%(%n%)
51 %$$
52 constructs the object $icode det$$ which can be used for
53 evaluating the determinant of $icode n$$ by $icode n$$ matrices
54 using LU factorization.
55 
56 $head Scalar$$
57 The type $icode Scalar$$ can be any
58 $cref NumericType$$
59 
60 $head n$$
61 The argument $icode n$$ has prototype
62 $codei%
63  size_t %n%
64 %$$
65 
66 $head det$$
67 The syntax
68 $codei%
69  %d% = %det%(%a%)
70 %$$
71 returns the determinant of the matrix $latex A$$ using LU factorization.
72 
73 $subhead a$$
74 The argument $icode a$$ has prototype
75 $codei%
76  const %Vector% &%a%
77 %$$
78 It must be a $icode Vector$$ with length $latex n * n$$ and with
79 It must be a $icode Vector$$ with length $latex n * n$$ and with
80 elements of type $icode Scalar$$.
81 The elements of the $latex n \times n$$ matrix $latex A$$ are defined,
82 for $latex i = 0 , \ldots , n-1$$ and $latex j = 0 , \ldots , n-1$$, by
83 $latex \[
84  A_{i,j} = a[ i * m + j]
85 \] $$
86 
87 $subhead d$$
88 The return value $icode d$$ has prototype
89 $codei%
90  %Scalar% %d%
91 %$$
92 
93 $head Vector$$
94 If $icode y$$ is a $icode Vector$$ object,
95 it must support the syntax
96 $codei%
97  %y%[%i%]
98 %$$
99 where $icode i$$ has type $code size_t$$ with value less than $latex n * n$$.
100 This must return a $icode Scalar$$ value corresponding to the $th i$$
101 element of the vector $icode y$$.
102 This is the only requirement of the type $icode Vector$$.
103 
104 $children%
105  speed/example/det_by_lu.cpp%
106  omh/det_by_lu_hpp.omh
107 %$$
108 
109 
110 $head Example$$
111 The file
112 $cref det_by_lu.cpp$$
113 contains an example and test of $code det_by_lu.hpp$$.
114 It returns true if it succeeds and false otherwise.
115 
116 $head Source Code$$
117 The file
118 $cref det_by_lu.hpp$$
119 contains the source for this template function.
120 
121 
122 $end
123 ---------------------------------------------------------------------------
124 */
125 // BEGIN C++
126 # include <cppad/utility/vector.hpp>
127 # include <cppad/utility/lu_solve.hpp>
128 
129 // BEGIN CppAD namespace
130 namespace CppAD {
131 
132 template <class Scalar>
133 class det_by_lu {
134 private:
135  const size_t m_;
136  const size_t n_;
140 public:
141  det_by_lu(size_t n) : m_(0), n_(n), A_(n * n)
142  { }
143 
144  template <class Vector>
145  inline Scalar operator()(const Vector &x)
146  {
147 
148  Scalar logdet;
149  Scalar det;
150  int signdet;
151  size_t i;
152 
153  // copy matrix so it is not overwritten
154  for(i = 0; i < n_ * n_; i++)
155  A_[i] = x[i];
156 
157  // comput log determinant
158  signdet = CppAD::LuSolve(
159  n_, m_, A_, B_, X_, logdet);
160 
161 /*
162  // Do not do this for speed test because it makes floating
163  // point operation sequence very simple.
164  if( signdet == 0 )
165  det = 0;
166  else det = Scalar( signdet ) * exp( logdet );
167 */
168 
169  // convert to determinant
170  det = Scalar( signdet ) * exp( logdet );
171 
172 # ifdef FADBAD
173  // Fadbad requires tempories to be set to constants
174  for(i = 0; i < n_ * n_; i++)
175  A_[i] = 0;
176 # endif
177 
178  return det;
179  }
180 };
181 } // END CppAD namespace
182 // END C++
183 # endif
CppAD::vector< Scalar > A_
Definition: det_by_lu.hpp:137
int LuSolve(size_t n, size_t m, const FloatVector &A, const FloatVector &B, FloatVector &X, Float &logdet)
Definition: lu_solve.hpp:266
Scalar operator()(const Vector &x)
Definition: det_by_lu.hpp:145
const size_t m_
Definition: det_by_lu.hpp:135
det_by_lu(size_t n)
Definition: det_by_lu.hpp:141
AD< Base > exp(const AD< Base > &x)
CppAD::vector< Scalar > B_
Definition: det_by_lu.hpp:138
const size_t n_
Definition: det_by_lu.hpp:136
File used to define CppAD::vector and CppAD::vectorBool.
CppAD::vector< Scalar > X_
Definition: det_by_lu.hpp:139