/usr/include/cppad/local/csum_op.hpp is in cppad 2018.00.00.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 | // $Id: csum_op.hpp 3845 2016-11-19 01:50:47Z bradbell $
# ifndef CPPAD_LOCAL_CSUM_OP_HPP
# define CPPAD_LOCAL_CSUM_OP_HPP
/* --------------------------------------------------------------------------
CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-16 Bradley M. Bell
CppAD is distributed under multiple licenses. This distribution is under
the terms of the
GNU General Public License Version 3.
A copy of this license is included in the COPYING file of this distribution.
Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
-------------------------------------------------------------------------- */
namespace CppAD { namespace local { // BEGIN_CPPAD_LOCAL_NAMESPACE
/*!
\file csum_op.hpp
Forward, reverse and sparsity calculations for cummulative summation.
*/
/*!
Compute forward mode Taylor coefficients for result of op = CsumOp.
This operation is
\verbatim
z = s + x(1) + ... + x(m) - y(1) - ... - y(n).
\endverbatim
\tparam Base
base type for the operator; i.e., this operation was recorded
using AD< \a Base > and computations by this routine are done using type
\a Base.
\param p
lowest order of the Taylor coefficient that we are computing.
\param q
highest order of the Taylor coefficient that we are computing.
\param i_z
variable index corresponding to the result for this operation;
i.e. the row index in \a taylor corresponding to z.
\param arg
\a arg[0]
is the number of addition variables in this cummulative summation; i.e.,
<tt>m</tt>.
\n
\a arg[1]
is the number of subtraction variables in this cummulative summation; i.e.,
\c m.
\n
<tt>parameter[ arg[2] ]</tt>
is the parameter value \c s in this cummunative summation.
\n
<tt>arg[2+i]</tt>
for <tt>i = 1 , ... , m</tt> is the variable index of <tt>x(i)</tt>.
\n
<tt>arg[2+arg[0]+i]</tt>
for <tt>i = 1 , ... , n</tt> is the variable index of <tt>y(i)</tt>.
\param num_par
is the number of parameters in \a parameter.
\param parameter
is the parameter vector for this operation sequence.
\param cap_order
number of colums in the matrix containing all the Taylor coefficients.
\param taylor
\b Input: <tt>taylor [ arg[2+i] * cap_order + k ]</tt>
for <tt>i = 1 , ... , m</tt>
and <tt>k = 0 , ... , q</tt>
is the k-th order Taylor coefficient corresponding to <tt>x(i)</tt>
\n
\b Input: <tt>taylor [ arg[2+m+i] * cap_order + k ]</tt>
for <tt>i = 1 , ... , n</tt>
and <tt>k = 0 , ... , q</tt>
is the k-th order Taylor coefficient corresponding to <tt>y(i)</tt>
\n
\b Input: <tt>taylor [ i_z * cap_order + k ]</tt>
for k = 0 , ... , p,
is the k-th order Taylor coefficient corresponding to z.
\n
\b Output: <tt>taylor [ i_z * cap_order + k ]</tt>
for k = p , ... , q,
is the \a k-th order Taylor coefficient corresponding to z.
*/
template <class Base>
inline void forward_csum_op(
size_t p ,
size_t q ,
size_t i_z ,
const addr_t* arg ,
size_t num_par ,
const Base* parameter ,
size_t cap_order ,
Base* taylor )
{ Base zero(0);
size_t i, j, k;
// check assumptions
CPPAD_ASSERT_UNKNOWN( NumRes(CSumOp) == 1 );
CPPAD_ASSERT_UNKNOWN( q < cap_order );
CPPAD_ASSERT_UNKNOWN( p <= q );
CPPAD_ASSERT_UNKNOWN( size_t(arg[2]) < num_par );
CPPAD_ASSERT_UNKNOWN(
arg[0] + arg[1] == arg[ arg[0] + arg[1] + 3 ]
);
// Taylor coefficients corresponding to result
Base* z = taylor + i_z * cap_order;
for(k = p; k <= q; k++)
z[k] = zero;
if( p == 0 )
z[p] = parameter[ arg[2] ];
Base* x;
i = arg[0];
j = 2;
while(i--)
{ CPPAD_ASSERT_UNKNOWN( size_t(arg[j+1]) < i_z );
x = taylor + arg[++j] * cap_order;
for(k = p; k <= q; k++)
z[k] += x[k];
}
i = arg[1];
while(i--)
{ CPPAD_ASSERT_UNKNOWN( size_t(arg[j+1]) < i_z );
x = taylor + arg[++j] * cap_order;
for(k = p; k <= q; k++)
z[k] -= x[k];
}
}
/*!
Multiple direction forward mode Taylor coefficients for op = CsumOp.
This operation is
\verbatim
z = s + x(1) + ... + x(m) - y(1) - ... - y(n).
\endverbatim
\tparam Base
base type for the operator; i.e., this operation was recorded
using AD<Base> and computations by this routine are done using type
\a Base.
\param q
order ot the Taylor coefficients that we are computing.
\param r
number of directions for Taylor coefficients that we are computing.
\param i_z
variable index corresponding to the result for this operation;
i.e. the row index in \a taylor corresponding to z.
\param arg
\a arg[0]
is the number of addition variables in this cummulative summation; i.e.,
<tt>m</tt>.
\n
\a arg[1]
is the number of subtraction variables in this cummulative summation; i.e.,
\c m.
\n
<tt>parameter[ arg[2] ]</tt>
is the parameter value \c s in this cummunative summation.
\n
<tt>arg[2+i]</tt>
for <tt>i = 1 , ... , m</tt> is the variable index of <tt>x(i)</tt>.
\n
<tt>arg[2+arg[0]+i]</tt>
for <tt>i = 1 , ... , n</tt> is the variable index of <tt>y(i)</tt>.
\param num_par
is the number of parameters in \a parameter.
\param parameter
is the parameter vector for this operation sequence.
\param cap_order
number of colums in the matrix containing all the Taylor coefficients.
\param taylor
\b Input: <tt>taylor [ arg[2+i]*((cap_order-1)*r + 1) + 0 ]</tt>
for <tt>i = 1 , ... , m</tt>
is the 0-th order Taylor coefficient corresponding to <tt>x(i)</tt> and
<tt>taylor [ arg[2+i]*((cap_order-1)*r + 1) + (q-1)*r + ell + 1 ]</tt>
for <tt>i = 1 , ... , m</tt>,
<tt>ell = 0 , ... , r-1</tt>
is the q-th order Taylor coefficient corresponding to <tt>x(i)</tt>
and direction ell.
\n
\b Input: <tt>taylor [ arg[2+m+i]*((cap_order-1)*r + 1) + 0 ]</tt>
for <tt>i = 1 , ... , n</tt>
is the 0-th order Taylor coefficient corresponding to <tt>y(i)</tt> and
<tt>taylor [ arg[2+m+i]*((cap_order-1)*r + 1) + (q-1)*r + ell + 1 ]</tt>
for <tt>i = 1 , ... , n</tt>,
<tt>ell = 0 , ... , r-1</tt>
is the q-th order Taylor coefficient corresponding to <tt>y(i)</tt>
and direction ell.
\n
\b Output: <tt>taylor [ i_z*((cap_order-1)*r+1) + (q-1)*r + ell + 1 ]</tt>
is the \a q-th order Taylor coefficient corresponding to z
for direction <tt>ell = 0 , ... , r-1</tt>.
*/
template <class Base>
inline void forward_csum_op_dir(
size_t q ,
size_t r ,
size_t i_z ,
const addr_t* arg ,
size_t num_par ,
const Base* parameter ,
size_t cap_order ,
Base* taylor )
{ Base zero(0);
size_t i, j, ell;
// check assumptions
CPPAD_ASSERT_UNKNOWN( NumRes(CSumOp) == 1 );
CPPAD_ASSERT_UNKNOWN( q < cap_order );
CPPAD_ASSERT_UNKNOWN( 0 < q );
CPPAD_ASSERT_UNKNOWN( size_t(arg[2]) < num_par );
CPPAD_ASSERT_UNKNOWN(
arg[0] + arg[1] == arg[ arg[0] + arg[1] + 3 ]
);
// Taylor coefficients corresponding to result
size_t num_taylor_per_var = (cap_order-1) * r + 1;
size_t m = (q-1)*r + 1;
Base* z = taylor + i_z * num_taylor_per_var + m;
for(ell = 0; ell < r; ell++)
z[ell] = zero;
Base* x;
i = arg[0];
j = 2;
while(i--)
{ CPPAD_ASSERT_UNKNOWN( size_t(arg[j+1]) < i_z );
x = taylor + arg[++j] * num_taylor_per_var + m;
for(ell = 0; ell < r; ell++)
z[ell] += x[ell];
}
i = arg[1];
while(i--)
{ CPPAD_ASSERT_UNKNOWN( size_t(arg[j+1]) < i_z );
x = taylor + arg[++j] * num_taylor_per_var + m;
for(ell = 0; ell < r; ell++)
z[ell] -= x[ell];
}
}
/*!
Compute reverse mode Taylor coefficients for result of op = CsumOp.
This operation is
\verbatim
z = q + x(1) + ... + x(m) - y(1) - ... - y(n).
H(y, x, w, ...) = G[ z(x, y), y, x, w, ... ]
\endverbatim
\tparam Base
base type for the operator; i.e., this operation was recorded
using AD< \a Base > and computations by this routine are done using type
\a Base.
\param d
order the highest order Taylor coefficient that we are computing
the partial derivatives with respect to.
\param i_z
variable index corresponding to the result for this operation;
i.e. the row index in \a taylor corresponding to z.
\param arg
\a arg[0]
is the number of addition variables in this cummulative summation; i.e.,
<tt>m</tt>.
\n
\a arg[1]
is the number of subtraction variables in this cummulative summation; i.e.,
\c m.
\n
<tt>parameter[ arg[2] ]</tt>
is the parameter value \c q in this cummunative summation.
\n
<tt>arg[2+i]</tt>
for <tt>i = 1 , ... , m</tt> is the value <tt>x(i)</tt>.
\n
<tt>arg[2+arg[0]+i]</tt>
for <tt>i = 1 , ... , n</tt> is the value <tt>y(i)</tt>.
\param nc_partial
number of colums in the matrix containing all the partial derivatives.
\param partial
\b Input: <tt>partial [ arg[2+i] * nc_partial + k ]</tt>
for <tt>i = 1 , ... , m</tt>
and <tt>k = 0 , ... , d</tt>
is the partial derivative of G(z, y, x, w, ...) with respect to the
k-th order Taylor coefficient corresponding to <tt>x(i)</tt>
\n
\b Input: <tt>partial [ arg[2+m+i] * nc_partial + k ]</tt>
for <tt>i = 1 , ... , n</tt>
and <tt>k = 0 , ... , d</tt>
is the partial derivative of G(z, y, x, w, ...) with respect to the
k-th order Taylor coefficient corresponding to <tt>y(i)</tt>
\n
\b Input: <tt>partial [ i_z * nc_partial + k ]</tt>
for <tt>i = 1 , ... , n</tt>
and <tt>k = 0 , ... , d</tt>
is the partial derivative of G(z, y, x, w, ...) with respect to the
k-th order Taylor coefficient corresponding to \c z.
\n
\b Output: <tt>partial [ arg[2+i] * nc_partial + k ]</tt>
for <tt>i = 1 , ... , m</tt>
and <tt>k = 0 , ... , d</tt>
is the partial derivative of H(y, x, w, ...) with respect to the
k-th order Taylor coefficient corresponding to <tt>x(i)</tt>
\n
\b Output: <tt>partial [ arg[2+m+i] * nc_partial + k ]</tt>
for <tt>i = 1 , ... , n</tt>
and <tt>k = 0 , ... , d</tt>
is the partial derivative of H(y, x, w, ...) with respect to the
k-th order Taylor coefficient corresponding to <tt>y(i)</tt>
*/
template <class Base>
inline void reverse_csum_op(
size_t d ,
size_t i_z ,
const addr_t* arg ,
size_t nc_partial ,
Base* partial )
{
// check assumptions
CPPAD_ASSERT_UNKNOWN( NumRes(CSumOp) == 1 );
CPPAD_ASSERT_UNKNOWN( d < nc_partial );
// Taylor coefficients and partial derivative corresponding to result
Base* pz = partial + i_z * nc_partial;
Base* px;
size_t i, j, k;
size_t d1 = d + 1;
i = arg[0];
j = 2;
while(i--)
{ CPPAD_ASSERT_UNKNOWN( size_t(arg[j+1]) < i_z );
px = partial + arg[++j] * nc_partial;
k = d1;
while(k--)
px[k] += pz[k];
}
i = arg[1];
while(i--)
{ CPPAD_ASSERT_UNKNOWN( size_t(arg[j+1]) < i_z );
px = partial + arg[++j] * nc_partial;
k = d1;
while(k--)
px[k] -= pz[k];
}
}
/*!
Forward mode Jacobian sparsity pattern for CSumOp operator.
This operation is
\verbatim
z = q + x(1) + ... + x(m) - y(1) - ... - y(n).
\endverbatim
\tparam Vector_set
is the type used for vectors of sets. It can be either
sparse_pack or sparse_list.
\param i_z
variable index corresponding to the result for this operation;
i.e. the index in \a sparsity corresponding to z.
\param arg
\a arg[0]
is the number of addition variables in this cummulative summation; i.e.,
<tt>m + n</tt>.
\n
\a arg[1]
is the number of subtraction variables in this cummulative summation; i.e.,
\c m.
\n
<tt>parameter[ arg[2] ]</tt>
is the parameter value \c q in this cummunative summation.
\n
<tt>arg[2+i]</tt>
for <tt>i = 1 , ... , m</tt> is the value <tt>x(i)</tt>.
\n
<tt>arg[2+arg[1]+i]</tt>
for <tt>i = 1 , ... , n</tt> is the value <tt>y(i)</tt>.
\param sparsity
\b Input:
For <tt>i = 1 , ... , m</tt>,
the set with index \a arg[2+i] in \a sparsity
is the sparsity bit pattern for <tt>x(i)</tt>.
This identifies which of the independent variables the variable
<tt>x(i)</tt> depends on.
\n
\b Input:
For <tt>i = 1 , ... , n</tt>,
the set with index \a arg[2+arg[0]+i] in \a sparsity
is the sparsity bit pattern for <tt>x(i)</tt>.
This identifies which of the independent variables the variable
<tt>y(i)</tt> depends on.
\n
\b Output:
The set with index \a i_z in \a sparsity
is the sparsity bit pattern for z.
This identifies which of the independent variables the variable z
depends on.
*/
template <class Vector_set>
inline void forward_sparse_jacobian_csum_op(
size_t i_z ,
const addr_t* arg ,
Vector_set& sparsity )
{ sparsity.clear(i_z);
size_t i, j;
i = arg[0] + arg[1];
j = 2;
while(i--)
{ CPPAD_ASSERT_UNKNOWN( size_t(arg[j+1]) < i_z );
sparsity.binary_union(
i_z , // index in sparsity for result
i_z , // index in sparsity for left operand
arg[++j] , // index for right operand
sparsity // sparsity vector for right operand
);
}
}
/*!
Reverse mode Jacobian sparsity pattern for CSumOp operator.
This operation is
\verbatim
z = q + x(1) + ... + x(m) - y(1) - ... - y(n).
H(y, x, w, ...) = G[ z(x, y), y, x, w, ... ]
\endverbatim
\tparam Vector_set
is the type used for vectors of sets. It can be either
sparse_pack or sparse_list.
\param i_z
variable index corresponding to the result for this operation;
i.e. the index in \a sparsity corresponding to z.
\param arg
\a arg[0]
is the number of addition variables in this cummulative summation; i.e.,
<tt>m + n</tt>.
\n
\a arg[1]
is the number of subtraction variables in this cummulative summation; i.e.,
\c m.
\n
<tt>parameter[ arg[2] ]</tt>
is the parameter value \c q in this cummunative summation.
\n
<tt>arg[2+i]</tt>
for <tt>i = 1 , ... , m</tt> is the value <tt>x(i)</tt>.
\n
<tt>arg[2+arg[1]+i]</tt>
for <tt>i = 1 , ... , n</tt> is the value <tt>y(i)</tt>.
\param sparsity
For <tt>i = 1 , ... , m</tt>,
the set with index \a arg[2+i] in \a sparsity
is the sparsity bit pattern for <tt>x(i)</tt>.
This identifies which of the dependent variables depend on <tt>x(i)</tt>.
On input, the sparsity patter corresponds to \c G,
and on ouput it corresponds to \c H.
\n
For <tt>i = 1 , ... , m</tt>,
the set with index \a arg[2+arg[0]+i] in \a sparsity
is the sparsity bit pattern for <tt>y(i)</tt>.
This identifies which of the dependent variables depend on <tt>y(i)</tt>.
On input, the sparsity patter corresponds to \c G,
and on ouput it corresponds to \c H.
\n
\b Input:
The set with index \a i_z in \a sparsity
is the sparsity bit pattern for z.
On input it corresponds to \c G and on output it is undefined.
*/
template <class Vector_set>
inline void reverse_sparse_jacobian_csum_op(
size_t i_z ,
const addr_t* arg ,
Vector_set& sparsity )
{
size_t i, j;
i = arg[0] + arg[1];
j = 2;
while(i--)
{ ++j;
CPPAD_ASSERT_UNKNOWN( size_t(arg[j]) < i_z );
sparsity.binary_union(
arg[j] , // index in sparsity for result
arg[j] , // index in sparsity for left operand
i_z , // index for right operand
sparsity // sparsity vector for right operand
);
}
}
/*!
Reverse mode Hessian sparsity pattern for CSumOp operator.
This operation is
\verbatim
z = q + x(1) + ... + x(m) - y(1) - ... - y(n).
H(y, x, w, ...) = G[ z(x, y), y, x, w, ... ]
\endverbatim
\tparam Vector_set
is the type used for vectors of sets. It can be either
sparse_pack or sparse_list.
\param i_z
variable index corresponding to the result for this operation;
i.e. the index in \a sparsity corresponding to z.
\param arg
\a arg[0]
is the number of addition variables in this cummulative summation; i.e.,
<tt>m + n</tt>.
\n
\a arg[1]
is the number of subtraction variables in this cummulative summation; i.e.,
\c m.
\n
<tt>parameter[ arg[2] ]</tt>
is the parameter value \c q in this cummunative summation.
\n
<tt>arg[2+i]</tt>
for <tt>i = 1 , ... , m</tt> is the value <tt>x(i)</tt>.
\n
<tt>arg[2+arg[0]+i]</tt>
for <tt>i = 1 , ... , n</tt> is the value <tt>y(i)</tt>.
\param rev_jacobian
<tt>rev_jacobian[i_z]</tt>
is all false (true) if the Jabobian of G with respect to z must be zero
(may be non-zero).
\n
\n
For <tt>i = 1 , ... , m</tt>
<tt>rev_jacobian[ arg[2+i] ]</tt>
is all false (true) if the Jacobian with respect to <tt>x(i)</tt>
is zero (may be non-zero).
On input, it corresponds to the function G,
and on output it corresponds to the function H.
\n
\n
For <tt>i = 1 , ... , n</tt>
<tt>rev_jacobian[ arg[2+arg[0]+i] ]</tt>
is all false (true) if the Jacobian with respect to <tt>y(i)</tt>
is zero (may be non-zero).
On input, it corresponds to the function G,
and on output it corresponds to the function H.
\param rev_hes_sparsity
The set with index \a i_z in in \a rev_hes_sparsity
is the Hessian sparsity pattern for the fucntion G
where one of the partials derivative is with respect to z.
\n
\n
For <tt>i = 1 , ... , m</tt>
The set with index <tt>arg[2+i]</tt> in \a rev_hes_sparsity
is the Hessian sparsity pattern
where one of the partials derivative is with respect to <tt>x(i)</tt>.
On input, it corresponds to the function G,
and on output it corresponds to the function H.
\n
\n
For <tt>i = 1 , ... , n</tt>
The set with index <tt>arg[2+arg[0]+i]</tt> in \a rev_hes_sparsity
is the Hessian sparsity pattern
where one of the partials derivative is with respect to <tt>y(i)</tt>.
On input, it corresponds to the function G,
and on output it corresponds to the function H.
*/
template <class Vector_set>
inline void reverse_sparse_hessian_csum_op(
size_t i_z ,
const addr_t* arg ,
bool* rev_jacobian ,
Vector_set& rev_hes_sparsity )
{
size_t i, j;
i = arg[0] + arg[1];
j = 2;
while(i--)
{ ++j;
CPPAD_ASSERT_UNKNOWN( size_t(arg[j]) < i_z );
rev_hes_sparsity.binary_union(
arg[j] , // index in sparsity for result
arg[j] , // index in sparsity for left operand
i_z , // index for right operand
rev_hes_sparsity // sparsity vector for right operand
);
rev_jacobian[arg[j]] |= rev_jacobian[i_z];
}
}
} } // END_CPPAD_LOCAL_NAMESPACE
# endif
|