CppAD: A C++ Algorithmic Differentiation Package  20130102
abs_op.hpp
Go to the documentation of this file.
00001 /* $Id$ */
00002 # ifndef CPPAD_ABS_OP_INCLUDED
00003 # define CPPAD_ABS_OP_INCLUDED
00004 
00005 /* --------------------------------------------------------------------------
00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 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 
00017 CPPAD_BEGIN_NAMESPACE
00018 /*!
00019 \defgroup abs_op_hpp abs_op.hpp
00020 \{
00021 \file abs_op.hpp
00022 Forward and reverse mode calculations for z = abs(x).
00023 */
00024 
00025 /*!
00026 Compute forward mode Taylor coefficient for result of op = AbsOp.
00027 
00028 The C++ source code corresponding to this operation is
00029 \verbatim
00030      z = abs(x)
00031 \endverbatim
00032 
00033 \copydetails forward_unary1_op
00034 */
00035 template <class Base>
00036 inline void forward_abs_op(
00037      size_t j           ,
00038      size_t i_z         ,
00039      size_t i_x         ,
00040      size_t nc_taylor   , 
00041      Base*  taylor      )
00042 {
00043      // check assumptions
00044      CPPAD_ASSERT_UNKNOWN( NumArg(AbsOp) == 1 );
00045      CPPAD_ASSERT_UNKNOWN( NumRes(AbsOp) == 1 );
00046      CPPAD_ASSERT_UNKNOWN( i_x < i_z );
00047      CPPAD_ASSERT_UNKNOWN( j < nc_taylor );
00048 
00049      // Taylor coefficients corresponding to argument and result
00050      Base* x = taylor + i_x * nc_taylor;
00051      Base* z = taylor + i_z * nc_taylor;
00052 
00053      z[j] = sign(x[0]) * x[j];
00054 }
00055 
00056 /*!
00057 Compute zero order forward mode Taylor coefficient for result of op = AbsOp.
00058 
00059 The C++ source code corresponding to this operation is
00060 \verbatim
00061      z = abs(x)
00062 \endverbatim
00063 
00064 \copydetails forward_unary1_op_0
00065 */
00066 template <class Base>
00067 inline void forward_abs_op_0(
00068      size_t i_z         ,
00069      size_t i_x         ,
00070      size_t nc_taylor   , 
00071      Base*  taylor      )
00072 {
00073 
00074      // check assumptions
00075      CPPAD_ASSERT_UNKNOWN( NumArg(AbsOp) == 1 );
00076      CPPAD_ASSERT_UNKNOWN( NumRes(AbsOp) == 1 );
00077      CPPAD_ASSERT_UNKNOWN( i_x < i_z );
00078      CPPAD_ASSERT_UNKNOWN( 0 < nc_taylor );
00079 
00080      // Taylor coefficients corresponding to argument and result
00081      Base x0 = *(taylor + i_x * nc_taylor);
00082      Base* z = taylor + i_z * nc_taylor;
00083 
00084      z[0] = abs(x0);
00085 }
00086 /*!
00087 Compute reverse mode partial derivatives for result of op = AbsOp.
00088 
00089 The C++ source code corresponding to this operation is
00090 \verbatim
00091      z = abs(x)
00092 \endverbatim
00093 
00094 \copydetails reverse_unary1_op
00095 */
00096 
00097 template <class Base>
00098 inline void reverse_abs_op(
00099      size_t      d            ,
00100      size_t      i_z          ,
00101      size_t      i_x          ,
00102      size_t      nc_taylor    , 
00103      const Base* taylor       ,
00104      size_t      nc_partial   ,
00105      Base*       partial      )
00106 {    size_t j; 
00107 
00108      // check assumptions
00109      CPPAD_ASSERT_UNKNOWN( NumArg(AbsOp) == 1 );
00110      CPPAD_ASSERT_UNKNOWN( NumRes(AbsOp) == 1 );
00111      CPPAD_ASSERT_UNKNOWN( i_x < i_z );
00112      CPPAD_ASSERT_UNKNOWN( d < nc_taylor );
00113      CPPAD_ASSERT_UNKNOWN( d < nc_partial );
00114 
00115      // Taylor coefficients and partials corresponding to argument
00116      const Base* x  = taylor  + i_x * nc_taylor;
00117      Base* px       = partial + i_x * nc_partial;
00118 
00119      // Taylor coefficients and partials corresponding to result
00120      Base* pz       = partial +    i_z * nc_partial;
00121 
00122      for(j = 0; j <= d; j++)
00123           px[j] += sign(x[0]) * pz[j];
00124 }
00125 
00126 /*! \} */
00127 CPPAD_END_NAMESPACE
00128 # endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines