Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FadUnitTests2.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Sacado Package
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25 // (etphipp@sandia.gov).
26 //
27 // ***********************************************************************
28 // @HEADER
29 
30 #ifndef FADUNITTESTS2_HPP
31 #define FADUNITTESTS2_HPP
32 
33 // Sacado includes
34 #include "Sacado_No_Kokkos.hpp"
35 #include "Sacado_Random.hpp"
36 
37 // Cppunit includes
38 #include <cppunit/extensions/HelperMacros.h>
39 
40 #define COMPARE_VALUES(a, b) \
41  CPPUNIT_ASSERT( std::abs(a-b) < this->tol_a + this->tol_r*std::abs(a) );
42 
43 #define COMPARE_FADS(a, b) \
44 CPPUNIT_ASSERT(a.size() == b.size()); \
45 CPPUNIT_ASSERT(a.hasFastAccess() == b.hasFastAccess()); \
46 COMPARE_VALUES(a.val(), b.val()); \
47 for (int i=0; i<a.size(); i++) { \
48  COMPARE_VALUES(a.dx(i), b.dx(i)); \
49  COMPARE_VALUES(a.fastAccessDx(i), b.fastAccessDx(i)); \
50  } \
51  ;
52 
53 // A class for testing each Fad operation based on hand-coded derivatives.
54 // Only those operations that are defined for both real and complex
55 // types are tested here. The others are tested below
56 template <class FadType, class ScalarType>
57 class FadOpsUnitTest2 : public CppUnit::TestFixture {
58 
60 
65 
68 
70 
73 
84 
89 
95 
97 
101 
103 
104 public:
105 
106  FadOpsUnitTest2();
107 
108  FadOpsUnitTest2(int numComponents, double absolute_tolerance,
109  double relative_tolerance);
110 
111  void setUp();
112 
113  void tearDown();
114 
115  void testAddition();
116  void testSubtraction();
117  void testMultiplication ();
118  void testDivision();
119 
120  void testEquals();
121  void testNotEquals();
122 
123  void testPow();
124 
125  void testUnaryPlus();
126  void testUnaryMinus();
127 
128  void testExp();
129  void testLog();
130  void testLog10();
131  void testSqrt();
132  void testCos();
133  void testSin();
134  void testTan();
135  void testCosh();
136  void testSinh();
137  void testTanh();
138 
139  void testPlusEquals();
140  void testMinusEquals();
141  void testTimesEquals();
142  void testDivideEquals();
143 
144  void testEqualsLR();
145  void testPlusEqualsLR();
146  void testMinusEqualsLR();
147  void testTimesEqualsLR();
148  void testDivideEqualsLR();
149 
150  void testResizeBug6135();
151 
152  void testEquality();
153  void testEqualityConstL();
154  void testEqualityConstR();
155 
156 protected:
157 
158  // DFad variables
160 
161  // Random number generator
163 
164  // Number of derivative components
165  int n;
166 
167  // Tolerances to which fad objects should be the same
168  double tol_a, tol_r;
169 
170 }; // class FadOpsUnitTest2
171 
172 template <class FadType, class ScalarType>
175  urand(), n(5), tol_a(1.0e-15), tol_r(1.0e-14) {}
176 
177 template <class FadType, class ScalarType>
179 FadOpsUnitTest2(int numComponents, double absolute_tolerance,
180  double relative_tolerance) :
181  urand(),
182  n(numComponents),
183  tol_a(absolute_tolerance),
184  tol_r(relative_tolerance) {}
185 
186 template <class FadType, class ScalarType>
188  ScalarType val;
189 
190  val = urand.number();
191  a_fad = FadType(n,val);
192 
193  val = urand.number();
194  b_fad = FadType(n,val);
195 
196  for (int i=0; i<n; i++) {
197  val = urand.number();
198  a_fad.fastAccessDx(i) = val;
199 
200  val = urand.number();
201  b_fad.fastAccessDx(i) = val;
202  }
203 }
204 
205 template <class FadType, class ScalarType>
208 
209 template <class FadType, class ScalarType>
210 void
213  c_fad = a_fad + b_fad;
214  FadType t1(n, a_fad.val()+b_fad.val());
215  for (int i=0; i<n; i++)
216  t1.fastAccessDx(i) = a_fad.dx(i) + b_fad.dx(i);
217  COMPARE_FADS(c_fad, t1);
218 
219  ScalarType val = urand.number();
220  c_fad = a_fad + val;
221  FadType t2(n, a_fad.val()+val);
222  for (int i=0; i<n; i++)
223  t2.fastAccessDx(i) = a_fad.dx(i);
224  COMPARE_FADS(c_fad, t2);
225 
226  c_fad = val + b_fad;
227  FadType t3(n, val+b_fad.val());
228  for (int i=0; i<n; i++)
229  t3.fastAccessDx(i) = b_fad.dx(i);
230  COMPARE_FADS(c_fad, t3);
231 }
232 
233 template <class FadType, class ScalarType>
234 void
237  c_fad = a_fad - b_fad;
238  FadType t1(n, a_fad.val()-b_fad.val());
239  for (int i=0; i<n; i++)
240  t1.fastAccessDx(i) = a_fad.dx(i) - b_fad.dx(i);
241  COMPARE_FADS(c_fad, t1);
242 
243  ScalarType val = urand.number();
244  c_fad = a_fad - val;
245  FadType t2(n, a_fad.val()-val);
246  for (int i=0; i<n; i++)
247  t2.fastAccessDx(i) = a_fad.dx(i);
248  COMPARE_FADS(c_fad, t2);
249 
250  c_fad = val - b_fad;
251  FadType t3(n, val-b_fad.val());
252  for (int i=0; i<n; i++)
253  t3.fastAccessDx(i) = -b_fad.dx(i);
254  COMPARE_FADS(c_fad, t3);
255 }
256 
257 template <class FadType, class ScalarType>
258 void
261  c_fad = a_fad * b_fad;
262  FadType t1(n, a_fad.val()*b_fad.val());
263  for (int i=0; i<n; i++)
264  t1.fastAccessDx(i) = a_fad.dx(i)*b_fad.val() + a_fad.val()*b_fad.dx(i);
265  COMPARE_FADS(c_fad, t1);
266 
267  ScalarType val = urand.number();
268  c_fad = a_fad * val;
269  FadType t2(n, a_fad.val()*val);
270  for (int i=0; i<n; i++)
271  t2.fastAccessDx(i) = a_fad.dx(i)*val;
272  COMPARE_FADS(c_fad, t2);
273 
274  c_fad = val * b_fad;
275  FadType t3(n, val*b_fad.val());
276  for (int i=0; i<n; i++)
277  t3.fastAccessDx(i) = val*b_fad.dx(i);
278  COMPARE_FADS(c_fad, t3);
279 }
280 
281 template <class FadType, class ScalarType>
282 void
285  c_fad = a_fad / b_fad;
286  FadType t1(n, a_fad.val()/b_fad.val());
287  for (int i=0; i<n; i++)
288  t1.fastAccessDx(i) =
289  (a_fad.dx(i)*b_fad.val() - a_fad.val()*b_fad.dx(i)) /
290  (b_fad.val()*b_fad.val());
291  COMPARE_FADS(c_fad, t1);
292 
293  ScalarType val = urand.number();
294  c_fad = a_fad / val;
295  FadType t2(n, a_fad.val()/val);
296  for (int i=0; i<n; i++)
297  t2.fastAccessDx(i) = a_fad.dx(i)/val;
298  COMPARE_FADS(c_fad, t2);
299 
300  c_fad = val / b_fad;
301  FadType t3(n, val/b_fad.val());
302  for (int i=0; i<n; i++)
303  t3.fastAccessDx(i) = -val*b_fad.dx(i)/(b_fad.val()*b_fad.val());
304  COMPARE_FADS(c_fad, t3);
305 }
306 
307 template <class FadType, class ScalarType>
308 void
311  bool r1 = a_fad == b_fad;
312  bool r2 = a_fad.val() == b_fad.val();
313  CPPUNIT_ASSERT(r1 == r2);
314 
315  ScalarType val = urand.number();
316  r1 = a_fad == val;
317  r2 = a_fad.val() == val;
318  CPPUNIT_ASSERT(r1 == r2);
319 
320  r1 = val == b_fad;
321  r2 = val == b_fad.val();
322  CPPUNIT_ASSERT(r1 == r2);
323 }
324 
325 template <class FadType, class ScalarType>
326 void
329  bool r1 = a_fad != b_fad;
330  bool r2 = a_fad.val() != b_fad.val();
331  CPPUNIT_ASSERT(r1 == r2);
332 
333  ScalarType val = urand.number();
334  r1 = a_fad != val;
335  r2 = a_fad.val() != val;
336  CPPUNIT_ASSERT(r1 == r2);
337 
338  r1 = val != b_fad;
339  r2 = val != b_fad.val();
340  CPPUNIT_ASSERT(r1 == r2);
341 }
342 
343 template <class FadType, class ScalarType>
344 void
347  c_fad = +(a_fad);
348  FadType t1(n, a_fad.val());
349  for (int i=0; i<n; i++)
350  t1.fastAccessDx(i) = a_fad.dx(i);
351  COMPARE_FADS(c_fad, t1);
352 }
353 
354 template <class FadType, class ScalarType>
355 void
358  c_fad = -(a_fad);
359  FadType t1(n, -a_fad.val());
360  for (int i=0; i<n; i++)
361  t1.fastAccessDx(i) = -a_fad.dx(i);
362  COMPARE_FADS(c_fad, t1);
363 }
364 
365 template <class FadType, class ScalarType>
366 void
369  c_fad = std::exp(a_fad);
370  FadType t1(n, std::exp(a_fad.val()));
371  for (int i=0; i<n; i++)
372  t1.fastAccessDx(i) = std::exp(a_fad.val())*a_fad.dx(i);
373  COMPARE_FADS(c_fad, t1);
374 }
375 
376 template <class FadType, class ScalarType>
377 void
380  c_fad = std::log(a_fad);
381  FadType t1(n, std::log(a_fad.val()));
382  for (int i=0; i<n; i++)
383  t1.fastAccessDx(i) = a_fad.dx(i)/a_fad.val();
384  COMPARE_FADS(c_fad, t1);
385 }
386 
387 template <class FadType, class ScalarType>
388 void
391  c_fad = std::log10(a_fad);
392  FadType t1(n, std::log10(a_fad.val()));
393  for (int i=0; i<n; i++)
394  t1.fastAccessDx(i) = a_fad.dx(i)/(a_fad.val()*std::log(10));
395  COMPARE_FADS(c_fad, t1);
396 }
397 
398 template <class FadType, class ScalarType>
399 void
402  c_fad = std::sqrt(a_fad);
403  FadType t1(n, std::sqrt(a_fad.val()));
404  for (int i=0; i<n; i++)
405  t1.fastAccessDx(i) = a_fad.dx(i)/(2.*std::sqrt(a_fad.val()));
406  COMPARE_FADS(c_fad, t1);
407 }
408 
409 template <class FadType, class ScalarType>
410 void
413  c_fad = std::cos(a_fad);
414  FadType t1(n, std::cos(a_fad.val()));
415  for (int i=0; i<n; i++)
416  t1.fastAccessDx(i) = -std::sin(a_fad.val())*a_fad.dx(i);
417  COMPARE_FADS(c_fad, t1);
418 }
419 
420 template <class FadType, class ScalarType>
421 void
424  c_fad = std::sin(a_fad);
425  FadType t1(n, std::sin(a_fad.val()));
426  for (int i=0; i<n; i++)
427  t1.fastAccessDx(i) = std::cos(a_fad.val())*a_fad.dx(i);
428  COMPARE_FADS(c_fad, t1);
429 }
430 
431 template <class FadType, class ScalarType>
432 void
435  c_fad = std::tan(a_fad);
436  FadType t1(n, std::tan(a_fad.val()));
437  for (int i=0; i<n; i++)
438  t1.fastAccessDx(i) =
439  a_fad.dx(i)/(std::cos(a_fad.val())*std::cos(a_fad.val()));;
440  COMPARE_FADS(c_fad, t1);
441 }
442 
443 template <class FadType, class ScalarType>
444 void
447  c_fad = std::cosh(a_fad);
448  FadType t1(n, std::cosh(a_fad.val()));
449  for (int i=0; i<n; i++)
450  t1.fastAccessDx(i) = std::sinh(a_fad.val())*a_fad.dx(i);
451  COMPARE_FADS(c_fad, t1);
452 }
453 
454 template <class FadType, class ScalarType>
455 void
458  c_fad = std::sinh(a_fad);
459  FadType t1(n, std::sinh(a_fad.val()));
460  for (int i=0; i<n; i++)
461  t1.fastAccessDx(i) = std::cosh(a_fad.val())*a_fad.dx(i);
462  COMPARE_FADS(c_fad, t1);
463 }
464 
465 template <class FadType, class ScalarType>
466 void
469  c_fad = std::tanh(a_fad);
470  FadType t1(n, std::tanh(a_fad.val()));
471  for (int i=0; i<n; i++)
472  t1.fastAccessDx(i) =
473  a_fad.dx(i)/(std::cosh(a_fad.val())*std::cosh(a_fad.val()));
474  COMPARE_FADS(c_fad, t1);
475 }
476 
477 template <class FadType, class ScalarType>
478 void
481  FadType t1(n, c_fad.val()+a_fad.val());
482  for (int i=0; i<n; i++)
483  t1.fastAccessDx(i) = c_fad.dx(i) + a_fad.dx(i);
484  c_fad += a_fad;
485  COMPARE_FADS(c_fad, t1);
486 
487  ScalarType val = urand.number();
488  FadType t2(n, c_fad.val()+val);
489  for (int i=0; i<n; i++)
490  t2.fastAccessDx(i) = c_fad.dx(i);
491  c_fad += val;
492  COMPARE_FADS(c_fad, t2);
493 }
494 
495 template <class FadType, class ScalarType>
496 void
499  FadType t1(n, c_fad.val()-a_fad.val());
500  for (int i=0; i<n; i++)
501  t1.fastAccessDx(i) = c_fad.dx(i) - a_fad.dx(i);
502  c_fad -= a_fad;
503  COMPARE_FADS(c_fad, t1);
504 
505  ScalarType val = urand.number();
506  FadType t2(n, c_fad.val()-val);
507  for (int i=0; i<n; i++)
508  t2.fastAccessDx(i) = c_fad.dx(i);
509  c_fad -= val;
510  COMPARE_FADS(c_fad, t2);
511 }
512 
513 template <class FadType, class ScalarType>
514 void
517  FadType t1(n, c_fad.val()*a_fad.val());
518  for (int i=0; i<n; i++)
519  t1.fastAccessDx(i) = c_fad.dx(i)*a_fad.val() + a_fad.dx(i)*c_fad.val();
520  c_fad *= a_fad;
521  COMPARE_FADS(c_fad, t1);
522 
523  ScalarType val = urand.number();
524  FadType t2(n, c_fad.val()*val);
525  for (int i=0; i<n; i++)
526  t2.fastAccessDx(i) = c_fad.dx(i)*val;
527  c_fad *= val;
528  COMPARE_FADS(c_fad, t2);
529 }
530 
531 template <class FadType, class ScalarType>
532 void
535  FadType t1(n, c_fad.val()/a_fad.val());
536  for (int i=0; i<n; i++)
537  t1.fastAccessDx(i) =
538  (a_fad.dx(i)*c_fad.val() - c_fad.dx(i)*a_fad.val()) /
539  (a_fad.val()*a_fad.val());
540  c_fad /= a_fad;
541  COMPARE_FADS(c_fad, t1);
542 
543  ScalarType val = urand.number();
544  FadType t2(n, c_fad.val()/val);
545  for (int i=0; i<n; i++)
546  t2.fastAccessDx(i) = c_fad.dx(i)/val;
547  c_fad /= val;
548  COMPARE_FADS(c_fad, t2);
549 }
550 
551 template <class FadType, class ScalarType>
552 void
555  c_fad = std::pow(a_fad, b_fad);
556  FadType t1(n, std::pow(a_fad.val(),b_fad.val()));
557  for (int i=0; i<n; i++)
558  t1.fastAccessDx(i) =
559  std::pow(a_fad.val(),b_fad.val())*(b_fad.val()*a_fad.dx(i)/a_fad.val() +
560  std::log(a_fad.val())*b_fad.dx(i));
561  COMPARE_FADS(c_fad, t1);
562 
563  ScalarType val = urand.number();
564  c_fad = std::pow(a_fad, val);
565  FadType t2(n, std::pow(a_fad.val(), val));
566  for (int i=0; i<n; i++)
567  t2.fastAccessDx(i) =
568  std::pow(a_fad.val(), val)*(val*a_fad.dx(i)/a_fad.val());
569  COMPARE_FADS(c_fad, t2);
570 
571  c_fad = std::pow(val, b_fad);
572  FadType t3(n, std::pow(val, b_fad.val()));
573  for (int i=0; i<n; i++)
574  t3.fastAccessDx(i) =
575  std::pow(val, b_fad.val())*std::log(val)*b_fad.dx(i);
576  COMPARE_FADS(c_fad, t3);
577 
578  val = 0.0;
579  c_fad = std::pow(a_fad, val);
580  FadType t4(n, std::pow(a_fad.val(), val));
581  for (int i=0; i<n; i++)
582  t4.fastAccessDx(i) = 0.0;
583  COMPARE_FADS(c_fad, t4);
584 
585  c_fad = std::pow(val, b_fad);
586  FadType t5(n, std::pow(val, b_fad.val()));
587  for (int i=0; i<n; i++)
588  t5.fastAccessDx(i) = 0.0;
589  COMPARE_FADS(c_fad, t5);
590 
591  FadType aa_fad = a_fad;
592  aa_fad.val() = 0.0;
593  c_fad = std::pow(aa_fad, b_fad);
594  FadType t6(n, std::pow(aa_fad.val(),b_fad.val()));
595  for (int i=0; i<n; i++)
596  t6.fastAccessDx(i) = 0.0;
597  COMPARE_FADS(c_fad, t6);
598 
599  FadType bb_fad = b_fad;
600  bb_fad.val() = 0.0;
601  c_fad = std::pow(a_fad, bb_fad);
602  FadType t7(n, std::pow(a_fad.val(),bb_fad.val()));
603  for (int i=0; i<n; i++)
604  t7.fastAccessDx(i) =
605  std::pow(a_fad.val(),bb_fad.val())*(bb_fad.val()*a_fad.dx(i)/a_fad.val()
606  + std::log(a_fad.val())*b_fad.dx(i));
607  COMPARE_FADS(c_fad, t7);
608 }
609 
610 template <class FadType, class ScalarType>
611 void
614  FadType aa_fad = a_fad;
615  aa_fad = 1.0;
616  aa_fad = aa_fad + b_fad;
617  c_fad = 1.0 + b_fad;
618  COMPARE_FADS(aa_fad, c_fad);
619 }
620 
621 template <class FadType, class ScalarType>
622 void
625  FadType aa_fad = a_fad;
626  aa_fad = 1.0;
627  aa_fad += aa_fad + b_fad;
628  c_fad = 1.0 + 1.0 + b_fad;
629  COMPARE_FADS(aa_fad, c_fad);
630 }
631 
632 template <class FadType, class ScalarType>
633 void
636  FadType aa_fad = a_fad;
637  aa_fad = 1.0;
638  aa_fad -= aa_fad + b_fad;
639  c_fad = 1.0 - 1.0 - b_fad;
640  COMPARE_FADS(aa_fad, c_fad);
641 }
642 
643 template <class FadType, class ScalarType>
644 void
647  FadType aa_fad = a_fad;
648  aa_fad = 2.0;
649  aa_fad *= aa_fad + b_fad;
650  c_fad = 2.0 * (2.0 + b_fad);
651  COMPARE_FADS(aa_fad, c_fad);
652 }
653 
654 template <class FadType, class ScalarType>
655 void
658  FadType aa_fad = a_fad;
659  aa_fad = 2.0;
660  aa_fad /= aa_fad + b_fad;
661  c_fad = 2.0 / (2.0 + b_fad);
662  COMPARE_FADS(aa_fad, c_fad);
663 }
664 
665 template <class FadType, class ScalarType>
666 void
669  FadType d_fad = ScalarType(1.0);
670  d_fad = d_fad + a_fad;
671  c_fad = 1.0 + a_fad;
672  COMPARE_FADS(d_fad, c_fad);
673 }
674 
675 template <class FadType, class ScalarType>
676 void
679  FadType aa_fad = a_fad;
680  FadType bb_fad = b_fad;
681  aa_fad.val() = 9.0;
682  bb_fad.val() = 3.0;
683  FadType d_fad;
684  if (aa_fad == bb_fad*bb_fad)
685  d_fad = aa_fad;
686  c_fad = aa_fad;
687  COMPARE_FADS(d_fad, c_fad);
688 }
689 
690 template <class FadType, class ScalarType>
691 void
694  FadType bb_fad = b_fad;
695  bb_fad.val() = 3.0;
696  FadType d_fad;
697  if (ScalarType(9.0) == bb_fad*bb_fad)
698  d_fad = a_fad;
699  c_fad = a_fad;
700  COMPARE_FADS(d_fad, c_fad);
701 }
702 
703 template <class FadType, class ScalarType>
704 void
707  FadType bb_fad = b_fad;
708  bb_fad.val() = 3.0;
709  FadType d_fad;
710  if (bb_fad*bb_fad == ScalarType(9.0))
711  d_fad = a_fad;
712  c_fad = a_fad;
713  COMPARE_FADS(d_fad, c_fad);
714 }
715 
716 // A class for testing each real Fad operation
717 // This class tests additional functions that aren't define for complex
718 // types
719 template <class FadType, class ScalarType>
720 class RealFadOpsUnitTest2 : public FadOpsUnitTest2<FadType,ScalarType> {
721 
723 
728 
735 
740 
743 
762 #ifdef HAVE_SACADO_CXX11
763  CPPUNIT_TEST(testCbrt);
764 #endif
765 
770 
776 
778 
782 
784 
785 public:
786 
788 
789  RealFadOpsUnitTest2(int numComponents, double absolute_tolerance,
790  double relative_tolerance) :
791  FadOpsUnitTest2<FadType,ScalarType>(numComponents, absolute_tolerance, relative_tolerance) {}
792 
793  void testLessThanOrEquals();
795  void testLessThan();
796  void testGreaterThan();
797 
798  void testACos();
799  void testASin();
800  void testATan();
801  void testACosh();
802  void testASinh();
803  void testATanh();
804  void testAbs();
805  void testFAbs();
806 #ifdef HAVE_SACADO_CXX11
807  void testCbrt();
808 #endif
809 
810  void testATan2();
811  void testMax();
812  void testMin();
813 };
814 
815 template <class FadType, class ScalarType>
816 void
819  bool r1 = this->a_fad <= this->b_fad;
820  bool r2 = this->a_fad.val() <= this->b_fad.val();
821  CPPUNIT_ASSERT(r1 == r2);
822 
823  ScalarType val = this->urand.number();
824  r1 = this->a_fad <= val;
825  r2 = this->a_fad.val() <= val;
826  CPPUNIT_ASSERT(r1 == r2);
827 
828  r1 = val <= this->b_fad;
829  r2 = val <= this->b_fad.val();
830  CPPUNIT_ASSERT(r1 == r2);
831 }
832 
833 template <class FadType, class ScalarType>
834 void
837  bool r1 = this->a_fad >= this->b_fad;
838  bool r2 = this->a_fad.val() >= this->b_fad.val();
839  CPPUNIT_ASSERT(r1 == r2);
840 
841  ScalarType val = this->urand.number();
842  r1 = this->a_fad >= val;
843  r2 = this->a_fad.val() >= val;
844  CPPUNIT_ASSERT(r1 == r2);
845 
846  r1 = val >= this->b_fad;
847  r2 = val >= this->b_fad.val();
848  CPPUNIT_ASSERT(r1 == r2);
849 }
850 
851 template <class FadType, class ScalarType>
852 void
855  bool r1 = this->a_fad < this->b_fad;
856  bool r2 = this->a_fad.val() < this->b_fad.val();
857  CPPUNIT_ASSERT(r1 == r2);
858 
859  ScalarType val = this->urand.number();
860  r1 = this->a_fad < val;
861  r2 = this->a_fad.val() < val;
862  CPPUNIT_ASSERT(r1 == r2);
863 
864  r1 = val < this->b_fad;
865  r2 = val < this->b_fad.val();
866  CPPUNIT_ASSERT(r1 == r2);
867 }
868 
869 template <class FadType, class ScalarType>
870 void
873  bool r1 = this->a_fad > this->b_fad;
874  bool r2 = this->a_fad.val() > this->b_fad.val();
875  CPPUNIT_ASSERT(r1 == r2);
876 
877  ScalarType val = this->urand.number();
878  r1 = this->a_fad > val;
879  r2 = this->a_fad.val() > val;
880  CPPUNIT_ASSERT(r1 == r2);
881 
882  r1 = val > this->b_fad;
883  r2 = val > this->b_fad.val();
884  CPPUNIT_ASSERT(r1 == r2);
885 }
886 
887 template <class FadType, class ScalarType>
888 void
891  this->c_fad = std::acos(this->a_fad);
892  FadType t1(this->n, std::acos(this->a_fad.val()));
893  for (int i=0; i<this->n; i++)
894  t1.fastAccessDx(i) = -this->a_fad.dx(i)/std::sqrt(1.0 - this->a_fad.val()*this->a_fad.val());
895  COMPARE_FADS(this->c_fad, t1);
896 }
897 
898 template <class FadType, class ScalarType>
899 void
902  this->c_fad = std::asin(this->a_fad);
903  FadType t1(this->n, std::asin(this->a_fad.val()));
904  for (int i=0; i<this->n; i++)
905  t1.fastAccessDx(i) = this->a_fad.dx(i)/std::sqrt(1.0 - this->a_fad.val()*this->a_fad.val());
906  COMPARE_FADS(this->c_fad, t1);
907 }
908 
909 template <class FadType, class ScalarType>
910 void
913  this->c_fad = std::atan(this->a_fad);
914  FadType t1(this->n, std::atan(this->a_fad.val()));
915  for (int i=0; i<this->n; i++)
916  t1.fastAccessDx(i) = this->a_fad.dx(i)/(1.0 + this->a_fad.val()*this->a_fad.val());
917  COMPARE_FADS(this->c_fad, t1);
918 }
919 
920 template <class FadType, class ScalarType>
921 void
924  FadType aa_fad = this->a_fad;
925  if (this->a_fad.val() < 1.0)
926  aa_fad.val() = 1.0 / this->a_fad.val();
927  this->c_fad = std::acosh(aa_fad);
928  FadType t1(this->n, std::acosh(aa_fad.val()));
929  for (int i=0; i<this->n; i++)
930  t1.fastAccessDx(i) = aa_fad.dx(i)/std::sqrt(aa_fad.val()*aa_fad.val()-1.0);
931  COMPARE_FADS(this->c_fad, t1);
932 }
933 
934 template <class FadType, class ScalarType>
935 void
938  this->c_fad = std::asinh(this->a_fad);
939  FadType t1(this->n, std::asinh(this->a_fad.val()));
940  for (int i=0; i<this->n; i++)
941  t1.fastAccessDx(i) = this->a_fad.dx(i)/std::sqrt(this->a_fad.val()*this->a_fad.val()+1.0);
942  COMPARE_FADS(this->c_fad, t1);
943 }
944 
945 template <class FadType, class ScalarType>
946 void
949  this->c_fad = std::atanh(this->a_fad);
950  FadType t1(this->n, std::atanh(this->a_fad.val()));
951  for (int i=0; i<this->n; i++)
952  t1.fastAccessDx(i) = this->a_fad.dx(i)/(1.0 - this->a_fad.val()*this->a_fad.val());
953  COMPARE_FADS(this->c_fad, t1);
954 }
955 
956 template <class FadType, class ScalarType>
957 void
960  this->c_fad = std::abs(this->a_fad);
961  FadType t1(this->n, std::abs(this->a_fad.val()));
962  for (int i=0; i<this->n; i++) {
963  if (this->a_fad.val() >= 0)
964  t1.fastAccessDx(i) = this->a_fad.dx(i);
965  else
966  t1.fastAccessDx(i) = -this->a_fad.dx(i);
967  }
968  COMPARE_FADS(this->c_fad, t1);
969 }
970 
971 template <class FadType, class ScalarType>
972 void
975  this->c_fad = std::fabs(this->a_fad);
976  FadType t1(this->n, std::fabs(this->a_fad.val()));
977  for (int i=0; i<this->n; i++) {
978  if (this->a_fad.val() >= 0)
979  t1.fastAccessDx(i) = this->a_fad.dx(i);
980  else
981  t1.fastAccessDx(i) = -this->a_fad.dx(i);
982  }
983  COMPARE_FADS(this->c_fad, t1);
984 }
985 
986 #ifdef HAVE_SACADO_CXX11
987 template <class FadType, class ScalarType>
988 void
990 testCbrt() {
991  this->c_fad = std::cbrt(this->a_fad);
992  FadType t1(this->n, std::cbrt(this->a_fad.val()));
993  for (int i=0; i<this->n; i++)
994  t1.fastAccessDx(i) =
995  this->a_fad.dx(i)/(3.*std::cbrt(this->a_fad.val()*this->a_fad.val()));
996  COMPARE_FADS(this->c_fad, t1);
997 }
998 #endif
999 
1000 template <class FadType, class ScalarType>
1001 void
1004  this->c_fad = std::atan2(this->a_fad, this->b_fad);
1005  FadType t1(this->n, std::atan2(this->a_fad.val(),this->b_fad.val()));
1006  ScalarType t = this->a_fad.val()*this->a_fad.val() +
1007  this->b_fad.val()*this->b_fad.val();
1008  for (int i=0; i<this->n; i++)
1009  t1.fastAccessDx(i) = (this->b_fad.val()*this->a_fad.dx(i) -
1010  this->a_fad.val()*this->b_fad.dx(i))/t;
1011  COMPARE_FADS(this->c_fad, t1);
1012 
1013  ScalarType val = this->urand.number();
1014  this->c_fad = std::atan2(this->a_fad, val);
1015  FadType t2(this->n, std::atan2(this->a_fad.val(), val));
1016  t = this->a_fad.val()*this->a_fad.val() + val*val;
1017  for (int i=0; i<this->n; i++)
1018  t2.fastAccessDx(i) = val*this->a_fad.dx(i)/t;
1019  COMPARE_FADS(this->c_fad, t2);
1020 
1021  this->c_fad = std::atan2(val, this->b_fad);
1022  FadType t3(this->n, std::atan2(val, this->b_fad.val()));
1023  t = val*val + this->b_fad.val()*this->b_fad.val();
1024  for (int i=0; i<this->n; i++)
1025  t3.fastAccessDx(i) = -val*this->b_fad.dx(i)/t;
1026  COMPARE_FADS(this->c_fad, t3);
1027 }
1028 
1029 template <class FadType, class ScalarType>
1030 void
1033  ScalarType val;
1034 
1035  // Fad, Fad
1036  FadType aa_fad = this->a_fad + 1.0;
1037  this->c_fad = max(aa_fad, this->a_fad);
1038  COMPARE_FADS(this->c_fad, aa_fad);
1039  this->c_fad = max(this->a_fad, aa_fad);
1040  COMPARE_FADS(this->c_fad, aa_fad);
1041 
1042  // Expr, Fad
1043  this->c_fad = max(this->a_fad+1.0, this->a_fad);
1044  COMPARE_FADS(this->c_fad, aa_fad);
1045  this->c_fad = max(this->a_fad, this->a_fad+1.0);
1046  COMPARE_FADS(this->c_fad, aa_fad);
1047 
1048  // Expr, Expr (same)
1049  this->c_fad = max(this->a_fad+1.0, this->a_fad+1.0);
1050  COMPARE_FADS(this->c_fad, aa_fad);
1051 
1052  // Expr, Expr (different)
1053  this->c_fad = max(this->a_fad+1.0, this->a_fad-1.0);
1054  COMPARE_FADS(this->c_fad, aa_fad);
1055  this->c_fad = max(this->a_fad-1.0, this->a_fad+1.0);
1056  COMPARE_FADS(this->c_fad, aa_fad);
1057 
1058  // Fad, const
1059  val = this->a_fad.val() + 1;
1060  this->c_fad = max(this->a_fad, val);
1061  COMPARE_VALUES(this->c_fad.val(), val);
1062  for (int i=0; i<this->n; i++)
1063  COMPARE_VALUES(this->c_fad.dx(i), 0.0);
1064  val = this->a_fad.val() - 1;
1065  this->c_fad = max(this->a_fad, val);
1066  COMPARE_FADS(this->c_fad, this->a_fad);
1067  val = this->b_fad.val() + 1;
1068  this->c_fad = max(val, this->b_fad);
1069  COMPARE_VALUES(this->c_fad.val(), val);
1070  for (int i=0; i<this->n; i++)
1071  COMPARE_VALUES(this->c_fad.dx(i), 0.0);
1072  val = this->b_fad.val() - 1;
1073  this->c_fad = max(val, this->b_fad);
1074  COMPARE_FADS(this->c_fad, this->b_fad);
1075 
1076  // Expr, const
1077  val = this->a_fad.val();
1078  this->c_fad = max(this->a_fad+1.0, val);
1079  COMPARE_FADS(this->c_fad, aa_fad);
1080  this->c_fad = max(val, this->a_fad+1.0);
1081  COMPARE_FADS(this->c_fad, aa_fad);
1082 }
1083 
1084 template <class FadType, class ScalarType>
1085 void
1088  ScalarType val;
1089 
1090  // Fad, Fad
1091  FadType aa_fad = this->a_fad - 1.0;
1092  this->c_fad = min(aa_fad, this->a_fad);
1093  COMPARE_FADS(this->c_fad, aa_fad);
1094  this->c_fad = min(this->a_fad, aa_fad);
1095  COMPARE_FADS(this->c_fad, aa_fad);
1096 
1097  // Expr, Fad
1098  this->c_fad = min(this->a_fad-1.0, this->a_fad);
1099  COMPARE_FADS(this->c_fad, aa_fad);
1100  this->c_fad = min(this->a_fad, this->a_fad-1.0);
1101  COMPARE_FADS(this->c_fad, aa_fad);
1102 
1103  // Expr, Expr (same)
1104  this->c_fad = min(this->a_fad-1.0, this->a_fad-1.0);
1105  COMPARE_FADS(this->c_fad, aa_fad);
1106 
1107  // Expr, Expr (different)
1108  this->c_fad = min(this->a_fad+1.0, this->a_fad-1.0);
1109  COMPARE_FADS(this->c_fad, aa_fad);
1110  this->c_fad = min(this->a_fad-1.0, this->a_fad+1.0);
1111  COMPARE_FADS(this->c_fad, aa_fad);
1112 
1113  // Fad, const
1114  val = this->a_fad.val() - 1;
1115  this->c_fad = min(this->a_fad, val);
1116  COMPARE_VALUES(this->c_fad.val(), val);
1117  for (int i=0; i<this->n; i++)
1118  COMPARE_VALUES(this->c_fad.dx(i), 0.0);
1119  val = this->a_fad.val() + 1;
1120  this->c_fad = min(this->a_fad, val);
1121  COMPARE_FADS(this->c_fad, this->a_fad);
1122  val = this->b_fad.val() - 1;
1123  this->c_fad = min(val, this->b_fad);
1124  COMPARE_VALUES(this->c_fad.val(), val);
1125  for (int i=0; i<this->n; i++)
1126  COMPARE_VALUES(this->c_fad.dx(i), 0.0);
1127  val = this->b_fad.val() + 1;
1128  this->c_fad = min(val, this->b_fad);
1129  COMPARE_FADS(this->c_fad, this->b_fad);
1130 
1131  // Expr, const
1132  val = this->a_fad.val();
1133  this->c_fad = min(this->a_fad-1.0, val);
1134  COMPARE_FADS(this->c_fad, aa_fad);
1135  this->c_fad = min(val, this->a_fad-1.0);
1136  COMPARE_FADS(this->c_fad, aa_fad);
1137 }
1138 
1139 #undef COMPARE_VALUES
1140 #undef COMPARE_FADS
1141 
1142 #endif // FADUNITTESTS2_HPP
cbrt(expr.val())
asin(expr.val())
#define COMPARE_FADS(a, b)
cosh(expr.val())
abs(expr.val())
CPPUNIT_TEST(testAddition)
Sacado::Fad::DFad< double > FadType
atan(expr.val())
KOKKOS_INLINE_FUNCTION mpl::enable_if_c< ExprLevel< Expr< T1 > >::value==ExprLevel< Expr< T2 > >::value, Expr< PowerOp< Expr< T1 >, Expr< T2 > > > >::type pow(const Expr< T1 > &expr1, const Expr< T2 > &expr2)
expr val()
CPPUNIT_TEST_SUITE(RealFadOpsUnitTest2)
tanh(expr.val())
SimpleFad< ValueT > min(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
CPPUNIT_TEST_SUITE(FadOpsUnitTest2)
sqrt(expr.val())
sinh(expr.val())
tan(expr.val())
Sacado::Random< ScalarType > urand
atan2(expr1.val(), expr2.val())
sin(expr.val())
log(expr.val())
CPPUNIT_TEST(testAddition)
acos(expr.val())
SimpleFad< ValueT > max(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
exp(expr.val())
fabs(expr.val())
RealFadOpsUnitTest2(int numComponents, double absolute_tolerance, double relative_tolerance)
int n
log10(expr.val())
cos(expr.val())
#define COMPARE_VALUES(a, b)