CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
print_for.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_CORE_PRINT_FOR_HPP
2 # define CPPAD_CORE_PRINT_FOR_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 /*
16 $begin PrintFor$$
17 $spell
18  pos
19  var
20  VecAD
21  std
22  cout
23  const
24 $$
25 
26 
27 $section Printing AD Values During Forward Mode$$
28 $mindex print text output debug$$
29 
30 $head Syntax$$
31 $icode%f%.Forward(0, %x%)
32 %$$
33 $codei%PrintFor(%before%, %var%)
34 %$$
35 $codei%PrintFor(%pos%, %before%, %var%, %after%)
36 %$$
37 
38 $head Purpose$$
39 The $cref/zero order forward/forward_zero/$$ mode command
40 $codei%
41  %f%.Forward(0, %x%)
42 %$$
43 assigns the
44 $cref/independent variable/glossary/Tape/Independent Variable/$$ vector
45 equal to $icode x$$.
46 It then computes a value for all of the dependent variables in the
47 $cref/operation sequence/glossary/Operation/Sequence/$$ corresponding
48 to $icode f$$.
49 Putting a $code PrintFor$$ in the operation sequence will
50 cause the value of $icode var$$, corresponding to $icode x$$,
51 to be printed during zero order forward operations.
52 
53 $head f.Forward(0, x)$$
54 The objects $icode f$$, $icode x$$, and the purpose
55 for this operation, are documented in $cref Forward$$.
56 
57 $head pos$$
58 If present, the argument $icode pos$$ has one of the following prototypes
59 $codei%
60  const AD<%Base%>& %pos%
61  const VecAD<%Base%>::reference& %pos%
62 %$$
63 In this case
64 the text and $icode var$$ will be printed if and only if
65 $icode pos$$ is not greater than zero and a finite number.
66 
67 $head before$$
68 The argument $icode before$$ has prototype
69 $codei%
70  const char* %before%
71 %$$
72 This text is written to $code std::cout$$ before $icode var$$.
73 
74 $head var$$
75 The argument $icode var$$ has one of the following prototypes
76 $codei%
77  const AD<%Base%>& %var%
78  const VecAD<%Base%>::reference& %var%
79 %$$
80 The value of $icode var$$, that corresponds to $icode x$$,
81 is written to $code std::cout$$ during the execution of
82 $codei%
83  %f%.Forward(0, %x%)
84 %$$
85 Note that $icode var$$ may be a
86 $cref/variable/glossary/Variable/$$ or
87 $cref/parameter/glossary/Parameter/$$.
88 (A parameters value does not depend on the value of
89 the independent variable vector $icode x$$.)
90 
91 $head after$$
92 The argument $icode after$$ has prototype
93 $codei%
94  const char* %after%
95 %$$
96 This text is written to $code std::cout$$ after $icode var$$.
97 
98 $head Redirecting Output$$
99 You can redirect this output to any standard output stream; see the
100 $cref/s/forward_order/s/$$ in the forward mode documentation.
101 
102 $head Discussion$$
103 This is helpful for understanding why tape evaluations
104 have trouble.
105 For example, if one of the operations in $icode f$$ is
106 $codei%log(%var%)%$$ and $icode%var% <= 0%$$,
107 the corresponding result will be $cref nan$$.
108 
109 $head Alternative$$
110 The $cref ad_output$$ section describes the normal
111 printing of values; i.e., printing when the corresponding
112 code is executed.
113 
114 $head Example$$
115 $children%
116  example/print_for/print_for.cpp%
117  example/general/print_for.cpp
118 %$$
119 The program
120 $cref print_for_cout.cpp$$
121 is an example and test that prints to standard output.
122 The output of this program
123 states the conditions for passing and failing the test.
124 The function
125 $cref print_for_string.cpp$$
126 is an example and test that prints to an standard string stream.
127 This function automatically check for correct output.
128 
129 $end
130 ------------------------------------------------------------------------------
131 */
132 
133 # include <cstring>
134 
135 namespace CppAD {
136  template <class Base>
137  void PrintFor(const AD<Base>& pos,
138  const char *before, const AD<Base>& var, const char* after)
140 
141  // check for case where we are not recording operations
143  if( tape == CPPAD_NULL )
144  return;
145 
147  std::strlen(before) <= 1000 ,
148  "PrintFor: length of before is greater than 1000 characters"
149  );
151  std::strlen(after) <= 1000 ,
152  "PrintFor: length of after is greater than 1000 characters"
153  );
154  addr_t ind0, ind1, ind2, ind3, ind4;
155 
156  // ind[0] = base 2 representation of the value [Var(pos), Var(var)]
157  ind0 = 0;
158 
159  // ind[1] = address for pos
160  if( Parameter(pos) )
161  ind1 = tape->Rec_.PutPar(pos.value_);
162  else
163  { ind0 += 1;
164  ind1 = pos.taddr_;
165  }
166 
167  // ind[2] = address of before
168  ind2 = tape->Rec_.PutTxt(before);
169 
170  // ind[3] = address for var
171  if( Parameter(var) )
172  ind3 = tape->Rec_.PutPar(var.value_);
173  else
174  { ind0 += 2;
175  ind3 = var.taddr_;
176  }
177 
178  // ind[4] = address of after
179  ind4 = tape->Rec_.PutTxt(after);
180 
181  // put the operator in the tape
182  tape->Rec_.PutArg(ind0, ind1, ind2, ind3, ind4);
183  tape->Rec_.PutOp(local::PriOp);
184  }
185  // Fold all other cases into the case above
186  template <class Base>
187  void PrintFor(const char* before, const AD<Base>& var)
188  { PrintFor(AD<Base>(0), before, var, "" ); }
189  //
190  template <class Base>
191  void PrintFor(const char* before, const VecAD_reference<Base>& var)
192  { PrintFor(AD<Base>(0), before, var.ADBase(), "" ); }
193  //
194  template <class Base>
195  void PrintFor(
196  const VecAD_reference<Base>& pos ,
197  const char *before ,
198  const VecAD_reference<Base>& var ,
199  const char *after )
200  { PrintFor(pos.ADBase(), before, var.ADBase(), after); }
201  //
202  template <class Base>
203  void PrintFor(
204  const VecAD_reference<Base>& pos ,
205  const char *before ,
206  const AD<Base>& var ,
207  const char *after )
208  { PrintFor(pos.ADBase(), before, var, after); }
209  //
210  template <class Base>
211  void PrintFor(
212  const AD<Base>& pos ,
213  const char *before ,
214  const VecAD_reference<Base>& var ,
215  const char *after )
216  { PrintFor(pos, before, var.ADBase(), after); }
217 }
218 
219 # endif
#define CPPAD_ASSERT_KNOWN(exp, msg)
Check that exp is true, if not print msg and terminate execution.
Base value_
Definition: ad.hpp:38
CPPAD_TAPE_ADDR_TYPE addr_t
Definition: declare_ad.hpp:44
Definition: ad.hpp:34
void PrintFor(const AD< Base > &pos, const char *before, const AD< Base > &var, const char *after)
Definition: print_for.hpp:137
AD< Base > ADBase(void) const
Conversion from VecAD_reference to AD&lt;Base&gt;. puts the correspond vecad load instruction in the tape...
Definition: vec_ad.hpp:392
static local::ADTape< Base > * tape_ptr(void)
Pointer for the tape for this AD&lt;Base&gt; class and the current thread.
Definition: tape_link.hpp:130
local::recorder< Base > Rec_
This is where the information is recorded.
Definition: ad_tape.hpp:106
#define CPPAD_ASSERT_NARG_NRES(op, n_arg, n_res)
Check that operator op has the specified number of of arguments and results.
Class used to hold a reference to an element of a VecAD object.
Definition: vec_ad.hpp:352
Class used to hold tape that records AD&lt;Base&gt; operations.
Definition: ad_tape.hpp:26
CPPAD_INLINE_FRIEND_TEMPLATE_FUNCTION bool Parameter(const AD< Base > &x)
Definition: par_var.hpp:80
addr_t taddr_
Definition: ad.hpp:44