30 #ifndef TAYLORUNITTESTS_HPP
31 #define TAYLORUNITTESTS_HPP
34 #include "adolc/adouble.h"
35 #include "adolc/interfaces.h"
41 inline adouble
max(
const adouble&
a,
const adouble& b) {
return fmax(a,b); }
42 inline adouble
max(
const adouble&
a,
double v) {
return fmax(a,v); }
43 inline adouble
max(
double v,
const adouble& b) {
return fmax(v,b); }
44 inline adouble
min(
const adouble&
a,
const adouble& b) {
return fmin(a,b); }
45 inline adouble
min(
const adouble&
a,
double v) {
return fmin(a,v); }
46 inline adouble
min(
double v,
const adouble& b) {
return fmin(v,b); }
49 #include <cppunit/extensions/HelperMacros.h>
51 #define COMPARE_DOUBLES(a, b) \
52 CPPUNIT_ASSERT( fabs(a-b) < tol_a + tol_r*fabs(a) );
54 #define COMPARE_POLYS(x_dtay, x_adolc) \
55 CPPUNIT_ASSERT(x_dtay.degree() == d); \
56 for (int i=0; i<=d; i++) { \
57 COMPARE_DOUBLES(x_dtay.coeff(i), x_adolc[i]); \
61 #define COMPARE_TAYS(x_dtay, y_dtay) \
62 CPPUNIT_ASSERT(x_dtay.degree() == y_dtay.degree()); \
63 for (int i=0; i<=x_dtay.degree(); i++) { \
64 COMPARE_DOUBLES(x_dtay.coeff(i), y_dtay.coeff(i)); \
68 #define BINARY_OP2_TEST(TESTNAME,OP) \
70 c_dtay = a_dtay OP b_dtay; \
78 forward(0,1,2,d,0,X,Y); \
79 COMPARE_POLYS(c_dtay,Y[0]); \
82 #define BINARY_OPRC_TEST(TESTNAME,OP) \
84 double val = urand.number(); \
85 c_dtay = a_dtay OP val; \
92 forward(0,1,1,d,0,X,Y); \
93 COMPARE_POLYS(c_dtay,Y[0]); \
96 #define BINARY_OPLC_TEST(TESTNAME,OP) \
98 double val = urand.number(); \
99 c_dtay = val OP a_dtay; \
106 forward(0,1,1,d,0,X,Y); \
107 COMPARE_POLYS(c_dtay,Y[0]); \
110 #define BINARY_OP_TEST(TESTNAME,OP) \
111 BINARY_OP2_TEST(TESTNAME,OP); \
112 BINARY_OPLC_TEST(TESTNAME ## LeftConstant,OP); \
113 BINARY_OPRC_TEST(TESTNAME ## RightConstant,OP)
115 #define CPPUNIT_BINARY_OP_TEST(TESTNAME) \
116 CPPUNIT_TEST(TESTNAME); \
117 CPPUNIT_TEST(TESTNAME ## LeftConstant); \
118 CPPUNIT_TEST(TESTNAME ## RightConstant)
120 #define RELOP_OP2_TEST(TESTNAME,OP) \
122 bool r1 = a_dtay OP b_dtay; \
123 bool r2 = a_dtay.coeff(0) OP b_dtay.coeff(0); \
124 CPPUNIT_ASSERT(r1 == r2); \
127 #define RELOP_OPLC_TEST(TESTNAME,OP) \
129 double val = urand.number(); \
130 bool r1 = val OP b_dtay; \
131 bool r2 = val OP b_dtay.coeff(0); \
132 CPPUNIT_ASSERT(r1 == r2); \
135 #define RELOP_OPRC_TEST(TESTNAME,OP) \
137 double val = urand.number(); \
138 bool r1 = a_dtay OP val; \
139 bool r2 = a_dtay.coeff(0) OP val; \
140 CPPUNIT_ASSERT(r1 == r2); \
143 #define RELOP_OP_TEST(TESTNAME,OP) \
144 RELOP_OP2_TEST(TESTNAME,OP); \
145 RELOP_OPLC_TEST(TESTNAME ## LeftConstant,OP); \
146 RELOP_OPRC_TEST(TESTNAME ## RightConstant,OP)
148 #define CPPUNIT_RELOP_OP_TEST(TESTNAME) \
149 CPPUNIT_TEST(TESTNAME); \
150 CPPUNIT_TEST(TESTNAME ## LeftConstant); \
151 CPPUNIT_TEST(TESTNAME ## RightConstant)
153 #define BINARY_FUNC2_TEST(TESTNAME,FUNC) \
155 c_dtay = FUNC (a_dtay, b_dtay); \
157 adouble aa, ab, ac; \
160 ac = FUNC (aa, ab); \
163 forward(0,1,2,d,0,X,Y); \
164 COMPARE_POLYS(c_dtay,Y[0]); \
167 #define BINARY_FUNCRC_TEST(TESTNAME,FUNC) \
169 double val = urand.number(); \
170 c_dtay = FUNC (a_dtay, val); \
174 ac = FUNC (aa, val); \
177 forward(0,1,1,d,0,X,Y); \
178 COMPARE_POLYS(c_dtay,Y[0]); \
181 #define BINARY_FUNCLC_TEST(TESTNAME,FUNC) \
183 double val = urand.number(); \
184 c_dtay = FUNC (val, a_dtay); \
188 ac = FUNC (val, aa); \
191 forward(0,1,1,d,0,X,Y); \
192 COMPARE_POLYS(c_dtay,Y[0]); \
195 #define BINARY_FUNC_TEST(TESTNAME,FUNC) \
196 BINARY_FUNC2_TEST(TESTNAME,FUNC); \
197 BINARY_FUNCLC_TEST(TESTNAME ## LeftConstant,FUNC); \
198 BINARY_FUNCRC_TEST(TESTNAME ## RightConstant,FUNC)
200 #define CPPUNIT_BINARY_FUNC_TEST(TESTNAME) \
201 CPPUNIT_TEST(TESTNAME); \
202 CPPUNIT_TEST(TESTNAME ## LeftConstant); \
203 CPPUNIT_TEST(TESTNAME ## RightConstant)
205 #define UNARY_OP_TEST(TESTNAME,OP) \
207 c_dtay = OP a_dtay; \
214 forward(0,1,1,d,0,X,Y); \
215 COMPARE_POLYS(c_dtay,Y[0]); \
218 #define UNARY_FUNC_TEST(TESTNAME,FUNC) \
220 c_dtay = FUNC (a_dtay); \
227 forward(0,1,1,d,0,X,Y); \
228 COMPARE_POLYS(c_dtay,Y[0]); \
231 #define UNARY_ASSIGNOP2_TEST(TESTNAME,OP) \
236 adouble aa, ab, ac; \
243 forward(0,1,2,d,0,X,Y); \
244 COMPARE_POLYS(c_dtay,Y[0]); \
247 #define UNARY_ASSIGNOPRC_TEST(TESTNAME,OP) \
249 double val = urand.number(); \
259 forward(0,1,1,d,0,X,Y); \
260 COMPARE_POLYS(c_dtay,Y[0]); \
263 #define UNARY_ASSIGNOPLC_TEST(TESTNAME,OP) \
265 double val = urand.number(); \
275 forward(0,1,1,d,0,X,Y); \
276 COMPARE_POLYS(c_dtay,Y[0]); \
279 #define UNARY_ASSIGNOP_TEST(TESTNAME,OP) \
280 UNARY_ASSIGNOP2_TEST(TESTNAME,OP); \
281 UNARY_ASSIGNOPLC_TEST(TESTNAME ## LeftConstant,OP); \
282 UNARY_ASSIGNOPRC_TEST(TESTNAME ## RightConstant,OP)
284 #define CPPUNIT_UNARY_ASSIGNOP_TEST(TESTNAME) \
285 CPPUNIT_TEST(TESTNAME); \
286 CPPUNIT_TEST(TESTNAME ## LeftConstant); \
287 CPPUNIT_TEST(TESTNAME ## RightConstant)
290 template <
class TaylorType>
346 double relative_tolerance);
393 template <
typename ScalarT>
395 ScalarT t1 = 3. * a +
sin(b) /
log(
fabs(a - b * 7.));
401 t1 /=
cosh(b - 0.7) + 7.*
sinh(t1 + 0.8)*
tanh(9./(a+1.)) - 9.;
416 forward(0,1,2,
d,0,
X,
Y);
423 for (
int i=1; i<=
d; ++i)
457 template <
class TaylorType>
459 urand(), d(5), tol_a(1.0e-11), tol_r(1.0e-10)
462 X[0] =
new double[
d+1];
463 X[1] =
new double[
d+1];
466 Y[0] =
new double[
d+1];
469 template <
class TaylorType>
471 double absolute_tolerance,
472 double relative_tolerance) :
475 tol_a(absolute_tolerance),
476 tol_r(relative_tolerance)
479 X[0] =
new double[
d+1];
480 X[1] =
new double[
d+1];
483 Y[0] =
new double[
d+1];
486 template <
class TaylorType>
497 template <
class TaylorType>
504 for (
int i=0; i<=d; i++) {
505 val = urand.number();
506 a_dtay.fastAccessCoeff(i) =
val;
509 val = urand.number();
510 b_dtay.fastAccessCoeff(i) =
val;
517 template <
class TaylorType>
520 template <
class TaylorType>
522 std::cout.setf(std::ios::fixed,std::ios::floatfield);
526 for (
int i=0; i<=d; i++) {
534 template <
class TaylorType>
537 std::cout.setf(std::ios::scientific,std::ios::floatfield);
541 for (
int i=0; i<=d; i++) {
543 std::cout << x_dtay.
coeff(i) - x[i] <<
" ";
605 double relative_tolerance) :
608 relative_tolerance) {}
617 #endif // TAYLORUNITTESTS_HPP
CPPUNIT_BINARY_FUNC_TEST(testPow)
CPPUNIT_UNARY_ASSIGNOP_TEST(testPlusEquals)
BINARY_OP_TEST(testAddition,+)
~CacheTaylorOpsUnitTest()
CPPUNIT_TEST(testUnaryPlus)
CPPUNIT_TEST_SUITE(CacheTaylorOpsUnitTest)
BINARY_FUNC_TEST(testPow, pow)
UNARY_OP_TEST(testUnaryPlus,+)
CPPUNIT_RELOP_OP_TEST(testEquals)
CPPUNIT_TEST(testUnaryPlus)
Sacado::Random< double > urand
#define COMPARE_TAYS(x_dtay, y_dtay)
Sacado::Tay::Taylor< double > TaylorType
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)
CacheTaylorOpsUnitTest(int degree, double absolute_tolerance, double relative_tolerance)
CPPUNIT_UNARY_ASSIGNOP_TEST(testPlusEquals)
CPPUNIT_BINARY_OP_TEST(testAddition)
ScalarT composite1(const ScalarT &a, const ScalarT &b)
CacheTaylor< T > diff(const CacheTaylor< T > &x, int n=1)
Compute Taylor series of n-th derivative of x.
T & fastAccessCoeff(int i)
Returns degree i term without bounds checking.
SimpleFad< ValueT > min(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
CPPUNIT_RELOP_OP_TEST(testEquals)
#define COMPARE_POLYS(x_dtay, x_adolc)
void print_diff(const TaylorType &x_dtay, double *x_adolc)
const T * coeff() const
Returns Taylor coefficient array.
void print_poly(double *x)
CPPUNIT_BINARY_FUNC_TEST(testPow)
UNARY_FUNC_TEST(testExp, exp)
SimpleFad< ValueT > max(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
CPPUNIT_TEST_SUITE(TaylorOpsUnitTest)
RELOP_OP_TEST(testEquals,==)
UNARY_ASSIGNOP_TEST(testPlusEquals,+=)
CPPUNIT_BINARY_OP_TEST(testAddition)