TaylorUnitTests.hpp

Go to the documentation of this file.
00001 // $Id: TaylorUnitTests.hpp,v 1.1.2.2 2007/08/14 00:19:08 etphipp Exp $ 
00002 // $Source: /space/CVS/Trilinos/packages/sacado/test/TestSuite/TaylorUnitTests.hpp,v $ 
00003 // @HEADER
00004 // ***********************************************************************
00005 // 
00006 //                           Sacado Package
00007 //                 Copyright (2006) Sandia Corporation
00008 // 
00009 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00010 // the U.S. Government retains certain rights in this software.
00011 // 
00012 // This library is free software; you can redistribute it and/or modify
00013 // it under the terms of the GNU Lesser General Public License as
00014 // published by the Free Software Foundation; either version 2.1 of the
00015 // License, or (at your option) any later version.
00016 //  
00017 // This library is distributed in the hope that it will be useful, but
00018 // WITHOUT ANY WARRANTY; without even the implied warranty of
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 // Lesser General Public License for more details.
00021 //  
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License along with this library; if not, write to the Free Software
00024 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00025 // USA
00026 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
00027 // (etphipp@sandia.gov).
00028 // 
00029 // ***********************************************************************
00030 // @HEADER
00031 
00032 #ifndef TAYLORUNITTESTS_HPP
00033 #define TAYLORUNITTESTS_HPP
00034 
00035 // ADOL-C includes
00036 #include "adouble.h"
00037 #include "interfaces.h"
00038 
00039 // Sacado includes
00040 #include "Sacado.hpp"
00041 #include "Sacado_Random.hpp"
00042 
00043 typedef Sacado::Tay::Taylor<double> TaylorType;
00044 
00045 inline adouble max(const adouble& a, const adouble& b) { return fmax(a,b); }
00046 inline adouble max(const adouble& a, double v) { return fmax(a,v); }
00047 inline adouble max(double v, const adouble& b) { return fmax(v,b); }
00048 inline adouble min(const adouble& a, const adouble& b) { return fmin(a,b); }
00049 inline adouble min(const adouble& a, double v) { return fmin(a,v); }
00050 inline adouble min(double v, const adouble& b) { return fmin(v,b); }
00051 
00052 // Cppunit includes
00053 #include <cppunit/extensions/HelperMacros.h>
00054 
00055 #define BINARY_OP2_TEST(TESTNAME,OP)      \
00056   void TESTNAME () {          \
00057     c_dtay = a_dtay OP b_dtay;        \
00058     trace_on(0);          \
00059     adouble aa, ab, ac;         \
00060     aa <<= X[0][0];         \
00061     ab <<= X[1][0];         \
00062     ac = aa OP ab;          \
00063     ac >>= Y[0][0];         \
00064     trace_off();          \
00065     forward(0,1,2,d,0,X,Y);       \
00066     comparePolys(c_dtay,Y[0]);        \
00067   }
00068 
00069 #define BINARY_OPRC_TEST(TESTNAME,OP)     \
00070   void TESTNAME () {          \
00071     double val = urand.number();      \
00072     c_dtay = a_dtay OP val;       \
00073     trace_on(0);          \
00074     adouble aa, ac;         \
00075     aa <<= X[0][0];         \
00076     ac = aa OP val;         \
00077     ac >>= Y[0][0];         \
00078     trace_off();          \
00079     forward(0,1,1,d,0,X,Y);       \
00080     comparePolys(c_dtay,Y[0]);        \
00081   }
00082 
00083 #define BINARY_OPLC_TEST(TESTNAME,OP)     \
00084   void TESTNAME () {          \
00085     double val = urand.number();      \
00086     c_dtay = val OP a_dtay;       \
00087     trace_on(0);          \
00088     adouble aa, ac;         \
00089     aa <<= X[0][0];         \
00090     ac = val OP aa;         \
00091     ac >>= Y[0][0];         \
00092     trace_off();          \
00093     forward(0,1,1,d,0,X,Y);       \
00094     comparePolys(c_dtay,Y[0]);        \
00095   }
00096 
00097 #define BINARY_OP_TEST(TESTNAME,OP)     \
00098   BINARY_OP2_TEST(TESTNAME,OP);       \
00099   BINARY_OPLC_TEST(TESTNAME ## LeftConstant,OP);  \
00100   BINARY_OPRC_TEST(TESTNAME ## RightConstant,OP)
00101 
00102 #define CPPUNIT_BINARY_OP_TEST(TESTNAME)  \
00103   CPPUNIT_TEST(TESTNAME);     \
00104   CPPUNIT_TEST(TESTNAME ## LeftConstant); \
00105   CPPUNIT_TEST(TESTNAME ## RightConstant)
00106 
00107 #define RELOP_OP2_TEST(TESTNAME,OP)                 \
00108   void TESTNAME () {                      \
00109     bool r1 = a_dtay OP b_dtay;                   \
00110     bool r2 = a_dtay.coeff(0) OP b_dtay.coeff(0); \
00111     CPPUNIT_ASSERT(r1 == r2);       \
00112   }
00113 
00114 #define RELOP_OPLC_TEST(TESTNAME,OP)                  \
00115   void TESTNAME () {                      \
00116     double val = urand.number();                        \
00117     bool r1 = val OP b_dtay;                    \
00118     bool r2 = val OP b_dtay.coeff(0);                 \
00119     CPPUNIT_ASSERT(r1 == r2);       \
00120   }
00121 
00122 #define RELOP_OPRC_TEST(TESTNAME,OP)                  \
00123   void TESTNAME () {                      \
00124     double val = urand.number();                        \
00125     bool r1 = a_dtay OP val;                    \
00126     bool r2 = a_dtay.coeff(0) OP val;                 \
00127     CPPUNIT_ASSERT(r1 == r2);       \
00128   }
00129 
00130 #define RELOP_OP_TEST(TESTNAME,OP)      \
00131   RELOP_OP2_TEST(TESTNAME,OP);                          \
00132   RELOP_OPLC_TEST(TESTNAME ## LeftConstant,OP);         \
00133   RELOP_OPRC_TEST(TESTNAME ## RightConstant,OP)
00134 
00135 #define CPPUNIT_RELOP_OP_TEST(TESTNAME)                 \
00136   CPPUNIT_TEST(TESTNAME);             \
00137   CPPUNIT_TEST(TESTNAME ## LeftConstant);         \
00138   CPPUNIT_TEST(TESTNAME ## RightConstant)
00139 
00140 #define BINARY_FUNC2_TEST(TESTNAME,FUNC)    \
00141   void TESTNAME () {          \
00142     c_dtay = FUNC (a_dtay, b_dtay);     \
00143     trace_on(0);          \
00144     adouble aa, ab, ac;         \
00145     aa <<= X[0][0];         \
00146     ab <<= X[1][0];         \
00147     ac = FUNC (aa, ab);         \
00148     ac >>= Y[0][0];         \
00149     trace_off();          \
00150     forward(0,1,2,d,0,X,Y);       \
00151     comparePolys(c_dtay,Y[0]);        \
00152   }
00153 
00154 #define BINARY_FUNCRC_TEST(TESTNAME,FUNC)   \
00155   void TESTNAME () {          \
00156     double val = urand.number();      \
00157     c_dtay = FUNC (a_dtay, val);      \
00158     trace_on(0);          \
00159     adouble aa, ac;         \
00160     aa <<= X[0][0];         \
00161     ac = FUNC (aa, val);        \
00162     ac >>= Y[0][0];         \
00163     trace_off();          \
00164     forward(0,1,1,d,0,X,Y);       \
00165     comparePolys(c_dtay,Y[0]);        \
00166   }
00167 
00168 #define BINARY_FUNCLC_TEST(TESTNAME,FUNC)   \
00169   void TESTNAME () {          \
00170     double val = urand.number();      \
00171     c_dtay = FUNC (val, a_dtay);      \
00172     trace_on(0);          \
00173     adouble aa, ac;         \
00174     aa <<= X[0][0];         \
00175     ac = FUNC (val, aa);        \
00176     ac >>= Y[0][0];         \
00177     trace_off();          \
00178     forward(0,1,1,d,0,X,Y);       \
00179     comparePolys(c_dtay,Y[0]);        \
00180   }
00181 
00182 #define BINARY_FUNC_TEST(TESTNAME,FUNC)       \
00183   BINARY_FUNC2_TEST(TESTNAME,FUNC);       \
00184   BINARY_FUNCLC_TEST(TESTNAME ## LeftConstant,FUNC);    \
00185   BINARY_FUNCRC_TEST(TESTNAME ## RightConstant,FUNC)
00186 
00187 #define CPPUNIT_BINARY_FUNC_TEST(TESTNAME)  \
00188   CPPUNIT_TEST(TESTNAME);     \
00189   CPPUNIT_TEST(TESTNAME ## LeftConstant); \
00190   CPPUNIT_TEST(TESTNAME ## RightConstant)
00191 
00192 #define UNARY_OP_TEST(TESTNAME,OP)        \
00193   void TESTNAME () {            \
00194     c_dtay = OP a_dtay;           \
00195     trace_on(0);            \
00196     adouble aa, ac;           \
00197     aa <<= X[0][0];           \
00198     ac = OP aa;             \
00199     ac >>= Y[0][0];           \
00200     trace_off();            \
00201     forward(0,1,1,d,0,X,Y);         \
00202     comparePolys(c_dtay,Y[0]);          \
00203   }
00204 
00205 #define UNARY_FUNC_TEST(TESTNAME,FUNC)        \
00206   void TESTNAME () {            \
00207     c_dtay = FUNC (a_dtay);         \
00208     trace_on(0);            \
00209     adouble aa, ac;           \
00210     aa <<= X[0][0];           \
00211     ac = FUNC (aa);           \
00212     ac >>= Y[0][0];           \
00213     trace_off();            \
00214     forward(0,1,1,d,0,X,Y);         \
00215     comparePolys(c_dtay,Y[0]);          \
00216   }
00217 
00218 #define UNARY_ASSIGNOP2_TEST(TESTNAME,OP)     \
00219   void TESTNAME () {            \
00220     c_dtay = a_dtay;            \
00221     c_dtay OP b_dtay;           \
00222     trace_on(0);            \
00223     adouble aa, ab, ac;           \
00224     aa <<= X[0][0];           \
00225     ab <<= X[1][0];           \
00226     ac = aa;              \
00227     ac OP ab;             \
00228     ac >>= Y[0][0];           \
00229     trace_off();            \
00230     forward(0,1,2,d,0,X,Y);         \
00231     comparePolys(c_dtay,Y[0]);          \
00232   }
00233 
00234 #define UNARY_ASSIGNOPRC_TEST(TESTNAME,OP)      \
00235   void TESTNAME () {            \
00236     double val = urand.number();        \
00237     c_dtay = a_dtay;            \
00238     c_dtay OP val;            \
00239     trace_on(0);            \
00240     adouble aa, ac;           \
00241     aa <<= X[0][0];           \
00242     ac = aa;              \
00243     ac OP val;              \
00244     ac >>= Y[0][0];           \
00245     trace_off();            \
00246     forward(0,1,1,d,0,X,Y);         \
00247     comparePolys(c_dtay,Y[0]);          \
00248   }
00249 
00250 #define UNARY_ASSIGNOPLC_TEST(TESTNAME,OP)      \
00251   void TESTNAME () {            \
00252     double val = urand.number();        \
00253     c_dtay = val;           \
00254     c_dtay OP a_dtay;           \
00255     trace_on(0);            \
00256     adouble aa, ac;           \
00257     aa <<= X[0][0];           \
00258     ac = val;             \
00259     ac OP aa;             \
00260     ac >>= Y[0][0];           \
00261     trace_off();            \
00262     forward(0,1,1,d,0,X,Y);         \
00263     comparePolys(c_dtay,Y[0]);          \
00264   }
00265 
00266 #define UNARY_ASSIGNOP_TEST(TESTNAME,OP)    \
00267   UNARY_ASSIGNOP2_TEST(TESTNAME,OP);      \
00268   UNARY_ASSIGNOPLC_TEST(TESTNAME ## LeftConstant,OP); \
00269   UNARY_ASSIGNOPRC_TEST(TESTNAME ## RightConstant,OP)
00270 
00271 #define CPPUNIT_UNARY_ASSIGNOP_TEST(TESTNAME) \
00272   CPPUNIT_TEST(TESTNAME);     \
00273   CPPUNIT_TEST(TESTNAME ## LeftConstant); \
00274   CPPUNIT_TEST(TESTNAME ## RightConstant)
00275 
00276 // A class for testing each Taylor operation
00277 class TaylorOpsUnitTest : public CppUnit::TestFixture {
00278 
00279   CPPUNIT_TEST_SUITE( TaylorOpsUnitTest );
00280 
00281   CPPUNIT_BINARY_OP_TEST(testAddition);
00282   CPPUNIT_BINARY_OP_TEST(testSubtraction);
00283   CPPUNIT_BINARY_OP_TEST(testMultiplication);
00284   CPPUNIT_BINARY_OP_TEST(testDivision);
00285 
00286   CPPUNIT_RELOP_OP_TEST(testEquals);
00287   CPPUNIT_RELOP_OP_TEST(testNotEquals);
00288   CPPUNIT_RELOP_OP_TEST(testLessThanOrEquals);
00289   CPPUNIT_RELOP_OP_TEST(testGreaterThanOrEquals);
00290   CPPUNIT_RELOP_OP_TEST(testLessThan);
00291   CPPUNIT_RELOP_OP_TEST(testGreaterThan);
00292 
00293   CPPUNIT_BINARY_FUNC_TEST(testPow);
00294   CPPUNIT_BINARY_FUNC_TEST(testMax);
00295   CPPUNIT_BINARY_FUNC_TEST(testMin);
00296 
00297   CPPUNIT_TEST(testUnaryPlus);
00298   CPPUNIT_TEST(testUnaryMinus);
00299   
00300   CPPUNIT_TEST(testExp);
00301   CPPUNIT_TEST(testLog);
00302   CPPUNIT_TEST(testLog10);
00303   CPPUNIT_TEST(testSqrt);
00304   CPPUNIT_TEST(testCos);
00305   CPPUNIT_TEST(testSin);
00306   CPPUNIT_TEST(testTan);
00307   CPPUNIT_TEST(testACos);
00308   CPPUNIT_TEST(testASin);
00309   CPPUNIT_TEST(testATan);
00310   CPPUNIT_TEST(testCosh);
00311   CPPUNIT_TEST(testSinh);
00312   CPPUNIT_TEST(testTanh);
00313   CPPUNIT_TEST(testFAbs);
00314 
00315   CPPUNIT_UNARY_ASSIGNOP_TEST(testPlusEquals);
00316   CPPUNIT_UNARY_ASSIGNOP_TEST(testMinusEquals);
00317   CPPUNIT_UNARY_ASSIGNOP_TEST(testTimesEquals);
00318   CPPUNIT_UNARY_ASSIGNOP_TEST(testDivideEquals);
00319 
00320   CPPUNIT_TEST(testComposite1);
00321 
00322   CPPUNIT_TEST_SUITE_END();
00323 
00324 public:
00325 
00326   TaylorOpsUnitTest();
00327 
00328   TaylorOpsUnitTest(unsigned int degree, double absolute_tolerance, 
00329          double relative_tolerance);
00330 
00331   ~TaylorOpsUnitTest();
00332 
00333   void setUp();
00334 
00335   void tearDown();
00336 
00337   // Assert to Fad objects are the same
00338   void comparePolys(const TaylorType& x_dtay,
00339         double* x_adolc);
00340 
00341   // Assert to doubles are the same to relative precision
00342   void compareDoubles(double a, double b);
00343 
00344   BINARY_OP_TEST(testAddition, +);
00345   BINARY_OP_TEST(testSubtraction, -);
00346   BINARY_OP_TEST(testMultiplication, *);
00347   BINARY_OP_TEST(testDivision, /);
00348 
00349   RELOP_OP_TEST(testEquals, ==);
00350   RELOP_OP_TEST(testNotEquals, !=);
00351   RELOP_OP_TEST(testLessThanOrEquals, <=);
00352   RELOP_OP_TEST(testGreaterThanOrEquals, >=);
00353   RELOP_OP_TEST(testLessThan, <);
00354   RELOP_OP_TEST(testGreaterThan, >);
00355 
00356   BINARY_FUNC_TEST(testPow, pow);
00357   BINARY_FUNC_TEST(testMax, max);
00358   BINARY_FUNC_TEST(testMin, min);
00359 
00360   UNARY_OP_TEST(testUnaryPlus, +);
00361   UNARY_OP_TEST(testUnaryMinus, -);
00362 
00363   UNARY_FUNC_TEST(testExp, exp);
00364   UNARY_FUNC_TEST(testLog, log);
00365   UNARY_FUNC_TEST(testLog10, log10);
00366   UNARY_FUNC_TEST(testSqrt, sqrt);
00367   UNARY_FUNC_TEST(testCos, cos);
00368   UNARY_FUNC_TEST(testSin, sin);
00369   UNARY_FUNC_TEST(testTan, tan);
00370   UNARY_FUNC_TEST(testACos, acos);
00371   UNARY_FUNC_TEST(testASin, asin);
00372   UNARY_FUNC_TEST(testATan, atan);
00373   UNARY_FUNC_TEST(testCosh, cosh);
00374   UNARY_FUNC_TEST(testSinh, sinh);
00375   UNARY_FUNC_TEST(testTanh, tanh);
00376   UNARY_FUNC_TEST(testFAbs, fabs);
00377 
00378   UNARY_ASSIGNOP_TEST(testPlusEquals, +=);
00379   UNARY_ASSIGNOP_TEST(testMinusEquals, -=);
00380   UNARY_ASSIGNOP_TEST(testTimesEquals, *=);
00381   UNARY_ASSIGNOP_TEST(testDivideEquals, /=);
00382 
00383   template <typename ScalarT>
00384   ScalarT composite1(const ScalarT& a, const ScalarT& b) {
00385     ScalarT t1 = 3. * a + sin(b) / log(fabs(a - b * 7.));
00386     ScalarT t2 = 1.0e3;
00387     ScalarT t3 = 5.7e4;
00388     ScalarT t4 = 3.2e5;
00389     t1 *= cos(a + exp(t1)) / 6. - tan(t1*sqrt(fabs(a * log10(fabs(b)))));
00390     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));
00391     t1 /= cosh(b - 0.7) + 7.*sinh(t1 + 0.8)*tanh(9./(a+1.)) - 9.;
00392     t1 += pow(fabs(a*4.),b-8.)/cos(a*b*a);
00393     
00394   return t1;
00395 }
00396 
00397   void testComposite1() {
00398     c_dtay = composite1(a_dtay, b_dtay);
00399     trace_on(0);
00400     adouble aa, ab, ac;
00401     aa <<= X[0][0];
00402     ab <<= X[1][0];
00403     ac = composite1(aa,ab);
00404     ac >>= Y[0][0];
00405     trace_off();
00406     forward(0,1,2,d,0,X,Y);
00407     comparePolys(c_dtay,Y[0]);
00408   }
00409 
00410   void print_poly(double *x);
00411 
00412   void print_diff(const TaylorType& x_dtay, double* x_adolc);
00413 
00414 protected:
00415 
00416   // Taylor variables
00417   TaylorType a_dtay, b_dtay, c_dtay;
00418 
00419   // ADOL-C arrays
00420   double **X, **Y;
00421 
00422   // Random number generator
00423   Sacado::Random urand;
00424 
00425   // Degree of polynomials
00426   unsigned int d;
00427 
00428   // Tolerances to which fad objects should be the same
00429   double tol_a, tol_r;
00430 
00431 }; // class TaylorOpsUnitTest
00432 
00433 #endif // TAYLORUNITTESTS_HPP

Generated on Tue Oct 20 12:55:08 2009 for Sacado Package Browser (Single Doxygen Collection) by doxygen 1.4.7