blib/lib/Math/Polynomial/Horner.pm

filter: Statementstmt 97.6% 167 / 171 Branchbran 91.9% 57 / 62 Conditioncond 75.7% 50 / 66 MC/DCmcdc 52.7% 19 / 36 Subroutinesub 100% 18 / 18 Podpod 100% 1 / 1 totaltotal 88.1% 312 / 354 CC 55 SCAR 29.0
CC
55
Cov
92%
as_string
34.7
__ANON__
5.0
__ANON__
5.0
CRAP 18.2
↑ Summary
1 # Copyright 2007, 2008, 2009, 2010, 2011, 2019 Kevin Ryde
2  
3 # This file is part of Math-Polynomial-Horner.
4 #
5 # Math-Polynomial-Horner is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as published
7 # by the Free Software Foundation; either version 3, or (at your option) any
8 # later version.
9 #
10 # Math-Polynomial-Horner is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Math-Polynomial-Horner. If not, see <http://www.gnu.org/licenses/>.
17  
18 package Math::Polynomial::Horner;
19 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # uncomment this to run the ### lines
25 #use Smart::Comments;
26  
27 1 $VERSION = 4;
28  
29 262 ▶sub _stringize {
Subroutine _stringize: called CC=1 SCAR=0.0
30 262 return "$_[0]";
31 }
32  
33 1 ▶use constant _config_defaults =>
Subroutine BEGIN: called CC=1 SCAR=0.0
34 (ascending => 0,
35 with_variable => 1,
36 fold_sign => 0,
37 fold_zero => 1,
38 fold_one => 1,
39 fold_exp_zero => 1,
40 fold_exp_one => 1,
41 convert_coeff => \&_stringize,
42 plus => q{ + },
43 minus => q{ - },
44 leading_plus => q{},
45 leading_minus => q{- },
46 times => q{ },
47 power => q{^},
48 variable => q{x},
49 prefix => q{(},
50 suffix => q{)},
51  
52 # extras
53 left_paren => '(',
54 right_paren => ')',
55  
56 # secret extras
57 fold_sign_swap_end => 0,
58 power_by_times_upto => 0,
59 1 );
60  
61 1 ▶use constant _config_perl =>
Subroutine BEGIN: called CC=1 SCAR=0.0
62 (fold_sign => 1,
63 fold_sign_swap_end => 1,
64 leading_minus => q{-},
65 power => q{**},
66 1 power_by_times_upto => 3);
67  
68 1 ▶use constant _EMPTY => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 ▶use constant _FACTOR => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 1 ▶use constant _SUM => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 168 ▶sub as_string {
Subroutine as_string: called CC=34 SCAR=35.5
Pod documented
73 168 my ($poly, $string_config) = @_;
74 168 my $degree = $poly->degree;
75 ### $degree
76  
77 168 ▶ $string_config ||= ($poly->string_config
Condition33%2/6
$string_config ||= $poly->string_config || ref($poly)->string_config
l!l&&r!l&&!r
01680
$poly->string_config || ref($poly)->string_config
l!l&&r!l&&!r
16800
MC/DC0%0/3
$string_config ||= $poly->string_config || ref($poly)->string_config
$string_config $poly->string_config ref($poly)->string_config
Truth table0%0/4
A ||= B || C
A= $string_configB= $poly->string_configC= ref($poly)->string_config
ABCresult
1XX1
01X1
0011
0000
78 || (ref $poly)->string_config);
79 168 my %config = do {
80 (_config_defaults(),
81 168 ▶ ($string_config->{'for_perl'} ? _config_perl() : ()),
Branch: 1 branch
BranchTrueFalseTotal
$string_config->{'for_perl'} ? : 0 168 168
82 %$string_config)
83 };
84  
85 168 ▶ if ($degree <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($degree <= 0) 35 133 168
86 ### empty or constant
87 35 return $poly->as_string(\%config);
88 }
89  
90 133 my $zero = $poly->coeff_zero;
91 133 my $one = $poly->coeff_one;
92 133 my $convert = $config{'convert_coeff'};
93 133 my $ret = '';
94 133 my $pre = '';
95 133 my $post = '';
96 133 my $last = _EMPTY;
97  
98 26 ▶ my $leading_const = sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
99 26 my ($coeff) = @_;
100 ### leading_const: "$coeff"
101 26 ▶ if ($config{'fold_sign'} && $coeff < $zero) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{'fold_sign'} and $coeff < $zero) { } 0 26 26
Condition66%2/3
$config{"fold_sign"} and $coeff < $zero
!ll&&!rl&&r
1970
MC/DC0%0/2
$config{"fold_sign"} and $coeff < $zero
$config{"fold_sign"} $coeff < $zero
Truth table66%2/3
A and B
A= $config{"fold_sign"}B= $coeff < $zero
ABresult
0X0
100
111
102 0 $ret .= $config{'leading_minus'};
103 0 $coeff = -$coeff;
104 } else {
105 26 $ret .= $config{'leading_plus'};
106 }
107 26 $ret .= $convert->($coeff);
108 26 $last = _FACTOR;
109 133 };
110  
111 102 ▶ my $leading_factor = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
112 102 my ($coeff) = @_;
113 ### leading_factor: "$coeff"
114 102 my $pm = '';
115 102 ▶ if ($config{'fold_sign'} && $coeff < $zero) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{'fold_sign'} and $coeff < $zero) { } 26 76 102
Condition100%3/3
$config{"fold_sign"} and $coeff < $zero
!ll&&!rl&&r
571926
MC/DC100%2/2
$config{"fold_sign"} and $coeff < $zero
$config{"fold_sign"} $coeff < $zero
Truth table100%3/3
A and B
A= $config{"fold_sign"}B= $coeff < $zero
ABresult
0X0
100
111
116 26 $ret .= $config{'leading_minus'};
117 26 $coeff = -$coeff;
118 } else {
119 76 $ret .= $config{'leading_plus'};
120 }
121 102 ▶ if ($config{'fold_one'} && $coeff == $one) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{"fold_one"} and $coeff == $one) 26 76 102
Condition100%3/3
$config{"fold_one"} and $coeff == $one
!ll&&!rl&&r
403626
MC/DC100%2/2
$config{"fold_one"} and $coeff == $one
$config{"fold_one"} $coeff == $one
Truth table100%3/3
A and B
A= $config{"fold_one"}B= $coeff == $one
ABresult
0X0
100
111
122 ### fold_one skip to ret: $ret
123 26 $last = _EMPTY;
124 26 return;
125 }
126 76 $ret .= $pm . $convert->($coeff);
127 76 $last = _FACTOR;
128 ### gives ret: $ret
129 133 };
130  
131 22 ▶ my $times_coeff = sub {
Subroutine __ANON__: called CC=4 SCAR=14.8
132 22 my ($coeff) = @_;
133 ### times_coeff: "$coeff"
134 22 ▶ if ($config{'fold_one'}
Branch: 1 branch
BranchTrueFalseTotal
if ($config{"fold_one"} and $coeff == $one) 0 22 22
Condition66%2/3
$config{"fold_one"} and $coeff == $one
!ll&&!rl&&r
4180
MC/DC0%0/2
$config{"fold_one"} and $coeff == $one
$config{"fold_one"} $coeff == $one
Truth table66%2/3
A and B
A= $config{"fold_one"}B= $coeff == $one
ABresult
0X0
100
111
135 && $coeff == $one) {
136 ### fold_one skip
137 0 return;
138 }
139 22 ▶ if ($last ne _EMPTY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last ne 0) 22 0 22
140 22 $ret .= $config{'times'};
141 }
142 22 $ret .= $convert->($coeff);
143 22 $last = _FACTOR;
144 ### times_coeff gives ret: $ret
145 133 };
146  
147 103 ▶ my $plus_coeff = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
148 103 my ($coeff) = @_;
149 103 ▶ if ($config{'fold_sign'} && $coeff < $zero) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{'fold_sign'} and $coeff < $zero) { } 23 80 103
Condition100%3/3
$config{"fold_sign"} and $coeff < $zero
!ll&&!rl&&r
582223
MC/DC100%2/2
$config{"fold_sign"} and $coeff < $zero
$config{"fold_sign"} $coeff < $zero
Truth table100%3/3
A and B
A= $config{"fold_sign"}B= $coeff < $zero
ABresult
0X0
100
111
150 23 $ret .= $config{'minus'};
151 23 $coeff = -$coeff;
152 } else {
153 80 $ret .= $config{'plus'};
154 }
155 103 $ret .= $convert->($coeff);
156 103 $last = _SUM;
157 133 };
158  
159 133 my $xpow = 0;
160 155 ▶ my $show_xpow = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
161 ### show_xpow: $xpow
162 155 ▶ return if ($xpow == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $xpow == 0 1 154 155
163 154 $ret .= $config{'variable'};
164 154 ▶ if ($xpow == 1 && $config{'fold_exp_one'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($xpow == 1 and $config{'fold_exp_one'}) { } 103 51 154
elsif ($xpow <= $config{'power_by_times_upto'}) { } 11 40 51
Condition66%2/3
$xpow == 1 and $config{"fold_exp_one"}
!ll&&!rl&&r
510103
MC/DC50%1/2
$xpow == 1 and $config{"fold_exp_one"}
$xpow == 1 $config{"fold_exp_one"}
Truth table66%2/3
A and B
A= $xpow == 1B= $config{"fold_exp_one"}
ABresult
0X0
100
111
165 # x^1 -> x
166 } elsif ($xpow <= $config{'power_by_times_upto'}) {
167 # x*x*...*x
168 11 $ret .= ($config{'times'} . $config{'variable'}) x ($xpow-1);
169 } else {
170 # x^123
171 40 $ret .= $config{'power'} . $xpow;
172 }
173 154 $xpow = 0;
174 154 $last = _FACTOR;
175 133 };
176  
177 207 ▶ my $times_xpow = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
178 ### times_xpow: $xpow, $ret
179 207 ▶ if ($xpow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xpow) 120 87 207
180 120 ▶ if ($last eq _SUM) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last eq 2) 14 106 120
181 14 $pre .= $config{'left_paren'};
182 14 $ret .= $config{'right_paren'};
183 14 $last = _FACTOR;
184 }
185 120 ▶ if ($last ne _EMPTY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last ne 0) 94 26 120
186 94 $ret .= $config{'times'};
187 }
188 120 $show_xpow->();
189 120 $last = _FACTOR;
190 }
191 ### times_xpow gives: "pre=$pre ret=$ret"
192 133 };
193  
194 133 ▶ if ($config{'ascending'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{'ascending'}) { } 29 104 133
195 ### ascending
196  
197 29 my $limit = $degree;
198 {
199 29 my ($j, $high, $second);
200 29 ▶ if ($config{'fold_sign'} && $config{'fold_sign_swap_end'}
Branch: 1 branch
BranchTrueFalseTotal
if ($config{"fold_sign"} and $config{"fold_sign_swap_end"} and ($high = $poly->coeff($degree)) > $zero and ($j, $second) = _second_highest_coeff($poly, $config{"fold_zero"}) and $second < $zero) 2 27 29
Condition75%9/12
$config{"fold_sign"} and $config{"fold_sign_swap_end"} and ($high = $poly->coeff($degree)) > $zero and ($j, $second) = _second_highest_coeff($poly, $config{"fold_zero"}) and $second < $zero
!ll&&!rl&&r
2702
$config{"fold_sign"} and $config{"fold_sign_swap_end"} and ($high = $poly->coeff($degree)) > $zero and ($j, $second) = _second_highest_coeff($poly, $config{"fold_zero"})
!ll&&!rl&&r
2702
$config{"fold_sign"} and $config{"fold_sign_swap_end"} and ($high = $poly->coeff($degree)) > $zero
!ll&&!rl&&r
2702
$config{"fold_sign"} and $config{"fold_sign_swap_end"}
!ll&&!rl&&r
2162
MC/DC40%2/5
$config{"fold_sign"} and $config{"fold_sign_swap_end"} and ($high = $poly->coeff($degree)) > $zero and ($j, $second) = _second_highest_coeff($poly, $config{"fold_zero"}) and $second < $zero
$config{"fold_sign"} $config{"fold_sign_swap_end"} ($high = $poly->coeff($degree)) > $zero ($j, $second) = _second_highest_coeff($poly, $config{"fold_zero"}) $second < $zero
Truth table50%3/6
A and B and C and D and E
A= $config{"fold_sign"}B= $config{"fold_sign_swap_end"}C= ($high = $poly->coeff($degree)) > $zeroD= ($j, $second) = _second_highest_coeff($poly, $config{"fold_zero"})E= $second < $zero
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
201 && ($high = $poly->coeff($degree)) > $zero
202 && (($j,$second) = _second_highest_coeff($poly,$config{'fold_zero'}))
203 && $second < $zero) {
204 2 $leading_const->($high);
205 2 $last = _FACTOR;
206  
207 2 $xpow = $degree - $j;
208 2 $times_xpow->();
209  
210 2 $plus_coeff->($second);
211 2 $limit = $j - 1;
212 2 $post = $ret;
213 2 ▶ if ($limit >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit >= 0) 1 1 2
214 $post = $config{'times'}
215 1 . $config{'left_paren'} . $post . $config{'right_paren'};
216 }
217 2 $ret = '';
218 2 $last = _EMPTY;
219 ### fold_sign_swap_end gives
220 ### $post
221 ### $limit
222 }
223 }
224  
225 29 $xpow = -1;
226 29 foreach my $i (0 .. $limit) {
227 ### $i
228 117 $xpow++;
229 117 my $coeff = $poly->coeff($i);
230 117 ▶ if ($config{'fold_zero'} && $coeff == $zero) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{"fold_zero"} and $coeff == $zero) 68 49 117
Condition66%2/3
$config{"fold_zero"} and $coeff == $zero
!ll&&!rl&&r
04968
MC/DC50%1/2
$config{"fold_zero"} and $coeff == $zero
$config{"fold_zero"} $coeff == $zero
Truth table66%2/3
A and B
A= $config{"fold_zero"}B= $coeff == $zero
ABresult
0X0
100
111
231 68 next;
232 }
233  
234 49 ▶ if ($xpow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xpow) 33 16 49
235 33 ▶ if (length($ret)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $ret) 21 12 33
236 21 ▶ if ($i == $degree
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $degree and $config{'fold_sign'} and $coeff < $zero) { } 2 19 21
Condition83%5/6
$i == $degree and $config{"fold_sign"} and $coeff < $zero
!ll&&!rl&&r
1902
$i == $degree and $config{"fold_sign"}
!ll&&!rl&&r
4152
MC/DC66%2/3
$i == $degree and $config{"fold_sign"} and $coeff < $zero
$i == $degree $config{"fold_sign"} $coeff < $zero
Truth table75%3/4
A and B and C
A= $i == $degreeB= $config{"fold_sign"}C= $coeff < $zero
ABCresult
0XX0
10X0
1100
1111
237 && $config{'fold_sign'}
238 && $coeff < $zero) {
239 ### highest coeff fold ... + x*-5 -> ... - x*5
240 2 $coeff = - $coeff;
241 2 $ret .= $config{'minus'};
242 } else {
243 # other coeffs ... + x*(...) or highest ... + x*5
244 19 $ret .= $config{'plus'};
245 }
246 }
247 33 $show_xpow->();
248 33 ▶ if ($i == $degree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $degree) 27 6 33
249 27 ▶ if ($config{'fold_one'}
Branch: 1 branch
BranchTrueFalseTotal
if ($config{'fold_one'} and $coeff == $one) { } 5 22 27
Condition100%3/3
$config{"fold_one"} and $coeff == $one
!ll&&!rl&&r
4185
MC/DC100%2/2
$config{"fold_one"} and $coeff == $one
$config{"fold_one"} $coeff == $one
Truth table100%3/3
A and B
A= $config{"fold_one"}B= $coeff == $one
ABresult
0X0
100
111
250 && $coeff == $one) {
251 ### highest coeff x*1 -> x
252 } else {
253 ### highest coeff: "$coeff"
254 22 $times_coeff->($coeff);
255 }
256 27 last;
257 }
258 6 $ret .= $config{'times'} . $config{'left_paren'};
259 6 $post .= $config{'right_paren'};
260 }
261 22 $leading_const->($coeff);
262 }
263  
264 ### final xpow: $xpow
265 29 ▶ if ($limit != $degree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit != $degree) 2 27 29
266 2 ▶ if ($last != _EMPTY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last != 0) 1 1 2
267 1 $ret .= $config{'plus'};
268 }
269 2 $xpow++;
270 2 $show_xpow->();
271 }
272  
273 } else {
274 ### descending
275  
276 104 my $coeff = $poly->coeff($degree);
277 ### highest coeff: "$coeff"
278 104 my $i = $degree;
279  
280 {
281 104 my ($j, $second);
282 104 ▶ if ($config{'fold_sign'} && $config{'fold_sign_swap_end'}
Branch: 1 branch
BranchTrueFalseTotal
if ($config{"fold_sign"} and $config{"fold_sign_swap_end"} and $coeff < $zero and () = _second_highest_coeff($poly, $config{"fold_zero"}) and $second > $zero) 2 102 104
Condition75%9/12
$config{"fold_sign"} and $config{"fold_sign_swap_end"} and $coeff < $zero and () = _second_highest_coeff($poly, $config{"fold_zero"}) and $second > $zero
!ll&&!rl&&r
10202
$config{"fold_sign"} and $config{"fold_sign_swap_end"} and $coeff < $zero and () = _second_highest_coeff($poly, $config{"fold_zero"})
!ll&&!rl&&r
10202
$config{"fold_sign"} and $config{"fold_sign_swap_end"} and $coeff < $zero
!ll&&!rl&&r
10202
$config{"fold_sign"} and $config{"fold_sign_swap_end"}
!ll&&!rl&&r
57452
MC/DC40%2/5
$config{"fold_sign"} and $config{"fold_sign_swap_end"} and $coeff < $zero and () = _second_highest_coeff($poly, $config{"fold_zero"}) and $second > $zero
$config{"fold_sign"} $config{"fold_sign_swap_end"} $coeff < $zero () = _second_highest_coeff($poly, $config{"fold_zero"}) $second > $zero
Truth table50%3/6
A and B and C and D and E
A= $config{"fold_sign"}B= $config{"fold_sign_swap_end"}C= $coeff < $zeroD= () = _second_highest_coeff($poly, $config{"fold_zero"})E= $second > $zero
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
283 && $coeff < $zero
284 && (($j,$second) = _second_highest_coeff($poly,$config{'fold_zero'}))
285 && $second > $zero) {
286 2 $leading_const->($second);
287 2 $plus_coeff->($coeff);
288 2 $last = _FACTOR;
289 2 $xpow = $degree - $j;
290 2 $times_xpow->();
291 2 $i = $j - 1;
292 2 $last = _SUM;
293 ### fold_sign_swap_end gives
294 ### $ret
295 ### $i
296 }
297 }
298  
299 # normal start from high coeff, ie. not the swap bit
300 104 ▶ if ($i == $degree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $degree) 102 2 104
301 102 $leading_factor->($coeff);
302 102 $i--;
303 }
304 104 ▶ for ( ; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 195 104 299
305 ### $i
306 195 $xpow++;
307 195 $coeff = $poly->coeff($i);
308 195 ▶ if ($config{'fold_zero'} && $coeff == $zero) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{"fold_zero"} and $coeff == $zero) 96 99 195
Condition100%3/3
$config{"fold_zero"} and $coeff == $zero
!ll&&!rl&&r
69396
MC/DC100%2/2
$config{"fold_zero"} and $coeff == $zero
$config{"fold_zero"} $coeff == $zero
Truth table100%3/3
A and B
A= $config{"fold_zero"}B= $coeff == $zero
ABresult
0X0
100
111
309 96 next;
310 }
311 99 $times_xpow->();
312 99 $plus_coeff->($coeff);
313 }
314 104 $times_xpow->();
315 }
316  
317 ### prefix: $config{'prefix'}
318 ### $pre
319 ### $ret
320 ### $post
321 ### suffix: $config{'suffix'}
322 133 return $config{'prefix'} . $pre . $ret . $post . $config{'suffix'};
323 }
324  
325 4 ▶sub _second_highest_coeff {
Subroutine _second_highest_coeff: called CC=4 SCAR=14.2
326 4 my ($poly, $fold_zero) = @_;
327 4 my $j = $poly->degree;
328 ### assert: $j >= 0
329  
330 4 for (;;) {
331 10 ▶ if (--$j < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$j < 0) 0 10 10
332 0 return; # not found
333 }
334 10 my $coeff = $poly->coeff($j);
335 10 ▶ unless ($fold_zero && $coeff == $poly->coeff_zero) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fold_zero and $coeff == $poly->coeff_zero) 4 6 10
Condition66%2/3
$fold_zero and $coeff == $poly->coeff_zero
!ll&&!rl&&r
046
MC/DC50%1/2
$fold_zero and $coeff == $poly->coeff_zero
$fold_zero $coeff == $poly->coeff_zero
Truth table66%2/3
A and B
A= $fold_zeroB= $coeff == $poly->coeff_zero
ABresult
0X0
100
111
336 4 return ($j, $coeff); # found
337 }
338 }
339 }
340  
341 1 1;
342 __END__
↑ Summary