CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cosh_op.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_LOCAL_COSH_OP_HPP
2 # define CPPAD_LOCAL_COSH_OP_HPP
3 
4 /* --------------------------------------------------------------------------
5 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
6 
7 CppAD is distributed under multiple licenses. This distribution is under
8 the terms of the
9  Eclipse Public License Version 1.0.
10 
11 A copy of this license is included in the COPYING file of this distribution.
12 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
13 -------------------------------------------------------------------------- */
14 
15 
16 namespace CppAD { namespace local { // BEGIN_CPPAD_LOCAL_NAMESPACE
17 /*!
18 \file cosh_op.hpp
19 Forward and reverse mode calculations for z = cosh(x).
20 */
21 
22 
23 /*!
24 Compute forward mode Taylor coefficient for result of op = CoshOp.
25 
26 The C++ source code corresponding to this operation is
27 \verbatim
28  z = cosh(x)
29 \endverbatim
30 The auxillary result is
31 \verbatim
32  y = sinh(x)
33 \endverbatim
34 The value of y, and its derivatives, are computed along with the value
35 and derivatives of z.
36 
37 \copydetails CppAD::local::forward_unary2_op
38 */
39 template <class Base>
40 inline void forward_cosh_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
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  // rest of this routine is identical for the following cases:
60  // forward_sin_op, forward_cos_op, forward_sinh_op, forward_cosh_op.
61  // (except that there is a sign difference for hyperbolic case).
62  size_t k;
63  if( p == 0 )
64  { s[0] = sinh( x[0] );
65  c[0] = cosh( x[0] );
66  p++;
67  }
68  for(size_t j = p; j <= q; j++)
69  {
70  s[j] = Base(0.0);
71  c[j] = Base(0.0);
72  for(k = 1; k <= j; k++)
73  { s[j] += Base(double(k)) * x[k] * c[j-k];
74  c[j] += Base(double(k)) * x[k] * s[j-k];
75  }
76  s[j] /= Base(double(j));
77  c[j] /= Base(double(j));
78  }
79 }
80 /*!
81 Compute forward mode Taylor coefficient for result of op = CoshOp.
82 
83 The C++ source code corresponding to this operation is
84 \verbatim
85  z = cosh(x)
86 \endverbatim
87 The auxillary result is
88 \verbatim
89  y = sinh(x)
90 \endverbatim
91 The value of y, and its derivatives, are computed along with the value
92 and derivatives of z.
93 
94 \copydetails CppAD::local::forward_unary2_op_dir
95 */
96 template <class Base>
97 inline void forward_cosh_op_dir(
98  size_t q ,
99  size_t r ,
100  size_t i_z ,
101  size_t i_x ,
102  size_t cap_order ,
103  Base* taylor )
104 {
105  // check assumptions
108  CPPAD_ASSERT_UNKNOWN( 0 < q );
109  CPPAD_ASSERT_UNKNOWN( q < cap_order );
110 
111  // Taylor coefficients corresponding to argument and result
112  size_t num_taylor_per_var = (cap_order-1) * r + 1;
113  Base* x = taylor + i_x * num_taylor_per_var;
114  Base* s = taylor + i_z * num_taylor_per_var;
115  Base* c = s - num_taylor_per_var;
116 
117 
118  // rest of this routine is identical for the following cases:
119  // forward_sin_op, forward_cos_op, forward_sinh_op, forward_cosh_op
120  // (except that there is a sign difference for the hyperbolic case).
121  size_t m = (q-1) * r + 1;
122  for(size_t ell = 0; ell < r; ell++)
123  { s[m+ell] = Base(double(q)) * x[m + ell] * c[0];
124  c[m+ell] = Base(double(q)) * x[m + ell] * s[0];
125  for(size_t k = 1; k < q; k++)
126  { s[m+ell] += Base(double(k)) * x[(k-1)*r+1+ell] * c[(q-k-1)*r+1+ell];
127  c[m+ell] += Base(double(k)) * x[(k-1)*r+1+ell] * s[(q-k-1)*r+1+ell];
128  }
129  s[m+ell] /= Base(double(q));
130  c[m+ell] /= Base(double(q));
131  }
132 }
133 
134 /*!
135 Compute zero order forward mode Taylor coefficient for result of op = CoshOp.
136 
137 The C++ source code corresponding to this operation is
138 \verbatim
139  z = cosh(x)
140 \endverbatim
141 The auxillary result is
142 \verbatim
143  y = sinh(x)
144 \endverbatim
145 The value of y is computed along with the value of z.
146 
147 \copydetails CppAD::local::forward_unary2_op_0
148 */
149 template <class Base>
150 inline void forward_cosh_op_0(
151  size_t i_z ,
152  size_t i_x ,
153  size_t cap_order ,
154  Base* taylor )
155 {
156  // check assumptions
159  CPPAD_ASSERT_UNKNOWN( 0 < cap_order );
160 
161  // Taylor coefficients corresponding to argument and result
162  Base* x = taylor + i_x * cap_order;
163  Base* c = taylor + i_z * cap_order; // called z in documentation
164  Base* s = c - cap_order; // called y in documentation
165 
166  c[0] = cosh( x[0] );
167  s[0] = sinh( x[0] );
168 }
169 /*!
170 Compute reverse mode partial derivatives for result of op = CoshOp.
171 
172 The C++ source code corresponding to this operation is
173 \verbatim
174  z = cosh(x)
175 \endverbatim
176 The auxillary result is
177 \verbatim
178  y = sinh(x)
179 \endverbatim
180 The value of y is computed along with the value of z.
181 
182 \copydetails CppAD::local::reverse_unary2_op
183 */
184 
185 template <class Base>
186 inline void reverse_cosh_op(
187  size_t d ,
188  size_t i_z ,
189  size_t i_x ,
190  size_t cap_order ,
191  const Base* taylor ,
192  size_t nc_partial ,
193  Base* partial )
194 {
195  // check assumptions
198  CPPAD_ASSERT_UNKNOWN( d < cap_order );
199  CPPAD_ASSERT_UNKNOWN( d < nc_partial );
200 
201  // Taylor coefficients and partials corresponding to argument
202  const Base* x = taylor + i_x * cap_order;
203  Base* px = partial + i_x * nc_partial;
204 
205  // Taylor coefficients and partials corresponding to first result
206  const Base* c = taylor + i_z * cap_order; // called z in doc
207  Base* pc = partial + i_z * nc_partial;
208 
209  // Taylor coefficients and partials corresponding to auxillary result
210  const Base* s = c - cap_order; // called y in documentation
211  Base* ps = pc - nc_partial;
212 
213 
214  // rest of this routine is identical for the following cases:
215  // reverse_sin_op, reverse_cos_op, reverse_sinh_op, reverse_cosh_op.
216  size_t j = d;
217  size_t k;
218  while(j)
219  {
220  ps[j] /= Base(double(j));
221  pc[j] /= Base(double(j));
222  for(k = 1; k <= j; k++)
223  {
224  px[k] += Base(double(k)) * azmul(ps[j], c[j-k]);
225  px[k] += Base(double(k)) * azmul(pc[j], s[j-k]);
226 
227  ps[j-k] += Base(double(k)) * azmul(pc[j], x[k]);
228  pc[j-k] += Base(double(k)) * azmul(ps[j], x[k]);
229 
230  }
231  --j;
232  }
233  px[0] += azmul(ps[0], c[0]);
234  px[0] += azmul(pc[0], s[0]);
235 }
236 
237 } } // END_CPPAD_LOCAL_NAMESPACE
238 # endif
AD< Base > cosh(const AD< Base > &x)
AD< Base > azmul(const AD< Base > &x, const AD< Base > &y)
Definition: azmul.hpp:94
AD< Base > sinh(const AD< Base > &x)
size_t NumArg(OpCode op)
Number of arguments for a specified operator.
Definition: op_code.hpp:175
size_t NumRes(OpCode op)
Number of variables resulting from the specified operation.
Definition: op_code.hpp:281
void forward_cosh_op_0(size_t i_z, size_t i_x, size_t cap_order, Base *taylor)
Compute zero order forward mode Taylor coefficient for result of op = CoshOp.
Definition: cosh_op.hpp:150
void forward_cosh_op(size_t p, size_t q, size_t i_z, size_t i_x, size_t cap_order, Base *taylor)
Compute forward mode Taylor coefficient for result of op = CoshOp.
Definition: cosh_op.hpp:40
#define CPPAD_ASSERT_UNKNOWN(exp)
Check that exp is true, if not terminate execution.
void forward_cosh_op_dir(size_t q, size_t r, size_t i_z, size_t i_x, size_t cap_order, Base *taylor)
Compute forward mode Taylor coefficient for result of op = CoshOp.
Definition: cosh_op.hpp:97
void reverse_cosh_op(size_t d, size_t i_z, size_t i_x, size_t cap_order, const Base *taylor, size_t nc_partial, Base *partial)
Compute reverse mode partial derivatives for result of op = CoshOp.
Definition: cosh_op.hpp:186