CppAD: A C++ Algorithmic Differentiation Package
20130918
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_PRINT_FOR_INCLUDED 00003 # define CPPAD_PRINT_FOR_INCLUDED 00004 00005 /* -------------------------------------------------------------------------- 00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-14 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 /* 00017 $begin PrintFor$$ 00018 $spell 00019 pos 00020 var 00021 VecAD 00022 std 00023 cout 00024 const 00025 $$ 00026 00027 $index print, forward mode$$ 00028 $index forward, mode print$$ 00029 $index text, output$$ 00030 $index output, text$$ 00031 $index debug, forward mode$$ 00032 $index forward, debug$$ 00033 00034 $section Printing AD Values During Forward Mode$$ 00035 00036 $head Syntax$$ 00037 $icode%f%.Forward(0, %x%) 00038 %$$ 00039 $codei%PrintFor(%before%, %var%) 00040 %$$ 00041 $codei%PrintFor(%pos%, %before%, %var%, %after%) 00042 %$$ 00043 00044 $head Purpose$$ 00045 The $cref/zero order forward/forward_zero/$$ mode command 00046 $codei% 00047 %f%.Forward(0, %x%) 00048 %$$ 00049 assigns the 00050 $cref/independent variable/glossary/Tape/Independent Variable/$$ vector 00051 equal to $icode x$$. 00052 It then computes a value for all of the dependent variables in the 00053 $cref/operation sequence/glossary/Operation/Sequence/$$ corresponding 00054 to $icode f$$. 00055 Putting a $code PrintFor$$ in the operation sequence will 00056 cause the value of $icode var$$, corresponding to $icode x$$, 00057 to be printed during zero order forward operations. 00058 00059 $head f.Forward(0, x)$$ 00060 The objects $icode f$$, $icode x$$, and the purpose 00061 for this operation, are documented in $cref Forward$$. 00062 00063 $head pos$$ 00064 If present, the argument $icode pos$$ has one of the following prototypes 00065 $codei% 00066 const AD<%Base%>& %pos% 00067 const VecAD<%Base%>::reference& %pos% 00068 %$$ 00069 In this case 00070 the text and $icode var$$ will be printed if and only if 00071 $icode pos$$ is not greater than zero and a finite number. 00072 00073 $head before$$ 00074 The argument $icode before$$ has prototype 00075 $codei% 00076 const char* %before% 00077 %$$ 00078 This text is written to $code std::cout$$ before $icode var$$. 00079 00080 $head var$$ 00081 The argument $icode var$$ has one of the following prototypes 00082 $codei% 00083 const AD<%Base%>& %var% 00084 const VecAD<%Base%>::reference& %var% 00085 %$$ 00086 The value of $icode var$$, that corresponds to $icode x$$, 00087 is written to $code std::cout$$ during the execution of 00088 $codei% 00089 %f%.Forward(0, %x%) 00090 %$$ 00091 Note that $icode var$$ may be a 00092 $cref/variable/glossary/Variable/$$ or 00093 $cref/parameter/glossary/Parameter/$$. 00094 (A parameters value does not depend on the value of 00095 the independent variable vector $icode x$$.) 00096 00097 $head after$$ 00098 The argument $icode after$$ has prototype 00099 $codei% 00100 const char* %after% 00101 %$$ 00102 This text is written to $code std::cout$$ after $icode var$$. 00103 00104 $head Discussion$$ 00105 This is helpful for understanding why tape evaluations 00106 have trouble. 00107 For example, if one of the operations in $icode f$$ is 00108 $codei%log(%var%)%$$ and $icode%var% <= 0%$$, 00109 the corresponding result will be $cref nan$$. 00110 00111 $head Alternative$$ 00112 The $cref ad_output$$ section describes the normal 00113 printing of values; i.e., printing when the corresponding 00114 code is executed. 00115 00116 $head Example$$ 00117 $children% 00118 print_for/print_for.cpp% 00119 example/print_for.cpp 00120 %$$ 00121 The program 00122 $cref print_for_cout.cpp$$ 00123 is an example and test that prints to standard output. 00124 The output of this program 00125 states the conditions for passing and failing the test. 00126 The function 00127 $cref print_for_string.cpp$$ 00128 is an example and test that prints to an standard string stream. 00129 This function automatically check for correct output. 00130 00131 $end 00132 ------------------------------------------------------------------------------ 00133 */ 00134 00135 # include <cstring> 00136 00137 namespace CppAD { 00138 template <class Base> 00139 void PrintFor(const AD<Base>& pos, 00140 const char *before, const AD<Base>& var, const char* after) 00141 { CPPAD_ASSERT_NARG_NRES(PriOp, 5, 0); 00142 00143 // check for case where we are not recording operations 00144 ADTape<Base>* tape = AD<Base>::tape_ptr(); 00145 if( tape == CPPAD_NULL ) 00146 return; 00147 00148 CPPAD_ASSERT_KNOWN( 00149 std::strlen(before) <= 1000 , 00150 "PrintFor: length of before is greater than 1000 characters" 00151 ); 00152 CPPAD_ASSERT_KNOWN( 00153 std::strlen(after) <= 1000 , 00154 "PrintFor: length of after is greater than 1000 characters" 00155 ); 00156 size_t ind0, ind1, ind2, ind3, ind4; 00157 00158 // ind[0] = base 2 representation of the value [Var(pos), Var(var)] 00159 ind0 = 0; 00160 00161 // ind[1] = address for pos 00162 if( Parameter(pos) ) 00163 ind1 = tape->Rec_.PutPar(pos.value_); 00164 else 00165 { ind0 += 1; 00166 ind1 = pos.taddr_; 00167 } 00168 00169 // ind[2] = address of before 00170 ind2 = tape->Rec_.PutTxt(before); 00171 00172 // ind[3] = address for var 00173 if( Parameter(var) ) 00174 ind3 = tape->Rec_.PutPar(var.value_); 00175 else 00176 { ind0 += 2; 00177 ind3 = var.taddr_; 00178 } 00179 00180 // ind[4] = address of after 00181 ind4 = tape->Rec_.PutTxt(after); 00182 00183 // put the operator in the tape 00184 tape->Rec_.PutArg(ind0, ind1, ind2, ind3, ind4); 00185 tape->Rec_.PutOp(PriOp); 00186 } 00187 // Fold all other cases into the case above 00188 template <class Base> 00189 void PrintFor(const char* before, const AD<Base>& var) 00190 { PrintFor(AD<Base>(0), before, var, "" ); } 00191 // 00192 template <class Base> 00193 void PrintFor(const char* before, const VecAD_reference<Base>& var) 00194 { PrintFor(AD<Base>(0), before, var.ADBase(), "" ); } 00195 // 00196 template <class Base> 00197 void PrintFor( 00198 const VecAD_reference<Base>& pos , 00199 const char *before , 00200 const VecAD_reference<Base>& var , 00201 const char *after ) 00202 { PrintFor(pos.ADBase(), before, var.ADBase(), after); } 00203 // 00204 template <class Base> 00205 void PrintFor( 00206 const VecAD_reference<Base>& pos , 00207 const char *before , 00208 const AD<Base>& var , 00209 const char *after ) 00210 { PrintFor(pos.ADBase(), before, var, after); } 00211 // 00212 template <class Base> 00213 void PrintFor( 00214 const AD<Base>& pos , 00215 const char *before , 00216 const VecAD_reference<Base>& var , 00217 const char *after ) 00218 { PrintFor(pos, before, var.ADBase(), after); } 00219 } 00220 00221 # endif