CppAD: A C++ Algorithmic Differentiation Package 20110419
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_PRINT_FOR_INCLUDED 00003 # define CPPAD_PRINT_FOR_INCLUDED 00004 00005 /* -------------------------------------------------------------------------- 00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-09 Bradley M. Bell 00007 00008 CppAD is distributed under multiple licenses. This distribution is under 00009 the terms of the 00010 Common 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 VecAD 00020 std 00021 cout 00022 const 00023 $$ 00024 00025 $index print, forward mode$$ 00026 $index forward, mode print$$ 00027 $index text, output$$ 00028 $index output, text$$ 00029 $index debug, forward mode$$ 00030 $index forward, debug$$ 00031 00032 $section Printing AD Values During Forward Mode$$ 00033 00034 $head Syntax$$ 00035 $syntax%PrintFor(%text%, %y%)%$$ 00036 $pre 00037 $$ 00038 $syntax%%f%.Forward(0, %x%)%$$ 00039 00040 00041 $head Purpose$$ 00042 The current value of an $syntax%AD<%Base%>%$$ 00043 object $italic y$$ is the result of an AD of $italic Base$$ operation. 00044 This operation may be part of the 00045 $xref/glossary/Operation/Sequence/operation sequence/1/$$ 00046 that is transferred to an $xref/ADFun/$$ object $italic f$$. 00047 The $code ADFun$$ object can be evaluated at different values for the 00048 $cref/independent variables/glossary/Tape/Independent Variable/$$. 00049 This may result in a corresponding value for $italic y$$ 00050 that is different from when the operation sequence was recorded. 00051 The routine $code PrintFor$$ requests a printing, 00052 when $syntax%%f%.Forward(0, %x%)%$$ is executed, 00053 of the value for $italic y$$ that corresponds to the 00054 independent variable values specified by $italic x$$. 00055 00056 $head text$$ 00057 The argument $italic text$$ has prototype 00058 $syntax% 00059 const char *%text% 00060 %$$ 00061 The corresponding text is written to $code std::cout$$ before the 00062 value of $italic y$$. 00063 00064 $head y$$ 00065 The argument $italic y$$ has one of the following prototypes 00066 $syntax% 00067 const AD<%Base%> &%y% 00068 const VecAD<%Base%>::reference &%y% 00069 %$$ 00070 The value of $italic y$$ that corresponds to $italic x$$ 00071 is written to $code std::cout$$ during the execution of 00072 $syntax% 00073 %f%.Forward(0, %x%) 00074 %$$ 00075 00076 $head f.Forward(0, x)$$ 00077 The objects $italic f$$, $italic x$$, and the purpose 00078 for this operation, are documented in $xref/Forward/$$. 00079 00080 00081 $head Discussion$$ 00082 This is can be helpful for understanding why tape evaluations 00083 have trouble, for example, if the result of a tape calculation 00084 is the IEEE code for not a number $code Nan$$. 00085 00086 $head Alternative$$ 00087 The $xref/Output/$$ section describes the normal 00088 printing of values; i.e., printing when the corresponding 00089 code is executed. 00090 00091 $head Example$$ 00092 $children% 00093 print_for/print_for.cpp 00094 %$$ 00095 The program 00096 $xref/PrintFor.cpp/$$ 00097 is an example and test of this operation. 00098 The output of this program 00099 states the conditions for passing and failing the test. 00100 00101 $end 00102 ------------------------------------------------------------------------------ 00103 */ 00104 00105 namespace CppAD { 00106 template <class Base> 00107 void PrintFor(const char *text, const AD<Base> &u) 00108 { ADTape<Base> *tape = AD<Base>::tape_ptr(); 00109 CPPAD_ASSERT_KNOWN( 00110 tape != CPPAD_NULL, 00111 "PrintFor: cannot use this function because no tape" 00112 "\nis currently active (for this thread)." 00113 ); 00114 00115 if( Parameter(u) ) 00116 { CPPAD_ASSERT_UNKNOWN( NumRes(PripOp) == 0 ); 00117 CPPAD_ASSERT_UNKNOWN( NumArg(PripOp) == 2 ); 00118 // put operand addresses in tape 00119 size_t t = tape->Rec_.PutTxt(text); 00120 size_t p = tape->Rec_.PutPar(u.value_); 00121 tape->Rec_.PutArg(t, p); 00122 // put operator in the tape 00123 tape->Rec_.PutOp(PripOp); 00124 } 00125 else 00126 { CPPAD_ASSERT_UNKNOWN( NumRes(PrivOp) == 0 ); 00127 CPPAD_ASSERT_UNKNOWN( NumArg(PrivOp) == 2 ); 00128 // put operand addresses in tape 00129 size_t t = tape->Rec_.PutTxt(text); 00130 tape->Rec_.PutArg(t, u.taddr_); 00131 // put operator in the tape 00132 tape->Rec_.PutOp(PrivOp); 00133 } 00134 } 00135 template <class Base> 00136 void PrintFor(const char *text, const VecAD_reference<Base> &u) 00137 { PrintFor(text, u.ADBase()); } 00138 } 00139 00140 # endif