CppAD: A C++ Algorithmic Differentiation Package
20130102
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_SPARSE_UNARY_OP_INCLUDED 00003 # define CPPAD_SPARSE_UNARY_OP_INCLUDED 00004 /* -------------------------------------------------------------------------- 00005 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 Bradley M. Bell 00006 00007 CppAD is distributed under multiple licenses. This distribution is under 00008 the terms of the 00009 Eclipse Public License Version 1.0. 00010 00011 A copy of this license is included in the COPYING file of this distribution. 00012 Please visit http://www.coin-or.org/CppAD/ for information on other licenses. 00013 -------------------------------------------------------------------------- */ 00014 00015 CPPAD_BEGIN_NAMESPACE 00016 /*! 00017 \defgroup sparse_unary_op_hpp sparse_unary_op.hpp 00018 \{ 00019 \file sparse_unary_op.hpp 00020 Forward and reverse mode sparsity patterns for unary operators. 00021 */ 00022 00023 00024 /*! 00025 Forward mode Jacobian sparsity pattern for all unary operators. 00026 00027 The C++ source code corresponding to a unary operation has the form 00028 \verbatim 00029 z = fun(x) 00030 \endverbatim 00031 where fun is a C++ unary function, or it has the form 00032 \verbatim 00033 z = x op p 00034 \endverbatim 00035 where op is a C++ binary unary operator and p is a parameter. 00036 00037 \tparam Vector_set 00038 is the type used for vectors of sets. It can be either 00039 \c sparse_pack, \c sparse_set or \c sparse_list. 00040 00041 \param i_z 00042 variable index corresponding to the result for this operation; 00043 i.e., z. 00044 00045 \param i_x 00046 variable index corresponding to the argument for this operator; 00047 i.e., x. 00048 00049 00050 \param sparsity 00051 \b Input: The set with index \a arg[0] in \a sparsity 00052 is the sparsity bit pattern for x. 00053 This identifies which of the independent variables the variable x 00054 depends on. 00055 \n 00056 \n 00057 \b Output: The set with index \a i_z in \a sparsity 00058 is the sparsity bit pattern for z. 00059 This identifies which of the independent variables the variable z 00060 depends on. 00061 \n 00062 00063 \par Checked Assertions: 00064 \li \a i_x < \a i_z 00065 */ 00066 00067 template <class Vector_set> 00068 inline void forward_sparse_jacobian_unary_op( 00069 size_t i_z , 00070 size_t i_x , 00071 Vector_set& sparsity ) 00072 { 00073 // check assumptions 00074 CPPAD_ASSERT_UNKNOWN( i_x < i_z ); 00075 00076 sparsity.assignment(i_z, i_x, sparsity); 00077 } 00078 00079 /*! 00080 Reverse mode Jacobian sparsity pattern for all unary operators. 00081 00082 The C++ source code corresponding to a unary operation has the form 00083 \verbatim 00084 z = fun(x) 00085 \endverbatim 00086 where fun is a C++ unary function, or it has the form 00087 \verbatim 00088 z = x op p 00089 \endverbatim 00090 where op is a C++ bianry operator and p is a parameter. 00091 00092 This routine is given the sparsity patterns 00093 for a function G(z, y, ... ) 00094 and it uses them to compute the sparsity patterns for 00095 \verbatim 00096 H( x , w , u , ... ) = G[ z(x) , x , w , u , ... ] 00097 \endverbatim 00098 00099 \tparam Vector_set 00100 is the type used for vectors of sets. It can be either 00101 \c sparse_pack, \c sparse_set, or \c sparse_list. 00102 00103 00104 \param i_z 00105 variable index corresponding to the result for this operation; 00106 i.e. the row index in sparsity corresponding to z. 00107 00108 \param i_x 00109 variable index corresponding to the argument for this operator; 00110 i.e. the row index in sparsity corresponding to x. 00111 00112 \param sparsity 00113 \b Input: 00114 The set with index \a i_z in \a sparsity 00115 is the sparsity bit pattern for G with respect to the variable z. 00116 \n 00117 \b Input: 00118 The set with index \a i_x in \a sparsity 00119 is the sparsity bit pattern for G with respect to the variable x. 00120 \n 00121 \b Output: 00122 The set with index \a i_x in \a sparsity 00123 is the sparsity bit pattern for H with respect to the variable x. 00124 00125 \par Checked Assertions: 00126 \li \a i_x < \a i_z 00127 */ 00128 00129 template <class Vector_set> 00130 inline void reverse_sparse_jacobian_unary_op( 00131 size_t i_z , 00132 size_t i_x , 00133 Vector_set& sparsity ) 00134 { 00135 // check assumptions 00136 CPPAD_ASSERT_UNKNOWN( i_x < i_z ); 00137 00138 sparsity.binary_union(i_x, i_x, i_z, sparsity); 00139 00140 return; 00141 } 00142 00143 /*! 00144 Reverse mode Hessian sparsity pattern for linear unary operators. 00145 00146 The C++ source code corresponding to this operation is 00147 \verbatim 00148 z = fun(x) 00149 \endverbatim 00150 where fun is a linear functions; e.g. abs, or 00151 \verbatim 00152 z = x op p 00153 \endverbatim 00154 where op is a C++ binary operator and p is a parameter. 00155 00156 \copydetails reverse_sparse_hessian_unary_op 00157 */ 00158 template <class Vector_set> 00159 inline void reverse_sparse_hessian_linear_unary_op( 00160 size_t i_z , 00161 size_t i_x , 00162 bool* rev_jacobian , 00163 Vector_set& for_jac_sparsity , 00164 Vector_set& rev_hes_sparsity ) 00165 { 00166 // check assumptions 00167 CPPAD_ASSERT_UNKNOWN( i_x < i_z ); 00168 00169 rev_hes_sparsity.binary_union(i_x, i_x, i_z, rev_hes_sparsity); 00170 00171 rev_jacobian[i_x] |= rev_jacobian[i_z]; 00172 return; 00173 } 00174 00175 /*! 00176 Reverse mode Hessian sparsity pattern for non-linear unary operators. 00177 00178 The C++ source code corresponding to this operation is 00179 \verbatim 00180 z = fun(x) 00181 \endverbatim 00182 where fun is a non-linear functions; e.g. sin. or 00183 \verbatim 00184 z = p / x 00185 \endverbatim 00186 where p is a parameter. 00187 00188 00189 \copydetails reverse_sparse_hessian_unary_op 00190 */ 00191 template <class Vector_set> 00192 inline void reverse_sparse_hessian_nonlinear_unary_op( 00193 size_t i_z , 00194 size_t i_x , 00195 bool* rev_jacobian , 00196 Vector_set& for_jac_sparsity , 00197 Vector_set& rev_hes_sparsity ) 00198 { 00199 // check assumptions 00200 CPPAD_ASSERT_UNKNOWN( i_x < i_z ); 00201 00202 rev_hes_sparsity.binary_union(i_x, i_x, i_z, rev_hes_sparsity); 00203 if( rev_jacobian[i_z] ) 00204 rev_hes_sparsity.binary_union(i_x, i_x, i_x, for_jac_sparsity); 00205 00206 rev_jacobian[i_x] |= rev_jacobian[i_z]; 00207 return; 00208 } 00209 00210 /*! \} */ 00211 CPPAD_END_NAMESPACE 00212 # endif