30 #ifndef FADUNITTESTS2_HPP
31 #define FADUNITTESTS2_HPP
38 #include <cppunit/extensions/HelperMacros.h>
40 #define COMPARE_VALUES(a, b) \
41 CPPUNIT_ASSERT( std::abs(a-b) < this->tol_a + this->tol_r*std::abs(a) );
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)); \
56 template <
class FadType,
class ScalarType>
109 double relative_tolerance);
172 template <
class FadType,
class ScalarType>
175 urand(),
n(5), tol_a(1.0e-15), tol_r(1.0e-14) {}
177 template <
class FadType,
class ScalarType>
180 double relative_tolerance) :
183 tol_a(absolute_tolerance),
184 tol_r(relative_tolerance) {}
186 template <
class FadType,
class ScalarType>
190 val = urand.number();
193 val = urand.number();
196 for (
int i=0; i<
n; i++) {
197 val = urand.number();
198 a_fad.fastAccessDx(i) =
val;
200 val = urand.number();
201 b_fad.fastAccessDx(i) =
val;
205 template <
class FadType,
class ScalarType>
209 template <
class FadType,
class ScalarType>
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);
219 ScalarType
val = urand.number();
222 for (
int i=0; i<n; i++)
223 t2.fastAccessDx(i) = a_fad.dx(i);
227 FadType t3(n, val+b_fad.val());
228 for (
int i=0; i<n; i++)
229 t3.fastAccessDx(i) = b_fad.dx(i);
233 template <
class FadType,
class ScalarType>
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);
243 ScalarType
val = urand.number();
246 for (
int i=0; i<n; i++)
247 t2.fastAccessDx(i) = a_fad.dx(i);
251 FadType t3(n, val-b_fad.val());
252 for (
int i=0; i<n; i++)
253 t3.fastAccessDx(i) = -b_fad.dx(i);
257 template <
class FadType,
class ScalarType>
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);
267 ScalarType
val = urand.number();
270 for (
int i=0; i<n; i++)
271 t2.fastAccessDx(i) = a_fad.dx(i)*
val;
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);
281 template <
class FadType,
class ScalarType>
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++)
289 (a_fad.dx(i)*b_fad.val() - a_fad.val()*b_fad.dx(i)) /
290 (b_fad.val()*b_fad.val());
293 ScalarType
val = urand.number();
296 for (
int i=0; i<n; i++)
297 t2.fastAccessDx(i) = a_fad.dx(i)/
val;
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());
307 template <
class FadType,
class ScalarType>
311 bool r1 = a_fad == b_fad;
312 bool r2 = a_fad.val() == b_fad.val();
313 CPPUNIT_ASSERT(r1 == r2);
315 ScalarType
val = urand.number();
317 r2 = a_fad.val() ==
val;
318 CPPUNIT_ASSERT(r1 == r2);
321 r2 = val == b_fad.val();
322 CPPUNIT_ASSERT(r1 == r2);
325 template <
class FadType,
class ScalarType>
329 bool r1 = a_fad != b_fad;
330 bool r2 = a_fad.val() != b_fad.val();
331 CPPUNIT_ASSERT(r1 == r2);
333 ScalarType
val = urand.number();
335 r2 = a_fad.val() !=
val;
336 CPPUNIT_ASSERT(r1 == r2);
339 r2 = val != b_fad.val();
340 CPPUNIT_ASSERT(r1 == r2);
343 template <
class FadType,
class ScalarType>
349 for (
int i=0; i<
n; i++)
350 t1.fastAccessDx(i) = a_fad.dx(i);
354 template <
class FadType,
class ScalarType>
360 for (
int i=0; i<
n; i++)
361 t1.fastAccessDx(i) = -a_fad.dx(i);
365 template <
class FadType,
class ScalarType>
371 for (
int i=0; i<
n; i++)
372 t1.fastAccessDx(i) =
std::exp(a_fad.val())*a_fad.dx(i);
376 template <
class FadType,
class ScalarType>
382 for (
int i=0; i<
n; i++)
383 t1.fastAccessDx(i) = a_fad.dx(i)/a_fad.val();
387 template <
class FadType,
class ScalarType>
393 for (
int i=0; i<
n; i++)
394 t1.fastAccessDx(i) = a_fad.dx(i)/(a_fad.val()*
std::log(10));
398 template <
class FadType,
class ScalarType>
404 for (
int i=0; i<
n; i++)
405 t1.fastAccessDx(i) = a_fad.dx(i)/(2.*
std::sqrt(a_fad.val()));
409 template <
class FadType,
class ScalarType>
415 for (
int i=0; i<
n; i++)
416 t1.fastAccessDx(i) = -
std::sin(a_fad.val())*a_fad.dx(i);
420 template <
class FadType,
class ScalarType>
426 for (
int i=0; i<
n; i++)
427 t1.fastAccessDx(i) =
std::cos(a_fad.val())*a_fad.dx(i);
431 template <
class FadType,
class ScalarType>
437 for (
int i=0; i<
n; i++)
443 template <
class FadType,
class ScalarType>
449 for (
int i=0; i<
n; i++)
450 t1.fastAccessDx(i) =
std::sinh(a_fad.val())*a_fad.dx(i);
454 template <
class FadType,
class ScalarType>
460 for (
int i=0; i<
n; i++)
461 t1.fastAccessDx(i) =
std::cosh(a_fad.val())*a_fad.dx(i);
465 template <
class FadType,
class ScalarType>
471 for (
int i=0; i<
n; i++)
477 template <
class FadType,
class ScalarType>
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);
487 ScalarType
val = urand.number();
489 for (
int i=0; i<n; i++)
490 t2.fastAccessDx(i) = c_fad.dx(i);
495 template <
class FadType,
class ScalarType>
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);
505 ScalarType
val = urand.number();
507 for (
int i=0; i<n; i++)
508 t2.fastAccessDx(i) = c_fad.dx(i);
513 template <
class FadType,
class ScalarType>
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();
523 ScalarType
val = urand.number();
525 for (
int i=0; i<n; i++)
526 t2.fastAccessDx(i) = c_fad.dx(i)*
val;
531 template <
class FadType,
class ScalarType>
535 FadType t1(
n, c_fad.val()/a_fad.val());
536 for (
int i=0; i<
n; i++)
538 (a_fad.dx(i)*c_fad.val() - c_fad.dx(i)*a_fad.val()) /
539 (a_fad.val()*a_fad.val());
543 ScalarType
val = urand.number();
545 for (
int i=0; i<n; i++)
546 t2.fastAccessDx(i) = c_fad.dx(i)/
val;
551 template <
class FadType,
class ScalarType>
557 for (
int i=0; i<
n; i++)
559 std::pow(a_fad.val(),b_fad.val())*(b_fad.val()*a_fad.dx(i)/a_fad.val() +
563 ScalarType
val = urand.number();
566 for (
int i=0; i<n; i++)
568 std::pow(a_fad.val(),
val)*(val*a_fad.dx(i)/a_fad.val());
573 for (
int i=0; i<n; i++)
581 for (
int i=0; i<n; i++)
582 t4.fastAccessDx(i) = 0.0;
587 for (
int i=0; i<n; i++)
588 t5.fastAccessDx(i) = 0.0;
595 for (
int i=0; i<n; i++)
596 t6.fastAccessDx(i) = 0.0;
603 for (
int i=0; i<n; 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));
610 template <
class FadType,
class ScalarType>
616 aa_fad = aa_fad + b_fad;
621 template <
class FadType,
class ScalarType>
627 aa_fad += aa_fad + b_fad;
628 c_fad = 1.0 + 1.0 + b_fad;
632 template <
class FadType,
class ScalarType>
638 aa_fad -= aa_fad + b_fad;
639 c_fad = 1.0 - 1.0 - b_fad;
643 template <
class FadType,
class ScalarType>
649 aa_fad *= aa_fad + b_fad;
650 c_fad = 2.0 * (2.0 + b_fad);
654 template <
class FadType,
class ScalarType>
660 aa_fad /= aa_fad + b_fad;
661 c_fad = 2.0 / (2.0 + b_fad);
665 template <
class FadType,
class ScalarType>
669 FadType d_fad = ScalarType(1.0);
670 d_fad = d_fad + a_fad;
675 template <
class FadType,
class ScalarType>
684 if (aa_fad == bb_fad*bb_fad)
690 template <
class FadType,
class ScalarType>
697 if (ScalarType(9.0) == bb_fad*bb_fad)
703 template <
class FadType,
class ScalarType>
710 if (bb_fad*bb_fad == ScalarType(9.0))
719 template <
class FadType,
class ScalarType>
762 #ifdef HAVE_SACADO_CXX11
790 double relative_tolerance) :
806 #ifdef HAVE_SACADO_CXX11
815 template <
class FadType,
class ScalarType>
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);
823 ScalarType
val = this->urand.number();
824 r1 = this->a_fad <=
val;
825 r2 = this->a_fad.val() <=
val;
826 CPPUNIT_ASSERT(r1 == r2);
828 r1 = val <= this->b_fad;
829 r2 = val <= this->b_fad.val();
830 CPPUNIT_ASSERT(r1 == r2);
833 template <
class FadType,
class ScalarType>
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);
841 ScalarType
val = this->urand.number();
842 r1 = this->a_fad >=
val;
843 r2 = this->a_fad.val() >=
val;
844 CPPUNIT_ASSERT(r1 == r2);
846 r1 = val >= this->b_fad;
847 r2 = val >= this->b_fad.val();
848 CPPUNIT_ASSERT(r1 == r2);
851 template <
class FadType,
class ScalarType>
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);
859 ScalarType
val = this->urand.number();
860 r1 = this->a_fad <
val;
861 r2 = this->a_fad.val() <
val;
862 CPPUNIT_ASSERT(r1 == r2);
864 r1 = val < this->b_fad;
865 r2 = val < this->b_fad.val();
866 CPPUNIT_ASSERT(r1 == r2);
869 template <
class FadType,
class ScalarType>
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);
877 ScalarType
val = this->urand.number();
878 r1 = this->a_fad >
val;
879 r2 = this->a_fad.val() >
val;
880 CPPUNIT_ASSERT(r1 == r2);
882 r1 = val > this->b_fad;
883 r2 = val > this->b_fad.val();
884 CPPUNIT_ASSERT(r1 == r2);
887 template <
class FadType,
class ScalarType>
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());
898 template <
class FadType,
class ScalarType>
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());
909 template <
class FadType,
class ScalarType>
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());
920 template <
class FadType,
class ScalarType>
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);
934 template <
class FadType,
class ScalarType>
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);
945 template <
class FadType,
class ScalarType>
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());
956 template <
class FadType,
class ScalarType>
960 this->c_fad =
std::abs(this->a_fad);
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);
966 t1.fastAccessDx(i) = -this->a_fad.dx(i);
971 template <
class FadType,
class ScalarType>
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);
981 t1.fastAccessDx(i) = -this->a_fad.dx(i);
986 #ifdef HAVE_SACADO_CXX11
987 template <
class FadType,
class ScalarType>
993 for (
int i=0; i<this->
n; i++)
995 this->a_fad.dx(i)/(3.*
std::cbrt(this->a_fad.val()*this->a_fad.val()));
1000 template <
class FadType,
class ScalarType>
1004 this->c_fad =
std::atan2(this->a_fad, this->b_fad);
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;
1013 ScalarType
val = this->urand.number();
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;
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;
1029 template <
class FadType,
class ScalarType>
1036 FadType aa_fad = this->a_fad + 1.0;
1037 this->c_fad =
max(aa_fad, this->a_fad);
1039 this->c_fad =
max(this->a_fad, aa_fad);
1043 this->c_fad =
max(this->a_fad+1.0, this->a_fad);
1045 this->c_fad =
max(this->a_fad, this->a_fad+1.0);
1049 this->c_fad =
max(this->a_fad+1.0, this->a_fad+1.0);
1053 this->c_fad =
max(this->a_fad+1.0, this->a_fad-1.0);
1055 this->c_fad =
max(this->a_fad-1.0, this->a_fad+1.0);
1059 val = this->a_fad.val() + 1;
1060 this->c_fad =
max(this->a_fad, val);
1062 for (
int i=0; i<this->n; i++)
1064 val = this->a_fad.val() - 1;
1065 this->c_fad =
max(this->a_fad, val);
1067 val = this->b_fad.val() + 1;
1068 this->c_fad =
max(val, this->b_fad);
1070 for (
int i=0; i<this->n; i++)
1072 val = this->b_fad.val() - 1;
1073 this->c_fad =
max(val, this->b_fad);
1077 val = this->a_fad.val();
1078 this->c_fad =
max(this->a_fad+1.0, val);
1080 this->c_fad =
max(val, this->a_fad+1.0);
1084 template <
class FadType,
class ScalarType>
1091 FadType aa_fad = this->a_fad - 1.0;
1092 this->c_fad =
min(aa_fad, this->a_fad);
1094 this->c_fad =
min(this->a_fad, aa_fad);
1098 this->c_fad =
min(this->a_fad-1.0, this->a_fad);
1100 this->c_fad =
min(this->a_fad, this->a_fad-1.0);
1104 this->c_fad =
min(this->a_fad-1.0, this->a_fad-1.0);
1108 this->c_fad =
min(this->a_fad+1.0, this->a_fad-1.0);
1110 this->c_fad =
min(this->a_fad-1.0, this->a_fad+1.0);
1114 val = this->a_fad.val() - 1;
1115 this->c_fad =
min(this->a_fad, val);
1117 for (
int i=0; i<this->n; i++)
1119 val = this->a_fad.val() + 1;
1120 this->c_fad =
min(this->a_fad, val);
1122 val = this->b_fad.val() - 1;
1123 this->c_fad =
min(val, this->b_fad);
1125 for (
int i=0; i<this->n; i++)
1127 val = this->b_fad.val() + 1;
1128 this->c_fad =
min(val, this->b_fad);
1132 val = this->a_fad.val();
1133 this->c_fad =
min(this->a_fad-1.0, val);
1135 this->c_fad =
min(val, this->a_fad-1.0);
1139 #undef COMPARE_VALUES
1142 #endif // FADUNITTESTS2_HPP
#define COMPARE_FADS(a, b)
CPPUNIT_TEST(testAddition)
Sacado::Fad::DFad< double > FadType
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)
CPPUNIT_TEST_SUITE(RealFadOpsUnitTest2)
SimpleFad< ValueT > min(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
CPPUNIT_TEST_SUITE(FadOpsUnitTest2)
Sacado::Random< ScalarType > urand
void testLessThanOrEquals()
atan2(expr1.val(), expr2.val())
void testDivideEqualsLR()
void testEqualityConstL()
CPPUNIT_TEST(testAddition)
SimpleFad< ValueT > max(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
void testMultiplication()
void testEqualityConstR()
void testGreaterThanOrEquals()
RealFadOpsUnitTest2(int numComponents, double absolute_tolerance, double relative_tolerance)
#define COMPARE_VALUES(a, b)