CppAD: A C++ Algorithmic Differentiation Package
20130102
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_DEPENDENT_INCLUDED 00003 # define CPPAD_DEPENDENT_INCLUDED 00004 00005 /* -------------------------------------------------------------------------- 00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 Bradley M. Bell 00007 00008 CppAD is distributed under multiple licenses. This distribution is under 00009 the terms of the 00010 Eclipse Public License Version 1.0. 00011 00012 A copy of this license is included in the COPYING file of this distribution. 00013 Please visit http://www.coin-or.org/CppAD/ for information on other licenses. 00014 -------------------------------------------------------------------------- */ 00015 /* 00016 $begin Dependent$$ 00017 $spell 00018 alloc 00019 num 00020 taylor_ 00021 ADvector 00022 const 00023 $$ 00024 00025 $spell 00026 $$ 00027 00028 $section Stop Recording and Store Operation Sequence$$ 00029 00030 $index ADFun, operation sequence$$ 00031 $index operation, sequence store$$ 00032 $index sequence, operation store$$ 00033 $index recording, stop$$ 00034 $index tape, stop recording$$ 00035 $index Dependent$$ 00036 00037 $head Syntax$$ 00038 $icode%f%.Dependent(%x%, %y%)%$$ 00039 00040 $head Purpose$$ 00041 Stop recording and the AD of $icode Base$$ 00042 $cref/operation sequence/glossary/Operation/Sequence/$$ 00043 that started with the call 00044 $codei% 00045 Independent(%x%) 00046 %$$ 00047 and store the operation sequence in $icode f$$. 00048 The operation sequence defines an 00049 $cref/AD function/glossary/AD Function/$$ 00050 $latex \[ 00051 F : B^n \rightarrow B^m 00052 \] $$ 00053 where $latex B$$ is the space corresponding to objects of type $icode Base$$. 00054 The value $latex n$$ is the dimension of the 00055 $cref/domain/seq_property/Domain/$$ space for the operation sequence. 00056 The value $latex m$$ is the dimension of the 00057 $cref/range/seq_property/Range/$$ space for the operation sequence 00058 (which is determined by the size of $icode y$$). 00059 00060 $head f$$ 00061 The object $icode f$$ has prototype 00062 $codei% 00063 ADFun<%Base%> %f% 00064 %$$ 00065 The AD of $icode Base$$ operation sequence is stored in $icode f$$; i.e., 00066 it becomes the operation sequence corresponding to $icode f$$. 00067 If a previous operation sequence was stored in $icode f$$, 00068 it is deleted. 00069 00070 $head x$$ 00071 The argument $icode x$$ 00072 must be the vector argument in a previous call to 00073 $cref Independent$$. 00074 Neither its size, or any of its values, are allowed to change 00075 between calling 00076 $codei% 00077 Independent(%x%) 00078 %$$ 00079 and 00080 $codei% 00081 %f%.Dependent(%x%, %y%) 00082 %$$. 00083 00084 $head y$$ 00085 The vector $icode y$$ has prototype 00086 $codei% 00087 const %ADvector% &%y% 00088 %$$ 00089 (see $cref/ADvector/FunConstruct/$$ below). 00090 The length of $icode y$$ must be greater than zero 00091 and is the dimension of the range space for $icode f$$. 00092 00093 $head ADvector$$ 00094 The type $icode ADvector$$ must be a $cref SimpleVector$$ class with 00095 $cref/elements of type/SimpleVector/Elements of Specified Type/$$ 00096 $codei%AD<%Base%>%$$. 00097 The routine $cref CheckSimpleVector$$ will generate an error message 00098 if this is not the case. 00099 00100 $head Taping$$ 00101 The tape, 00102 that was created when $codei%Independent(%x%)%$$ was called, 00103 will stop recording. 00104 The AD operation sequence will be transferred from 00105 the tape to the object $icode f$$ and the tape will then be deleted. 00106 00107 $head Forward$$ 00108 No $cref Forward$$ calculation is preformed during this operation. 00109 Thus, directly after this operation, 00110 $codei% 00111 %f%.size_taylor() 00112 %$$ 00113 is zero (see $cref size_taylor$$). 00114 00115 $head Parallel Mode$$ 00116 $index parallel, Dependent$$ 00117 $index Dependent, parallel$$ 00118 The call to $code Independent$$, 00119 and the corresponding call to 00120 $codei% 00121 ADFun<%Base%> %f%( %x%, %y%) 00122 %$$ 00123 or 00124 $codei% 00125 %f%.Dependent( %x%, %y%) 00126 %$$ 00127 or $cref abort_recording$$, 00128 must be preformed by the same thread; i.e., 00129 $cref/thread_alloc::thread_num/ta_thread_num/$$ must be the same. 00130 00131 $head Example$$ 00132 The file 00133 $cref fun_check.cpp$$ 00134 contains an example and test of this operation. 00135 It returns true if it succeeds and false otherwise. 00136 00137 $end 00138 ---------------------------------------------------------------------------- 00139 */ 00140 00141 00142 // BEGIN CppAD namespace 00143 namespace CppAD { 00144 00145 /*! 00146 Determine the \c tape corresponding to this exeuction thread and then use 00147 <code>Dependent(tape, y)</code> to store this tapes recording in a function. 00148 00149 \param y [in] 00150 The dependent variable vector for the corresponding function. 00151 */ 00152 template <typename Base> 00153 template <typename ADvector> 00154 void ADFun<Base>::Dependent(const ADvector &y) 00155 { ADTape<Base>* tape = AD<Base>::tape_ptr(); 00156 CPPAD_ASSERT_KNOWN( 00157 tape != CPPAD_NULL, 00158 "Can't store current operation sequence in this ADFun object" 00159 "\nbecause there is no active tape (for this thread)." 00160 ); 00161 00162 // code above just determines the tape and checks for errors 00163 Dependent(tape, y); 00164 } 00165 00166 00167 /*! 00168 Determine the \c tape corresponding to this exeuction thread and then use 00169 <code>Dependent(tape, y)</code> to store this tapes recording in a function. 00170 00171 \param x [in] 00172 The independent variable vector for this tape. This informaiton is 00173 also stored in the tape so a check is done to make sure it is correct 00174 (if NDEBUG is not defined). 00175 00176 \param y [in] 00177 The dependent variable vector for the corresponding function. 00178 */ 00179 template <typename Base> 00180 template <typename ADvector> 00181 void ADFun<Base>::Dependent(const ADvector &x, const ADvector &y) 00182 { 00183 CPPAD_ASSERT_KNOWN( 00184 x.size() > 0, 00185 "Dependent: independent variable vector has size zero." 00186 ); 00187 CPPAD_ASSERT_KNOWN( 00188 Variable(x[0]), 00189 "Dependent: independent variable vector has been changed." 00190 ); 00191 ADTape<Base> *tape = AD<Base>::tape_ptr(x[0].tape_id_); 00192 CPPAD_ASSERT_KNOWN( 00193 tape->size_independent_ == size_t( x.size() ), 00194 "Dependent: independent variable vector has been changed." 00195 ); 00196 # ifndef NDEBUG 00197 size_t i, j; 00198 for(j = 0; j < size_t(x.size()); j++) 00199 { CPPAD_ASSERT_KNOWN( 00200 size_t(x[j].taddr_) == (j+1), 00201 "ADFun<Base>: independent variable vector has been changed." 00202 ); 00203 CPPAD_ASSERT_KNOWN( 00204 x[j].tape_id_ == x[0].tape_id_, 00205 "ADFun<Base>: independent variable vector has been changed." 00206 ); 00207 } 00208 for(i = 0; i < size_t(y.size()); i++) 00209 { CPPAD_ASSERT_KNOWN( 00210 CppAD::Parameter( y[i] ) | (y[i].tape_id_ == x[0].tape_id_) , 00211 "ADFun<Base>: dependent vector contains a variable for" 00212 "\na different tape (thread) than the independent variables." 00213 ); 00214 } 00215 # endif 00216 00217 // code above just determines the tape and checks for errors 00218 Dependent(tape, y); 00219 } 00220 00221 /*! 00222 Replace the floationg point operations sequence for this function object. 00223 00224 \param tape 00225 is a tape that contains the new floating point operation sequence 00226 for this function. 00227 After this operation, all memory allocated for this tape is deleted. 00228 00229 \param y 00230 The dependent variable vector for the function being stored in this object. 00231 */ 00232 00233 template <typename Base> 00234 template <typename ADvector> 00235 void ADFun<Base>::Dependent(ADTape<Base> *tape, const ADvector &y) 00236 { 00237 size_t m = y.size(); 00238 size_t n = tape->size_independent_; 00239 size_t i, j; 00240 size_t y_taddr; 00241 00242 // check ADvector is Simple Vector class with AD<Base> elements 00243 CheckSimpleVector< AD<Base>, ADvector>(); 00244 00245 CPPAD_ASSERT_KNOWN( 00246 y.size() > 0, 00247 "ADFun operation sequence dependent variable size is zero size" 00248 ); 00249 00250 // set total number of variables in tape, parameter flag, 00251 // make a tape copy of dependent variables that are parameters, 00252 // and store tape address for each dependent variable 00253 CPPAD_ASSERT_UNKNOWN( NumRes(ParOp) == 1 ); 00254 dep_parameter_.resize(m); 00255 dep_taddr_.resize(m); 00256 for(i = 0; i < m; i++) 00257 { dep_parameter_[i] = CppAD::Parameter(y[i]); 00258 if( dep_parameter_[i] ) 00259 y_taddr = tape->RecordParOp( y[i].value_ ); 00260 else y_taddr = y[i].taddr_; 00261 00262 CPPAD_ASSERT_UNKNOWN( y_taddr > 0 ); 00263 dep_taddr_[i] = y_taddr; 00264 } 00265 00266 // put an EndOp at the end of the tape 00267 tape->Rec_.PutOp(EndOp); 00268 00269 // total number of variables on the tape 00270 total_num_var_ = tape->Rec_.num_rec_var(); 00271 00272 // now that each dependent variable has a place in the tape, 00273 // and there is a EndOp at the end of the tape, we can transfer the 00274 // recording to the player and and erase the tape. 00275 play_.get(tape->Rec_); 00276 00277 // now we can delete the tape 00278 AD<Base>::tape_manage(tape_manage_delete); 00279 00280 // total number of varables in this recording 00281 CPPAD_ASSERT_UNKNOWN( total_num_var_ == play_.num_rec_var() ); 00282 00283 // used to determine if there is an operation sequence in *this 00284 CPPAD_ASSERT_UNKNOWN( total_num_var_ > 0 ); 00285 00286 // free old buffers 00287 for_jac_sparse_pack_.resize(0, 0); 00288 for_jac_sparse_set_.resize(0,0); 00289 00290 // initial row and column dimensions 00291 taylor_.erase(); 00292 taylor_per_var_ = 0; 00293 taylor_col_dim_ = 0; 00294 00295 // set tape address 00296 ind_taddr_.resize(n); 00297 CPPAD_ASSERT_UNKNOWN( 00298 n < total_num_var_ 00299 ); 00300 for(j = 0; j < n; j++) 00301 { CPPAD_ASSERT_UNKNOWN( play_.GetOp(j+1) == InvOp ); 00302 ind_taddr_[j] = j+1; 00303 } 00304 00305 } 00306 00307 } // END CppAD namespace 00308 00309 # endif