numtest.cc

Go to the documentation of this file.
00001 
00004 
00005 //
00006 // Copyright (c) 2005-2007 University of Southern California
00007 // Rob Peters <rjpeters at usc dot edu>
00008 //
00009 // created: Tue Feb 22 11:24:07 2005
00010 // commit: $Id: numtest.cc 10065 2007-04-12 05:54:56Z rjpeters $
00011 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/pkgs/whitebox/numtest.cc $
00012 //
00013 // --------------------------------------------------------------------
00014 //
00015 // This file is part of GroovX.
00016 //   [http://ilab.usc.edu/rjpeters/groovx/]
00017 //
00018 // GroovX is free software; you can redistribute it and/or modify it
00019 // under the terms of the GNU General Public License as published by
00020 // the Free Software Foundation; either version 2 of the License, or
00021 // (at your option) any later version.
00022 //
00023 // GroovX is distributed in the hope that it will be useful, but
00024 // WITHOUT ANY WARRANTY; without even the implied warranty of
00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00026 // General Public License for more details.
00027 //
00028 // You should have received a copy of the GNU General Public License
00029 // along with GroovX; if not, write to the Free Software Foundation,
00030 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00031 //
00033 
00034 #ifndef GROOVX_PKGS_WHITEBOX_NUMTEST_CC_UTC20050626084022_DEFINED
00035 #define GROOVX_PKGS_WHITEBOX_NUMTEST_CC_UTC20050626084022_DEFINED
00036 
00037 #include "pkgs/whitebox/numtest.h"
00038 
00039 #include "mtx/mathspecial.h"
00040 
00041 #include "tcl/pkg.h"
00042 
00043 #include "rutz/unittest.h"
00044 
00045 #include "rutz/trace.h"
00046 
00047 namespace
00048 {
00049   void testErfc()
00050   {
00051     // Compare our erfc() function with the one from cmath
00052     TEST_REQUIRE_APPROX(dash::erfc(0), ::erfc(0), 1e-5);
00053     TEST_REQUIRE_APPROX(dash::erfc(1), ::erfc(1), 1e-5);
00054     TEST_REQUIRE_APPROX(dash::erfc(-1), ::erfc(-1), 1e-5);
00055 
00056     // Compare our erfc() function with known values
00057     TEST_REQUIRE_APPROX(dash::erfc(0), 1, 1e-5);
00058     TEST_REQUIRE_APPROX(dash::erfc(1), 0.1573, 1e-5);
00059     TEST_REQUIRE_APPROX(dash::erfc(-1), 1.8427, 1e-5);
00060 
00061     // Check the symmetry properties of our erfc function
00062     //  erfc(x)+erfc(-x) == 2
00063     TEST_REQUIRE_APPROX(dash::erfc(1), 2-dash::erfc(-1), 1e-5);
00064     TEST_REQUIRE_APPROX(dash::erfc(2), 2-dash::erfc(-2), 1e-5);
00065 
00066 
00067     static rutz::prof p1("testprof/erfc/custom", __FILE__, __LINE__);
00068     static rutz::prof p2("testprof/erfc/builtin", __FILE__, __LINE__);
00069 
00070     {
00071       rutz::trace t(p1, false);
00072       double x = 0.0;
00073       for (int i = 0; i < 2500; ++i)
00074         for (int f = 1; f <= 100; ++f)
00075           x += dash::erfc(double(i)/1000.0);
00076     }
00077 
00078     {
00079       rutz::trace t(p2, false);
00080       double x = 0.0;
00081       for (int i = 0; i < 2500; ++i)
00082         for (int f = 1; f <= 100; ++f)
00083           x += ::erfc(double(i)/1000.0);
00084     }
00085   }
00086 
00087   void testGammaln()
00088   {
00089     // Compare custom gammaln() with builtin lgamma() function
00090     for (int f = 1; f <= 100; ++f)
00091       TEST_REQUIRE_APPROX(dash::gammaln(f), ::lgamma(f), 1e-5);
00092 
00093     static rutz::prof p1("testprof/gammaln/custom", __FILE__, __LINE__);
00094     static rutz::prof p2("testprof/gammaln/builtin", __FILE__, __LINE__);
00095 
00096     {
00097       rutz::trace t(p1, false);
00098       double x = 0.0;
00099       for (int i = 0; i < 2500; ++i)
00100         for (int f = 1; f <= 100; ++f)
00101           x += dash::gammaln(f);
00102     }
00103 
00104     {
00105       rutz::trace t(p2, false);
00106       double x = 0.0;
00107       for (int i = 0; i < 2500; ++i)
00108         for (int f = 1; f <= 100; ++f)
00109           x += ::lgamma(f);
00110     }
00111   }
00112 }
00113 
00114 extern "C"
00115 int Numtest_Init(Tcl_Interp* interp)
00116 {
00117 GVX_TRACE("Numtest_Init");
00118 
00119   GVX_PKG_CREATE(pkg, interp, "Numtest", "4.$Revision: 10065 $");
00120 
00121   DEF_TEST(pkg, testErfc);
00122   DEF_TEST(pkg, testGammaln);
00123 
00124   GVX_PKG_RETURN(pkg);
00125 }
00126 
00127 // Need these to avoid dyld errors on Mac OS X
00128 extern "C" int Numtest_SafeInit(Tcl_Interp*) { return 1; }
00129 
00130 extern "C" int Numtest_Unload(Tcl_Interp* interp, int /*flags*/)
00131 {
00132 GVX_TRACE("Numtest_Unload");
00133   return tcl::pkg::destroy_on_unload(interp, "Numtest");
00134 }
00135 
00136 extern "C" int Numtest_SafeUnload(Tcl_Interp*, int /*flags*/) { return 1; }
00137 
00138 static const char __attribute__((used)) vcid_groovx_pkgs_whitebox_numtest_cc_utc20050626084022[] = "$Id: numtest.cc 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00139 #endif // !GROOVX_PKGS_WHITEBOX_NUMTEST_CC_UTC20050626084022_DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:40 2008 by Doxygen version 1.5.5.