source: trunk/cppad/local/exp_op.hpp @ 3320

Last change on this file since 3320 was 3320, checked in by bradbell, 6 years ago
  1. g++ 4.8.2 has shadow warnings by default, but eigen and fadbad do not

these warnings, so supress then in these cases.

  1. Move check that arguments come before result into on place,

CPPAD_ASSERT_ARG_BEFORE_RESULT (only one argument case so far).

main.cpp: fix shadowing of index variable.
CMakeLists.txt: adapt to change in teuchos library name.
sparse_jacobian.cpp: fix a shadowed variable.
check_svn_id.sh: ignore svn_commit.sh.
gpl_license.sh: ignore svn_commit.sh.

  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
1/* $Id: exp_op.hpp 3320 2014-09-11 23:06:21Z bradbell $ */
2# ifndef CPPAD_EXP_OP_INCLUDED
3# define CPPAD_EXP_OP_INCLUDED
4
5/* --------------------------------------------------------------------------
6CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-14 Bradley M. Bell
7
8CppAD is distributed under multiple licenses. This distribution is under
9the terms of the
10                    Eclipse Public License Version 1.0.
11
12A copy of this license is included in the COPYING file of this distribution.
13Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
14-------------------------------------------------------------------------- */
15
16
17namespace CppAD { // BEGIN_CPPAD_NAMESPACE
18/*!
19\file exp_op.hpp
20Forward and reverse mode calculations for z = exp(x).
21*/
22
23
24/*!
25Forward mode Taylor coefficient for result of op = ExpOp.
26
27The C++ source code corresponding to this operation is
28\verbatim
29        z = exp(x)
30\endverbatim
31
32\copydetails forward_unary1_op
33*/
34template <class Base>
35inline void forward_exp_op(
36        size_t p           ,
37        size_t q           ,
38        size_t i_z         ,
39        size_t i_x         ,
40        size_t cap_order   , 
41        Base*  taylor      )
42{       
43        // check assumptions
44        CPPAD_ASSERT_UNKNOWN( NumArg(ExpOp) == 1 );
45        CPPAD_ASSERT_UNKNOWN( NumRes(ExpOp) == 1 );
46        CPPAD_ASSERT_UNKNOWN( q < cap_order );
47        CPPAD_ASSERT_UNKNOWN( p <= q );
48
49        // Taylor coefficients corresponding to argument and result
50        Base* x = taylor + i_x * cap_order;
51        Base* z = taylor + i_z * cap_order;
52
53        size_t k;
54        if( p == 0 )
55        {       z[0] = exp( x[0] );
56                p++;
57        }
58        for(size_t j = p; j <= q; j++)
59        {
60                z[j] = x[1] * z[j-1];
61                for(k = 2; k <= j; k++)
62                        z[j] += Base(k) * x[k] * z[j-k];
63                z[j] /= Base(j);
64        }
65}
66
67
68/*!
69Multiple direction forward mode Taylor coefficient for op = ExpOp.
70
71The C++ source code corresponding to this operation is
72\verbatim
73        z = exp(x)
74\endverbatim
75
76\copydetails forward_unary1_op_dir
77*/
78template <class Base>
79inline void forward_exp_op_dir(
80        size_t q           ,
81        size_t r           ,
82        size_t i_z         ,
83        size_t i_x         ,
84        size_t cap_order   , 
85        Base*  taylor      )
86{       
87        // check assumptions
88        CPPAD_ASSERT_UNKNOWN( NumArg(ExpOp) == 1 );
89        CPPAD_ASSERT_UNKNOWN( NumRes(ExpOp) == 1 );
90        CPPAD_ASSERT_UNKNOWN( q < cap_order );
91        CPPAD_ASSERT_UNKNOWN( 0 < q );
92
93        // Taylor coefficients corresponding to argument and result
94        size_t num_taylor_per_var = (cap_order-1) * r + 1;
95        Base* x = taylor + i_x * num_taylor_per_var;
96        Base* z = taylor + i_z * num_taylor_per_var; 
97
98        size_t m = (q-1)*r + 1;
99        for(size_t ell = 0; ell < r; ell++)
100        {       z[m+ell] = Base(q) * x[m+ell] * z[0];
101                for(size_t k = 1; k < q; k++)
102                        z[m+ell] += Base(k) * x[(k-1)*r+ell+1] * z[(q-k-1)*r+ell+1];
103                z[m+ell] /= Base(q);
104        }
105}
106
107/*!
108Zero order forward mode Taylor coefficient for result of op = ExpOp.
109
110The C++ source code corresponding to this operation is
111\verbatim
112        z = exp(x)
113\endverbatim
114
115\copydetails forward_unary1_op_0
116*/
117template <class Base>
118inline void forward_exp_op_0(
119        size_t i_z         ,
120        size_t i_x         ,
121        size_t cap_order   , 
122        Base*  taylor      )
123{
124        // check assumptions
125        CPPAD_ASSERT_UNKNOWN( NumArg(ExpOp) == 1 );
126        CPPAD_ASSERT_UNKNOWN( NumRes(ExpOp) == 1 );
127        CPPAD_ASSERT_UNKNOWN( 0 < cap_order );
128
129        // Taylor coefficients corresponding to argument and result
130        Base* x = taylor + i_x * cap_order;
131        Base* z = taylor + i_z * cap_order;
132
133        z[0] = exp( x[0] );
134}
135/*!
136Reverse mode partial derivatives for result of op = ExpOp.
137
138The C++ source code corresponding to this operation is
139\verbatim
140        z = exp(x)
141\endverbatim
142
143\copydetails reverse_unary1_op
144*/
145
146template <class Base>
147inline void reverse_exp_op(
148        size_t      d            ,
149        size_t      i_z          ,
150        size_t      i_x          ,
151        size_t      cap_order    , 
152        const Base* taylor       ,
153        size_t      nc_partial   ,
154        Base*       partial      )
155{
156        // check assumptions
157        CPPAD_ASSERT_UNKNOWN( NumArg(ExpOp) == 1 );
158        CPPAD_ASSERT_UNKNOWN( NumRes(ExpOp) == 1 );
159        CPPAD_ASSERT_UNKNOWN( d < cap_order );
160        CPPAD_ASSERT_UNKNOWN( d < nc_partial );
161
162        // Taylor coefficients and partials corresponding to argument
163        const Base* x  = taylor  + i_x * cap_order;
164        Base* px       = partial + i_x * nc_partial;
165
166        // Taylor coefficients and partials corresponding to result
167        const Base* z  = taylor  + i_z * cap_order;
168        Base* pz       = partial + i_z * nc_partial;
169
170        // loop through orders in reverse
171        size_t j, k;
172        j = d;
173        while(j)
174        {       // scale partial w.r.t z[j]
175                pz[j] /= Base(j);
176
177                for(k = 1; k <= j; k++)
178                {       px[k]   += pz[j] * Base(k) * z[j-k];   
179                        pz[j-k] += pz[j] * Base(k) * x[k];
180                }
181                --j;
182        }
183        px[0] += pz[0] * z[0];
184}
185
186} // END_CPPAD_NAMESPACE
187# endif
Note: See TracBrowser for help on using the repository browser.