blib/lib/Math/Polynom.pm

filter: Statementstmt 97.6% 291 / 298 Branchbran 88.9% 185 / 208 Conditioncond 84.6% 33 / 39 MC/DCmcdc 68.1% 15 / 22 Subroutinesub 100% 34 / 34 Podpod 100% 13 / 13 totaltotal 93.0% 571 / 614 CC 119 SCAR 29.8
CC
119
Cov
93%
brent
41.9
secant
26.8
newton_raphson
16.1
CRAP 19.6
↑ Summary
1 #################################################################
2 #
3 # Math::Polynom - Operations on polynoms
4 #
5 # $Id: Polynom.pm,v 1.10 2007/07/11 13:01:48 erwan_lemonnier Exp $
6 #
7 # 061025 erwan Started implementation
8 # 061206 erwan Added the secant method
9 # 061214 erwan Added Brent's method
10 # 070112 erwan Fixed bug in identification of nan scalars
11 # 070220 erwan Updated POD to warn for convergence toward non roots
12 # 070404 erwan Added $DEBUG
13 # 070412 erwan Updated POD
14 # 070417 erwan Modified disclaimer
15 # 070711 erwan Use looks_like_number and float_is_nan
16 #
17  
18 package Math::Polynom;
19  
20 16 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 16 ▶use Carp qw(confess croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 16 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 16 ▶use Data::Float qw(float_is_nan);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 16 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 16 ▶use accessors qw(error error_message iterations xpos xneg);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 16 ▶use constant NO_ERROR => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 16 ▶use constant ERROR_NAN => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 16 ▶use constant ERROR_MAX_DEPTH => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 16 ▶use constant ERROR_EMPTY_POLYNOM => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 16 ▶use constant ERROR_DIVIDE_BY_ZERO => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 16 ▶use constant ERROR_WRONG_SIGNS => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 16 ▶use constant ERROR_NOT_A_ROOT => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 16 our $VERSION = '0.13';
39 16 our $DEBUG = 0;
40  
41 #----------------------------------------------------------------
42 #
43 # _debug
44 #
45  
46 414 ▶sub _debug {
Subroutine _debug: called CC=2 SCAR=7.2
47 414 my $msg = shift;
48 414 ▶ print STDOUT "Math::Polynom: $msg\n" if ($DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if $Math::Polynom::DEBUG 0 414 414
49 }
50  
51 #----------------------------------------------------------------
52 #
53 # _add_monom - add a monom to a polynomial, ie a $coef**$power
54 #
55  
56 134 ▶sub _add_monom {
Subroutine _add_monom: called CC=2 SCAR=6.9
57 134 my($self,$coef,$power) = @_;
58  
59 134 ▶ if (exists $self->{polynom}->{$power}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'polynom'}{$power}) { } 13 121 134
60 13 $self->{polynom}->{$power} += $coef;
61 } else {
62 121 $self->{polynom}->{$power} = $coef;
63 }
64 134 return $self;
65 }
66  
67 #----------------------------------------------------------------
68 #
69 # _clean - remove terms with zero as coefficient
70 #
71  
72 238 ▶sub _clean {
Subroutine _clean: called CC=3 SCAR=11.0
73 238 my $self = shift;
74  
75 238 ▶ while (my($power,$coef) = each %{$self->{polynom}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"polynom"};}) 488 238 726
76 488 ▶ if ($coef == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coef == 0) 16 472 488
77 16 delete $self->{polynom}->{$power};
78 }
79 }
80  
81 238 return $self;
82 }
83  
84 #----------------------------------------------------------------
85 #
86 # _is_root - return true if the polynomial evaluates to something close enough to 0 on the root
87 #
88  
89 42 ▶sub _is_root {
Subroutine _is_root: called CC=1 SCAR=0.0
90 42 my ($self,$value) = @_;
91 42 return (abs($self->eval($value)) < 1);
92 }
93  
94 #----------------------------------------------------------------
95 #
96 # _error - die nicely
97 #
98  
99 44 ▶sub _error {
Subroutine _error: called CC=1 SCAR=0.0
100 44 my $msg = shift;
101 44 croak __PACKAGE__." ERROR: $msg\n";
102 }
103  
104 10 ▶sub _exception {
Subroutine _exception: called CC=2 SCAR=7.0
105 10 my ($self,$code,$msg,$args) = @_;
106  
107 10 $msg = "ERROR: $msg\nwith polynom:\n".$self->stringify."\n";
108 10 ▶ if (defined $args) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args) 10 0 10
109 10 $msg .= "with arguments:\n".Dumper($args);
110 }
111 10 $msg .= "at iteration ".$self->iterations."\n";
112  
113 10 $self->error_message($msg);
114 10 $self->error($code);
115  
116 10 croak $self->error_message;
117 }
118  
119 #################################################################
120 #
121 #
122 # PUBLIC
123 #
124 #
125 #################################################################
126  
127 #----------------------------------------------------------------
128 #
129 # new - construct a new polynomial
130 #
131  
132 155 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
Pod documented
133 155 my($pkg,@args) = @_;
134 155 ▶ $pkg = ref $pkg || $pkg;
Condition33%1/3
ref $pkg || $pkg
l!l&&r!l&&!r
01550
MC/DC0%0/2
ref $pkg || $pkg
ref $pkg $pkg
Truth table33%1/3
A || B
A= ref $pkgB= $pkg
ABresult
1X1
011
000
135  
136 155 ▶ _error("new() got odd number of arguments. can not be a hash") if (scalar(@args) % 2);
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@args) % 2 1 154 155
137  
138 154 my %hash = @args;
139 154 foreach my $n (@args) {
140 483 ▶ _error("at least one argument of new() is not numeric:\n".Dumper(\%hash)) if (!looks_like_number($n));
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($n) 1 482 483
141 }
142  
143 153 my $self = bless({polynom => \%hash},$pkg)->_clean;
144 153 $self->error(NO_ERROR);
145 153 $self->iterations(0);
146  
147 153 return $self;
148 }
149  
150 #----------------------------------------------------------------
151 #
152 # clone - return a clone of self
153 #
154  
155 36 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
156 36 my $self = shift;
157 36 return __PACKAGE__->new(%{$self->{polynom}});
158 }
159  
160 #----------------------------------------------------------------
161 #
162 # stringify - return current polynomial as a string
163 #
164  
165 118 ▶sub stringify {
Subroutine stringify: called CC=1 SCAR=0.0
Pod documented
166 118 my $self = shift;
167 118 return join(" + ", map { $self->{polynom}->{$_}."*x^".$_ } reverse sort keys %{$self->{polynom}});
168 }
169  
170 #----------------------------------------------------------------
171 #
172 # derivate - return the polynomial's derivate
173 #
174  
175 20 ▶sub derivate {
Subroutine derivate: called CC=2 SCAR=6.9
Pod documented
176 20 my $self = shift;
177 20 my $result = __PACKAGE__->new();
178 20 ▶ while (my($power,$coef) = each %{$self->{polynom}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"polynom"};}) 46 20 66
179 46 $result->_add_monom($coef*$power,$power-1);
180 }
181 20 return $result->_clean;
182 }
183  
184 #----------------------------------------------------------------
185 #
186 # eval - evaluate the polynomial on a given value, return result
187 #
188  
189 1641 ▶sub eval {
Subroutine eval: called CC=10 SCAR=23.0
Pod documented
190 1641 my($self,$x) = @_;
191  
192 1641 ▶ _error("eval() got wrong number of arguments") if (scalar @_ != 2);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ != 2 2 1639 1641
193 1639 ▶ _error("eval() got undefined argument") if (!defined $x);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 1 1638 1639
194 1638 ▶ _error("eval()'s argument is not numeric ($x)") if (!looks_like_number($x));
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($x) 4 1634 1638
195  
196 1634 my $r = 0;
197 1634 ▶ while (my($power,$coef) = each %{$self->{polynom}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"polynom"};}) 4236 1634 5870
198 4236 $r += $coef*($x**$power);
199 }
200  
201 1634 ▶ if (!float_is_nan($r)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&float_is_nan($r)) 1630 4 1634
202 1630 ▶ if (!defined $self->xpos && $r > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->xpos and $r > 0) { } 37 1593 1630
elsif (not defined $self->xneg and $r < 0) { } 18 2953 2971
Condition100%3/3
not defined $self->xpos and $r > 0
!ll&&!rl&&r
15474637
MC/DC100%2/2
not defined $self->xpos and $r > 0
not defined $self->xpos $r > 0
Truth table100%3/3
not(A) and B
A= not defined $self->xposB= $r > 0
ABresult
0X0
100
111
203 37 $self->xpos($x);
204 ▶ } elsif (!defined $self->xneg && $r < 0) {
Condition100%3/3
not defined $self->xneg and $r < 0
!ll&&!rl&&r
137819718
MC/DC100%2/2
not defined $self->xneg and $r < 0
not defined $self->xneg $r < 0
Truth table100%3/3
not(A) and B
A= not defined $self->xnegB= $r < 0
ABresult
0X0
100
111
205 18 $self->xneg($x);
206 }
207 }
208  
209 1634 return $r;
210 }
211  
212 #----------------------------------------------------------------
213 #
214 # add - add a polynomial/number to current polynomial
215 #
216  
217 27 ▶sub add {
Subroutine add: called CC=6 SCAR=17.9
Pod documented
218 27 my($self,$p) = @_;
219  
220 27 ▶ _error("add() got wrong number of arguments") if (scalar @_ != 2);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ != 2 2 25 27
221 25 ▶ _error("add() got undefined argument") if (!defined $p);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p 1 24 25
222  
223 # adding 2 polynomials
224 24 ▶ if (ref $p eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p eq __PACKAGE__) 20 4 24
225 20 my $result = $self->clone;
226 20 ▶ while (my($power,$coef) = each %{$p->{polynom}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$p{"polynom"};}) 48 20 68
227 48 $result->_add_monom($coef,$power);
228 }
229 20 return $result->_clean;
230 }
231  
232 # adding a constant to a polynomial
233 4 ▶ _error("add() got non numeric argument") if (!looks_like_number($p));
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($p) 1 3 4
234  
235 3 return $self->clone->_add_monom($p,0)->_clean;
236 }
237  
238 #----------------------------------------------------------------
239 #
240 # minus - substract a polynomial/number to current polynomial
241 #
242  
243 15 ▶sub minus {
Subroutine minus: called CC=5 SCAR=16.1
Pod documented
244 15 my($self,$p) = @_;
245  
246 15 ▶ _error("minus() got wrong number of arguments") if (scalar @_ != 2);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ != 2 2 13 15
247 13 ▶ _error("minus() got undefined argument") if (!defined $p);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p 1 12 13
248  
249 12 ▶ if (ref $p eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p eq __PACKAGE__) 8 4 12
250 8 return $self->clone->add($p->negate)->_clean;
251 }
252  
253 4 ▶ _error("minus() got non numeric argument") if (!looks_like_number($p));
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($p) 1 3 4
254  
255 3 return $self->clone->_add_monom(-$p,0)->_clean;
256 }
257  
258 #----------------------------------------------------------------
259 #
260 # negate - negate current polynomial
261 #
262  
263 10 ▶sub negate {
Subroutine negate: called CC=1 SCAR=0.0
Pod documented
264 10 my $self = shift;
265 10 return __PACKAGE__->new(map { $_, - $self->{polynom}->{$_} } keys %{$self->{polynom}})->_clean;
266 }
267  
268 #----------------------------------------------------------------
269 #
270 # multiply - multiply current polynomial with a polynomial/number
271 #
272  
273 22 ▶sub multiply {
Subroutine multiply: called CC=7 SCAR=19.7
Pod documented
274 22 my($self,$p) = @_;
275  
276 22 ▶ _error("multiply() got wrong number of arguments") if (scalar @_ != 2);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ != 2 2 20 22
277 20 ▶ _error("multiply() got undefined argument") if (!defined $p);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p 1 19 20
278  
279 19 ▶ if (ref $p eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p eq __PACKAGE__) 18 1 19
280 18 my $result = __PACKAGE__->new;
281 18 ▶ while (my($power1,$coef1) = each %{$self->{polynom}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"polynom"};}) 21 18 39
282 21 ▶ while (my($power2,$coef2) = each %{$p->{polynom}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$p{"polynom"};}) 34 21 55
283 34 $result->_add_monom($coef1 * $coef2, $power1 + $power2);
284 }
285 }
286 18 return $result->_clean;
287 }
288  
289 1 ▶ _error("multiply() got non numeric argument") if (!looks_like_number($p));
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($p) 1 0 1
290  
291 0 return __PACKAGE__->new(map { $_, $p * $self->{polynom}->{$_} } keys %{$self->{polynom}})->_clean;
292 }
293  
294 #----------------------------------------------------------------
295 #
296 # divide - divide the current polynomial with a float
297 #
298  
299 8 ▶sub divide {
Subroutine divide: called CC=5 SCAR=16.1
Pod documented
300 8 my($self,$x) = @_;
301  
302 8 ▶ _error("divide() got wrong number of arguments") if (scalar @_ != 2);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ != 2 1 7 8
303 7 ▶ _error("divide() got undefined argument") if (!defined $x);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 1 6 7
304 6 ▶ _error("divide() got non numeric argument") if (!looks_like_number($x));
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($x) 2 4 6
305 4 ▶ _error("cannot divide by 0") if ($x == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $x == 0 1 3 4
306  
307 3 return __PACKAGE__->new(map { $_, $self->{polynom}->{$_}/$x } keys %{$self->{polynom}})->_clean;
308 }
309  
310 #----------------------------------------------------------------
311 #
312 # newton_raphson - attempt to find a polynomial's root with Newton Raphson
313 #
314  
315 19 ▶sub newton_raphson {
Subroutine newton_raphson: called CC=16 SCAR=27.8
Pod documented
316 19 my($self,%hash) = @_;
317 19 my $new_guess = 1;
318 19 my $precision = 0.1;
319 19 my $max_depth = 100;
320  
321 19 $self->iterations(0);
322 19 $self->error(NO_ERROR);
323  
324 19 ▶ $new_guess = $hash{guess} if (exists $hash{guess});
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash{"guess"} 14 5 19
325 19 ▶ $precision = $hash{precision} if (exists $hash{precision});
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash{"precision"} 9 10 19
326 19 ▶ $max_depth = $hash{max_depth} if (exists $hash{max_depth});
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash{"max_depth"} 1 18 19
327  
328 19 ▶ _error("newton_raphson() got undefined guess") if (!defined $new_guess);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_guess 1 18 19
329 18 ▶ _error("newton_raphson() got undefined precision") if (!defined $precision);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $precision 1 17 18
330 17 ▶ _error("newton_raphson() got undefined max_depth") if (!defined $max_depth);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max_depth 0 17 17
331 17 ▶ _error("newton_raphson() got non numeric guess") if (!looks_like_number($new_guess));
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($new_guess) 1 16 17
332 16 ▶ _error("newton_raphson() got non numeric precision") if (!looks_like_number($precision));
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($precision) 1 15 16
333 15 ▶ _error("newton_raphson() got non integer max_depth") if ($max_depth !~ /^\d+$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $max_depth =~ /^\d+$/ 0 15 15
334  
335 $self->_exception(ERROR_EMPTY_POLYNOM,"cannot find the root of an empty polynom",\%hash)
336 15 ▶ if (scalar keys %{$self->{polynom}} == 0);
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$$self{"polynom"};} == 0 1 14 15
337  
338 14 my $derivate = $self->derivate;
339 14 my $old_guess = $new_guess - 2*$precision; # pass the while condition first time
340  
341 14 ▶ while (abs($new_guess - $old_guess) > $precision) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $new_guess - $old_guess > $precision) 126 12 138
342 126 $old_guess = $new_guess;
343  
344 126 my $dividend = $derivate->eval($old_guess);
345 126 ▶ $self->_exception(ERROR_DIVIDE_BY_ZERO,"division by zero: polynomial's derivate is 0 at $old_guess",\%hash)
Branch: 1 branch
BranchTrueFalseTotal
if $dividend == 0 0 126 126
346 if ($dividend == 0);
347  
348 126 $new_guess = $old_guess - $self->eval($old_guess)/$dividend;
349  
350 126 $self->iterations($self->iterations + 1);
351 126 ▶ $self->_exception(ERROR_MAX_DEPTH,"reached maximum number of iterations [$max_depth] without getting close enough to the root.",\%hash)
Branch: 1 branch
BranchTrueFalseTotal
if $self->iterations > $max_depth 1 125 126
352 if ($self->iterations > $max_depth);
353  
354 125 ▶ $self->_exception(ERROR_NAN,"new guess is not a real number in newton_raphson().",\%hash)
Branch: 1 branch
BranchTrueFalseTotal
if &float_is_nan($new_guess) 1 124 125
355 if (float_is_nan($new_guess));
356 }
357  
358 12 ▶ if (!$self->_is_root($new_guess)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_root($new_guess)) 0 12 12
359 0 $self->_exception(ERROR_NOT_A_ROOT,"newton_raphson() converges toward $new_guess but that doesn't appear to be a root.",\%hash);
360 }
361  
362 12 return $new_guess;
363 }
364  
365 #----------------------------------------------------------------
366 #
367 # secant - implement the Secant algorithm to approximate the root of this polynomial
368 #
369  
370 23 ▶sub secant {
Subroutine secant: called CC=26 SCAR=32.9
Pod documented
371 23 my ($self,%hash) = @_;
372 23 my $precision = 0.1;
373 23 my $max_depth = 100;
374 23 my ($p0,$p1);
375  
376 23 $self->iterations(0);
377 23 $self->error(NO_ERROR);
378  
379 23 ▶ $precision = $hash{precision} if (exists $hash{precision});
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash{"precision"} 7 16 23
380 23 ▶ $max_depth = $hash{max_depth} if (exists $hash{max_depth});
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash{"max_depth"} 1 22 23
381 23 ▶ $p0 = $hash{p0} if (exists $hash{p0});
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash{"p0"} 21 2 23
382 23 ▶ $p1 = $hash{p1} if (exists $hash{p1});
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash{"p1"} 21 2 23
383  
384 23 ▶ _error("secant() got undefined precision") if (!defined $precision);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $precision 1 22 23
385 22 ▶ _error("secant() got undefined max_depth") if (!defined $max_depth);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max_depth 0 22 22
386 22 ▶ _error("secant() got non numeric precision") if (!looks_like_number($precision));
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($precision) 1 21 22
387 21 ▶ _error("secant() got non integer max_depth") if ($max_depth !~ /^\d+$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $max_depth =~ /^\d+$/ 0 21 21
388 21 ▶ _error("secant() got undefined p0") if (!defined $p0);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p0 1 20 21
389 20 ▶ _error("secant() got undefined p1") if (!defined $p1);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p1 1 19 20
390 19 ▶ _error("secant() got non numeric p0") if (!looks_like_number($p0));
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($p0) 1 18 19
391 18 ▶ _error("secant() got non numeric p1") if (!looks_like_number($p1));
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($p1) 1 17 18
392 17 ▶ _error("secant() got same value for p0 and p1") if ($p0 == $p1);
Branch: 1 branch
BranchTrueFalseTotal
if $p0 == $p1 1 16 17
393  
394 $self->_exception(ERROR_EMPTY_POLYNOM,"cannot find the root of an empty polynomial",\%hash)
395 16 ▶ if (scalar keys %{$self->{polynom}} == 0);
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$$self{"polynom"};} == 0 1 15 16
396  
397 # NOTE: this code is almost a copy/paste from Math::Function::Roots, I just added exception handling
398  
399 15 my $q0 = $self->eval($p0);
400 15 my $q1 = $self->eval($p1);
401 15 my $p;
402  
403 15 ▶ $self->_exception(ERROR_NAN,"q0 or q1 are not a real number in first eval in secant()",\%hash)
Branch: 1 branch
BranchTrueFalseTotal
if &float_is_nan($q0) or &float_is_nan($q1) 0 15 15
Condition33%1/3
&float_is_nan($q0) or &float_is_nan($q1)
l!l&&r!l&&!r
0015
MC/DC0%0/2
&float_is_nan($q0) or &float_is_nan($q1)
&float_is_nan($q0) &float_is_nan($q1)
Truth table33%1/3
A or B
A= &float_is_nan($q0)B= &float_is_nan($q1)
ABresult
1X1
011
000
404 if (float_is_nan($q0) || float_is_nan($q1));
405  
406 15 ▶ return $p0 if ($q0 == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $q0 == 0 0 15 15
407 15 ▶ return $p1 if ($q1 == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $q1 == 0 1 14 15
408  
409 14 ▶ for (my $depth = 1; $depth <= $max_depth; $depth++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth <= $max_depth) 119 1 120
410  
411 119 $self->iterations($depth);
412  
413 119 ▶ $self->_exception(ERROR_DIVIDE_BY_ZERO,"division by zero with p0=$p0, p1=$p1, q1=q0=$q1 in secant()",\%hash)
Branch: 1 branch
BranchTrueFalseTotal
if $q1 - $q0 == 0 0 119 119
414 if (($q1 - $q0) == 0);
415  
416 119 $p = ($q1 * $p0 - $p1 * $q0) / ($q1 - $q0);
417  
418 119 ▶ $self->_exception(ERROR_NAN,"p is not a real number in secant()",\%hash)
Branch: 1 branch
BranchTrueFalseTotal
if &float_is_nan($p) 0 119 119
419 if (float_is_nan($p));
420  
421 119 my $debug = "secant at depth ".$self->iterations.", p0=$p0, p1=$p1, p=$p";
422  
423 119 $p0 = $p1;
424 119 $q0 = $q1;
425 119 $q1 = $self->eval($p);
426  
427 119 ▶ $self->_exception(ERROR_NAN,"q1 is not a real number in secant()",\%hash)
Branch: 1 branch
BranchTrueFalseTotal
if &float_is_nan($q1) 1 118 119
428 if (float_is_nan($q1));
429  
430 118 _debug($debug.", poly(p)=$q1");
431  
432 118 ▶ if ($q1 == 0 || abs($p - $p1) <= $precision) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q1 == 0 or abs $p - $p1 <= $precision) 12 106 118
Condition100%3/3
$q1 == 0 or abs $p - $p1 <= $precision
l!l&&r!l&&!r
111106
MC/DC100%2/2
$q1 == 0 or abs $p - $p1 <= $precision
$q1 == 0 abs $p - $p1 <= $precision
Truth table100%3/3
A or B
A= $q1 == 0B= abs $p - $p1 <= $precision
ABresult
1X1
011
000
433 12 ▶ if (!$self->_is_root($p)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_root($p)) 0 12 12
434 0 $self->_exception(ERROR_NOT_A_ROOT,"secant() converges toward $p but that doesn't appear to be a root.",\%hash);
435 }
436 12 return $p;
437 }
438  
439 106 $p1 = $p;
440 }
441  
442 1 $self->_exception(ERROR_MAX_DEPTH,"reached maximum number of iterations [$max_depth] without getting close enough to the root in secant()",\%hash);
443 }
444  
445 #----------------------------------------------------------------
446 #
447 # brent - implement Brent's method to approximate the root of this polynomial
448 #
449  
450 32 ▶sub brent {
Subroutine brent: called CC=41 SCAR=37.4
Pod documented
451 32 my($self,%hash) = @_;
452 32 my $precision = 0.1;
453 32 my $max_depth = 100;
454 32 my $mflag;
455 32 my($a,$b,$c,$s,$d);
456 32 my($f_a,$f_b,$f_c,$f_s);
457  
458 32 $self->iterations(0);
459 32 $self->error(NO_ERROR);
460  
461 32 ▶ $precision = $hash{precision} if (exists $hash{precision});
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash{"precision"} 8 24 32
462 32 ▶ $max_depth = $hash{max_depth} if (exists $hash{max_depth});
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash{"max_depth"} 2 30 32
463 32 ▶ $a = $hash{a} if (exists $hash{a});
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash{"a"} 30 2 32
464 32 ▶ $b = $hash{b} if (exists $hash{b});
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash{"b"} 30 2 32
465  
466 32 ▶ _error("brent() got undefined precision") if (!defined $precision);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $precision 1 31 32
467 31 ▶ _error("brent() got undefined max_depth") if (!defined $max_depth);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max_depth 0 31 31
468 31 ▶ _error("brent() got non numeric precision") if (!looks_like_number($precision));
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($precision) 1 30 31
469 30 ▶ _error("brent() got non integer max_depth") if ($max_depth !~ /^\d+$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $max_depth =~ /^\d+$/ 0 30 30
470 30 ▶ _error("brent() got undefined a") if (!defined $a);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a 1 29 30
471 29 ▶ _error("brent() got undefined b") if (!defined $b);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 1 28 29
472 28 ▶ _error("brent() got non numeric a") if (!looks_like_number($a));
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($a) 1 27 28
473 27 ▶ _error("brent() got non numeric b") if (!looks_like_number($b));
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($b) 1 26 27
474 26 ▶ _error("brent() got same value for a and b") if ($a == $b);
Branch: 1 branch
BranchTrueFalseTotal
if $a == $b 1 25 26
475  
476 $self->_exception(ERROR_EMPTY_POLYNOM,"cannot find the root of an empty polynomial in brent()",\%hash)
477 25 ▶ if (scalar keys %{$self->{polynom}} == 0);
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$$self{"polynom"};} == 0 1 24 25
478  
479 # The following is an implementation of Brent's method as described on wikipedia
480 # variable names are chosen to match the pseudocode listed on wikipedia
481 # There are a few differences between this code and the pseudocode on wikipedia though...
482  
483 24 $f_a = $self->eval($a);
484 24 $f_b = $self->eval($b);
485  
486 # if the polynom evaluates to a complex number on $a or $b (ex: square root, when $a = -1)
487 24 ▶ $self->_exception(ERROR_NAN,"polynomial is not defined on interval [a=$a, b=$b] in brent()",\%hash)
Branch: 1 branch
BranchTrueFalseTotal
if &float_is_nan($f_a) or &float_is_nan($f_b) 0 24 24
Condition33%1/3
&float_is_nan($f_a) or &float_is_nan($f_b)
l!l&&r!l&&!r
0024
MC/DC0%0/2
&float_is_nan($f_a) or &float_is_nan($f_b)
&float_is_nan($f_a) &float_is_nan($f_b)
Truth table33%1/3
A or B
A= &float_is_nan($f_a)B= &float_is_nan($f_b)
ABresult
1X1
011
000
488 if (float_is_nan($f_a) || float_is_nan($f_b));
489  
490 # did we hit the root by chance?
491 24 ▶ return $a if ($f_a == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $f_a == 0 1 23 24
492 23 ▶ return $b if ($f_b == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $f_b == 0 2 21 23
493  
494 # $a and $b should be chosen so that poly($a) and poly($b) have opposite signs.
495 # It is a prerequisite for the bisection part of Brent's method to work
496 21 ▶ $self->_exception(ERROR_WRONG_SIGNS,"polynomial does not have opposite signs at a=$a and b=$b in brent()",\%hash)
Branch: 1 branch
BranchTrueFalseTotal
if $f_a * $f_b > 0 2 19 21
497 if ($f_a*$f_b > 0);
498  
499 # eventually swap $a and $b (don't forget to even switch f(c))
500 19 ▶ if (abs($f_a) < abs($f_b)) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $f_a < abs $f_b) 14 5 19
501 14 ($a,$b) = ($b,$a);
502 14 ($f_a,$f_b) = ($f_b,$f_a);
503 }
504  
505 19 $c = $a;
506 19 $f_c = $f_a;
507  
508 19 $mflag = 1;
509  
510 # repeat while we haven't found the root nor are close enough to it
511 19 ▶ while ($f_b != 0 && abs($b - $a) > $precision) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f_b != 0 and abs $b - $a > $precision) 297 18 315
Condition100%3/3
$f_b != 0 and abs $b - $a > $precision
!ll&&!rl&&r
513297
MC/DC100%2/2
$f_b != 0 and abs $b - $a > $precision
$f_b != 0 abs $b - $a > $precision
Truth table100%3/3
A and B
A= $f_b != 0B= abs $b - $a > $precision
ABresult
0X0
100
111
512  
513 # did we reach the maximum number of iterations?
514 297 ▶ $self->_exception(ERROR_MAX_DEPTH,"reached maximum number of iterations [$max_depth] without getting close enough to the root in brent()",\%hash)
Branch: 1 branch
BranchTrueFalseTotal
if $self->iterations > $max_depth 1 296 297
515 if ($self->iterations > $max_depth);
516  
517 # evaluate f(a), f(b) and f(c) if necessary
518 296 ▶ if ($self->iterations != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->iterations != 0) 277 19 296
519 277 $f_a = $self->eval($a);
520 277 $f_b = $self->eval($b);
521 277 $f_c = $self->eval($c);
522  
523 277 ▶ $self->_exception(ERROR_NAN,"polynomial leads to an imaginary number on a=$a in brent()",\%hash) if (float_is_nan($f_a));
Branch: 1 branch
BranchTrueFalseTotal
if &float_is_nan($f_a) 0 277 277
524 277 ▶ $self->_exception(ERROR_NAN,"polynomial leads to an imaginary number on b=$b in brent()",\%hash) if (float_is_nan($f_b));
Branch: 1 branch
BranchTrueFalseTotal
if &float_is_nan($f_b) 0 277 277
525 277 ▶ $self->_exception(ERROR_NAN,"polynomial leads to an imaginary number on c=$c in brent()",\%hash) if (float_is_nan($f_c));
Branch: 1 branch
BranchTrueFalseTotal
if &float_is_nan($f_c) 0 277 277
526 }
527  
528 296 my $debug = "brent at depth ".$self->iterations.", a=$a, b=$b";
529  
530 # calculate the next root candidate
531 296 ▶ if ($f_a == $f_b) {
Branch: 2 branches
BranchTrueFalseTotal
if ($f_a == $f_b) { } 0 296 296
elsif ($f_a != $f_c and $f_b != $f_c) { } 51 245 296
532 # we should not be able to get $f_b == $f_a since it's a prerequisite of the method. that would be a bug
533 0 _error("BUG: got same values for polynomial at a=$a and b=$b:\n".$self->stringify);
534  
535 ▶ } elsif ( ($f_a != $f_c) && ($f_b != $f_c) ) {
Condition100%3/3
$f_a != $f_c and $f_b != $f_c
!ll&&!rl&&r
8815751
MC/DC100%2/2
$f_a != $f_c and $f_b != $f_c
$f_a != $f_c $f_b != $f_c
Truth table100%3/3
A and B
A= $f_a != $f_cB= $f_b != $f_c
ABresult
0X0
100
111
536 # use quadratic interpolation
537 51 $s = ($a*$f_b*$f_c)/(($f_a - $f_b)*($f_a - $f_c)) +
538 ($b*$f_a*$f_c)/(($f_b - $f_a)*($f_b - $f_c)) +
539 ($c*$f_a*$f_b)/(($f_c - $f_a)*($f_c - $f_b));
540 } else {
541 # otherwise use the secant
542 245 $s = $b - $f_b*($b - $a)/($f_b - $f_a);
543 }
544  
545 # now comes the main difference between Brent's method and Dekker's method: we want to use bisection when appropriate
546 296 ▶ if ( ( ($s < (3*$a+$b)/4) && ($s > $b) ) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($s < (3 * $a + $b) / 4 and $s > $b or $mflag and abs $s - $b >= abs($b - $c) / 2 or not $mflag and abs $s - $b >= abs($c - $d) / 2) { } 225 71 296
Condition100%15/15
$s < (3 * $a + $b) / 4 and $s > $b or $mflag and abs $s - $b >= abs($b - $c) / 2 or not $mflag and abs $s - $b >= abs($c - $d) / 2
l!l&&r!l&&!r
2071871
$s < (3 * $a + $b) / 4 and $s > $b or $mflag and abs $s - $b >= abs($b - $c) / 2
l!l&&r!l&&!r
1466189
$s < (3 * $a + $b) / 4 and $s > $b
!ll&&!rl&&r
10941146
$mflag and abs $s - $b >= abs($b - $c) / 2
!ll&&!rl&&r
484161
not $mflag and abs $s - $b >= abs($c - $d) / 2
!ll&&!rl&&r
413018
MC/DC83%5/6
$s < (3 * $a + $b) / 4 and $s > $b or $mflag and abs $s - $b >= abs($b - $c) / 2 or not $mflag and abs $s - $b >= abs($c - $d) / 2
$s < (3 * $a + $b) / 4 $s > $b $mflag abs $s - $b >= abs($b - $c) / 2 not $mflag abs $s - $b >= abs($c - $d) / 2
Truth table53%8/15
A and B or C and D or not(E) and F
A= $s < (3 * $a + $b) / 4B= $s > $bC= $mflagD= abs $s - $b >= abs($b - $c) / 2E= not $mflagF= abs $s - $b >= abs($c - $d) / 2
ABCDEFresult
11XXXX1
0X11XX1
1011XX1
0X0X111
0X10111
100X111
1010111
0X0X0X0
0X0X100
0X100X0
0X10100
100X0X0
100X100
10100X0
1010100
547 ( $mflag && (abs($s-$b) >= (abs($b-$c)/2)) ) ||
548 ( !$mflag && (abs($s-$b) >= (abs($c-$d)/2)) ) ) {
549 # in that case, use the bisection to get $s
550 225 $s = ($a + $b)/2;
551 225 $mflag = 1;
552 } else {
553 71 $mflag = 0;
554 }
555  
556 # calculate f($s)
557 296 $f_s = $self->eval($s);
558  
559 296 ▶ $self->_exception(ERROR_NAN,"polynomial leads to an imaginary number on s=$s in brent()",\%hash) if (float_is_nan($f_s));
Branch: 1 branch
BranchTrueFalseTotal
if &float_is_nan($f_s) 0 296 296
560  
561 296 _debug($debug.", s=$s, poly(s)=$f_s");
562  
563 296 $d = $c;
564 296 $c = $b;
565 296 $f_c = $f_b;
566  
567 296 ▶ if ($f_a*$f_s <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f_a * $f_s <= 0) { } 62 234 296
568 # important that b=s if f(s)=0 since the while loop checks f(b)
569 # if f(a)=0, and f(b)!=0, then a and b will be swaped and we will therefore have f(b)=0
570 62 $b = $s;
571 62 $f_b = $f_s;
572 } else {
573 234 $a = $s;
574 234 $f_a = $f_s;
575 }
576  
577 # eventually swap $a and $b
578 296 ▶ if (abs($f_a) < abs($f_b)) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $f_a < abs $f_b) 73 223 296
579 # in the special case when
580 73 ($a,$b) = ($b,$a);
581 73 ($f_a,$f_b) = ($f_b,$f_a);
582 }
583  
584 296 $self->iterations($self->iterations + 1);
585 }
586  
587 18 ▶ if (!$self->_is_root($b)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_root($b)) 0 18 18
588 0 $self->_exception(ERROR_NOT_A_ROOT,"brent() converges toward $b but that doesn't appear to be a root.",\%hash);
589 }
590  
591 18 return $b;
592 }
593  
594 16 1;
595  
596 __END__
↑ Summary