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