Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NestedFadUnitTests.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef NESTED_FADUNITTESTS_HPP
11 #define NESTED_FADUNITTESTS_HPP
12 
13 // Sacado includes
14 #include "Sacado_No_Kokkos.hpp"
15 #include "Sacado_Random.hpp"
16 
17 // Fad includes
18 #include "Fad/fad.h"
19 
20 // gtest includes
21 #include <gtest/gtest.h>
22 
23 #include "GTestUtils.hpp"
24 
25 // A class for testing each Fad operation
26 template <class FadFadType>
28 protected:
31 
32  // DFad variables
33  FadFadType a_dfad_, b_dfad_, c_dfad_;
34 
35  // Fad variables
36  FAD::Fad<FadType> a_fad_, b_fad_, c_fad_;
37 
38  // Random number generator
40 
41  // Number of derivative components
42  int n1, n2;
43 
44  // Tolerances to which fad objects should be the same
46 
47  // Set the random number generator with a specific seed to prevent NaN's
48  // in the second derivatives, likely due to overflow
50  urand(0.0, 1.0, 123456), n1(5), n2(3), tol_a(1.0e-15), tol_r(1.0e-14) {}
51 
52  void SetUp() override {
54 
55  val = urand.number();
56  a_dfad_ = FadFadType(n1,FadType(n2,val));
57  a_fad_ = FAD::Fad<FadType>(n1,FadType(n2,val));
58 
59  val = urand.number();
60  b_dfad_ = FadFadType(n1,FadType(n2,val));
61  b_fad_ = FAD::Fad<FadType>(n1,FadType(n2,val));
62 
63  for (int j=0; j<n2; j++) {
64  ScalarType val2;
65  val2 = urand.number();
66  a_dfad_.val().fastAccessDx(j) = val2;
67  a_fad_.val().fastAccessDx(j) = val2;
68 
69  val2 = urand.number();
70  b_dfad_.val().fastAccessDx(j) = val2;
71  b_fad_.val().fastAccessDx(j) = val2;
72  }
73 
74  for (int i=0; i<n1; i++) {
75  val = urand.number();
76  a_dfad_.fastAccessDx(i) = FadType(n2,val);
77  a_fad_.fastAccessDx(i) = FadType(n2,val);
78 
79  val = urand.number();
80  b_dfad_.fastAccessDx(i) = FadType(n2,val);
81  b_fad_.fastAccessDx(i) = FadType(n2,val);
82 
83  for (int j=0; j<n2; j++) {
84  ScalarType val2;
85  val2 = urand.number();
86  a_dfad_.fastAccessDx(i).fastAccessDx(j) = val2;
87  a_fad_.fastAccessDx(i).fastAccessDx(j) = val2;
88 
89  val2 = urand.number();
90  b_dfad_.fastAccessDx(i).fastAccessDx(j) = val2;
91  b_fad_.fastAccessDx(i).fastAccessDx(j) = val2;
92  }
93  }
94 
95  val = 0.0;
96  c_dfad_ = FadFadType(n1, FadType(n2,val));
97  c_fad_ = FAD::Fad<FadType>(n1,FadType(n2,val));
98  }
99 
100  void TearDown() override {}
101 
102  template <typename ScalarT>
103  ScalarT composite1(const ScalarT& a, const ScalarT& b) {
104  ScalarT t1 = 3. * a + sin(b) / log(fabs(a - b * 7.));
105  ScalarT t2 = 1.0e3;
106  ScalarT t3 = 5.7e4;
107  ScalarT t4 = 3.2e5;
108  t1 *= cos(a + exp(t1)) / 6. - tan(t1*sqrt(abs(a * log10(abs(b)))));
109  t1 -= acos((6.+asin(pow(fabs(a),b)/t2))/t3) * asin(pow(fabs(b),2.)*1.0/t4) * atan((b*pow(2.,log(abs(a))))/(t3*t4));
110  t1 /= cosh(b - 0.7) + 7.*sinh(t1 + 0.8)*tanh(9./a) - 9.;
111  t1 += pow(abs(a*4.),b-8.)/cos(a*b*a);
112 
113  return t1;
114  }
115 
116  template <typename ScalarT>
117  ScalarT composite1_fad(const ScalarT& a, const ScalarT& b) {
118  ScalarT t1 = FadType(3.) * a + sin(b) / log(fabs(a - b * FadType(7.)));
119  ScalarT t2 = FadType(1.0e3);
120  ScalarT t3 = FadType(7e4);
121  ScalarT t4 = FadType(3.2e5);
122  t1 *= cos(a + exp(t1)) / FadType(6.) - tan(t1*sqrt(abs(a * log10(abs(b)))));
123  t1 -= acos((FadType(6.)+asin(pow(fabs(a),b)/t2))/t3) * asin(pow(fabs(b),FadType(2.))*FadType(1.0)/t4) * atan((b*pow(FadType(2.),log(abs(a))))/(t3*t4));
124  t1 /= cosh(b - FadType(0.7)) + FadType(7.)*sinh(t1 + FadType(0.8))*tanh(FadType(9.)/a) - FadType(9.);
125  t1 += pow(abs(a*FadType(4.)),b-FadType(8.))/cos(a*b*a);
126 
127  return t1;
128  }
129 
130 }; // class FadFadOpsUnitTest
131 
133 
134 #define BINARY_OP_TEST(FIXTURENAME,TESTNAME,OP) \
135  TYPED_TEST_P(FIXTURENAME, TESTNAME) { \
136  typedef decltype(this->a_dfad_) FadFadType; \
137  typedef typename Sacado::ValueType<FadFadType>::type FadType; \
138  auto a_dfad = this->a_dfad_; \
139  auto b_dfad = this->b_dfad_; \
140  auto c_dfad = this->c_dfad_; \
141  auto a_fad = this->a_fad_; \
142  auto b_fad = this->b_fad_; \
143  auto c_fad = this->c_fad_; \
144  c_dfad = a_dfad OP b_dfad; \
145  c_fad = a_fad OP b_fad; \
146  COMPARE_NESTED_FADS(c_dfad, c_fad); \
147  \
148  double val = this->urand.number(); \
149  c_dfad = a_dfad OP val; \
150  c_fad = a_fad OP FadType(val); \
151  COMPARE_NESTED_FADS(c_dfad, c_fad); \
152  \
153  c_dfad = val OP b_dfad; \
154  c_fad = FadType(val) OP b_fad; \
155  COMPARE_NESTED_FADS(c_dfad, c_fad); \
156  }
157 
158 #define RELOP_TEST(FIXTURENAME,TESTNAME,OP) \
159  TYPED_TEST_P(FIXTURENAME, TESTNAME) { \
160  typedef decltype(this->a_dfad_) FadFadType; \
161  typedef typename Sacado::ValueType<FadFadType>::type FadType; \
162  auto a_dfad = this->a_dfad_; \
163  auto b_dfad = this->b_dfad_; \
164  auto a_fad = this->a_fad_; \
165  auto b_fad = this->b_fad_; \
166  bool r1 = a_dfad OP b_dfad; \
167  bool r2 = a_fad OP b_fad; \
168  ASSERT_TRUE(r1 == r2); \
169  \
170  double val = this->urand.number(); \
171  r1 = a_dfad OP val; \
172  r2 = a_fad OP FadType(val); \
173  ASSERT_TRUE(r1 == r2); \
174  \
175  r1 = val OP b_dfad; \
176  r2 = FadType(val) OP b_fad; \
177  ASSERT_TRUE(r1 == r2); \
178  }
179 
180 #define BINARY_FUNC_TEST(FIXTURENAME,TESTNAME,FUNC) \
181  TYPED_TEST_P(FIXTURENAME, TESTNAME) { \
182  typedef decltype(this->a_dfad_) FadFadType; \
183  typedef typename Sacado::ValueType<FadFadType>::type FadType; \
184  auto a_dfad = this->a_dfad_; \
185  auto b_dfad = this->b_dfad_; \
186  auto c_dfad = this->c_dfad_; \
187  auto a_fad = this->a_fad_; \
188  auto b_fad = this->b_fad_; \
189  auto c_fad = this->c_fad_; \
190  c_dfad = FUNC (a_dfad,b_dfad); \
191  c_fad = FUNC (a_fad,b_fad); \
192  COMPARE_NESTED_FADS(c_dfad, c_fad); \
193  \
194  double val = this->urand.number(); \
195  c_dfad = FUNC (a_dfad,val); \
196  c_fad = FUNC (a_fad,FadType(val)); \
197  COMPARE_NESTED_FADS(c_dfad, c_fad); \
198  \
199  c_dfad = FUNC (val,b_dfad); \
200  c_fad = FUNC (FadType(val),b_fad); \
201  COMPARE_NESTED_FADS(c_dfad, c_fad); \
202  }
203 
204 #define UNARY_OP_TEST(FIXTURENAME,TESTNAME,OP) \
205  TYPED_TEST_P(FIXTURENAME, TESTNAME) { \
206  auto a_dfad = this->a_dfad_; \
207  auto c_dfad = this->c_dfad_; \
208  auto a_fad = this->a_fad_; \
209  auto c_fad = this->c_fad_; \
210  c_dfad = OP a_dfad; \
211  c_fad = OP a_fad; \
212  COMPARE_NESTED_FADS(c_dfad, c_fad); \
213  }
214 
215 #define UNARY_FUNC_TEST(FIXTURENAME,TESTNAME,FUNC) \
216  TYPED_TEST_P(FIXTURENAME, TESTNAME) { \
217  auto a_dfad = this->a_dfad_; \
218  auto c_dfad = this->c_dfad_; \
219  auto a_fad = this->a_fad_; \
220  auto c_fad = this->c_fad_; \
221  c_dfad = FUNC (a_dfad); \
222  c_fad = FUNC (a_fad); \
223  COMPARE_NESTED_FADS(c_dfad, c_fad); \
224  }
225 
226 #define UNARY_ASSIGNOP_TEST(FIXTURENAME,TESTNAME,OP) \
227  TYPED_TEST_P(FIXTURENAME, TESTNAME){ \
228  auto a_dfad = this->a_dfad_; \
229  auto b_dfad = this->b_dfad_; \
230  auto c_dfad = this->c_dfad_; \
231  auto a_fad = this->a_fad_; \
232  auto b_fad = this->b_fad_; \
233  auto c_fad = this->c_fad_; \
234  c_dfad = b_dfad; \
235  c_fad = b_fad; \
236  c_dfad OP a_dfad; \
237  c_fad OP a_fad; \
238  COMPARE_NESTED_FADS(c_dfad, c_fad); \
239  \
240  double val = this->urand.number(); \
241  c_dfad OP val; \
242  c_fad OP val; \
243  COMPARE_NESTED_FADS(c_dfad, c_fad); \
244  }
245 
246 BINARY_OP_TEST(FadFadOpsUnitTest, testAddition, +)
247 BINARY_OP_TEST(FadFadOpsUnitTest, testSubtraction, -)
248 BINARY_OP_TEST(FadFadOpsUnitTest, testMultiplication, *)
249 BINARY_OP_TEST(FadFadOpsUnitTest, testDivision, /)
250 
251 RELOP_TEST(FadFadOpsUnitTest, testEquals, ==)
252 RELOP_TEST(FadFadOpsUnitTest, testNotEquals, !=)
253 RELOP_TEST(FadFadOpsUnitTest, testLessThanOrEquals, <=)
254 RELOP_TEST(FadFadOpsUnitTest, testGreaterThanOrEquals, >=)
255 RELOP_TEST(FadFadOpsUnitTest, testLessThan, <)
256 RELOP_TEST(FadFadOpsUnitTest, testGreaterThan, >)
257 
258 BINARY_FUNC_TEST(FadFadOpsUnitTest, testPow, pow)
259 
260 UNARY_OP_TEST(FadFadOpsUnitTest, testUnaryPlus, +)
261 UNARY_OP_TEST(FadFadOpsUnitTest, testUnaryMinus, -)
262 
263 UNARY_FUNC_TEST(FadFadOpsUnitTest, testExp, exp)
264 UNARY_FUNC_TEST(FadFadOpsUnitTest, testLog, log)
265 UNARY_FUNC_TEST(FadFadOpsUnitTest, testLog10, log10)
266 UNARY_FUNC_TEST(FadFadOpsUnitTest, testSqrt, sqrt)
267 UNARY_FUNC_TEST(FadFadOpsUnitTest, testCos, cos)
268 UNARY_FUNC_TEST(FadFadOpsUnitTest, testSin, sin)
269 UNARY_FUNC_TEST(FadFadOpsUnitTest, testTan, tan)
270 UNARY_FUNC_TEST(FadFadOpsUnitTest, testACos, acos)
271 UNARY_FUNC_TEST(FadFadOpsUnitTest, testASin, asin)
272 UNARY_FUNC_TEST(FadFadOpsUnitTest, testATan, atan)
273 UNARY_FUNC_TEST(FadFadOpsUnitTest, testCosh, cosh)
274 UNARY_FUNC_TEST(FadFadOpsUnitTest, testSinh, sinh)
275 UNARY_FUNC_TEST(FadFadOpsUnitTest, testTanh, tanh)
276 UNARY_FUNC_TEST(FadFadOpsUnitTest, testAbs, abs)
277 UNARY_FUNC_TEST(FadFadOpsUnitTest, testFAbs, fabs)
278 
279 UNARY_ASSIGNOP_TEST(FadFadOpsUnitTest, testPlusEquals, +=)
280 UNARY_ASSIGNOP_TEST(FadFadOpsUnitTest, testMinusEquals, -=)
281 UNARY_ASSIGNOP_TEST(FadFadOpsUnitTest, testTimesEquals, *=)
282 UNARY_ASSIGNOP_TEST(FadFadOpsUnitTest, testDivideEquals, /=)
283 
284 TYPED_TEST_P(FadFadOpsUnitTest, testMax) {
285  typedef decltype(this->a_dfad_) FadFadType;
287 
288  FadType val;
289 
290  auto a_dfad = this->a_dfad_;
291  auto b_dfad = this->b_dfad_;
292  auto c_dfad = this->c_dfad_;
293  auto a_fad = this->a_fad_;
294  auto b_fad = this->b_fad_;
295  auto c_fad = this->c_fad_;
296 
297  FadFadType aa_dfad = a_dfad + 1.0;
298  c_dfad = max(aa_dfad, a_dfad);
299  COMPARE_FADS(c_dfad.val(), aa_dfad.val());
300  for (int i=0; i<this->n1; i++) {
301  COMPARE_FADS(c_dfad.dx(i), aa_dfad.dx(i));
302  COMPARE_FADS(c_dfad.fastAccessDx(i), aa_dfad.fastAccessDx(i));
303  }
304 
305  c_dfad = max(a_dfad, aa_dfad);
306  COMPARE_FADS(c_dfad.val(), aa_dfad.val());
307  for (int i=0; i<this->n1; i++) {
308  COMPARE_FADS(c_dfad.dx(i), aa_dfad.dx(i));
309  COMPARE_FADS(c_dfad.fastAccessDx(i), aa_dfad.fastAccessDx(i));
310  }
311 
312  c_dfad = max(a_dfad+1.0, a_dfad);
313  COMPARE_FADS(c_dfad.val(), aa_dfad.val());
314  for (int i=0; i<this->n1; i++) {
315  COMPARE_FADS(c_dfad.dx(i), aa_dfad.dx(i));
316  COMPARE_FADS(c_dfad.fastAccessDx(i), aa_dfad.fastAccessDx(i));
317  }
318 
319  c_dfad = max(a_dfad, a_dfad+1.0);
320  COMPARE_FADS(c_dfad.val(), aa_dfad.val());
321  for (int i=0; i<this->n1; i++) {
322  COMPARE_FADS(c_dfad.dx(i), aa_dfad.dx(i));
323  COMPARE_FADS(c_dfad.fastAccessDx(i), aa_dfad.fastAccessDx(i));
324  }
325 
326  val = a_dfad.val() + 1;
327  c_dfad = max(a_dfad, val);
328  COMPARE_FADS(c_dfad.val(), val);
329  for (int i=0; i<this->n1; i++) {
330  COMPARE_FADS(c_dfad.dx(i), FadType(0.0));
331  }
332 
333  val = a_dfad.val() - 1;
334  c_dfad = max(a_dfad, val);
335  COMPARE_FADS(c_dfad.val(), a_dfad.val());
336  for (int i=0; i<this->n1; i++) {
337  COMPARE_FADS(c_dfad.dx(i), a_dfad.dx(i));
338  COMPARE_FADS(c_dfad.fastAccessDx(i), a_dfad.fastAccessDx(i));
339  }
340 
341  val = b_dfad.val() + 1;
342  c_dfad = max(val, b_dfad);
343  COMPARE_FADS(c_dfad.val(), val);
344  for (int i=0; i<this->n1; i++) {
345  COMPARE_FADS(c_dfad.dx(i), FadType(0.0));
346  }
347 
348  val = b_dfad.val() - 1;
349  c_dfad = max(val, b_dfad);
350  COMPARE_FADS(c_dfad.val(), b_dfad.val());
351  for (int i=0; i<this->n1; i++) {
352  COMPARE_FADS(c_dfad.dx(i), b_dfad.dx(i));
353  COMPARE_FADS(c_dfad.fastAccessDx(i), b_dfad.fastAccessDx(i));
354  }
355 }
356 
358  typedef decltype(this->a_dfad_) FadFadType;
360 
361  FadType val;
362 
363  auto a_dfad = this->a_dfad_;
364  auto b_dfad = this->b_dfad_;
365  auto c_dfad = this->c_dfad_;
366  auto a_fad = this->a_fad_;
367  auto b_fad = this->b_fad_;
368  auto c_fad = this->c_fad_;
369 
370  FadFadType aa_dfad = a_dfad - 1.0;
371  c_dfad = min(aa_dfad, a_dfad);
372  COMPARE_FADS(c_dfad.val(), aa_dfad.val());
373  for (int i=0; i<this->n1; i++) {
374  COMPARE_FADS(c_dfad.dx(i), aa_dfad.dx(i));
375  COMPARE_FADS(c_dfad.fastAccessDx(i), aa_dfad.fastAccessDx(i));
376  }
377 
378  c_dfad = min(a_dfad, aa_dfad);
379  COMPARE_FADS(c_dfad.val(), aa_dfad.val());
380  for (int i=0; i<this->n1; i++) {
381  COMPARE_FADS(c_dfad.dx(i), aa_dfad.dx(i));
382  COMPARE_FADS(c_dfad.fastAccessDx(i), aa_dfad.fastAccessDx(i));
383  }
384 
385  val = a_dfad.val() - 1;
386  c_dfad = min(a_dfad, val);
387  COMPARE_FADS(c_dfad.val(), val);
388  for (int i=0; i<this->n1; i++) {
389  COMPARE_FADS(c_dfad.dx(i), FadType(0.0));
390  }
391 
392  val = a_dfad.val() + 1;
393  c_dfad = min(a_dfad, val);
394  COMPARE_FADS(c_dfad.val(), a_dfad.val());
395  for (int i=0; i<this->n1; i++) {
396  COMPARE_FADS(c_dfad.dx(i), a_dfad.dx(i));
397  COMPARE_FADS(c_dfad.fastAccessDx(i), a_dfad.fastAccessDx(i));
398  }
399 
400  val = b_dfad.val() - 1;
401  c_dfad = min(val, b_dfad);
402  COMPARE_FADS(c_dfad.val(), val);
403  for (int i=0; i<this->n1; i++) {
404  COMPARE_FADS(c_dfad.dx(i), FadType(0.0));
405  }
406 
407  val = b_dfad.val() + 1;
408  c_dfad = min(val, b_dfad);
409  COMPARE_FADS(c_dfad.val(), b_dfad.val());
410  for (int i=0; i<this->n1; i++) {
411  COMPARE_FADS(c_dfad.dx(i), b_dfad.dx(i));
412  COMPARE_FADS(c_dfad.fastAccessDx(i), b_dfad.fastAccessDx(i));
413  }
414 }
415 
416 TYPED_TEST_P(FadFadOpsUnitTest, testComposite1) {
417  auto a_dfad = this->a_dfad_;
418  auto b_dfad = this->b_dfad_;
419  auto c_dfad = this->c_dfad_;
420  auto a_fad = this->a_fad_;
421  auto b_fad = this->b_fad_;
422  auto c_fad = this->c_fad_;
423 
424  c_dfad = this->composite1(a_dfad, b_dfad);
425  c_fad = this->composite1_fad(a_fad, b_fad);
426  COMPARE_NESTED_FADS(c_dfad, c_fad);
427 }
428 
430  typedef decltype(this->a_dfad_) FadFadType;
432 
433  auto a_dfad = this->a_dfad_;
434  auto b_dfad = this->b_dfad_;
435  auto a_fad = this->a_fad_;
436  auto b_fad = this->b_fad_;
437 
438  FadFadType aa_dfad = a_dfad;
439  FAD::Fad< FadType > aa_fad = a_fad;
440  aa_dfad = 1.0;
441  aa_fad = FadType(1.0);
442  aa_dfad = aa_dfad + b_dfad;
443  aa_fad = aa_fad + b_fad;
444  COMPARE_NESTED_FADS(aa_dfad, aa_fad);
445 }
446 
448  typedef decltype(this->a_dfad_) FadFadType;
450 
451  auto a_dfad = this->a_dfad_;
452  auto b_dfad = this->b_dfad_;
453  auto a_fad = this->a_fad_;
454  auto b_fad = this->b_fad_;
455 
456  FadFadType aa_dfad = a_dfad;
457  FAD::Fad< FadType > aa_fad = a_fad;
458  aa_dfad = 1.0;
459  aa_fad = FadType(1.0);
460  aa_dfad = aa_dfad - b_dfad;
461  aa_fad = aa_fad - b_fad;
462  COMPARE_NESTED_FADS(aa_dfad, aa_fad);
463 }
464 
466  typedef decltype(this->a_dfad_) FadFadType;
468 
469  auto a_dfad = this->a_dfad_;
470  auto b_dfad = this->b_dfad_;
471  auto a_fad = this->a_fad_;
472  auto b_fad = this->b_fad_;
473 
474  FadFadType aa_dfad = a_dfad;
475  FAD::Fad< FadType > aa_fad = a_fad;
476  aa_dfad = 2.0;
477  aa_fad = FadType(2.0);
478  aa_dfad = aa_dfad * b_dfad;
479  aa_fad = aa_fad * b_fad;
480  COMPARE_NESTED_FADS(aa_dfad, aa_fad);
481 }
482 
484  typedef decltype(this->a_dfad_) FadFadType;
486 
487  auto a_dfad = this->a_dfad_;
488  auto b_dfad = this->b_dfad_;
489  auto a_fad = this->a_fad_;
490  auto b_fad = this->b_fad_;
491 
492  FadFadType aa_dfad = a_dfad;
493  FAD::Fad< FadType > aa_fad = a_fad;
494  aa_dfad = 2.0;
495  aa_fad = FadType(2.0);
496  aa_dfad = aa_dfad / b_dfad;
497  aa_fad = aa_fad / b_fad;
498  COMPARE_NESTED_FADS(aa_dfad, aa_fad);
499 }
500 
501  // Check various corner cases for pow()
503  typedef decltype(this->a_dfad_) FadFadType;
505  typedef typename Sacado::ScalarType<FadFadType>::type ScalarType;
506 
507  auto a_dfad = this->a_dfad_;
508 
509  FadFadType a, b, c, cc;
510 
511  // Constant b
512  a = a_dfad;
513  b = 3.456;
514  c = pow(a, b);
515  ScalarType f = pow(a.val().val(), b.val().val());
516  ScalarType fp = b.val().val()*pow(a.val().val(),b.val().val()-1);
517  ScalarType fpp = b.val().val()*(b.val().val()-1)*pow(a.val().val(),b.val().val()-2);
518  cc = FadFadType(this->n1,FadType(this->n2,f));
519  for (int i=0; i<this->n2; ++i)
520  cc.val().fastAccessDx(i) = fp*a.val().dx(i);
521  for (int i=0; i<this->n1; ++i) {
522  cc.fastAccessDx(i) = FadType(this->n2,fp*a.dx(i).val());
523  for (int j=0; j<this->n2; ++j)
524  cc.fastAccessDx(i).fastAccessDx(j) = fpp*a.dx(i).val()*a.val().dx(j) + fp*a.dx(i).dx(j);
525  }
526  COMPARE_NESTED_FADS(c, cc);
527 
528  // Constant scalar b
529  c = pow(a, b.val());
530  COMPARE_NESTED_FADS(c, cc);
531  c = pow(a, b.val().val());
532  COMPARE_NESTED_FADS(c, cc);
533 
534  // Constant b == 0
535  b = 0.0;
536  c = pow(a, b);
537  cc.val() = FadType(this->n2,1.0);
538  for (int i=0; i<this->n1; ++i)
539  cc.fastAccessDx(i) = 0.0;
540  COMPARE_NESTED_FADS(c, cc);
541 
542  // Constant scalar b == 0
543  c = pow(a, b.val());
544  cc.val() = FadType(this->n2,1.0);
545  for (int i=0; i<this->n1; ++i)
546  cc.fastAccessDx(i) = 0.0;
547  COMPARE_NESTED_FADS(c, cc);
548  c = pow(a, b.val().val());
549  COMPARE_NESTED_FADS(c, cc);
550 
551  // a == 0 and constant b as a Fad
552  // This only works for DFad/SLFad, because there is no such thing as a
553  // constant SFad.
555  a.val() = 0.0;
556  b = 3.456;
557  c = pow(a, b);
558  cc.val() = 0.0;
559  for (int i=0; i<this->n1; ++i)
560  cc.fastAccessDx(i) = FadType(this->n2,0.0);
561  COMPARE_NESTED_FADS(c, cc);
562  }
563 
564  // a == 0 and constant scalar b
565  a.val() = 0.0;
566  b = 3.456;
567  c = pow(a, b.val());
568  cc.val() = 0.0;
569  for (int i=0; i<this->n1; ++i)
570  cc.fastAccessDx(i) = FadType(this->n2,0.0);
571  COMPARE_NESTED_FADS(c, cc);
572  c = pow(a, b.val().val());
573  COMPARE_NESTED_FADS(c, cc);
574 
575  // a == 0 and b == 0
576  b = 0.0;
577  cc.val() = 1.0;
578  for (int i=0; i<this->n1; ++i)
579  cc.fastAccessDx(i) = 0.0;
581  c = pow(a, b);
582  COMPARE_NESTED_FADS(c, cc);
583  c = pow(a, b.val());
584  COMPARE_NESTED_FADS(c, cc);
585  }
586  c = pow(a, b.val().val());
587  COMPARE_NESTED_FADS(c, cc);
588 
589  // a nonzero and b == 2
590  a = a_dfad;
591  a.val().val() = 0.0;
592  b = 2.0;
593  c = pow(a, b);
594  f = pow(a.val().val(), b.val().val());
595  fp = b.val().val()*pow(a.val().val(),b.val().val()-1);
596  fpp = b.val().val()*(b.val().val()-1)*pow(a.val().val(),b.val().val()-2);
597  cc = FadFadType(this->n1,FadType(this->n2,f));
598  for (int i=0; i<this->n2; ++i)
599  cc.val().fastAccessDx(i) = fp*a.val().dx(i);
600  for (int i=0; i<this->n1; ++i) {
601  cc.fastAccessDx(i) = FadType(this->n2,fp*a.dx(i).val());
602  for (int j=0; j<this->n2; ++j)
603  cc.fastAccessDx(i).fastAccessDx(j) = fpp*a.dx(i).val()*a.val().dx(j) + fp*a.dx(i).dx(j);
604  }
605 
606  // a.val().val() == 0 with a.val().dx() != 0
608  COMPARE_NESTED_FADS(c, cc);
609  c = pow(a, b.val());
610  COMPARE_NESTED_FADS(c, cc);
611  }
612  c = pow(a, b.val().val());
613  COMPARE_NESTED_FADS(c, cc);
614 
615  // a.val().val() == 0 and b == 1
616  b = 1.0;
617  c = pow(a, b);
618  cc = a;
620  COMPARE_NESTED_FADS(c, cc);
621  c = pow(a, b.val());
622  COMPARE_NESTED_FADS(c, cc);
623  }
624  c = pow(a, b.val().val());
625  COMPARE_NESTED_FADS(c, cc);
626 
627  // a.val().val() == 0 and b == 0
628  b = 0.0;
629  c = pow(a, b);
630  cc.val() = FadType(this->n2, 1.0);
631  for (int i=0; i<this->n1; ++i)
632  cc.fastAccessDx(i) = 0.0;
634  COMPARE_NESTED_FADS(c, cc);
635  c = pow(a, b.val());
636  COMPARE_NESTED_FADS(c, cc);
637  }
638  c = pow(a, b.val().val());
639  COMPARE_NESTED_FADS(c, cc);
640 
641  // a.val().val() == 0, b == 2, nested expression
642  c = pow(2.0*a,2.0);
643  cc = 4.0*a*a;
644  COMPARE_NESTED_FADS(c, cc);
645 }
646 
649  testAddition,
650  testSubtraction,
651  testMultiplication,
652  testDivision,
653  testEquals,
654  testNotEquals,
655  testLessThanOrEquals,
656  testGreaterThanOrEquals,
657  testLessThan,
658  testGreaterThan,
659  testPow,
660  testUnaryPlus,
661  testUnaryMinus,
662  testExp,
663  testLog,
664  testLog10,
665  testSqrt,
666  testCos,
667  testSin,
668  testTan,
669  testACos,
670  testASin,
671  testATan,
672  testCosh,
673  testSinh,
674  testTanh,
675  testAbs,
676  testFAbs,
677  testPlusEquals,
678  testMinusEquals,
679  testTimesEquals,
680  testDivideEquals,
681  testMax,
682  testMin,
683  testComposite1,
684  testPlusLR,
685  testMinusLR,
686  testTimesLR,
687  testDivideLR,
688  testPowConstB);
689 
690 #endif // NESETD_FADUNITTESTS_HPP
FAD::Fad< FadType > a_fad_
REGISTER_TYPED_TEST_SUITE_P(FadBLASUnitTests, testSCAL1, testSCAL2, testSCAL3, testSCAL4, testCOPY1, testCOPY2, testCOPY3, testCOPY4, testAXPY1, testAXPY2, testAXPY3, testAXPY4, testDOT1, testDOT2, testDOT3, testDOT4, testNRM21, testNRM22, testGEMV1, testGEMV2, testGEMV3, testGEMV4, testGEMV5, testGEMV6, testGEMV7, testGEMV8, testGEMV9, testTRMV1, testTRMV2, testTRMV3, testTRMV4, testGER1, testGER2, testGER3, testGER4, testGER5, testGER6, testGER7, testGEMM1, testGEMM2, testGEMM3, testGEMM4, testGEMM5, testGEMM6, testGEMM7, testGEMM8, testGEMM9, testGEMM10, testSYMM1, testSYMM2, testSYMM3, testSYMM4, testSYMM5, testSYMM6, testSYMM7, testSYMM8, testSYMM9, testTRMM1, testTRMM2, testTRMM3, testTRMM4, testTRMM5, testTRMM6, testTRMM7, testTRSM1, testTRSM2, testTRSM3, testTRSM4, testTRSM5, testTRSM6, testTRSM7)
void f()
asin(expr.val())
cosh(expr.val())
abs(expr.val())
ScalarT composite1(const ScalarT &a, const ScalarT &b)
TYPED_TEST_P(FadBLASUnitTests, testSCAL1)
#define COMPARE_NESTED_FADS(a, b)
Definition: GTestUtils.hpp:95
FAD::Fad< FadType > b_fad_
Sacado::Fad::DFad< double > FadType
atan(expr.val())
Sacado::ScalarType< FadFadType >::type ScalarType
#define BINARY_OP_TEST(FIXTURENAME, TESTNAME, OP)
#define UNARY_FUNC_TEST(FIXTURENAME, TESTNAME, FUNC)
expr val()
tanh(expr.val())
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
FAD::Fad< FadType > c_fad_
SimpleFad< ValueT > min(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
sqrt(expr.val())
sinh(expr.val())
tan(expr.val())
void TearDown() override
#define UNARY_OP_TEST(FIXTURENAME, TESTNAME, OP)
void SetUp() override
sin(expr.val())
Sacado::ValueType< FadFadType >::type FadType
TYPED_TEST_SUITE_P(FadBLASUnitTests)
log(expr.val())
Sacado::Random< ScalarType > urand
acos(expr.val())
SimpleFad< ValueT > max(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
SACADO_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)
#define BINARY_FUNC_TEST(FIXTURENAME, TESTNAME, FUNC)
A random number generator that generates random numbers uniformly distributed in the interval (a...
exp(expr.val())
fabs(expr.val())
#define COMPARE_FADS(a, b)
Definition: GTestUtils.hpp:92
ScalarT composite1_fad(const ScalarT &a, const ScalarT &b)
log10(expr.val())
#define UNARY_ASSIGNOP_TEST(FIXTURENAME, TESTNAME, OP)
cos(expr.val())
#define RELOP_TEST(FIXTURENAME, TESTNAME, OP)
Base template specification for testing whether type is statically sized.