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