1 # ifndef CPPAD_CORE_FORWARD_HPP
2 # define CPPAD_CORE_FORWARD_HPP
76 template <
typename Base>
77 template <
typename VectorBase>
80 const VectorBase& xq ,
86 size_t n = ind_taddr_.size();
89 size_t m = dep_taddr_.size();
92 CheckSimpleVector<Base, VectorBase>();
96 size_t(xq.size()) == n ||
size_t(xq.size()) == n*(q+1),
97 "Forward(q, xq): xq.size() is not equal n or n*(q+1)"
101 size_t p = q + 1 - size_t(xq.size()) / n;
104 q <= num_order_taylor_ || p == 0,
105 "Forward(q, xq): Number of Taylor coefficient orders stored in this"
106 " ADFun\nis less than q and xq.size() != n*(q+1)."
109 p <= 1 || num_direction_taylor_ == 1,
110 "Forward(q, xq): computing order q >= 2"
111 " and number of directions is not one."
112 "\nMust use Forward(q, r, xq) for this case"
115 if( (cap_order_taylor_ <= q) | (num_direction_taylor_ != 1) )
118 num_order_taylor_ = 0;
120 else num_order_taylor_ = q;
121 size_t c = std::max(q + 1, cap_order_taylor_);
123 capacity_order(c, r);
129 size_t C = cap_order_taylor_;
134 for(j = 0; j < num_var_tape_; j++)
135 {
for(k = p; k <= q; k++)
140 for(j = 0; j < n; j++)
147 taylor_[ C * ind_taddr_[j] + q] = xq[j];
149 {
for(k = 0; k <= q; k++)
150 taylor_[ C * ind_taddr_[j] + k] = xq[ (q+1)*j + k];
160 taylor_.data(), cskip_op_.data(), load_op_,
161 compare_change_count_,
162 compare_change_number_,
163 compare_change_op_index_
169 taylor_.data(), cskip_op_.data(), load_op_,
170 compare_change_count_,
171 compare_change_number_,
172 compare_change_op_index_
180 for(i = 0; i < m; i++)
182 yq[i] = taylor_[ C * dep_taddr_[i] + q];
186 { yq.resize(m * (q+1) );
187 for(i = 0; i < m; i++)
188 {
for(k = 0; k <= q; k++)
190 taylor_[ C * dep_taddr_[i] + k ];
198 {
for(i = 0; i < m; i++)
211 for(j = 0; j < n; j++)
212 x0[j] = taylor_[ C * ind_taddr_[j] + 0 ];
213 std::string file_name;
215 std::stringstream ss;
217 "yq = f.Forward(q, xq): a zero order Taylor coefficient is nan.\n"
218 "Corresponding independent variables vector was written "
219 "to binary a file.\n"
220 "vector_size = " << n <<
"\n" <<
221 "file_name = " << file_name <<
"\n" <<
222 "index = " << index <<
"\n";
225 std::string msg_str = ss.str();
228 const char* msg_char_star = msg_str.c_str();
233 "if( CppAD::isnan( yq[ (q+1) * index + 0 ] )",
238 "with the value nan."
241 {
for(i = 0; i < m; i++)
242 {
for(k = p; k <= q; k++)
249 "yq = f.Forward(q, xq): has a non-zero order Taylor coefficient\n"
250 "with the value nan (but zero order coefficients are not nan)."
256 num_order_taylor_ = q + 1;
322 template <
typename Base>
323 template <
typename VectorBase>
327 const VectorBase& xq )
332 size_t n = ind_taddr_.size();
335 size_t m = dep_taddr_.size();
338 CheckSimpleVector<Base, VectorBase>();
342 size_t(xq.size()) == r * n,
343 "Forward(q, r, xq): xq.size() is not equal r * n"
346 q <= num_order_taylor_ ,
347 "Forward(q, r, xq): Number of Taylor coefficient orders stored in"
348 " this ADFun is less than q"
351 q == 1 || num_direction_taylor_ == r ,
352 "Forward(q, r, xq): q > 1 and number of Taylor directions r"
353 " is not same as previous Forward(1, r, xq)"
357 if( cap_order_taylor_ <= q || num_direction_taylor_ != r )
358 {
if( num_direction_taylor_ != r )
359 num_order_taylor_ = 1;
361 size_t c = std::max(q + 1, cap_order_taylor_);
362 capacity_order(c, r);
368 size_t c = cap_order_taylor_;
371 for(j = 0; j < n; j++)
377 for(ell = 0; ell < r; ell++)
378 {
size_t index = ((c-1)*r + 1)*ind_taddr_[j] + (q-1)*r + ell + 1;
379 taylor_[ index ] = xq[ r * j + ell ];
401 for(i = 0; i < m; i++)
403 for(ell = 0; ell < r; ell++)
404 {
size_t index = ((c-1)*r + 1)*dep_taddr_[i] + (q-1)*r + ell + 1;
405 yq[ r * i + ell ] = taylor_[ index ];
411 for(i = 0; i < m; i++)
412 {
for(ell = 0; ell < r; ell++)
418 "yq = f.Forward(q, r, xq): has a non-zero order Taylor coefficient\n"
419 "with the value nan (but zero order coefficients are not nan)."
425 num_order_taylor_ = q + 1;
#define CPPAD_ASSERT_KNOWN(exp, msg)
Check that exp is true, if not print msg and terminate execution.
void put_check_for_nan(const CppAD::vector< Base > &vec, std::string &file_name)
Stores a vector in a file when nans occur.
void forward0sweep(const local::player< Base > *play, std::ostream &s_out, bool print, size_t n, size_t numvar, size_t J, Base *taylor, bool *cskip_op, pod_vector< addr_t > &var_by_load_op, size_t compare_change_count, size_t &compare_change_number, size_t &compare_change_op_index)
Compute zero order forward mode Taylor coefficients.
bool isnan(const Scalar &s)
static void Call(bool known, int line, const char *file, const char *exp, const char *msg)
All tthese defaults correspond to errors.
#define CPPAD_ASSERT_UNKNOWN(exp)
Check that exp is true, if not terminate execution.
VectorBase Forward(size_t q, size_t r, const VectorBase &x)
forward mode user API, one order multiple directions.
void forward1sweep(const local::player< Base > *play, std::ostream &s_out, const bool print, const size_t p, const size_t q, const size_t n, const size_t numvar, const size_t J, Base *taylor, bool *cskip_op, pod_vector< addr_t > &var_by_load_op, size_t compare_change_count, size_t &compare_change_number, size_t &compare_change_op_index)
Compute arbitrary order forward mode Taylor coefficients.
Control of number of orders allocated.
void forward2sweep(const local::player< Base > *play, const size_t q, const size_t r, const size_t n, const size_t numvar, const size_t J, Base *taylor, const bool *cskip_op, const pod_vector< addr_t > &var_by_load_op)
Compute multiple directions forward mode Taylor coefficients.