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 // 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 FADUNITTESTS2_HPP
11 #define FADUNITTESTS2_HPP
12 
13 // Sacado includes
14 #include "Sacado_No_Kokkos.hpp"
15 #include "Sacado_Random.hpp"
16 
17 // gtest includes
18 #include <gtest/gtest.h>
19 
20 #include "GTestUtils.hpp"
21 
22 // A class for testing each Fad operation
23 template <typename FadType>
25 protected:
27 
28  // DFad variables
30 
31  // Random number generator
33 
34  // Number of derivative components
35  int n_;
36 
37  // Tolerances to which fad objects should be the same
38  double tol_a, tol_r;
39 
40  FadOpsUnitTest2() : urand(), n_(5), tol_a(1.0e-15), tol_r(1.0e-12) {}
41 
42  void SetUp() override {
44 
45  val = urand.number();
46  a_fad_ = FadType(n_,val);
47 
48  val = urand.number();
49  b_fad_ = FadType(n_,val);
50 
51  for (int i=0; i<n_; i++) {
52  val = urand.number();
53  a_fad_.fastAccessDx(i) = val;
54 
55  val = urand.number();
56  b_fad_.fastAccessDx(i) = val;
57  }
58 
59  val = 0.0;
60  c_fad_ = FadType(n_, val);
61  }
62 
63  void TearDown() override {}
64 
65 }; // class FadOpsUnitTest2
66 
67 // A class for testing each real Fad operation
68 // This class tests additional functions that aren't define for complex
69 // types
70 template <typename FadType>
71 class RealFadOpsUnitTest2 : public FadOpsUnitTest2<FadType> {
72 protected:
73 
75 
76 }; // class RealFadOpsUnitTest2
77 
80 
81 TYPED_TEST_P(FadOpsUnitTest2, testAddition) {
82  typedef decltype(this->a_fad_) FadType;
83  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
84  auto a_fad = this->a_fad_;
85  auto b_fad = this->b_fad_;
86  auto c_fad = this->c_fad_;
87  auto n = this->n_;
88 
89  c_fad = a_fad + b_fad;
90  FadType t1(n, a_fad.val()+b_fad.val());
91  for (int i=0; i<n; i++)
92  t1.fastAccessDx(i) = a_fad.dx(i) + b_fad.dx(i);
93  COMPARE_FADS(c_fad, t1);
94 
95  ScalarType val = this->urand.number();
96  c_fad = a_fad + val;
97  FadType t2(n, a_fad.val()+val);
98  for (int i=0; i<n; i++)
99  t2.fastAccessDx(i) = a_fad.dx(i);
100  COMPARE_FADS(c_fad, t2);
101 
102  c_fad = val + b_fad;
103  FadType t3(n, val+b_fad.val());
104  for (int i=0; i<n; i++)
105  t3.fastAccessDx(i) = b_fad.dx(i);
106  COMPARE_FADS(c_fad, t3);
107 }
108 
109 TYPED_TEST_P(FadOpsUnitTest2, testSubtraction) {
110  typedef decltype(this->a_fad_) FadType;
111  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
112  auto a_fad = this->a_fad_;
113  auto b_fad = this->b_fad_;
114  auto c_fad = this->c_fad_;
115  auto n = this->n_;
116 
117  c_fad = a_fad - b_fad;
118  FadType t1(n, a_fad.val()-b_fad.val());
119  for (int i=0; i<n; i++)
120  t1.fastAccessDx(i) = a_fad.dx(i) - b_fad.dx(i);
121  COMPARE_FADS(c_fad, t1);
122 
123  ScalarType val = this->urand.number();
124  c_fad = a_fad - val;
125  FadType t2(n, a_fad.val()-val);
126  for (int i=0; i<n; i++)
127  t2.fastAccessDx(i) = a_fad.dx(i);
128  COMPARE_FADS(c_fad, t2);
129 
130  c_fad = val - b_fad;
131  FadType t3(n, val-b_fad.val());
132  for (int i=0; i<n; i++)
133  t3.fastAccessDx(i) = -b_fad.dx(i);
134  COMPARE_FADS(c_fad, t3);
135 }
136 
137 TYPED_TEST_P(FadOpsUnitTest2, testMultiplication) {
138  typedef decltype(this->a_fad_) FadType;
139  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
140  auto a_fad = this->a_fad_;
141  auto b_fad = this->b_fad_;
142  auto c_fad = this->c_fad_;
143  auto n = this->n_;
144 
145  c_fad = a_fad * b_fad;
146  FadType t1(n, a_fad.val()*b_fad.val());
147  for (int i=0; i<n; i++)
148  t1.fastAccessDx(i) = a_fad.dx(i)*b_fad.val() + a_fad.val()*b_fad.dx(i);
149  COMPARE_FADS(c_fad, t1);
150 
151  ScalarType val = this->urand.number();
152  c_fad = a_fad * val;
153  FadType t2(n, a_fad.val()*val);
154  for (int i=0; i<n; i++)
155  t2.fastAccessDx(i) = a_fad.dx(i)*val;
156  COMPARE_FADS(c_fad, t2);
157 
158  c_fad = val * b_fad;
159  FadType t3(n, val*b_fad.val());
160  for (int i=0; i<n; i++)
161  t3.fastAccessDx(i) = val*b_fad.dx(i);
162  COMPARE_FADS(c_fad, t3);
163 }
164 
166  typedef decltype(this->a_fad_) FadType;
167  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
168  auto a_fad = this->a_fad_;
169  auto b_fad = this->b_fad_;
170  auto c_fad = this->c_fad_;
171  auto n = this->n_;
172 
173  c_fad = a_fad / b_fad;
174  FadType t1(n, a_fad.val()/b_fad.val());
175  for (int i=0; i<n; i++)
176  t1.fastAccessDx(i) =
177  (a_fad.dx(i)*b_fad.val() - a_fad.val()*b_fad.dx(i)) /
178  (b_fad.val()*b_fad.val());
179  COMPARE_FADS(c_fad, t1);
180 
181  ScalarType val = this->urand.number();
182  c_fad = a_fad / val;
183  FadType t2(n, a_fad.val()/val);
184  for (int i=0; i<n; i++)
185  t2.fastAccessDx(i) = a_fad.dx(i)/val;
186  COMPARE_FADS(c_fad, t2);
187 
188  c_fad = val / b_fad;
189  FadType t3(n, val/b_fad.val());
190  for (int i=0; i<n; i++)
191  t3.fastAccessDx(i) = -val*b_fad.dx(i)/(b_fad.val()*b_fad.val());
192  COMPARE_FADS(c_fad, t3);
193 }
194 
196  typedef decltype(this->a_fad_) FadType;
197  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
198  auto a_fad = this->a_fad_;
199  auto b_fad = this->b_fad_;
200  auto c_fad = this->c_fad_;
201 
202  bool r1 = a_fad == b_fad;
203  bool r2 = a_fad.val() == b_fad.val();
204  ASSERT_TRUE(r1 == r2);
205 
206  ScalarType val = this->urand.number();
207  r1 = a_fad == val;
208  r2 = a_fad.val() == val;
209  ASSERT_TRUE(r1 == r2);
210 
211  r1 = val == b_fad;
212  r2 = val == b_fad.val();
213  ASSERT_TRUE(r1 == r2);
214 }
215 
216 TYPED_TEST_P(FadOpsUnitTest2, testNotEquals) {
217  typedef decltype(this->a_fad_) FadType;
218  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
219  auto a_fad = this->a_fad_;
220  auto b_fad = this->b_fad_;
221  auto c_fad = this->c_fad_;
222 
223  bool r1 = a_fad != b_fad;
224  bool r2 = a_fad.val() != b_fad.val();
225  ASSERT_TRUE(r1 == r2);
226 
227  ScalarType val = this->urand.number();
228  r1 = a_fad != val;
229  r2 = a_fad.val() != val;
230  ASSERT_TRUE(r1 == r2);
231 
232  r1 = val != b_fad;
233  r2 = val != b_fad.val();
234  ASSERT_TRUE(r1 == r2);
235 }
236 
237 TYPED_TEST_P(FadOpsUnitTest2, testUnaryPlus) {
238  typedef decltype(this->a_fad_) FadType;
239  auto a_fad = this->a_fad_;
240  auto c_fad = this->c_fad_;
241  auto n = this->n_;
242 
243  c_fad = +(a_fad);
244  FadType t1(n, a_fad.val());
245  for (int i=0; i<n; i++)
246  t1.fastAccessDx(i) = a_fad.dx(i);
247  COMPARE_FADS(c_fad, t1);
248 }
249 
250 TYPED_TEST_P(FadOpsUnitTest2, testUnaryMinus) {
251  typedef decltype(this->a_fad_) FadType;
252  auto a_fad = this->a_fad_;
253  auto c_fad = this->c_fad_;
254  auto n = this->n_;
255 
256  c_fad = -(a_fad);
257  FadType t1(n, -a_fad.val());
258  for (int i=0; i<n; i++)
259  t1.fastAccessDx(i) = -a_fad.dx(i);
260  COMPARE_FADS(c_fad, t1);
261 }
262 
264  typedef decltype(this->a_fad_) FadType;
265  auto a_fad = this->a_fad_;
266  auto c_fad = this->c_fad_;
267  auto n = this->n_;
268 
269  c_fad = std::exp(a_fad);
270  FadType t1(n, std::exp(a_fad.val()));
271  for (int i=0; i<n; i++)
272  t1.fastAccessDx(i) = std::exp(a_fad.val())*a_fad.dx(i);
273  COMPARE_FADS(c_fad, t1);
274 }
275 
277  typedef decltype(this->a_fad_) FadType;
278  auto a_fad = this->a_fad_;
279  auto c_fad = this->c_fad_;
280  auto n = this->n_;
281 
282  c_fad = std::log(a_fad);
283  FadType t1(n, std::log(a_fad.val()));
284  for (int i=0; i<n; i++)
285  t1.fastAccessDx(i) = a_fad.dx(i)/a_fad.val();
286  COMPARE_FADS(c_fad, t1);
287 }
288 
290  typedef decltype(this->a_fad_) FadType;
291  auto a_fad = this->a_fad_;
292  auto c_fad = this->c_fad_;
293  auto n = this->n_;
294 
295  c_fad = std::log10(a_fad);
296  FadType t1(n, std::log10(a_fad.val()));
297  for (int i=0; i<n; i++)
298  t1.fastAccessDx(i) = a_fad.dx(i)/(a_fad.val()*std::log(10));
299  COMPARE_FADS(c_fad, t1);
300 }
301 
303  typedef decltype(this->a_fad_) FadType;
304  auto a_fad = this->a_fad_;
305  auto c_fad = this->c_fad_;
306  auto n = this->n_;
307 
308  c_fad = std::sqrt(a_fad);
309  FadType t1(n, std::sqrt(a_fad.val()));
310  for (int i=0; i<n; i++)
311  t1.fastAccessDx(i) = a_fad.dx(i)/(2.*std::sqrt(a_fad.val()));
312  COMPARE_FADS(c_fad, t1);
313 }
314 
316  typedef decltype(this->a_fad_) FadType;
317  auto a_fad = this->a_fad_;
318  auto c_fad = this->c_fad_;
319  auto n = this->n_;
320 
321  c_fad = std::cos(a_fad);
322  FadType t1(n, std::cos(a_fad.val()));
323  for (int i=0; i<n; i++)
324  t1.fastAccessDx(i) = -std::sin(a_fad.val())*a_fad.dx(i);
325  COMPARE_FADS(c_fad, t1);
326 }
327 
329  typedef decltype(this->a_fad_) FadType;
330  auto a_fad = this->a_fad_;
331  auto c_fad = this->c_fad_;
332  auto n = this->n_;
333 
334  c_fad = std::sin(a_fad);
335  FadType t1(n, std::sin(a_fad.val()));
336  for (int i=0; i<n; i++)
337  t1.fastAccessDx(i) = std::cos(a_fad.val())*a_fad.dx(i);
338  COMPARE_FADS(c_fad, t1);
339 }
340 
342  typedef decltype(this->a_fad_) FadType;
343  auto a_fad = this->a_fad_;
344  auto c_fad = this->c_fad_;
345  auto n = this->n_;
346 
347  c_fad = std::tan(a_fad);
348  FadType t1(n, std::tan(a_fad.val()));
349  for (int i=0; i<n; i++)
350  t1.fastAccessDx(i) =
351  a_fad.dx(i)/(std::cos(a_fad.val())*std::cos(a_fad.val()));;
352  COMPARE_FADS(c_fad, t1);
353 }
354 
356  typedef decltype(this->a_fad_) FadType;
357  auto a_fad = this->a_fad_;
358  auto c_fad = this->c_fad_;
359  auto n = this->n_;
360 
361  c_fad = std::cosh(a_fad);
362  FadType t1(n, std::cosh(a_fad.val()));
363  for (int i=0; i<n; i++)
364  t1.fastAccessDx(i) = std::sinh(a_fad.val())*a_fad.dx(i);
365  COMPARE_FADS(c_fad, t1);
366 }
367 
369  typedef decltype(this->a_fad_) FadType;
370  auto a_fad = this->a_fad_;
371  auto c_fad = this->c_fad_;
372  auto n = this->n_;
373 
374  c_fad = std::sinh(a_fad);
375  FadType t1(n, std::sinh(a_fad.val()));
376  for (int i=0; i<n; i++)
377  t1.fastAccessDx(i) = std::cosh(a_fad.val())*a_fad.dx(i);
378  COMPARE_FADS(c_fad, t1);
379 }
380 
382  typedef decltype(this->a_fad_) FadType;
383  auto a_fad = this->a_fad_;
384  auto c_fad = this->c_fad_;
385  auto n = this->n_;
386 
387  c_fad = std::tanh(a_fad);
388  FadType t1(n, std::tanh(a_fad.val()));
389  for (int i=0; i<n; i++)
390  t1.fastAccessDx(i) =
391  a_fad.dx(i)/(std::cosh(a_fad.val())*std::cosh(a_fad.val()));
392  COMPARE_FADS(c_fad, t1);
393 }
394 
395 TYPED_TEST_P(FadOpsUnitTest2, testPlusEquals) {
396  typedef decltype(this->a_fad_) FadType;
397  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
398  auto a_fad = this->a_fad_;
399  auto c_fad = this->c_fad_;
400  auto n = this->n_;
401 
402  FadType t1(n, c_fad.val()+a_fad.val());
403  for (int i=0; i<n; i++)
404  t1.fastAccessDx(i) = c_fad.dx(i) + a_fad.dx(i);
405  c_fad += a_fad;
406  COMPARE_FADS(c_fad, t1);
407 
408  ScalarType val = this->urand.number();
409  FadType t2(n, c_fad.val()+val);
410  for (int i=0; i<n; i++)
411  t2.fastAccessDx(i) = c_fad.dx(i);
412  c_fad += val;
413  COMPARE_FADS(c_fad, t2);
414 }
415 
416 TYPED_TEST_P(FadOpsUnitTest2, testMinusEquals) {
417  typedef decltype(this->a_fad_) FadType;
418  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
419  auto a_fad = this->a_fad_;
420  auto c_fad = this->c_fad_;
421  auto n = this->n_;
422 
423  FadType t1(n, c_fad.val()-a_fad.val());
424  for (int i=0; i<n; i++)
425  t1.fastAccessDx(i) = c_fad.dx(i) - a_fad.dx(i);
426  c_fad -= a_fad;
427  COMPARE_FADS(c_fad, t1);
428 
429  ScalarType val = this->urand.number();
430  FadType t2(n, c_fad.val()-val);
431  for (int i=0; i<n; i++)
432  t2.fastAccessDx(i) = c_fad.dx(i);
433  c_fad -= val;
434  COMPARE_FADS(c_fad, t2);
435 }
436 
437 TYPED_TEST_P(FadOpsUnitTest2, testTimesEquals) {
438  typedef decltype(this->a_fad_) FadType;
439  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
440  auto a_fad = this->a_fad_;
441  auto c_fad = this->c_fad_;
442  auto n = this->n_;
443 
444  FadType t1(n, c_fad.val()*a_fad.val());
445  for (int i=0; i<n; i++)
446  t1.fastAccessDx(i) = c_fad.dx(i)*a_fad.val() + a_fad.dx(i)*c_fad.val();
447  c_fad *= a_fad;
448  COMPARE_FADS(c_fad, t1);
449 
450  ScalarType val = this->urand.number();
451  FadType t2(n, c_fad.val()*val);
452  for (int i=0; i<n; i++)
453  t2.fastAccessDx(i) = c_fad.dx(i)*val;
454  c_fad *= val;
455  COMPARE_FADS(c_fad, t2);
456 }
457 
458 TYPED_TEST_P(FadOpsUnitTest2, testDivideEquals) {
459  typedef decltype(this->a_fad_) FadType;
460  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
461  auto a_fad = this->a_fad_;
462  auto b_fad = this->b_fad_;
463  auto c_fad = this->c_fad_;
464  auto n = this->n_;
465 
466  FadType t1(n, c_fad.val()/a_fad.val());
467  for (int i=0; i<n; i++)
468  t1.fastAccessDx(i) =
469  (a_fad.dx(i)*c_fad.val() - c_fad.dx(i)*a_fad.val()) /
470  (a_fad.val()*a_fad.val());
471  c_fad /= a_fad;
472  COMPARE_FADS(c_fad, t1);
473 
474  ScalarType val = this->urand.number();
475  FadType t2(n, c_fad.val()/val);
476  for (int i=0; i<n; i++)
477  t2.fastAccessDx(i) = c_fad.dx(i)/val;
478  c_fad /= val;
479  COMPARE_FADS(c_fad, t2);
480 }
481 
483  typedef decltype(this->a_fad_) FadType;
484  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
485  auto a_fad = this->a_fad_;
486  auto b_fad = this->b_fad_;
487  auto c_fad = this->c_fad_;
488  auto n = this->n_;
489 
490  c_fad = std::pow(a_fad, b_fad);
491  FadType t1(n, std::pow(a_fad.val(),b_fad.val()));
492  for (int i=0; i<n; i++)
493  t1.fastAccessDx(i) =
494  std::pow(a_fad.val(),b_fad.val())*(b_fad.val()*a_fad.dx(i)/a_fad.val() +
495  std::log(a_fad.val())*b_fad.dx(i));
496  COMPARE_FADS(c_fad, t1);
497 
498  ScalarType val = this->urand.number();
499  c_fad = std::pow(a_fad, val);
500  FadType t2(n, std::pow(a_fad.val(), val));
501  for (int i=0; i<n; i++)
502  t2.fastAccessDx(i) =
503  std::pow(a_fad.val(), val)*(val*a_fad.dx(i)/a_fad.val());
504  COMPARE_FADS(c_fad, t2);
505 
506  c_fad = std::pow(val, b_fad);
507  FadType t3(n, std::pow(val, b_fad.val()));
508  for (int i=0; i<n; i++)
509  t3.fastAccessDx(i) =
510  std::pow(val, b_fad.val())*std::log(val)*b_fad.dx(i);
511  COMPARE_FADS(c_fad, t3);
512 
513  val = 0.0;
514  c_fad = std::pow(a_fad, val);
515  FadType t4(n, std::pow(a_fad.val(), val));
516  for (int i=0; i<n; i++)
517  t4.fastAccessDx(i) = 0.0;
518  COMPARE_FADS(c_fad, t4);
519 
520  c_fad = std::pow(val, b_fad);
521  FadType t5(n, std::pow(val, b_fad.val()));
522  for (int i=0; i<n; i++)
523  t5.fastAccessDx(i) = 0.0;
524  COMPARE_FADS(c_fad, t5);
525 
526  FadType aa_fad = a_fad;
527  aa_fad.val() = 0.0;
528  c_fad = std::pow(aa_fad, b_fad);
529  FadType t6(n, std::pow(aa_fad.val(),b_fad.val()));
530  for (int i=0; i<n; i++)
531  t6.fastAccessDx(i) = 0.0;
532  COMPARE_FADS(c_fad, t6);
533 
534  FadType bb_fad = b_fad;
535  bb_fad.val() = 0.0;
536  c_fad = std::pow(a_fad, bb_fad);
537  FadType t7(n, std::pow(a_fad.val(),bb_fad.val()));
538  for (int i=0; i<n; i++)
539  t7.fastAccessDx(i) =
540  std::pow(a_fad.val(),bb_fad.val())*(bb_fad.val()*a_fad.dx(i)/a_fad.val()
541  + std::log(a_fad.val())*b_fad.dx(i));
542  COMPARE_FADS(c_fad, t7);
543 }
544 
546  typedef decltype(this->a_fad_) FadType;
547  auto a_fad = this->a_fad_;
548  auto b_fad = this->b_fad_;
549  auto c_fad = this->c_fad_;
550 
551  FadType aa_fad = a_fad;
552  aa_fad = 1.0;
553  aa_fad = aa_fad + b_fad;
554  c_fad = 1.0 + b_fad;
555  COMPARE_FADS(aa_fad, c_fad);
556 }
557 
558 TYPED_TEST_P(FadOpsUnitTest2, testPlusEqualsLR) {
559  typedef decltype(this->a_fad_) FadType;
560  auto a_fad = this->a_fad_;
561  auto b_fad = this->b_fad_;
562  auto c_fad = this->c_fad_;
563 
564  FadType aa_fad = a_fad;
565  aa_fad = 1.0;
566  aa_fad += aa_fad + b_fad;
567  c_fad = 1.0 + 1.0 + b_fad;
568  COMPARE_FADS(aa_fad, c_fad);
569 }
570 
571 TYPED_TEST_P(FadOpsUnitTest2, testMinusEqualsLR) {
572  typedef decltype(this->a_fad_) FadType;
573  auto a_fad = this->a_fad_;
574  auto b_fad = this->b_fad_;
575  auto c_fad = this->c_fad_;
576 
577  FadType aa_fad = a_fad;
578  aa_fad = 1.0;
579  aa_fad -= aa_fad + b_fad;
580  c_fad = 1.0 - 1.0 - b_fad;
581  COMPARE_FADS(aa_fad, c_fad);
582 }
583 
584 TYPED_TEST_P(FadOpsUnitTest2, testTimesEqualsLR) {
585  typedef decltype(this->a_fad_) FadType;
586  auto a_fad = this->a_fad_;
587  auto b_fad = this->b_fad_;
588  auto c_fad = this->c_fad_;
589 
590  FadType aa_fad = a_fad;
591  aa_fad = 2.0;
592  aa_fad *= aa_fad + b_fad;
593  c_fad = 2.0 * (2.0 + b_fad);
594  COMPARE_FADS(aa_fad, c_fad);
595 }
596 
597 TYPED_TEST_P(FadOpsUnitTest2, testDivideEqualsLR) {
598  typedef decltype(this->a_fad_) FadType;
599  auto a_fad = this->a_fad_;
600  auto b_fad = this->b_fad_;
601  auto c_fad = this->c_fad_;
602 
603  FadType aa_fad = a_fad;
604  aa_fad = 2.0;
605  aa_fad /= aa_fad + b_fad;
606  c_fad = 2.0 / (2.0 + b_fad);
607  COMPARE_FADS(aa_fad, c_fad);
608 }
609 
610 TYPED_TEST_P(FadOpsUnitTest2, testResizeBug6135) {
611  typedef decltype(this->a_fad_) FadType;
612  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
613  auto a_fad = this->a_fad_;
614  auto c_fad = this->c_fad_;
615 
616  FadType d_fad = ScalarType(1.0);
617  d_fad = d_fad + a_fad;
618  c_fad = 1.0 + a_fad;
619  COMPARE_FADS(d_fad, c_fad);
620 }
621 
623  typedef decltype(this->a_fad_) FadType;
624  auto a_fad = this->a_fad_;
625  auto b_fad = this->b_fad_;
626  auto c_fad = this->c_fad_;
627 
628  FadType aa_fad = a_fad;
629  FadType bb_fad = b_fad;
630  aa_fad.val() = 9.0;
631  bb_fad.val() = 3.0;
632  FadType d_fad;
633  if (aa_fad == bb_fad*bb_fad)
634  d_fad = aa_fad;
635  c_fad = aa_fad;
636  COMPARE_FADS(d_fad, c_fad);
637 }
638 
639 TYPED_TEST_P(FadOpsUnitTest2, testEqualityConstL) {
640  typedef decltype(this->a_fad_) FadType;
641  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
642  auto a_fad = this->a_fad_;
643  auto b_fad = this->b_fad_;
644  auto c_fad = this->c_fad_;
645 
646  FadType bb_fad = b_fad;
647  bb_fad.val() = 3.0;
648  FadType d_fad;
649  if (ScalarType(9.0) == bb_fad*bb_fad)
650  d_fad = a_fad;
651  c_fad = a_fad;
652  COMPARE_FADS(d_fad, c_fad);
653 }
654 
655 TYPED_TEST_P(FadOpsUnitTest2, testEqualityConstR) {
656  typedef decltype(this->a_fad_) FadType;
657  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
658  auto a_fad = this->a_fad_;
659  auto b_fad = this->b_fad_;
660  auto c_fad = this->c_fad_;
661 
662  FadType bb_fad = b_fad;
663  bb_fad.val() = 3.0;
664  FadType d_fad;
665  if (bb_fad*bb_fad == ScalarType(9.0))
666  d_fad = a_fad;
667  c_fad = a_fad;
668  COMPARE_FADS(d_fad, c_fad);
669 }
670 
671 TYPED_TEST_P(RealFadOpsUnitTest2, testLessThanOrEquals) {
672  typedef decltype(this->a_fad_) FadType;
673  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
674  auto a_fad = this->a_fad_;
675  auto b_fad = this->b_fad_;
676 
677  bool r1 = a_fad <= b_fad;
678  bool r2 = a_fad.val() <= b_fad.val();
679  ASSERT_TRUE(r1 == r2);
680 
681  ScalarType val = this->urand.number();
682  r1 = a_fad <= val;
683  r2 = a_fad.val() <= val;
684  ASSERT_TRUE(r1 == r2);
685 
686  r1 = val <= b_fad;
687  r2 = val <= b_fad.val();
688  ASSERT_TRUE(r1 == r2);
689 }
690 
691 TYPED_TEST_P(RealFadOpsUnitTest2, testGreaterThanOrEquals) {
692  typedef decltype(this->a_fad_) FadType;
693  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
694  auto a_fad = this->a_fad_;
695  auto b_fad = this->b_fad_;
696 
697  bool r1 = a_fad >= b_fad;
698  bool r2 = a_fad.val() >= b_fad.val();
699  ASSERT_TRUE(r1 == r2);
700 
701  ScalarType val = this->urand.number();
702  r1 = a_fad >= val;
703  r2 = a_fad.val() >= val;
704  ASSERT_TRUE(r1 == r2);
705 
706  r1 = val >= b_fad;
707  r2 = val >= b_fad.val();
708  ASSERT_TRUE(r1 == r2);
709 }
710 
712  typedef decltype(this->a_fad_) FadType;
713  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
714  auto a_fad = this->a_fad_;
715  auto b_fad = this->b_fad_;
716 
717  bool r1 = a_fad < b_fad;
718  bool r2 = a_fad.val() < b_fad.val();
719  ASSERT_TRUE(r1 == r2);
720 
721  ScalarType val = this->urand.number();
722  r1 = a_fad < val;
723  r2 = a_fad.val() < val;
724  ASSERT_TRUE(r1 == r2);
725 
726  r1 = val < b_fad;
727  r2 = val < b_fad.val();
728  ASSERT_TRUE(r1 == r2);
729 }
730 
732  typedef decltype(this->a_fad_) FadType;
733  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
734  auto a_fad = this->a_fad_;
735  auto b_fad = this->b_fad_;
736 
737  bool r1 = a_fad > b_fad;
738  bool r2 = a_fad.val() > b_fad.val();
739  ASSERT_TRUE(r1 == r2);
740 
741  ScalarType val = this->urand.number();
742  r1 = a_fad > val;
743  r2 = a_fad.val() > val;
744  ASSERT_TRUE(r1 == r2);
745 
746  r1 = val > b_fad;
747  r2 = val > b_fad.val();
748  ASSERT_TRUE(r1 == r2);
749 }
750 
752  typedef decltype(this->a_fad_) FadType;
753  auto a_fad = this->a_fad_;
754  auto c_fad = this->c_fad_;
755  auto n = this->n_;
756 
757  c_fad = std::acos(a_fad);
758  FadType t1(n, std::acos(a_fad.val()));
759  for (int i=0; i<n; i++)
760  t1.fastAccessDx(i) = -a_fad.dx(i)/std::sqrt(1.0 - a_fad.val()*a_fad.val());
761  COMPARE_FADS(c_fad, t1);
762 }
763 
765  typedef decltype(this->a_fad_) FadType;
766  auto a_fad = this->a_fad_;
767  auto c_fad = this->c_fad_;
768  auto n = this->n_;
769 
770  c_fad = std::asin(a_fad);
771  FadType t1(n, std::asin(a_fad.val()));
772  for (int i=0; i<n; i++)
773  t1.fastAccessDx(i) = a_fad.dx(i)/std::sqrt(1.0 - a_fad.val()*a_fad.val());
774  COMPARE_FADS(c_fad, t1);
775 }
776 
778  typedef decltype(this->a_fad_) FadType;
779  auto a_fad = this->a_fad_;
780  auto c_fad = this->c_fad_;
781  auto n = this->n_;
782 
783  c_fad = std::atan(a_fad);
784  FadType t1(n, std::atan(a_fad.val()));
785  for (int i=0; i<n; i++)
786  t1.fastAccessDx(i) = a_fad.dx(i)/(1.0 + a_fad.val()*a_fad.val());
787  COMPARE_FADS(c_fad, t1);
788 }
789 
791  typedef decltype(this->a_fad_) FadType;
792  auto a_fad = this->a_fad_;
793  auto c_fad = this->c_fad_;
794  auto n = this->n_;
795 
796  FadType aa_fad = a_fad;
797  if (a_fad.val() < 1.0)
798  aa_fad.val() = 1.0 / a_fad.val();
799  c_fad = std::acosh(aa_fad);
800  FadType t1(n, std::acosh(aa_fad.val()));
801  for (int i=0; i<n; i++)
802  t1.fastAccessDx(i) = aa_fad.dx(i)/std::sqrt(aa_fad.val()*aa_fad.val()-1.0);
803  COMPARE_FADS(c_fad, t1);
804 }
805 
807  typedef decltype(this->a_fad_) FadType;
808  auto a_fad = this->a_fad_;
809  auto c_fad = this->c_fad_;
810  auto n = this->n_;
811 
812  c_fad = std::asinh(a_fad);
813  FadType t1(n, std::asinh(a_fad.val()));
814  for (int i=0; i<n; i++)
815  t1.fastAccessDx(i) = a_fad.dx(i)/std::sqrt(a_fad.val()*a_fad.val()+1.0);
816  COMPARE_FADS(c_fad, t1);
817 }
818 
820  typedef decltype(this->a_fad_) FadType;
821  auto a_fad = this->a_fad_;
822  auto c_fad = this->c_fad_;
823  auto n = this->n_;
824 
825  c_fad = std::atanh(a_fad);
826  FadType t1(n, std::atanh(a_fad.val()));
827  for (int i=0; i<n; i++)
828  t1.fastAccessDx(i) = a_fad.dx(i)/(1.0 - a_fad.val()*a_fad.val());
829  COMPARE_FADS(c_fad, t1);
830 }
831 
833  typedef decltype(this->a_fad_) FadType;
834  auto a_fad = this->a_fad_;
835  auto c_fad = this->c_fad_;
836  auto n = this->n_;
837 
838  c_fad = std::abs(a_fad);
839  FadType t1(n, std::abs(a_fad.val()));
840  for (int i=0; i<n; i++) {
841  if (a_fad.val() >= 0)
842  t1.fastAccessDx(i) = a_fad.dx(i);
843  else
844  t1.fastAccessDx(i) = -a_fad.dx(i);
845  }
846  COMPARE_FADS(c_fad, t1);
847 }
848 
850  typedef decltype(this->a_fad_) FadType;
851  auto a_fad = this->a_fad_;
852  auto c_fad = this->c_fad_;
853  auto n = this->n_;
854 
855  c_fad = std::fabs(a_fad);
856  FadType t1(n, std::fabs(a_fad.val()));
857  for (int i=0; i<n; i++) {
858  if (a_fad.val() >= 0)
859  t1.fastAccessDx(i) = a_fad.dx(i);
860  else
861  t1.fastAccessDx(i) = -a_fad.dx(i);
862  }
863  COMPARE_FADS(c_fad, t1);
864 }
865 
867  typedef decltype(this->a_fad_) FadType;
868  auto a_fad = this->a_fad_;
869  auto c_fad = this->c_fad_;
870  auto n = this->n_;
871 
872  c_fad = std::cbrt(a_fad);
873  FadType t1(n, std::cbrt(a_fad.val()));
874  for (int i=0; i<n; i++)
875  t1.fastAccessDx(i) =
876  a_fad.dx(i)/(3.*std::cbrt(a_fad.val()*a_fad.val()));
877  COMPARE_FADS(c_fad, t1);
878 }
879 
881  typedef decltype(this->a_fad_) FadType;
882  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
883  auto a_fad = this->a_fad_;
884  auto b_fad = this->b_fad_;
885  auto c_fad = this->c_fad_;
886  auto n = this->n_;
887 
888  c_fad = std::atan2(a_fad, b_fad);
889  FadType t1(n, std::atan2(a_fad.val(),b_fad.val()));
890  ScalarType t = a_fad.val()*a_fad.val() +
891  b_fad.val()*b_fad.val();
892  for (int i=0; i<n; i++)
893  t1.fastAccessDx(i) = (b_fad.val()*a_fad.dx(i) -
894  a_fad.val()*b_fad.dx(i))/t;
895  COMPARE_FADS(c_fad, t1);
896 
897  ScalarType val = this->urand.number();
898  c_fad = std::atan2(a_fad, val);
899  FadType t2(n, std::atan2(a_fad.val(), val));
900  t = a_fad.val()*a_fad.val() + val*val;
901  for (int i=0; i<n; i++)
902  t2.fastAccessDx(i) = val*a_fad.dx(i)/t;
903  COMPARE_FADS(c_fad, t2);
904 
905  c_fad = std::atan2(val, b_fad);
906  FadType t3(n, std::atan2(val, b_fad.val()));
907  t = val*val + b_fad.val()*b_fad.val();
908  for (int i=0; i<n; i++)
909  t3.fastAccessDx(i) = -val*b_fad.dx(i)/t;
910  COMPARE_FADS(c_fad, t3);
911 }
912 
914  typedef decltype(this->a_fad_) FadType;
915  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
916  auto a_fad = this->a_fad_;
917  auto b_fad = this->b_fad_;
918  auto c_fad = this->c_fad_;
919  auto n = this->n_;
920 
921  ScalarType val;
922 
923  // Fad, Fad
924  FadType aa_fad = a_fad + 1.0;
925  c_fad = max(aa_fad, a_fad);
926  COMPARE_FADS(c_fad, aa_fad);
927  c_fad = max(a_fad, aa_fad);
928  COMPARE_FADS(c_fad, aa_fad);
929 
930  // Expr, Fad
931  c_fad = max(a_fad+1.0, a_fad);
932  COMPARE_FADS(c_fad, aa_fad);
933  c_fad = max(a_fad, a_fad+1.0);
934  COMPARE_FADS(c_fad, aa_fad);
935 
936  // Expr, Expr (same)
937  c_fad = max(a_fad+1.0, a_fad+1.0);
938  COMPARE_FADS(c_fad, aa_fad);
939 
940  // Expr, Expr (different)
941  c_fad = max(a_fad+1.0, a_fad-1.0);
942  COMPARE_FADS(c_fad, aa_fad);
943  c_fad = max(a_fad-1.0, a_fad+1.0);
944  COMPARE_FADS(c_fad, aa_fad);
945 
946  // Fad, const
947  val = a_fad.val() + 1;
948  c_fad = max(a_fad, val);
949  COMPARE_VALUES(c_fad.val(), val);
950  for (int i=0; i<n; i++)
951  COMPARE_VALUES(c_fad.dx(i), 0.0);
952  val = a_fad.val() - 1;
953  c_fad = max(a_fad, val);
954  COMPARE_FADS(c_fad, a_fad);
955  val = b_fad.val() + 1;
956  c_fad = max(val, b_fad);
957  COMPARE_VALUES(c_fad.val(), val);
958  for (int i=0; i<n; i++)
959  COMPARE_VALUES(c_fad.dx(i), 0.0);
960  val = b_fad.val() - 1;
961  c_fad = max(val, b_fad);
962  COMPARE_FADS(c_fad, b_fad);
963 
964  // Expr, const
965  val = a_fad.val();
966  c_fad = max(a_fad+1.0, val);
967  COMPARE_FADS(c_fad, aa_fad);
968  c_fad = max(val, a_fad+1.0);
969  COMPARE_FADS(c_fad, aa_fad);
970 }
971 
973  typedef decltype(this->a_fad_) FadType;
974  typedef typename Sacado::ScalarType<FadType>::type ScalarType;
975  auto a_fad = this->a_fad_;
976  auto b_fad = this->b_fad_;
977  auto c_fad = this->c_fad_;
978  auto n = this->n_;
979 
980  ScalarType val;
981 
982  // Fad, Fad
983  FadType aa_fad = a_fad - 1.0;
984  c_fad = min(aa_fad, a_fad);
985  COMPARE_FADS(c_fad, aa_fad);
986  c_fad = min(a_fad, aa_fad);
987  COMPARE_FADS(c_fad, aa_fad);
988 
989  // Expr, Fad
990  c_fad = min(a_fad-1.0, a_fad);
991  COMPARE_FADS(c_fad, aa_fad);
992  c_fad = min(a_fad, a_fad-1.0);
993  COMPARE_FADS(c_fad, aa_fad);
994 
995  // Expr, Expr (same)
996  c_fad = min(a_fad-1.0, a_fad-1.0);
997  COMPARE_FADS(c_fad, aa_fad);
998 
999  // Expr, Expr (different)
1000  c_fad = min(a_fad+1.0, a_fad-1.0);
1001  COMPARE_FADS(c_fad, aa_fad);
1002  c_fad = min(a_fad-1.0, a_fad+1.0);
1003  COMPARE_FADS(c_fad, aa_fad);
1004 
1005  // Fad, const
1006  val = a_fad.val() - 1;
1007  c_fad = min(a_fad, val);
1008  COMPARE_VALUES(c_fad.val(), val);
1009  for (int i=0; i<n; i++)
1010  COMPARE_VALUES(c_fad.dx(i), 0.0);
1011  val = a_fad.val() + 1;
1012  c_fad = min(a_fad, val);
1013  COMPARE_FADS(c_fad, a_fad);
1014  val = b_fad.val() - 1;
1015  c_fad = min(val, b_fad);
1016  COMPARE_VALUES(c_fad.val(), val);
1017  for (int i=0; i<n; i++)
1018  COMPARE_VALUES(c_fad.dx(i), 0.0);
1019  val = b_fad.val() + 1;
1020  c_fad = min(val, b_fad);
1021  COMPARE_FADS(c_fad, b_fad);
1022 
1023  // Expr, const
1024  val = a_fad.val();
1025  c_fad = min(a_fad-1.0, val);
1026  COMPARE_FADS(c_fad, aa_fad);
1027  c_fad = min(val, a_fad-1.0);
1028  COMPARE_FADS(c_fad, aa_fad);
1029 }
1030 
1033  testAddition,
1034  testSubtraction,
1035  testMultiplication,
1036  testDivision,
1037  testEquals,
1038  testNotEquals,
1039  testUnaryPlus,
1040  testUnaryMinus,
1041  testExp,
1042  testLog,
1043  testLog10,
1044  testSqrt,
1045  testCos,
1046  testSin,
1047  testTan,
1048  testCosh,
1049  testSinh,
1050  testTanh,
1051  testPlusEquals,
1052  testMinusEquals,
1053  testTimesEquals,
1054  testDivideEquals,
1055  testPow,
1056  testEqualsLR,
1057  testPlusEqualsLR,
1058  testMinusEqualsLR,
1059  testTimesEqualsLR,
1060  testDivideEqualsLR,
1061  testResizeBug6135,
1062  testEquality,
1063  testEqualityConstL,
1064  testEqualityConstR);
1065 
1068  testLessThanOrEquals,
1069  testGreaterThanOrEquals,
1070  testLessThan,
1071  testGreaterThan,
1072  testACos,
1073  testASin,
1074  testATan,
1075  testACosh,
1076  testASinh,
1077  testATanh,
1078  testAbs,
1079  testFAbs,
1080  testCbrt,
1081  testATan2,
1082  testMax,
1083  testMin
1084  );
1085 
1086 #endif // FADUNITTESTS2_HPP
cbrt(expr.val())
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)
asinh(expr.val())
asin(expr.val())
cosh(expr.val())
abs(expr.val())
TYPED_TEST_P(FadBLASUnitTests, testSCAL1)
#define COMPARE_VALUES(a, b)
Definition: GTestUtils.hpp:89
atanh(expr.val())
Sacado::Fad::DFad< double > FadType
atan(expr.val())
expr val()
Sacado::ScalarType< FadType >::type ScalarType
tanh(expr.val())
#define ASSERT_TRUE(condition)
Definition: gtest.h:1985
SimpleFad< ValueT > min(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
sqrt(expr.val())
sinh(expr.val())
tan(expr.val())
Sacado::Random< ScalarType > urand
void SetUp() override
atan2(expr1.val(), expr2.val())
sin(expr.val())
TYPED_TEST_SUITE_P(FadBLASUnitTests)
log(expr.val())
acosh(expr.val())
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)
A random number generator that generates random numbers uniformly distributed in the interval (a...
void TearDown() override
exp(expr.val())
fabs(expr.val())
#define COMPARE_FADS(a, b)
Definition: GTestUtils.hpp:92
int n
log10(expr.val())
cos(expr.val())