CppAD: A C++ Algorithmic Differentiation Package  20130918
sub_op.hpp
Go to the documentation of this file.
00001 /* $Id$ */
00002 # ifndef CPPAD_SUB_OP_INCLUDED
00003 # define CPPAD_SUB_OP_INCLUDED
00004 
00005 /* --------------------------------------------------------------------------
00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-14 Bradley M. Bell
00007 
00008 CppAD is distributed under multiple licenses. This distribution is under
00009 the terms of the 
00010                     Eclipse 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 namespace CppAD { // BEGIN_CPPAD_NAMESPACE
00017 /*!
00018 \file sub_op.hpp
00019 Forward and reverse mode calculations for z = x - y.
00020 */
00021 
00022 // --------------------------- Subvv -----------------------------------------
00023 /*!
00024 Compute forward mode Taylor coefficients for result of op = SubvvOp.
00025 
00026 The C++ source code corresponding to this operation is
00027 \verbatim
00028      z = x - y
00029 \endverbatim
00030 In the documentation below,
00031 this operations is for the case where both x and y are variables
00032 and the argument \a parameter is not used.
00033 
00034 \copydetails forward_binary_op
00035 */
00036 
00037 template <class Base>
00038 inline void forward_subvv_op(
00039      size_t        p           , 
00040      size_t        q           , 
00041      size_t        i_z         ,
00042      const addr_t* arg         ,
00043      const Base*   parameter   ,
00044      size_t        nc_taylor   ,
00045      Base*         taylor      )
00046 {
00047      // check assumptions
00048      CPPAD_ASSERT_UNKNOWN( NumArg(SubvvOp) == 2 );
00049      CPPAD_ASSERT_UNKNOWN( NumRes(SubvvOp) == 1 );
00050      CPPAD_ASSERT_UNKNOWN( size_t(arg[0]) < i_z );
00051      CPPAD_ASSERT_UNKNOWN( size_t(arg[1]) < i_z );
00052      CPPAD_ASSERT_UNKNOWN( q < nc_taylor );
00053      CPPAD_ASSERT_UNKNOWN( p <= q );
00054 
00055      // Taylor coefficients corresponding to arguments and result
00056      Base* x = taylor + arg[0] * nc_taylor;
00057      Base* y = taylor + arg[1] * nc_taylor;
00058      Base* z = taylor + i_z    * nc_taylor;
00059 
00060      for(size_t d = p; d <= q; d++)
00061           z[d] = x[d] - y[d];
00062 }
00063 
00064 /*!
00065 Compute zero order forward mode Taylor coefficients for result of op = SubvvOp.
00066 
00067 The C++ source code corresponding to this operation is
00068 \verbatim
00069      z = x - y
00070 \endverbatim
00071 In the documentation below,
00072 this operations is for the case where both x and y are variables
00073 and the argument \a parameter is not used.
00074 
00075 \copydetails forward_binary_op_0
00076 */
00077 
00078 template <class Base>
00079 inline void forward_subvv_op_0(
00080      size_t        i_z         ,
00081      const addr_t* arg         ,
00082      const Base*   parameter   ,
00083      size_t        nc_taylor   ,
00084      Base*         taylor      )
00085 {
00086      // check assumptions
00087      CPPAD_ASSERT_UNKNOWN( NumArg(SubvvOp) == 2 );
00088      CPPAD_ASSERT_UNKNOWN( NumRes(SubvvOp) == 1 );
00089      CPPAD_ASSERT_UNKNOWN( size_t(arg[0]) < i_z );
00090      CPPAD_ASSERT_UNKNOWN( size_t(arg[1]) < i_z );
00091 
00092      // Taylor coefficients corresponding to arguments and result
00093      Base* x = taylor + arg[0] * nc_taylor;
00094      Base* y = taylor + arg[1] * nc_taylor;
00095      Base* z = taylor + i_z    * nc_taylor;
00096 
00097      z[0] = x[0] - y[0];
00098 }
00099 
00100 /*!
00101 Compute reverse mode partial derivatives for result of op = SubvvOp.
00102 
00103 The C++ source code corresponding to this operation is
00104 \verbatim
00105      z = x - y
00106 \endverbatim
00107 In the documentation below,
00108 this operations is for the case where both x and y are variables
00109 and the argument \a parameter is not used.
00110 
00111 \copydetails reverse_binary_op
00112 */
00113 
00114 template <class Base>
00115 inline void reverse_subvv_op(
00116      size_t        d           , 
00117      size_t        i_z         ,
00118      const addr_t* arg         ,
00119      const Base*   parameter   ,
00120      size_t        nc_taylor   ,
00121      const Base*   taylor      ,
00122      size_t        nc_partial  ,
00123      Base*         partial     )
00124 {
00125      // check assumptions
00126      CPPAD_ASSERT_UNKNOWN( NumArg(SubvvOp) == 2 );
00127      CPPAD_ASSERT_UNKNOWN( NumRes(SubvvOp) == 1 );
00128      CPPAD_ASSERT_UNKNOWN( size_t(arg[0]) < i_z );
00129      CPPAD_ASSERT_UNKNOWN( size_t(arg[1]) < i_z );
00130      CPPAD_ASSERT_UNKNOWN( d < nc_taylor );
00131      CPPAD_ASSERT_UNKNOWN( d < nc_partial );
00132 
00133      // Partial derivatives corresponding to arguments and result
00134      Base* px = partial + arg[0] * nc_partial;
00135      Base* py = partial + arg[1] * nc_partial;
00136      Base* pz = partial + i_z    * nc_partial;
00137 
00138      // number of indices to access
00139      size_t i = d + 1;
00140      while(i)
00141      {    --i;
00142           px[i] += pz[i];
00143           py[i] -= pz[i];
00144      }
00145 }
00146 
00147 // --------------------------- Subpv -----------------------------------------
00148 /*!
00149 Compute forward mode Taylor coefficients for result of op = SubpvOp.
00150 
00151 The C++ source code corresponding to this operation is
00152 \verbatim
00153      z = x - y
00154 \endverbatim
00155 In the documentation below,
00156 this operations is for the case where x is a parameter and y is a variable.
00157 
00158 \copydetails forward_binary_op
00159 */
00160 
00161 template <class Base>
00162 inline void forward_subpv_op(
00163      size_t        p           , 
00164      size_t        q           , 
00165      size_t        i_z         ,
00166      const addr_t* arg         ,
00167      const Base*   parameter   ,
00168      size_t        nc_taylor   ,
00169      Base*         taylor      )
00170 {
00171      // check assumptions
00172      CPPAD_ASSERT_UNKNOWN( NumArg(SubpvOp) == 2 );
00173      CPPAD_ASSERT_UNKNOWN( NumRes(SubpvOp) == 1 );
00174      CPPAD_ASSERT_UNKNOWN( size_t(arg[1]) < i_z );
00175      CPPAD_ASSERT_UNKNOWN( q < nc_taylor );
00176      CPPAD_ASSERT_UNKNOWN( p <= q );
00177 
00178      // Taylor coefficients corresponding to arguments and result
00179      Base* y = taylor + arg[1] * nc_taylor;
00180      Base* z = taylor + i_z    * nc_taylor;
00181 
00182      // Paraemter value
00183      Base x = parameter[ arg[0] ];
00184      if( p == 0 )
00185      {    z[0] = x - y[0];
00186           p++;
00187      }
00188      for(size_t d = p; d <= q; d++)
00189           z[d] = - y[d];
00190 }
00191 /*!
00192 Compute zero order forward mode Taylor coefficient for result of op = SubpvOp.
00193 
00194 The C++ source code corresponding to this operation is
00195 \verbatim
00196      z = x - y
00197 \endverbatim
00198 In the documentation below,
00199 this operations is for the case where x is a parameter and y is a variable.
00200 
00201 \copydetails forward_binary_op_0
00202 */
00203 
00204 template <class Base>
00205 inline void forward_subpv_op_0(
00206      size_t        i_z         ,
00207      const addr_t* arg         ,
00208      const Base*   parameter   ,
00209      size_t        nc_taylor   ,
00210      Base*         taylor      )
00211 {
00212      // check assumptions
00213      CPPAD_ASSERT_UNKNOWN( NumArg(SubpvOp) == 2 );
00214      CPPAD_ASSERT_UNKNOWN( NumRes(SubpvOp) == 1 );
00215      CPPAD_ASSERT_UNKNOWN( size_t(arg[1]) < i_z );
00216 
00217      // Paraemter value
00218      Base x = parameter[ arg[0] ];
00219 
00220      // Taylor coefficients corresponding to arguments and result
00221      Base* y = taylor + arg[1] * nc_taylor;
00222      Base* z = taylor + i_z    * nc_taylor;
00223 
00224      z[0] = x - y[0];
00225 }
00226 
00227 /*!
00228 Compute reverse mode partial derivative for result of op = SubpvOp.
00229 
00230 The C++ source code corresponding to this operation is
00231 \verbatim
00232      z = x - y
00233 \endverbatim
00234 In the documentation below,
00235 this operations is for the case where x is a parameter and y is a variable.
00236 
00237 \copydetails reverse_binary_op
00238 */
00239 
00240 template <class Base>
00241 inline void reverse_subpv_op(
00242      size_t        d           , 
00243      size_t        i_z         ,
00244      const addr_t* arg         ,
00245      const Base*   parameter   ,
00246      size_t        nc_taylor   ,
00247      const Base*   taylor      ,
00248      size_t        nc_partial  ,
00249      Base*         partial     )
00250 {
00251      // check assumptions
00252      CPPAD_ASSERT_UNKNOWN( NumArg(SubvvOp) == 2 );
00253      CPPAD_ASSERT_UNKNOWN( NumRes(SubvvOp) == 1 );
00254      CPPAD_ASSERT_UNKNOWN( size_t(arg[1]) < i_z );
00255      CPPAD_ASSERT_UNKNOWN( d < nc_taylor );
00256      CPPAD_ASSERT_UNKNOWN( d < nc_partial );
00257 
00258      // Partial derivatives corresponding to arguments and result
00259      Base* py = partial + arg[1] * nc_partial;
00260      Base* pz = partial + i_z    * nc_partial;
00261 
00262      // number of indices to access
00263      size_t i = d + 1;
00264      while(i)
00265      {    --i;
00266           py[i] -= pz[i];
00267      }
00268 }
00269 
00270 // --------------------------- Subvp -----------------------------------------
00271 /*!
00272 Compute forward mode Taylor coefficients for result of op = SubvvOp.
00273 
00274 The C++ source code corresponding to this operation is
00275 \verbatim
00276      z = x - y
00277 \endverbatim
00278 In the documentation below,
00279 this operations is for the case where x is a variable and y is a parameter.
00280 
00281 \copydetails forward_binary_op
00282 */
00283 
00284 template <class Base>
00285 inline void forward_subvp_op(
00286      size_t        p           , 
00287      size_t        q           , 
00288      size_t        i_z         ,
00289      const addr_t* arg         ,
00290      const Base*   parameter   ,
00291      size_t        nc_taylor   ,
00292      Base*         taylor      )
00293 {
00294      // check assumptions
00295      CPPAD_ASSERT_UNKNOWN( NumArg(SubvpOp) == 2 );
00296      CPPAD_ASSERT_UNKNOWN( NumRes(SubvpOp) == 1 );
00297      CPPAD_ASSERT_UNKNOWN( size_t(arg[0]) < i_z );
00298      CPPAD_ASSERT_UNKNOWN( q < nc_taylor );
00299      CPPAD_ASSERT_UNKNOWN( p <= q );
00300 
00301      // Taylor coefficients corresponding to arguments and result
00302      Base* x = taylor + arg[0] * nc_taylor;
00303      Base* z = taylor + i_z    * nc_taylor;
00304 
00305      // Parameter value
00306      Base y = parameter[ arg[1] ];
00307      if( p == 0 )
00308      {    z[0] = x[0] - y;
00309           p++;
00310      }
00311      for(size_t d = p; d <= q; d++)
00312           z[d] = x[d];
00313 }
00314 
00315 /*!
00316 Compute zero order forward mode Taylor coefficients for result of op = SubvvOp.
00317 
00318 The C++ source code corresponding to this operation is
00319 \verbatim
00320      z = x - y
00321 \endverbatim
00322 In the documentation below,
00323 this operations is for the case where x is a variable and y is a parameter.
00324 
00325 \copydetails forward_binary_op_0
00326 */
00327 
00328 template <class Base>
00329 inline void forward_subvp_op_0(
00330      size_t        i_z         ,
00331      const addr_t* arg         ,
00332      const Base*   parameter   ,
00333      size_t        nc_taylor   ,
00334      Base*         taylor      )
00335 {
00336      // check assumptions
00337      CPPAD_ASSERT_UNKNOWN( NumArg(SubvpOp) == 2 );
00338      CPPAD_ASSERT_UNKNOWN( NumRes(SubvpOp) == 1 );
00339      CPPAD_ASSERT_UNKNOWN( size_t(arg[0]) < i_z );
00340 
00341      // Parameter value
00342      Base y = parameter[ arg[1] ];
00343 
00344      // Taylor coefficients corresponding to arguments and result
00345      Base* x = taylor + arg[0] * nc_taylor;
00346      Base* z = taylor + i_z    * nc_taylor;
00347 
00348      z[0] = x[0] - y;
00349 }
00350 
00351 /*!
00352 Compute reverse mode partial derivative for result of op = SubvpOp.
00353 
00354 The C++ source code corresponding to this operation is
00355 \verbatim
00356      z = x - y
00357 \endverbatim
00358 In the documentation below,
00359 this operations is for the case where x is a variable and y is a parameter.
00360 
00361 \copydetails reverse_binary_op
00362 */
00363 
00364 template <class Base>
00365 inline void reverse_subvp_op(
00366      size_t        d           , 
00367      size_t        i_z         ,
00368      const addr_t* arg         ,
00369      const Base*   parameter   ,
00370      size_t        nc_taylor   ,
00371      const Base*   taylor      ,
00372      size_t        nc_partial  ,
00373      Base*         partial     )
00374 {
00375      // check assumptions
00376      CPPAD_ASSERT_UNKNOWN( NumArg(SubvpOp) == 2 );
00377      CPPAD_ASSERT_UNKNOWN( NumRes(SubvpOp) == 1 );
00378      CPPAD_ASSERT_UNKNOWN( size_t(arg[0]) < i_z );
00379      CPPAD_ASSERT_UNKNOWN( d < nc_taylor );
00380      CPPAD_ASSERT_UNKNOWN( d < nc_partial );
00381 
00382      // Partial derivatives corresponding to arguments and result
00383      Base* px = partial + arg[0] * nc_partial;
00384      Base* pz = partial + i_z    * nc_partial;
00385 
00386      // number of indices to access
00387      size_t i = d + 1;
00388      while(i)
00389      {    --i;
00390           px[i] += pz[i];
00391      }
00392 }
00393 
00394 } // END_CPPAD_NAMESPACE
00395 # endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines