blib/lib/Math/Integral/Romberg.pm

filter: Statementstmt 95.9% 47 / 49 Branchbran 58.3% 7 / 12 Conditioncond 68.9% 20 / 29 MC/DCmcdc 18.7% 3 / 16 Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 1 totaltotal 72.9% 81 / 111 CC 18 SCAR 29.5
CC
18
Cov
82%
integral
22.0
BEGIN
1.0
BEGIN
1.0
CRAP 19.0
↑ Summary
1 # -*- perl -*-
2  
3 package Math::Integral::Romberg;
4  
5 1 require Exporter;
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use vars qw( @ISA @EXPORT @EXPORT_OK );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 @ISA = qw(Exporter);
12 1 @EXPORT = qw();
13 1 @EXPORT_OK = qw(integral return_point_count);
14  
15 1 ▶use vars qw( $VERSION $abort $return_point_count
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 $rel_err $abs_err $max_split $min_split );
17  
18 1 $VERSION = "0.04";
19  
20 1 $abort = 0;
21 1 $return_point_count = 0;
22  
23 1 $rel_err = 1e-10;
24 1 $abs_err = 1e-20;
25 1 $max_split = 16;
26 1 $min_split = 5;
27  
28 =head1 NAME
29  
30 Math::Integral::Romberg - Scalar numerical integration
31  
32 =head1 SYNOPSIS
33  
34 use Math::Integral::Romberg 'integral';
35 sub f { my ($x) = @_; 1/($x ** 2 + 1) } # ... or whatever
36 $area = integral(\&f, $x1, $x2); # Short form
37 $area = integral # Long form
38 (\&f, $x1, $x2, $rel_err, $abs_err, $max_split, $min_split);
39  
40 # an alternative way of doing the long form
41 $Math::Integral::Romberg::rel_err = $rel_err;
42 $Math::Integral::Romberg::abs_err = $abs_err;
43 $Math::Integral::Romberg::max_split = $max_split;
44 $Math::Integral::Romberg::min_split = $min_split;
45 $area = integral(\&f, $x1, $x2);
46  
47 =head1 DESCRIPTION
48  
49 integral() numerically estimates the integral of f() using Romberg
50 integration, a faster relative of Simpson's method.
51  
52 =head2 Parameters
53  
54 =over
55  
56 =item $f
57  
58 A reference to the function to be integrated.
59  
60 =item $x1, $x2
61  
62 The limits of the integration domain. C<&$f(x1)> and C<&$f(x2)> must
63 be finite.
64  
65 =item $rel_err
66  
67 Maximum acceptable relative error. Estimates of relative and absolute
68 error are based on a comparison of the estimate computed using C<2**n
69 + 1> points with the estimate computed using C<2**(n-1) + 1>
70 points.
71  
72 Once $min_split has been reached (see below), computation stops as
73 soon as relative error drops below $rel_err, absolute error drops
74 below $abs_err, or $max_split is reached.
75  
76 If not supplied, uses the value B<$Math::Integral::Romberg::rel_err>
77 whose default is 10**-10. The accuracy limit of double-precision
78 floating point is about 10**-15.
79  
80 =item $abs_err
81  
82 Maximum acceptable absolute error. If not supplied, uses
83 B<$Math::Integral::Romberg::abs_err>, which defaults to
84 10**-20.
85  
86 =item $max_split
87  
88 At most C<2 ** $max_split + 1> different sample x values are used to
89 estimate the integral of C<f()>. If not supplied, uses the value
90 B<$Math::Integral::Romberg::max_split>, which defaults to 16,
91 corresponding to 65537 sample points.
92  
93 =item $min_split
94  
95 At least C<2 ** $min_split + 1> different sample x values are used to
96 estimate the integral of C<f()>. If not supplied, uses the value of
97 B<$Math::Integral::Romberg::max_split>, which defaults to 5,
98 corresponding to 33 sample points.
99  
100 =item $Math::Integral::Romberg::return_point_count
101  
102 This value defaults to 0. If you set it to 1, then when invoked in a
103 list context, integral() will return a two-element list, containing
104 the estimate followed by the number of sample points used to compute
105 the estimate.
106  
107 =item $Math::Integral::Romberg::abort
108  
109 This value is set to 1 if neither the $rel_err nor the $abs_err
110 thresholds are reached before computation stops. Once set, this
111 variable remains set until you reset it to 0.
112  
113 =back
114  
115 =head2 Default values
116  
117 Using the long form of integral() sets the convergence parameters
118 for that call only - you must use the package-qualified variable
119 names (e.g. $Math::Integral::Romberg::abs_tol) to change the values
120 for all calls.
121  
122 =head2 About the Algorithm
123  
124 Romberg integration uses progressively higher-degree polynomial
125 approximations each time you double the number of sample points. For
126 example, it uses a 2nd-degree polynomial approximation (as Simpson's
127 method does) after one split (2**1 + 1 sample points), and it uses a
128 10th-degree polynomial approximation after five splits (2**5 + 1
129 sample points). Typically, this will greatly improve accuracy
130 (compared to simpler methods) for smooth functions, while not making
131 much difference for badly behaved ones.
132  
133 =head1 AUTHOR
134  
135 Eric Boesch (ericboesch@gmail.com)
136  
137 =cut
138  
139 2 ▶sub integral {
Subroutine integral: called CC=18 SCAR=30.9
Pod undocumented
140 2 ▶ my $return_pts = wantarray && $Math::Integral::Romberg::return_point_count;
Condition33%1/3
wantarray && $Math::Integral::Romberg::return_point_count
!ll&&!rl&&r
200
MC/DC0%0/2
wantarray && $Math::Integral::Romberg::return_point_count
wantarray $Math::Integral::Romberg::return_point_count
Truth table33%1/3
A && B
A= wantarrayB= $Math::Integral::Romberg::return_point_count
ABresult
0X0
100
111
141 2 my $abort = \$Math::Integral::Romberg::abort;
142 2 my ($f,$lo,$hi,$rel_err,$abs_err,$max_split,$min_split)=@_;
143 2 ▶ ($lo, $hi) = ($hi, $lo) if $lo > $hi;
Branch: 1 branch
BranchTrueFalseTotal
if $lo > $hi 0 2 2
144  
145 2 ▶ $rel_err ||= $Math::Integral::Romberg::rel_err;
Condition66%2/3
$rel_err ||= $Math::Integral::Romberg::rel_err
l!l&&r!l&&!r
110
MC/DC0%0/2
$rel_err ||= $Math::Integral::Romberg::rel_err
$rel_err $Math::Integral::Romberg::rel_err
Truth table66%2/3
A ||= B
A= $rel_errB= $Math::Integral::Romberg::rel_err
ABresult
1X1
011
000
146 2 ▶ $abs_err ||= $Math::Integral::Romberg::abs_err;
Condition66%2/3
$abs_err ||= $Math::Integral::Romberg::abs_err
l!l&&r!l&&!r
110
MC/DC0%0/2
$abs_err ||= $Math::Integral::Romberg::abs_err
$abs_err $Math::Integral::Romberg::abs_err
Truth table66%2/3
A ||= B
A= $abs_errB= $Math::Integral::Romberg::abs_err
ABresult
1X1
011
000
147 2 ▶ $max_split ||= $Math::Integral::Romberg::max_split;
Condition66%2/3
$max_split ||= $Math::Integral::Romberg::max_split
l!l&&r!l&&!r
110
MC/DC0%0/2
$max_split ||= $Math::Integral::Romberg::max_split
$max_split $Math::Integral::Romberg::max_split
Truth table66%2/3
A ||= B
A= $max_splitB= $Math::Integral::Romberg::max_split
ABresult
1X1
011
000
148 2 ▶ $min_split ||= $Math::Integral::Romberg::min_split;
Condition66%2/3
$min_split ||= $Math::Integral::Romberg::min_split
l!l&&r!l&&!r
110
MC/DC0%0/2
$min_split ||= $Math::Integral::Romberg::min_split
$min_split $Math::Integral::Romberg::min_split
Truth table66%2/3
A ||= B
A= $min_splitB= $Math::Integral::Romberg::min_split
ABresult
1X1
011
000
149  
150 2 my ($estimate, $split, $steps);
151 2 my $step_len = $hi - $lo;
152 2 my $tot = (&$f($lo) + &$f($hi))/2;
153  
154 # tot is used to compute the trapezoid approximations. It is more or
155 # less a total of all f() values computed so far. The trapezoid
156 # method assigns half as much weight to f(hi) and f(lo) as it does to
157 # all other f() values, so f(hi) and f(lo) are divided by two here.
158  
159 2 my @row = $estimate = $tot * $step_len; # 0th trapezoid approximation.
160  
161 2 for ($split = 1, $steps=2; ; $split++, $step_len /=2, $steps *= 2) {
162 13 my ($x, $new_estimate);
163  
164 # Don't let $step_len drop below the limits of numeric precision.
165 # (This should prevent infinite loops, but not loss of accuracy.)
166 13 ▶ if ($lo + $step_len/$steps == $lo || $hi - $step_len/$steps == $hi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lo + $step_len / $steps == $lo or $hi - $step_len / $steps == $hi) 0 13 13
Condition33%1/3
$lo + $step_len / $steps == $lo or $hi - $step_len / $steps == $hi
l!l&&r!l&&!r
0013
MC/DC0%0/2
$lo + $step_len / $steps == $lo or $hi - $step_len / $steps == $hi
$lo + $step_len / $steps == $lo $hi - $step_len / $steps == $hi
Truth table33%1/3
A or B
A= $lo + $step_len / $steps == $loB= $hi - $step_len / $steps == $hi
ABresult
1X1
011
000
167 0 $$abort = 1;
168 0 ▶ return $return_pts ? ($estimate, $steps/2 + 1) : $estimate;
Branch: 1 branch
BranchTrueFalseTotal
$return_pts ? : 0 0 0
169 }
170  
171 # Compute the (split)th trapezoid approximation.
172 13 ▶ for ($x = $lo + $step_len/2; $x < $hi; $x += $step_len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $hi) 190 13 203
173 190 $tot += &$f($x);
174 }
175 13 unshift @row, $tot * $step_len / 2;
176  
177 # Compute the more refined approximations, based on the (split)th
178 # trapezoid approximation and the various (split-1)th refined
179 # approximations stored in @row.
180  
181 13 my $pow4 = 4;
182  
183 13 foreach my $td ( 1 .. $split ) {
184 49 $row[$td] = $row[$td-1] +
185 ($row[$td-1]-$row[$td])/($pow4 - 1);
186 49 $pow4 *= 4;
187 }
188  
189 # row[0] now contains the (split)th trapezoid approximation,
190 # row[1] now contains the (split)th Simpson approximation, and
191 # so on up to row[split] which contains the (split)th Romberg
192 # approximation.
193  
194 # Is this estimate accurate enough?
195 13 $new_estimate = $row[-1];
196 13 ▶ if (($split >= $min_split &&
Branch: 1 branch
BranchTrueFalseTotal
if ($split >= $min_split and abs $new_estimate - $estimate < $abs_err || abs $new_estimate - $estimate < $rel_err * abs($estimate) or $split == $max_split and $$abort = 1) 2 11 13
Condition90%10/11
$split >= $min_split and abs $new_estimate - $estimate < $abs_err || abs $new_estimate - $estimate < $rel_err * abs($estimate) or $split == $max_split and $$abort = 1
l!l&&r!l&&!r
1111
$split >= $min_split and abs $new_estimate - $estimate < $abs_err || abs $new_estimate - $estimate < $rel_err * abs($estimate)
!ll&&!rl&&r
931
abs $new_estimate - $estimate < $abs_err or abs $new_estimate - $estimate < $rel_err * abs($estimate)
l!l&&r!l&&!r
013
$split == $max_split and $$abort = 1
!ll
111
MC/DC75%3/4
$split >= $min_split and abs $new_estimate - $estimate < $abs_err || abs $new_estimate - $estimate < $rel_err * abs($estimate) or $split == $max_split and $$abort = 1
$split >= $min_split abs $new_estimate - $estimate < $abs_err abs $new_estimate - $estimate < $rel_err * abs($estimate) $split == $max_split
Truth table66%4/6
A and B or C or D
A= $split >= $min_splitB= abs $new_estimate - $estimate < $abs_errC= abs $new_estimate - $estimate < $rel_err * abs($estimate)D= $split == $max_split
ABCDresult
11XX1
101X1
0XX11
10011
0XX00
10000
197 (abs($new_estimate - $estimate) < $abs_err ||
198 abs($new_estimate - $estimate) < $rel_err * abs($estimate))) ||
199 ($split == $max_split && ($$abort = 1))) {
200 2 ▶ return $return_pts ? ($new_estimate, $steps + 1) : $new_estimate;
Branch: 1 branch
BranchTrueFalseTotal
$return_pts ? : 0 2 2
201 }
202 11 $estimate = $new_estimate;
203 }
204 }
205  
206 1 1;
↑ Summary