source: trunk/cppad/local/cos_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: 6.2 KB
Line 
1/* $Id: cos_op.hpp 3320 2014-09-11 23:06:21Z bradbell $ */
2# ifndef CPPAD_COS_OP_INCLUDED
3# define CPPAD_COS_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 cos_op.hpp
20Forward and reverse mode calculations for z = cos(x).
21*/
22
23/*!
24Compute forward mode Taylor coefficient for result of op = CosOp.
25
26The C++ source code corresponding to this operation is
27\verbatim
28        z = cos(x)
29\endverbatim
30The auxillary result is
31\verbatim
32        y = sin(x)
33\endverbatim
34The value of y, and its derivatives, are computed along with the value
35and derivatives of z.
36
37\copydetails forward_unary2_op
38*/
39template <class Base>
40inline void forward_cos_op(
41        size_t p           ,
42        size_t q           ,
43        size_t i_z         ,
44        size_t i_x         ,
45        size_t cap_order   , 
46        Base*  taylor      )
47{       
48        // check assumptions
49        CPPAD_ASSERT_UNKNOWN( NumArg(CosOp) == 1 );
50        CPPAD_ASSERT_UNKNOWN( NumRes(CosOp) == 2 );
51        CPPAD_ASSERT_UNKNOWN( q < cap_order );
52        CPPAD_ASSERT_UNKNOWN( p <= q );
53
54        // Taylor coefficients corresponding to argument and result
55        Base* x = taylor + i_x * cap_order;
56        Base* c = taylor + i_z * cap_order;
57        Base* s = c      -       cap_order;
58
59
60        // rest of this routine is identical for the following cases:
61        // forward_sin_op, forward_cos_op, forward_sinh_op, forward_cosh_op.
62        // (except that there is a sign difference for the hyperbolic case).
63        size_t k;
64        if( p == 0 )
65        {       s[0] = sin( x[0] );
66                c[0] = cos( x[0] );
67                p++;
68        }
69        for(size_t j = p; j <= q; j++)
70        {
71                s[j] = Base(0);
72                c[j] = Base(0);
73                for(k = 1; k <= j; k++)
74                {       s[j] += Base(k) * x[k] * c[j-k];
75                        c[j] -= Base(k) * x[k] * s[j-k];
76                }
77                s[j] /= Base(j);
78                c[j] /= Base(j);
79        }
80}
81/*!
82Compute forward mode Taylor coefficient for result of op = CosOp.
83
84The C++ source code corresponding to this operation is
85\verbatim
86        z = cos(x)
87\endverbatim
88The auxillary result is
89\verbatim
90        y = sin(x)
91\endverbatim
92The value of y, and its derivatives, are computed along with the value
93and derivatives of z.
94
95\copydetails forward_unary2_op_dir
96*/
97template <class Base>
98inline void forward_cos_op_dir(
99        size_t q           ,
100        size_t r           ,
101        size_t i_z         ,
102        size_t i_x         ,
103        size_t cap_order   , 
104        Base*  taylor      )
105{       
106        // check assumptions
107        CPPAD_ASSERT_UNKNOWN( NumArg(CosOp) == 1 );
108        CPPAD_ASSERT_UNKNOWN( NumRes(CosOp) == 2 );
109        CPPAD_ASSERT_UNKNOWN( 0 < q );
110        CPPAD_ASSERT_UNKNOWN( q < cap_order );
111
112        // Taylor coefficients corresponding to argument and result
113        size_t num_taylor_per_var = (cap_order-1) * r + 1;
114        Base* x = taylor + i_x * num_taylor_per_var;
115        Base* c = taylor + i_z * num_taylor_per_var;
116        Base* s = c      -       num_taylor_per_var;
117
118
119        // rest of this routine is identical for the following cases:
120        // forward_sin_op, forward_cos_op, forward_sinh_op, forward_cosh_op
121        // (except that there is a sign difference for the hyperbolic case).
122        size_t m = (q-1) * r + 1;
123        for(size_t ell = 0; ell < r; ell++)
124        {       s[m+ell] =   Base(q) * x[m + ell] * c[0];
125                c[m+ell] = - Base(q) * x[m + ell] * s[0];
126                for(size_t k = 1; k < q; k++)
127                {       s[m+ell] += Base(k) * x[(k-1)*r+1+ell] * c[(q-k-1)*r+1+ell];
128                        c[m+ell] -= Base(k) * x[(k-1)*r+1+ell] * s[(q-k-1)*r+1+ell];
129                }
130                s[m+ell] /= Base(q);
131                c[m+ell] /= Base(q);
132        }
133}
134
135/*!
136Compute zero order forward mode Taylor coefficient for result of op = CosOp.
137
138The C++ source code corresponding to this operation is
139\verbatim
140        z = cos(x)
141\endverbatim
142The auxillary result is
143\verbatim
144        y = sin(x)
145\endverbatim
146The value of y is computed along with the value of z.
147
148\copydetails forward_unary2_op_0
149*/
150template <class Base>
151inline void forward_cos_op_0(
152        size_t i_z         ,
153        size_t i_x         ,
154        size_t cap_order   , 
155        Base*  taylor      )
156{
157        // check assumptions
158        CPPAD_ASSERT_UNKNOWN( NumArg(CosOp) == 1 );
159        CPPAD_ASSERT_UNKNOWN( NumRes(CosOp) == 2 );
160        CPPAD_ASSERT_UNKNOWN( 0 < cap_order );
161
162        // Taylor coefficients corresponding to argument and result
163        Base* x = taylor + i_x * cap_order;
164        Base* c = taylor + i_z * cap_order;  // called z in documentation
165        Base* s = c      -       cap_order;  // called y in documentation
166
167        c[0] = cos( x[0] );
168        s[0] = sin( x[0] );
169}
170/*!
171Compute reverse mode partial derivatives for result of op = CosOp.
172
173The C++ source code corresponding to this operation is
174\verbatim
175        z = cos(x)
176\endverbatim
177The auxillary result is
178\verbatim
179        y = sin(x)
180\endverbatim
181The value of y is computed along with the value of z.
182
183\copydetails reverse_unary2_op
184*/
185
186template <class Base>
187inline void reverse_cos_op(
188        size_t      d            ,
189        size_t      i_z          ,
190        size_t      i_x          ,
191        size_t      cap_order    , 
192        const Base* taylor       ,
193        size_t      nc_partial   ,
194        Base*       partial      )
195{
196        // check assumptions
197        CPPAD_ASSERT_UNKNOWN( NumArg(CosOp) == 1 );
198        CPPAD_ASSERT_UNKNOWN( NumRes(CosOp) == 2 );
199        CPPAD_ASSERT_UNKNOWN( d < cap_order );
200        CPPAD_ASSERT_UNKNOWN( d < nc_partial );
201
202        // Taylor coefficients and partials corresponding to argument
203        const Base* x  = taylor  + i_x * cap_order;
204        Base* px       = partial + i_x * nc_partial;
205
206        // Taylor coefficients and partials corresponding to first result
207        const Base* c  = taylor  + i_z * cap_order; // called z in doc
208        Base* pc       = partial + i_z * nc_partial;
209
210        // Taylor coefficients and partials corresponding to auxillary result
211        const Base* s  = c  - cap_order; // called y in documentation
212        Base* ps       = pc - nc_partial;
213
214
215        // rest of this routine is identical for the following cases:
216        // reverse_sin_op, reverse_cos_op, reverse_sinh_op, reverse_cosh_op.
217        size_t j = d;
218        size_t k;
219        while(j)
220        {
221                ps[j]   /= Base(j);
222                pc[j]   /= Base(j);
223                for(k = 1; k <= j; k++)
224                {
225                        px[k]   += ps[j] * Base(k) * c[j-k];
226                        px[k]   -= pc[j] * Base(k) * s[j-k];
227       
228                        ps[j-k] -= pc[j] * Base(k) * x[k];
229                        pc[j-k] += ps[j] * Base(k) * x[k];
230
231                }
232                --j;
233        }
234        px[0] += ps[0] * c[0];
235        px[0] -= pc[0] * s[0];
236}
237
238} // END_CPPAD_NAMESPACE
239# endif
Note: See TracBrowser for help on using the repository browser.