source: branches/cache/cppad/local/tan_op.hpp @ 3324

Last change on this file since 3324 was 3324, checked in by bradbell, 6 years ago

merge trunk changes into cache

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