File: romberg_one.cpp

package info (click to toggle)
cppad 2026.00.00.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,584 kB
  • sloc: cpp: 112,960; sh: 6,146; ansic: 179; python: 71; sed: 12; makefile: 10
file content (88 lines) | stat: -rw-r--r-- 1,876 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-22 Bradley M. Bell
// ----------------------------------------------------------------------------

/*
{xrst_begin romberg_one.cpp}

One Dimensional Romberg Integration: Example and Test
#####################################################

{xrst_literal
   // BEGIN C++
   // END C++
}

{xrst_end romberg_one.cpp}
*/
// BEGIN C++

# include <cppad/utility/romberg_one.hpp>
# include <cppad/utility/vector.hpp>
# include <cppad/utility/near_equal.hpp>

namespace {
   class Fun {
   private:
      const size_t degree;
   public:
      // constructor
      Fun(size_t degree_) : degree(degree_)
      { }

      // function F(x) = x^degree
      template <class Type>
      Type operator () (const Type &x)
      {  size_t i;
         Type   f = 1;
         for(i = 0; i < degree; i++)
            f *= x;
         return f;
      }
   };
}

bool RombergOne(void)
{  bool ok = true;
   size_t i;

   size_t degree = 4;
   Fun F(degree);

   // arguments to RombergOne
   double a = 0.;
   double b = 1.;
   size_t n = 4;
   size_t p;
   double r, e;

   // int_a^b F(x) dx = [ b^(degree+1) - a^(degree+1) ] / (degree+1)
   double bpow = 1.;
   double apow = 1.;
   for(i = 0; i <= degree; i++)
   {  bpow *= b;
      apow *= a;
   }
   double check = (bpow - apow) / double(degree+1);

   // step size corresponding to r
   double step = (b - a) / exp(log(2.)*double(n-1));
   // step size corresponding to error estimate
   step *= 2.;
   // step size raised to a power
   double spow = 1;

   for(p = 0; p < n; p++)
   {  spow = spow * step * step;

      r = CppAD::RombergOne(F, a, b, n, p, e);

      ok  &= e < double(degree+1) * spow;
      ok  &= CppAD::NearEqual(check, r, 0., e);
   }

   return ok;
}

// END C++