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