00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <cmath>
00014 #include <tut.h>
00015 #include "romberg.h"
00016
00017 static double zero(double x) {
00018 return 0.0;
00019 }
00020
00021 static double one(double x) {
00022 return 1.0;
00023 }
00024
00025 static double ramp(double x) {
00026 return x;
00027 }
00028
00029 static double sawtooth(double x) {
00030 return x-floor(x);
00031 }
00032
00033 static double recip(double x) {
00034 return 1.0/x;
00035 }
00036
00037 namespace
00038 {
00039
00040 struct romberg_data
00041 {
00042 romberg_data()
00043 : pi(::acos(-1.0)),
00044 tol(1e-8)
00045 {
00046 }
00047 const double pi;
00048 const double tol;
00049 };
00050
00051 typedef tut::test_group<romberg_data> testgroup;
00052 typedef testgroup::object testobject;
00053 testgroup romberg_group("trapezoidal");
00054
00055 template<>
00056 template<>
00057 void testobject::test<1>()
00058 {
00059 int info;
00060 double I = NR::qtrap(zero, 0.0, 1.0, 1e-12, info);
00061 ensure(I == 0.0);
00062 }
00063
00064 template<>
00065 template<>
00066 void testobject::test<2>()
00067 {
00068 int info;
00069 double I = NR::qtrap(one, 0.0, 1.0, 1e-12, info);
00070 ensure(I == 1.0);
00071 }
00072
00073 template<>
00074 template<>
00075 void testobject::test<3>()
00076 {
00077 int info;
00078 double I = NR::qtrap(ramp, 0.0, 1.0, 1e-12, info);
00079 ensure(I == 0.5);
00080 }
00081
00082 template<>
00083 template<>
00084 void testobject::test<4>()
00085 {
00086 int info;
00087 double I = NR::qtrap(sin, 0.0, pi, 1e-12, info);
00088 ensure(fabs(I - 2.0) <= tol);
00089 }
00090
00091 template<>
00092 template<>
00093 void testobject::test<5>()
00094 {
00095 int info;
00096 double I = NR::qtrap(sin, 0.0, 2.0*pi, 1e-12, info);
00097 ensure(fabs(I) <= tol);
00098 }
00099
00100 template<>
00101 template<>
00102 void testobject::test<6>()
00103 {
00104 int info;
00105 double I = NR::qtrap(sawtooth, 0.0, 3.0, 1e-12, info);
00106 rDebug("I - 1.5 = %e", I - 1.5);
00107 ensure(fabs(I - 1.5) <= tol);
00108 }
00109
00110 template<>
00111 template<>
00112 void testobject::test<7>()
00113 {
00114 int info;
00115 const double left = 1e-5;
00116 const double right = 1.0;
00117 double I = NR::qtrap(recip, left, 1.0, 1e-12, info);
00118 double expected = std::log(right) - std::log(left);
00119 rDebug("I - %e = %e", expected, I - expected);
00120 ensure(fabs(I - expected) <= tol);
00121 }
00122
00124 template<>
00125 template<>
00126 void testobject::test<9>()
00127 {
00128 int info;
00129 double I = NR::qtrap(sin, pi, 0.0, 1e-12, info);
00130 ensure(fabs(I + 2.0) <= tol);
00131 }
00132
00133 };