CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cskip_op.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_LOCAL_CSKIP_OP_HPP
2 # define CPPAD_LOCAL_CSKIP_OP_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 namespace CppAD { namespace local { // BEGIN_CPPAD_LOCAL_NAMESPACE
16 /*!
17 \file cskip_op.hpp
18 Zero order forward mode set which operations to skip.
19 */
20 
21 /*!
22 Zero order forward mode execution of op = CSkipOp.
23 
24 \par Parameters and Variables
25 The terms parameter and variable depend on if we are referring to its
26 AD<Base> or Base value.
27 We use Base parameter and Base variable to refer to the
28 correspond Base value.
29 We use AD<Base> parameter and AD<Base> variable to refer to the
30 correspond AD<Base> value.
31 
32 \tparam Base
33 base type for the operator; i.e., this operation was recorded
34 using AD<Base> and computations by this routine are done using type Base.
35 
36 \param i_z
37 variable index corresponding to the result of the previous operation.
38 This is used for error checking. To be specific,
39 the left and right operands for the CExpOp operation must have indexes
40 less than or equal this value.
41 
42 \param arg [in]
43 \n
44 \a arg[0]
45 is static cast to size_t from the enum type
46 \verbatim
47  enum CompareOp {
48  CompareLt,
49  CompareLe,
50  CompareEq,
51  CompareGe,
52  CompareGt,
53  CompareNe
54  }
55 \endverbatim
56 for this operation.
57 Note that arg[0] cannot be equal to CompareNe.
58 \n
59 \n
60 \a arg[1] & 1
61 \n
62 If this is zero, left is an AD<Base> parameter.
63 Otherwise it is an AD<Base> variable.
64 \n
65 \n
66 \a arg[1] & 2
67 \n
68 If this is zero, right is an AD<Base> parameter.
69 Otherwise it is an AD<Base> variable.
70 \n
71 \a arg[2]
72 is the index corresponding to left in comparision.
73 \n
74 \a arg[3]
75 is the index corresponding to right in comparision.
76 \n
77 \a arg[4]
78 is the number of operations to skip if the comparision result is true.
79 \n
80 \a arg[5]
81 is the number of operations to skip if the comparision result is false.
82 \n
83 <tt>arg[5+i]</tt>
84 for <tt>i = 1 , ... , arg[4]</tt> are the operations to skip if the
85 comparision result is true and both left and right are
86 identically Base parameters.
87 \n
88 <tt>arg[5+arg[4]+i]</tt>
89 for <tt>i = 1 , ... , arg[5]</tt> are the operations to skip if the
90 comparision result is false and both left and right are
91 identically Base parameters.
92 
93 \param num_par [in]
94 is the total number of values in the vector parameter.
95 
96 \param parameter [in]
97 If left is an AD<Base> parameter,
98 <code>parameter [ arg[2] ]</code> is its value.
99 If right is an AD<Base> parameter,
100 <code>parameter [ arg[3] ]</code> is its value.
101 
102 \param cap_order [in]
103 number of columns in the matrix containing the Taylor coefficients.
104 
105 \param taylor [in]
106 If left is an AD<Base> variable,
107 <code>taylor [ arg[2] * cap_order + 0 ]</code>
108 is the zeroth order Taylor coefficient corresponding to left.
109 If right is an AD<Base> variable,
110 <code>taylor [ arg[3] * cap_order + 0 ]</code>
111 is the zeroth order Taylor coefficient corresponding to right.
112 
113 \param cskip_op [in,out]
114 is vector specifying which operations are at this point are know to be
115 unecessary and can be skipped.
116 This is both an input and an output.
117 */
118 template <class Base>
119 inline void forward_cskip_op_0(
120  size_t i_z ,
121  const addr_t* arg ,
122  size_t num_par ,
123  const Base* parameter ,
124  size_t cap_order ,
125  Base* taylor ,
126  bool* cskip_op )
127 {
128  CPPAD_ASSERT_UNKNOWN( size_t(arg[0]) < size_t(CompareNe) );
129  CPPAD_ASSERT_UNKNOWN( arg[1] != 0 );
130 
131  Base left, right;
132  if( arg[1] & 1 )
133  { // If variable arg[2] <= i_z, it has already been computed,
134  // but it will be skipped for higher orders.
135  CPPAD_ASSERT_UNKNOWN( size_t(arg[2]) <= i_z );
136  left = taylor[ arg[2] * cap_order + 0 ];
137  }
138  else
139  { CPPAD_ASSERT_UNKNOWN( size_t(arg[2]) < num_par );
140  left = parameter[ arg[2] ];
141  }
142  if( arg[1] & 2 )
143  { // If variable arg[3] <= i_z, it has already been computed,
144  // but it will be skipped for higher orders.
145  CPPAD_ASSERT_UNKNOWN( size_t(arg[3]) <= i_z );
146  right = taylor[ arg[3] * cap_order + 0 ];
147  }
148  else
149  { CPPAD_ASSERT_UNKNOWN( size_t(arg[3]) < num_par );
150  right = parameter[ arg[3] ];
151  }
152  bool ok_to_skip = IdenticalPar(left) & IdenticalPar(right);
153  if( ! ok_to_skip )
154  return;
155 
156  // initialize to avoid compiler warning
157  bool true_case = false;
158  Base diff = left - right;
159  switch( CompareOp( arg[0] ) )
160  {
161  case CompareLt:
162  true_case = LessThanZero(diff);
163  break;
164 
165  case CompareLe:
166  true_case = LessThanOrZero(diff);
167  break;
168 
169  case CompareEq:
170  true_case = IdenticalZero(diff);
171  break;
172 
173  case CompareGe:
174  true_case = GreaterThanOrZero(diff);
175  break;
176 
177  case CompareGt:
178  true_case = GreaterThanZero(diff);
179  break;
180 
181  case CompareNe:
182  true_case = ! IdenticalZero(diff);
183  break;
184 
185  default:
186  CPPAD_ASSERT_UNKNOWN(false);
187  }
188  if( true_case )
189  { for(size_t i = 0; i < size_t(arg[4]); i++)
190  cskip_op[ arg[6+i] ] = true;
191  }
192  else
193  { for(size_t i = 0; i < size_t(arg[5]); i++)
194  cskip_op[ arg[6+arg[4]+i] ] = true;
195  }
196  return;
197 }
198 } } // END_CPPAD_LOCAL_NAMESPACE
199 # endif
200 
CPPAD_TAPE_ADDR_TYPE addr_t
Definition: declare_ad.hpp:44
bool GreaterThanOrZero(const std::complex< double > &x)
void forward_cskip_op_0(size_t i_z, const addr_t *arg, size_t num_par, const Base *parameter, size_t cap_order, Base *taylor, bool *cskip_op)
Zero order forward mode execution of op = CSkipOp.
Definition: cskip_op.hpp:119
bool IdenticalPar(const std::complex< double > &x)
bool IdenticalZero(const std::complex< double > &x)
bool LessThanOrZero(const std::complex< double > &x)
#define CPPAD_ASSERT_UNKNOWN(exp)
Check that exp is true, if not terminate execution.
bool LessThanZero(const std::complex< double > &x)
bool GreaterThanZero(const std::complex< double > &x)