00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <cmath>
00014 #include <tut.h>
00015 #include "rlog/rlog.h"
00016 #include "adaptsim.h"
00017
00018 namespace
00019 {
00020
00021 double zero(double x) {
00022 return 0.0;
00023 }
00024
00025 double one(double x) {
00026 return 1.0;
00027 }
00028
00029 double ramp(double x) {
00030 return x;
00031 }
00032
00033 double sawtooth(double x) {
00034 return x-floor(x);
00035 }
00036
00037 double recip(double x) {
00038 return 1.0/x;
00039 }
00040
00041
00042 struct gg_example {
00043 gg_example() : num_eval(0)
00044 {
00045 }
00046 double operator()(double x) {
00047 num_eval ++;
00048 if (x < 1.0)
00049 return x + 1.0;
00050 else if (x < 3.0)
00051 return 3.0 - x;
00052 else
00053 return 2.0;
00054 }
00055 int num_eval;
00056 };
00057
00058 struct test_data
00059 {
00060 test_data()
00061 : pi(::acos(-1.0)),
00062 tol(1e-8),
00063 tol1(1e-11)
00064 {
00065 }
00066 const double pi;
00067 const double tol;
00068 const double tol1;
00069 };
00070
00071 typedef tut::test_group<test_data> testgroup;
00072 typedef testgroup::object testobject;
00073 testgroup group("adaptsim");
00074
00075 template<>
00076 template<>
00077 void testobject::test<1>()
00078 {
00079 int info;
00080 double I = NR::adaptsim(zero, 0.0, 1.0, tol1, info);
00081 ensure(I == 0.0);
00082 }
00083
00084 template<>
00085 template<>
00086 void testobject::test<2>()
00087 {
00088 int info;
00089 double I = NR::adaptsim(one, 0.0, 1.0, tol1, info);
00090 ensure(I == 1.0);
00091 }
00092
00093 template<>
00094 template<>
00095 void testobject::test<3>()
00096 {
00097 int info;
00098 double I = NR::adaptsim(ramp, 0.0, 1.0, tol1, info);
00099 ensure(I == 0.5);
00100 }
00101
00102 template<>
00103 template<>
00104 void testobject::test<4>()
00105 {
00106 int info;
00107 double I = NR::adaptsim(sin, 0.0, pi, tol1, info);
00108 ensure(fabs(I - 2.0) <= tol);
00109 }
00110
00111 template<>
00112 template<>
00113 void testobject::test<5>()
00114 {
00115 int info;
00116 double I = NR::adaptsim(sin, 0.0, 2.0*pi, tol1, info);
00117 ensure(fabs(I) <= tol);
00118 }
00119
00120 template<>
00121 template<>
00122 void testobject::test<6>()
00123 {
00124 int info;
00125 double I = NR::adaptsim(sawtooth, 0.0, 3.0, tol1, info);
00126 rDebug("I - 1.5 = %e", I - 1.5);
00127 ensure(fabs(I - 1.5) <= tol);
00128 }
00129
00130 template<>
00131 template<>
00132 void testobject::test<7>()
00133 {
00134 int info;
00135 const double left = 1e-5;
00136 const double right = 1.0;
00137 double I = NR::adaptsim(recip, left, right, tol1, info);
00138 double expected = std::log(right) - std::log(left);
00139 rDebug("I - %e = %e", expected, I - expected);
00140 ensure(fabs(I - expected) <= tol);
00141 }
00142
00143 template<>
00144 template<>
00145 void testobject::test<8>()
00146 {
00147 int info;
00148 gg_example f;
00149 double I = NR::adaptsim(f, 0.0, 5.0, 1e-7, info);
00150 double expected = 7.5;
00151 rDebug("I - %e = %e, num_eval=%d", expected, I - expected, f.num_eval);
00152 ensure(fabs((I - expected)/expected) <= 1e-5);
00153 }
00154
00156 template<>
00157 template<>
00158 void testobject::test<9>()
00159 {
00160 int info;
00161 double I = NR::adaptsim(sin, pi, 0.0, tol1, info);
00162 ensure(fabs(I + 2.0) <= tol);
00163 }
00164
00165 };