TaylorUnitTests.cpp

Go to the documentation of this file.
00001 // $Id: TaylorUnitTests.cpp,v 1.1 2007/06/29 22:18:34 etphipp Exp $ 
00002 // $Source: /space/CVS/Trilinos/packages/sacado/test/TestSuite/TaylorUnitTests.cpp,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 #include "TaylorUnitTests.hpp"
00033 
00034 CPPUNIT_TEST_SUITE_REGISTRATION( TaylorOpsUnitTest );
00035 
00036 TaylorOpsUnitTest::TaylorOpsUnitTest() :
00037   urand(0.0, 1.0), d(5), tol_a(1.0e-14), tol_r(1.0e-13) 
00038 {
00039   X = new double*[2];
00040   X[0] = new double[d+1];
00041   X[1] = new double[d+1];
00042 
00043   Y = new double*[1];
00044   Y[0] = new double[d+1];
00045 }
00046 
00047 TaylorOpsUnitTest::TaylorOpsUnitTest(unsigned int degree, 
00048              double absolute_tolerance, 
00049              double relative_tolerance) :
00050   urand(0.0, 1.0), 
00051   d(degree), 
00052   tol_a(absolute_tolerance), 
00053   tol_r(relative_tolerance) 
00054 {
00055   X = new double*[2];
00056   X[0] = new double[d+1];
00057   X[1] = new double[d+1];
00058 
00059   Y = new double*[1];
00060   Y[0] = new double[d+1];
00061 }
00062 
00063 TaylorOpsUnitTest::~TaylorOpsUnitTest()
00064 {
00065   delete [] X[1];
00066   delete [] X[0];
00067   delete [] X;
00068 
00069   delete [] Y[0];
00070   delete [] Y;
00071 }
00072 
00073 void TaylorOpsUnitTest::setUp() {
00074   double val;
00075 
00076   a_dtay = TaylorType(d,0.0);
00077   b_dtay = TaylorType(d,0.0);
00078   
00079   for (unsigned int i=0; i<=d; i++) {
00080     val = urand.number();
00081     a_dtay.fastAccessCoeff(i) = val;
00082     X[0][i] = val;
00083 
00084     val = urand.number();
00085     b_dtay.fastAccessCoeff(i) = val;
00086     X[1][i] = val;
00087 
00088     Y[0][i] = 0.0;
00089   }
00090 }
00091 
00092 void TaylorOpsUnitTest::tearDown() {}
00093 
00094 void TaylorOpsUnitTest::comparePolys(const TaylorType& x_dtay,
00095              double* x_adolc) {
00096 
00097   // Compare degrees
00098   CPPUNIT_ASSERT(x_dtay.degree() == d);
00099 
00100 //   std::cout << std::endl << "Taylor:" << x_dtay << std::endl;
00101 //   std::cout << "ADOLC:  ";
00102 //   print_poly(x_adolc);
00103 //   std::cout << ":Diff:  ";
00104 //   print_diff(x_dtay, x_adolc);
00105 
00106   
00107   // Compare coefficients
00108   for (unsigned int i=0; i<=d; i++) {
00109     compareDoubles(x_dtay.coeff(i), x_adolc[i]);
00110     //compareDoubles(x_dtay.fastAccessCoeff(i), x_adolc[i]);
00111   }
00112 }
00113 
00114 void TaylorOpsUnitTest::compareDoubles(double a, double b) {
00115   //cout << fabs(a-b) << "   " << tol_a + tol_r*fabs(a) << endl;
00116   CPPUNIT_ASSERT( fabs(a-b) < tol_a + tol_r*fabs(a) );
00117 }
00118 
00119 void TaylorOpsUnitTest::print_poly(double *x) {
00120   std::cout.setf(std::ios::fixed,std::ios::floatfield);
00121   std::cout.width(12);
00122   std::cout << "[";
00123       
00124   for (unsigned int i=0; i<=d; i++) {
00125     std::cout.width(12);
00126     std::cout << x[i];
00127   }
00128 
00129   std::cout << "]\n";
00130 }
00131 
00132 void TaylorOpsUnitTest::print_diff(const TaylorType& x_dtay,
00133            double *x) {
00134   std::cout.setf(std::ios::scientific,std::ios::floatfield);
00135   //std::cout.width(12);
00136   std::cout << "[";
00137       
00138   for (unsigned int i=0; i<=d; i++) {
00139     //std::cout.width(12);
00140     std::cout << x_dtay.coeff(i) - x[i] << " ";
00141   }
00142 
00143   std::cout << "]\n";
00144 }

Generated on Wed May 12 21:59:08 2010 for Sacado Package Browser (Single Doxygen Collection) by  doxygen 1.4.7