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