00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef CACHETAYLORUNITTESTS_HPP
00033 #define CACHETAYLORUNITTESTS_HPP
00034
00035
00036 #include "adouble.h"
00037 #include "interfaces.h"
00038
00039
00040 #include "Sacado.hpp"
00041 #include "Sacado_Random.hpp"
00042 #include "Sacado_Tay_CacheTaylor.hpp"
00043
00044 typedef Sacado::Tay::CacheTaylor<double> TaylorType;
00045
00046 inline adouble max(const adouble& a, const adouble& b) { return fmax(a,b); }
00047 inline adouble max(const adouble& a, double v) { return fmax(a,v); }
00048 inline adouble max(double v, const adouble& b) { return fmax(v,b); }
00049 inline adouble min(const adouble& a, const adouble& b) { return fmin(a,b); }
00050 inline adouble min(const adouble& a, double v) { return fmin(a,v); }
00051 inline adouble min(double v, const adouble& b) { return fmin(v,b); }
00052
00053
00054 #include <cppunit/extensions/HelperMacros.h>
00055
00056 using namespace std;
00057
00058 #define BINARY_OP2_TEST(TESTNAME,OP) \
00059 void TESTNAME () { \
00060 c_dtay = a_dtay OP b_dtay; \
00061 trace_on(0); \
00062 adouble aa, ab, ac; \
00063 aa <<= X[0][0]; \
00064 ab <<= X[1][0]; \
00065 ac = aa OP ab; \
00066 ac >>= Y[0][0]; \
00067 trace_off(); \
00068 forward(0,1,2,d,0,X,Y); \
00069 comparePolys(c_dtay,Y[0]); \
00070 }
00071
00072 #define BINARY_OPRC_TEST(TESTNAME,OP) \
00073 void TESTNAME () { \
00074 double val = urand.number(); \
00075 c_dtay = a_dtay OP val; \
00076 trace_on(0); \
00077 adouble aa, ac; \
00078 aa <<= X[0][0]; \
00079 ac = aa OP val; \
00080 ac >>= Y[0][0]; \
00081 trace_off(); \
00082 forward(0,1,1,d,0,X,Y); \
00083 comparePolys(c_dtay,Y[0]); \
00084 }
00085
00086 #define BINARY_OPLC_TEST(TESTNAME,OP) \
00087 void TESTNAME () { \
00088 double val = urand.number(); \
00089 c_dtay = val OP a_dtay; \
00090 trace_on(0); \
00091 adouble aa, ac; \
00092 aa <<= X[0][0]; \
00093 ac = val OP aa; \
00094 ac >>= Y[0][0]; \
00095 trace_off(); \
00096 forward(0,1,1,d,0,X,Y); \
00097 comparePolys(c_dtay,Y[0]); \
00098 }
00099
00100 #define BINARY_OP_TEST(TESTNAME,OP) \
00101 BINARY_OP2_TEST(TESTNAME,OP); \
00102 BINARY_OPLC_TEST(TESTNAME ## LeftConstant,OP); \
00103 BINARY_OPRC_TEST(TESTNAME ## RightConstant,OP)
00104
00105 #define CPPUNIT_BINARY_OP_TEST(TESTNAME) \
00106 CPPUNIT_TEST(TESTNAME); \
00107 CPPUNIT_TEST(TESTNAME ## LeftConstant); \
00108 CPPUNIT_TEST(TESTNAME ## RightConstant)
00109
00110 #define RELOP_OP2_TEST(TESTNAME,OP) \
00111 void TESTNAME () { \
00112 bool r1 = a_dtay OP b_dtay; \
00113 bool r2 = a_dtay.coeff(0) OP b_dtay.coeff(0); \
00114 CPPUNIT_ASSERT(r1 == r2); \
00115 }
00116
00117 #define RELOP_OPLC_TEST(TESTNAME,OP) \
00118 void TESTNAME () { \
00119 double val = urand.number(); \
00120 bool r1 = val OP b_dtay; \
00121 bool r2 = val OP b_dtay.coeff(0); \
00122 CPPUNIT_ASSERT(r1 == r2); \
00123 }
00124
00125 #define RELOP_OPRC_TEST(TESTNAME,OP) \
00126 void TESTNAME () { \
00127 double val = urand.number(); \
00128 bool r1 = a_dtay OP val; \
00129 bool r2 = a_dtay.coeff(0) OP val; \
00130 CPPUNIT_ASSERT(r1 == r2); \
00131 }
00132
00133 #define RELOP_OP_TEST(TESTNAME,OP) \
00134 RELOP_OP2_TEST(TESTNAME,OP); \
00135 RELOP_OPLC_TEST(TESTNAME ## LeftConstant,OP); \
00136 RELOP_OPRC_TEST(TESTNAME ## RightConstant,OP)
00137
00138 #define CPPUNIT_RELOP_OP_TEST(TESTNAME) \
00139 CPPUNIT_TEST(TESTNAME); \
00140 CPPUNIT_TEST(TESTNAME ## LeftConstant); \
00141 CPPUNIT_TEST(TESTNAME ## RightConstant)
00142
00143 #define BINARY_FUNC2_TEST(TESTNAME,FUNC) \
00144 void TESTNAME () { \
00145 c_dtay = FUNC (a_dtay, b_dtay); \
00146 trace_on(0); \
00147 adouble aa, ab, ac; \
00148 aa <<= X[0][0]; \
00149 ab <<= X[1][0]; \
00150 ac = FUNC (aa, ab); \
00151 ac >>= Y[0][0]; \
00152 trace_off(); \
00153 forward(0,1,2,d,0,X,Y); \
00154 comparePolys(c_dtay,Y[0]); \
00155 }
00156
00157 #define BINARY_FUNCRC_TEST(TESTNAME,FUNC) \
00158 void TESTNAME () { \
00159 double val = urand.number(); \
00160 c_dtay = FUNC (a_dtay, val); \
00161 trace_on(0); \
00162 adouble aa, ac; \
00163 aa <<= X[0][0]; \
00164 ac = FUNC (aa, val); \
00165 ac >>= Y[0][0]; \
00166 trace_off(); \
00167 forward(0,1,1,d,0,X,Y); \
00168 comparePolys(c_dtay,Y[0]); \
00169 }
00170
00171 #define BINARY_FUNCLC_TEST(TESTNAME,FUNC) \
00172 void TESTNAME () { \
00173 double val = urand.number(); \
00174 c_dtay = FUNC (val, a_dtay); \
00175 trace_on(0); \
00176 adouble aa, ac; \
00177 aa <<= X[0][0]; \
00178 ac = FUNC (val, aa); \
00179 ac >>= Y[0][0]; \
00180 trace_off(); \
00181 forward(0,1,1,d,0,X,Y); \
00182 comparePolys(c_dtay,Y[0]); \
00183 }
00184
00185 #define BINARY_FUNC_TEST(TESTNAME,FUNC) \
00186 BINARY_FUNC2_TEST(TESTNAME,FUNC); \
00187 BINARY_FUNCLC_TEST(TESTNAME ## LeftConstant,FUNC); \
00188 BINARY_FUNCRC_TEST(TESTNAME ## RightConstant,FUNC)
00189
00190 #define CPPUNIT_BINARY_FUNC_TEST(TESTNAME) \
00191 CPPUNIT_TEST(TESTNAME); \
00192 CPPUNIT_TEST(TESTNAME ## LeftConstant); \
00193 CPPUNIT_TEST(TESTNAME ## RightConstant)
00194
00195 #define UNARY_OP_TEST(TESTNAME,OP) \
00196 void TESTNAME () { \
00197 c_dtay = OP a_dtay; \
00198 trace_on(0); \
00199 adouble aa, ac; \
00200 aa <<= X[0][0]; \
00201 ac = OP aa; \
00202 ac >>= Y[0][0]; \
00203 trace_off(); \
00204 forward(0,1,1,d,0,X,Y); \
00205 comparePolys(c_dtay,Y[0]); \
00206 }
00207
00208 #define UNARY_FUNC_TEST(TESTNAME,FUNC) \
00209 void TESTNAME () { \
00210 c_dtay = FUNC (a_dtay); \
00211 trace_on(0); \
00212 adouble aa, ac; \
00213 aa <<= X[0][0]; \
00214 ac = FUNC (aa); \
00215 ac >>= Y[0][0]; \
00216 trace_off(); \
00217 forward(0,1,1,d,0,X,Y); \
00218 comparePolys(c_dtay,Y[0]); \
00219 }
00220
00221 #define UNARY_ASSIGNOP2_TEST(TESTNAME,OP) \
00222 void TESTNAME () { \
00223 c_dtay = a_dtay; \
00224 c_dtay OP b_dtay; \
00225 trace_on(0); \
00226 adouble aa, ab, ac; \
00227 aa <<= X[0][0]; \
00228 ab <<= X[1][0]; \
00229 ac = aa; \
00230 ac OP ab; \
00231 ac >>= Y[0][0]; \
00232 trace_off(); \
00233 forward(0,1,2,d,0,X,Y); \
00234 comparePolys(c_dtay,Y[0]); \
00235 }
00236
00237 #define UNARY_ASSIGNOPRC_TEST(TESTNAME,OP) \
00238 void TESTNAME () { \
00239 double val = urand.number(); \
00240 c_dtay = a_dtay; \
00241 c_dtay OP val; \
00242 trace_on(0); \
00243 adouble aa, ac; \
00244 aa <<= X[0][0]; \
00245 ac = aa; \
00246 ac OP val; \
00247 ac >>= Y[0][0]; \
00248 trace_off(); \
00249 forward(0,1,1,d,0,X,Y); \
00250 comparePolys(c_dtay,Y[0]); \
00251 }
00252
00253 #define UNARY_ASSIGNOPLC_TEST(TESTNAME,OP) \
00254 void TESTNAME () { \
00255 double val = urand.number(); \
00256 c_dtay = val; \
00257 c_dtay OP a_dtay; \
00258 trace_on(0); \
00259 adouble aa, ac; \
00260 aa <<= X[0][0]; \
00261 ac = val; \
00262 ac OP aa; \
00263 ac >>= Y[0][0]; \
00264 trace_off(); \
00265 forward(0,1,1,d,0,X,Y); \
00266 comparePolys(c_dtay,Y[0]); \
00267 }
00268
00269 #define UNARY_ASSIGNOP_TEST(TESTNAME,OP) \
00270 UNARY_ASSIGNOP2_TEST(TESTNAME,OP); \
00271 UNARY_ASSIGNOPLC_TEST(TESTNAME ## LeftConstant,OP); \
00272 UNARY_ASSIGNOPRC_TEST(TESTNAME ## RightConstant,OP)
00273
00274 #define CPPUNIT_UNARY_ASSIGNOP_TEST(TESTNAME) \
00275 CPPUNIT_TEST(TESTNAME); \
00276 CPPUNIT_TEST(TESTNAME ## LeftConstant); \
00277 CPPUNIT_TEST(TESTNAME ## RightConstant)
00278
00279
00280 class CacheTaylorOpsUnitTest : public CppUnit::TestFixture {
00281
00282 CPPUNIT_TEST_SUITE( CacheTaylorOpsUnitTest );
00283
00284 CPPUNIT_BINARY_OP_TEST(testAddition);
00285 CPPUNIT_BINARY_OP_TEST(testSubtraction);
00286 CPPUNIT_BINARY_OP_TEST(testMultiplication);
00287 CPPUNIT_BINARY_OP_TEST(testDivision);
00288
00289 CPPUNIT_RELOP_OP_TEST(testEquals);
00290 CPPUNIT_RELOP_OP_TEST(testNotEquals);
00291 CPPUNIT_RELOP_OP_TEST(testLessThanOrEquals);
00292 CPPUNIT_RELOP_OP_TEST(testGreaterThanOrEquals);
00293 CPPUNIT_RELOP_OP_TEST(testLessThan);
00294 CPPUNIT_RELOP_OP_TEST(testGreaterThan);
00295
00296 CPPUNIT_BINARY_FUNC_TEST(testPow);
00297 CPPUNIT_BINARY_FUNC_TEST(testMax);
00298 CPPUNIT_BINARY_FUNC_TEST(testMin);
00299
00300 CPPUNIT_TEST(testUnaryPlus);
00301 CPPUNIT_TEST(testUnaryMinus);
00302
00303 CPPUNIT_TEST(testExp);
00304 CPPUNIT_TEST(testLog);
00305 CPPUNIT_TEST(testLog10);
00306 CPPUNIT_TEST(testSqrt);
00307 CPPUNIT_TEST(testCos);
00308 CPPUNIT_TEST(testSin);
00309 CPPUNIT_TEST(testTan);
00310 CPPUNIT_TEST(testACos);
00311 CPPUNIT_TEST(testASin);
00312 CPPUNIT_TEST(testATan);
00313 CPPUNIT_TEST(testCosh);
00314 CPPUNIT_TEST(testSinh);
00315 CPPUNIT_TEST(testTanh);
00316 CPPUNIT_TEST(testFAbs);
00317
00318 CPPUNIT_UNARY_ASSIGNOP_TEST(testPlusEquals);
00319 CPPUNIT_UNARY_ASSIGNOP_TEST(testMinusEquals);
00320 CPPUNIT_UNARY_ASSIGNOP_TEST(testTimesEquals);
00321 CPPUNIT_UNARY_ASSIGNOP_TEST(testDivideEquals);
00322
00323 CPPUNIT_TEST(testComposite1);
00324
00325 CPPUNIT_TEST_SUITE_END();
00326
00327 public:
00328
00329 CacheTaylorOpsUnitTest();
00330
00331 CacheTaylorOpsUnitTest(unsigned int degree, double absolute_tolerance,
00332 double relative_tolerance);
00333
00334 ~CacheTaylorOpsUnitTest();
00335
00336 void setUp();
00337
00338 void tearDown();
00339
00340
00341 void comparePolys(const TaylorType& x_dtay,
00342 double* x_adolc);
00343
00344
00345 void compareDoubles(double a, double b);
00346
00347 BINARY_OP_TEST(testAddition, +);
00348 BINARY_OP_TEST(testSubtraction, -);
00349 BINARY_OP_TEST(testMultiplication, *);
00350 BINARY_OP_TEST(testDivision, /);
00351
00352 RELOP_OP_TEST(testEquals, ==);
00353 RELOP_OP_TEST(testNotEquals, !=);
00354 RELOP_OP_TEST(testLessThanOrEquals, <=);
00355 RELOP_OP_TEST(testGreaterThanOrEquals, >=);
00356 RELOP_OP_TEST(testLessThan, <);
00357 RELOP_OP_TEST(testGreaterThan, >);
00358
00359 BINARY_FUNC_TEST(testPow, pow);
00360 BINARY_FUNC_TEST(testMax, max);
00361 BINARY_FUNC_TEST(testMin, min);
00362
00363 UNARY_OP_TEST(testUnaryPlus, +);
00364 UNARY_OP_TEST(testUnaryMinus, -);
00365
00366 UNARY_FUNC_TEST(testExp, exp);
00367 UNARY_FUNC_TEST(testLog, log);
00368 UNARY_FUNC_TEST(testLog10, log10);
00369 UNARY_FUNC_TEST(testSqrt, sqrt);
00370 UNARY_FUNC_TEST(testCos, cos);
00371 UNARY_FUNC_TEST(testSin, sin);
00372 UNARY_FUNC_TEST(testTan, tan);
00373 UNARY_FUNC_TEST(testACos, acos);
00374 UNARY_FUNC_TEST(testASin, asin);
00375 UNARY_FUNC_TEST(testATan, atan);
00376 UNARY_FUNC_TEST(testCosh, cosh);
00377 UNARY_FUNC_TEST(testSinh, sinh);
00378 UNARY_FUNC_TEST(testTanh, tanh);
00379 UNARY_FUNC_TEST(testFAbs, fabs);
00380
00381 UNARY_ASSIGNOP_TEST(testPlusEquals, +=);
00382 UNARY_ASSIGNOP_TEST(testMinusEquals, -=);
00383 UNARY_ASSIGNOP_TEST(testTimesEquals, *=);
00384 UNARY_ASSIGNOP_TEST(testDivideEquals, /=);
00385
00386 template <typename ScalarT>
00387 ScalarT composite1(const ScalarT& a, const ScalarT& b) {
00388 ScalarT t1 = 3. * a + sin(b) / log(fabs(a - b * 7.));
00389 ScalarT t2 = 1.0e3;
00390 ScalarT t3 = 5.7e4;
00391 ScalarT t4 = 3.2e5;
00392 t1 *= cos(a + exp(t1)) / 6. - tan(t1*sqrt(fabs(a * log10(fabs(b)))));
00393 t1 -= acos((6.+asin(pow(fabs(a),b)/t2))/t3) * asin(pow(fabs(b),2.)*1.0/t4) * atan((b*pow(2.,log(fabs(a))))/(t3*t4));
00394 t1 /= cosh(b - 0.7) + 7.*sinh(t1 + 0.8)*tanh(9./(a+1.)) - 9.;
00395 t1 += pow(fabs(a*4.),b-8.)/cos(a*b*a);
00396
00397 return t1;
00398 }
00399
00400 void testComposite1() {
00401 c_dtay = composite1(a_dtay, b_dtay);
00402 trace_on(0);
00403 adouble aa, ab, ac;
00404 aa <<= X[0][0];
00405 ab <<= X[1][0];
00406 ac = composite1(aa,ab);
00407 ac >>= Y[0][0];
00408 trace_off();
00409 forward(0,1,2,d,0,X,Y);
00410 comparePolys(c_dtay,Y[0]);
00411 }
00412
00413 void print_poly(double *x);
00414
00415 void print_diff(const TaylorType& x_dtay, double* x_adolc);
00416
00417 protected:
00418
00419
00420 TaylorType a_dtay, b_dtay, c_dtay;
00421
00422
00423 double **X, **Y;
00424
00425
00426 Sacado::Random urand;
00427
00428
00429 unsigned int d;
00430
00431
00432 double tol_a, tol_r;
00433
00434 };
00435
00436 #endif // CACHETAYLORUNITTESTS_HPP