File: sin_cos.cpp

package info (click to toggle)
cppad 2026.00.00.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,584 kB
  • sloc: cpp: 112,960; sh: 6,146; ansic: 179; python: 71; sed: 12; makefile: 10
file content (400 lines) | stat: -rw-r--r-- 9,763 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-24 Bradley M. Bell
// ----------------------------------------------------------------------------

/*
Comprehensive test of Trigonometric and Hyperbolic Sine and Cosine
*/

# include <cppad/cppad.hpp>
# include <cmath>

namespace { // Begin empty namespace

bool Sin(void)
{  bool ok = true;
   using CppAD::sin;
   using CppAD::cos;
   using namespace CppAD;
   double eps99 = 99.0 * std::numeric_limits<double>::epsilon();

   // independent variable vector
   double x = .5;
   double y = .8;
   CPPAD_TESTVECTOR(AD<double>) X(2);
   X[0]     = x;
   X[1]     = y;
   Independent(X);

   // dependent variable vector
   CPPAD_TESTVECTOR(AD<double>) Z(1);
   AD<double> U = X[0] * X[1];
   Z[0] = sin( U );

   // create f: X -> Z and vectors used for derivative calculations
   // f(x, y) = sin(x, y)
   ADFun<double> f(X, Z);
   CPPAD_TESTVECTOR(double) v( 2 );
   CPPAD_TESTVECTOR(double) w( 1 );

   // check value
   double sin_u = sin( Value(U) );
   double cos_u = cos( Value(U) );

   ok &= NearEqual(sin_u, Value(Z[0]),  eps99 , eps99);

   // forward computation of partials w.r.t. u
   size_t j;
   size_t p     = 5;
   double jfac  = 1.;
   v[0]         = 1.;  // differential w.r.t. x
   v[1]         = 0;   // differential w.r.t. y
   double yj    = 1;   // y^j
   for(j = 1; j < p; j++)
   {  w      = f.Forward(j, v);

      // compute j-th power of y
      yj *= y ;

      // compute j-th derivartive of sin function
      double sinj;
      if( j % 4 == 1 )
         sinj = cos_u;
      else if( j % 4 == 2 )
         sinj = -sin_u;
      else if( j % 4 == 3 )
         sinj = -cos_u;
      else
         sinj = sin_u;

      jfac *= double(j);

      // check j-th derivative of z w.r.t x
      ok &= NearEqual(jfac*w[0], sinj * yj, eps99 , eps99);

      v[0]  = 0.;
   }

   // reverse computation of partials of Taylor coefficients
   CPPAD_TESTVECTOR(double) r( 2 * p);
   w[0]  = 1.;
   r     = f.Reverse(p, w);
   jfac  = 1.;
   yj    = 1.;
   double sinjp = 0.;
   for(j = 0; j < p; j++)
   {
      double sinj = sinjp;

      // compute j+1 derivative of sin function
      if( j % 4 == 0 )
         sinjp = cos_u;
      else if( j % 4 == 1 )
         sinjp = -sin_u;
      else if( j % 4 == 2 )
         sinjp = -cos_u;
      else
         sinjp = sin_u;

      // derivative w.r.t x of sin^{(j)} (x * y) * y^j
      ok &= NearEqual(jfac*r[0+j], sinjp * yj * y, eps99 , eps99);

      // derivative w.r.t y of sin^{(j)} (x * y) * y^j
      double value = sinjp * yj * x + double(j) * sinj * yj / y;
      ok &= NearEqual(r[p+j], value/jfac, eps99, eps99);

      jfac  *= double(j + 1);
      yj    *= y;
   }

   return ok;
}

bool Cos(void)
{  bool ok = true;
   using CppAD::sin;
   using CppAD::cos;
   using namespace CppAD;
   double eps99 = 99.0 * std::numeric_limits<double>::epsilon();

   // independent variable vector
   double x = .5;
   double y = .8;
   CPPAD_TESTVECTOR(AD<double>) X(2);
   X[0]     = x;
   X[1]     = y;
   Independent(X);

   // dependent variable vector
   CPPAD_TESTVECTOR(AD<double>) Z(1);
   AD<double> U = X[0] * X[1];
   Z[0] = cos( U );

   // create f: X -> Z and vectors used for derivative calculations
   // f(x, y) = cos(x, y)
   ADFun<double> f(X, Z);
   CPPAD_TESTVECTOR(double) v( 2 );
   CPPAD_TESTVECTOR(double) w( 1 );

   // check value
   double sin_u = sin( Value(U) );
   double cos_u = cos( Value(U) );

   ok &= NearEqual(cos_u, Value(Z[0]),  eps99 , eps99);

   // forward computation of partials w.r.t. u
   size_t j;
   size_t p     = 5;
   double jfac  = 1.;
   v[0]         = 1.;  // differential w.r.t. x
   v[1]         = 0;   // differential w.r.t. y
   double yj    = 1;   // y^j
   for(j = 1; j < p; j++)
   {  w      = f.Forward(j, v);

      // compute j-th power of y
      yj *= y ;

      // compute j-th derivartive of cos function
      double cosj;
      if( j % 4 == 1 )
         cosj = -sin_u;
      else if( j % 4 == 2 )
         cosj = -cos_u;
      else if( j % 4 == 3 )
         cosj = sin_u;
      else
         cosj = cos_u;

      jfac *= double(j);

      // check j-th derivative of z w.r.t x
      ok &= NearEqual(jfac*w[0], cosj * yj, eps99 , eps99);

      v[0]  = 0.;
   }

   // reverse computation of partials of Taylor coefficients
   CPPAD_TESTVECTOR(double) r( 2 * p);
   w[0]  = 1.;
   r     = f.Reverse(p, w);
   jfac  = 1.;
   yj    = 1.;
   double cosjp = 0.;
   for(j = 0; j < p; j++)
   {
      double cosj = cosjp;

      // compute j+1 derivative of cos function
      if( j % 4 == 0 )
         cosjp = -sin_u;
      else if( j % 4 == 1 )
         cosjp = -cos_u;
      else if( j % 4 == 2 )
         cosjp = sin_u;
      else
         cosjp = cos_u;

      // derivative w.r.t x of cos^{(j)} (x * y) * y^j
      ok &= NearEqual(jfac*r[0+j], cosjp * yj * y, eps99 , eps99);

      // derivative w.r.t y of cos^{(j)} (x * y) * y^j
      double value = cosjp * yj * x + double(j) * cosj * yj / y;
      ok &= NearEqual(r[p+j], value/jfac, eps99, eps99);

      jfac  *= double(j + 1);
      yj    *= y;
   }

   return ok;
}

bool Cosh(void)
{  bool ok = true;
   using CppAD::sinh;
   using CppAD::cosh;
   using namespace CppAD;
   double eps99 = 99.0 * std::numeric_limits<double>::epsilon();

   // independent variable vector
   double x = .5;
   double y = .8;
   CPPAD_TESTVECTOR(AD<double>) X(2);
   X[0]     = x;
   X[1]     = y;
   Independent(X);

   // dependent variable vector
   CPPAD_TESTVECTOR(AD<double>) Z(1);
   AD<double> U = X[0] * X[1];
   Z[0] = cosh( U );

   // create f: X -> Z and vectors used for derivative calculations
   // f(x, y) = cosh(x, y)
   ADFun<double> f(X, Z);
   CPPAD_TESTVECTOR(double) v( 2 );
   CPPAD_TESTVECTOR(double) w( 1 );

   // check value
   double sinh_u = sinh( Value(U) );
   double cosh_u = cosh( Value(U) );

   ok &= NearEqual(cosh_u, Value(Z[0]),  eps99 , eps99);

   // forward computation of partials w.r.t. u
   size_t j;
   size_t p     = 5;
   double jfac  = 1.;
   v[0]         = 1.;  // differential w.r.t. x
   v[1]         = 0;   // differential w.r.t. y
   double yj    = 1;   // y^j
   for(j = 1; j < p; j++)
   {  w      = f.Forward(j, v);

      // compute j-th power of y
      yj *= y ;

      // compute j-th derivartive of cosh function
      double coshj;
      if( j % 2 == 1 )
         coshj = sinh_u;
      else
         coshj = cosh_u;

      jfac *= double(j);

      // check j-th derivative of z w.r.t x
      ok &= NearEqual(jfac*w[0], coshj * yj, eps99 , eps99);

      v[0]  = 0.;
   }

   // reverse computation of partials of Taylor coefficients
   CPPAD_TESTVECTOR(double) r( 2 * p);
   w[0]  = 1.;
   r     = f.Reverse(p, w);
   jfac  = 1.;
   yj    = 1.;
   double coshjp = 0.;
   for(j = 0; j < p; j++)
   {
      double coshj = coshjp;

      // compute j+1 derivative of cosh function
      if( j % 2 == 0 )
         coshjp = sinh_u;
      else
         coshjp = cosh_u;

      // derivative w.r.t x of cosh^{(j)} (x * y) * y^j
      ok &= NearEqual(jfac*r[0+j], coshjp * yj * y, eps99 , eps99);

      // derivative w.r.t y of cosh^{(j)} (x * y) * y^j
      double value = coshjp * yj * x + double(j) * coshj * yj / y;
      ok &= NearEqual(r[p+j], value/jfac, eps99, eps99);

      jfac  *= double(j + 1);
      yj    *= y;
   }

   return ok;
}

bool Sinh(void)
{  bool ok = true;
   using CppAD::sinh;
   using CppAD::cosh;
   using namespace CppAD;
   double eps99 = 99.0 * std::numeric_limits<double>::epsilon();

   // independent variable vector
   double x = .5;
   double y = .8;
   CPPAD_TESTVECTOR(AD<double>) X(2);
   X[0]     = x;
   X[1]     = y;
   Independent(X);

   // dependent variable vector
   CPPAD_TESTVECTOR(AD<double>) Z(1);
   AD<double> U = X[0] * X[1];
   Z[0] = sinh( U );

   // create f: X -> Z and vectors used for derivative calculations
   // f(x, y) = sinh(x, y)
   ADFun<double> f(X, Z);
   CPPAD_TESTVECTOR(double) v( 2 );
   CPPAD_TESTVECTOR(double) w( 1 );

   // check value
   double sinh_u = sinh( Value(U) );
   double cosh_u = cosh( Value(U) );

   ok &= NearEqual(sinh_u, Value(Z[0]),  eps99 , eps99);

   // forward computation of partials w.r.t. u
   size_t j;
   size_t p     = 5;
   double jfac  = 1.;
   v[0]         = 1.;  // differential w.r.t. x
   v[1]         = 0;   // differential w.r.t. y
   double yj    = 1;   // y^j
   for(j = 1; j < p; j++)
   {  w      = f.Forward(j, v);

      // compute j-th power of y
      yj *= y ;

      // compute j-th derivartive of sinh function
      double sinhj;
      if( j % 2 == 1 )
         sinhj = cosh_u;
      else
         sinhj = sinh_u;

      jfac *= double(j);

      // check j-th derivative of z w.r.t x
      ok &= NearEqual(jfac*w[0], sinhj * yj, eps99 , eps99);

      v[0]  = 0.;
   }

   // reverse computation of partials of Taylor coefficients
   CPPAD_TESTVECTOR(double) r( 2 * p);
   w[0]  = 1.;
   r     = f.Reverse(p, w);
   jfac  = 1.;
   yj    = 1.;
   double sinhjp = 0.;
   for(j = 0; j < p; j++)
   {
      double sinhj = sinhjp;

      // compute j+1 derivative of sinh function
      if( j % 2 == 0 )
         sinhjp = cosh_u;
      else
         sinhjp = sinh_u;

      // derivative w.r.t x of sinh^{(j)} (x * y) * y^j
      ok &= NearEqual(jfac*r[0+j], sinhjp * yj * y, eps99 , eps99);

      // derivative w.r.t y of sinh^{(j)} (x * y) * y^j
      double value = sinhjp * yj * x + double(j) * sinhj * yj / y;
      ok &= NearEqual(r[p+j], value/jfac, eps99, eps99);

      jfac  *= double(j + 1);
      yj    *= y;
   }

   return ok;
}

} // End empty namespace

bool SinCos(void)
{  bool ok = Sin() && Cos() && Cosh() && Sinh();
   return ok;
}