blib/lib/Math/Vector/Real/Farthest.pm

filter: Statementstmt 73.7% 174 / 236 Branchbran 58.9% 66 / 112 Conditioncond 50.0% 3 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 84.2% 16 / 19 Podpod 100% 3 / 3 totaltotal 69.4% 264 / 380 CC 66 SCAR 46.2
CC
66
Cov
69%
_find_2d_convex_hull
380.0
find
34.2
find_2d_convex_hull
12.0
CRAP 101.7
↑ Summary
1 package Math::Vector::Real::Farthest;
2  
3 1 our $VERSION = '0.02';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Math::Vector::Real;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Sort::Key::Top qw(nslotpartref);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Math::nSphere qw(nsphere_volumen);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our $optimization_core = 1;
14 1 our $optimization_convex_hull = 0;
15 1 our $threshold_brute_force = 16;
16 1 our $O = 0;
17  
18 1 ▶use constant _c0 => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use constant _c1 => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use constant _n => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use constant _vs => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use constant _s0 => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use constant _s1 => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 121 ▶sub _find_brute_force {
Subroutine _find_brute_force: called CC=5 SCAR=16.1
26 121 my $best_d2 = 0;
27 121 my @best_vs = ($_[0], $_[0]);
28  
29 121 for my $i (0..$#_) {
30 14464 for my $j ($i + 1..$#_) {
31 3197919 my $vi = $_[$i];
32 3197919 my $vj = $_[$j];
33 3197919 my $d2 = Math::Vector::Real::dist2($vi, $vj);
34 3197919 ▶ if ($d2 > $best_d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d2 > $best_d2) 924 3196995 3197919
35 924 $best_d2 = $d2;
36 924 @best_vs = ($vi, $vj);
37 }
38 }
39 }
40  
41 121 ▶ wantarray ? ($best_d2, map V(@$_), @best_vs) : $best_d2;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 121 0 121
42 }
43  
44 13 ▶sub _find_1d {
Subroutine _find_1d: called CC=9 SCAR=22.0
45 13 my $max = $_[0][0];
46 13 my $min = $max;
47 13 ▶ shift if @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 5 8 13
48 13 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1448 13 1461
49 1448 my $a = shift->[0];
50 1448 my $b = shift->[0];
51 1448 ▶ if ($a > $b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a > $b) { } 735 713 1448
52 735 ▶ $max = $a if $a > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $a > $max 21 714 735
53 735 ▶ $min = $b if $b < $min;
Branch: 1 branch
BranchTrueFalseTotal
if $b < $min 25 710 735
54 }
55 else {
56 713 ▶ $max = $b if $b > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $b > $max 30 683 713
57 713 ▶ $min = $a if $a < $min;
Branch: 1 branch
BranchTrueFalseTotal
if $a < $min 21 692 713
58 }
59 }
60 13 my $d = $max - $min;
61 13 my $d2 = $d * $d;
62 13 ▶ wantarray ? ($d2, V($min), V($max)) : $d2;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 13 0 13
63 }
64  
65  
66 1 my $skr_loaded;
67 0 ▶sub _find_2d_convex_hull {
Subroutine _find_2d_convex_hull: not called CC=19 SCAR=59.4
68  
69 0 ▶ $skr_loaded++ or require Sort::Key::Radix;
Branch: 1 branch
BranchTrueFalseTotal
$skr_loaded++ or require Sort::Key::Radix 0 0 0
70 0 ▶ my @p = &Sort::Key::Radix::nkeysort(sub { $_->[0] }, @_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
71  
72 # use GD;
73 # my $size = 1024;
74 # my $im = new GD::Image($size, $size);
75 # my $white = $im->colorAllocate(255,255,255);
76 # my $black = $im->colorAllocate(0,0,0);
77 # my $blue = $im->colorAllocate(0,0,255);
78 # my $red = $im->colorAllocate(255,0,0);
79 # my $green = $im->colorAllocate(0,255,0);
80 # my $gray = $im->colorAllocate(200, 200, 200);
81 # my $yellow = $im->colorAllocate(255, 255, 0);
82 # $im->rectangle(0, 0, $size, $size, $white);
83 # $im->filledEllipse($_->[0] * $size, $_->[1] * $size, 2, 2, $black) for @p;
84  
85 0 my (@u, @l);
86 0 my $i = 0;
87 0 ▶ while ($i < @p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @p) 0 0 0
88 0 my $iu = my $il = $i;
89 0 my ($x, $yu) = @{$p[$i]};
90 0 my $yl = $yu;
91 # search for upper and lower Y for the current X
92 0 ▶ while (++$i < @p and $p[$i][0] == $x) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$i < @p and $p[$i][0] == $x) 0 0 0
Condition0%0/3
++$i < @p and $p[$i][0] == $x
!ll&&!rl&&r
000
MC/DC0%0/2
++$i < @p and $p[$i][0] == $x
++$i < @p $p[$i][0] == $x
Truth table0%0/3
A and B
A= ++$i < @pB= $p[$i][0] == $x
ABresult
0X0
100
111
93 0 my $y = $p[$i][1];
94 0 ▶ if ($y < $yl) {
Branch: 2 branches
BranchTrueFalseTotal
if ($y < $yl) { } 0 0 0
elsif ($y > $yu) { } 0 0 0
95 0 $il = $i;
96 0 $yl = $y;
97 }
98 elsif ($y > $yu) {
99 0 $iu = $i;
100 0 $yu = $y;
101 }
102 }
103 0 ▶ while (@l >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@l >= 2) 0 0 0
104 0 my ($ox, $oy) = @{$l[-2]};
105 0 ▶ last if ($l[-1][1] - $oy) * ($x - $ox) < ($yl - $oy) * ($l[-1][0] - $ox);
Branch: 1 branch
BranchTrueFalseTotal
if ($l[-1][1] - $oy) * ($x - $ox) < ($yl - $oy) * ($l[-1][0] - $ox) 0 0 0
106 0 pop @l;
107 }
108 0 push @l, $p[$il];
109 0 ▶ while (@u >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@u >= 2) 0 0 0
110 0 my ($ox, $oy) = @{$u[-2]};
111 0 ▶ last if ($u[-1][1] - $oy) * ($x - $ox) > ($yu - $oy) * ($u[-1][0] - $ox);
Branch: 1 branch
BranchTrueFalseTotal
if ($u[-1][1] - $oy) * ($x - $ox) > ($yu - $oy) * ($u[-1][0] - $ox) 0 0 0
112 0 pop @u;
113 }
114 0 push @u, $p[$iu];
115 }
116  
117 # $im->filledEllipse($_->[0] * $size, $_->[1] * $size, 12, 12, $blue) for @u;
118 # $im->filledEllipse($_->[0] * $size, $_->[1] * $size, 12, 12, $green) for @l;
119  
120 0 my $u = $l[-1];
121 0 my $l = $u[0];
122 0 my $d = V(0, 1);
123 0 ▶ pop @u if $u->[1] == $u[-1][1];
Branch: 1 branch
BranchTrueFalseTotal
if $u->[1] == $u[-1][1] 0 0 0
124 0 ▶ shift @l if $l->[1] == $l[0][1];
Branch: 1 branch
BranchTrueFalseTotal
if $l->[1] == $l[0][1] 0 0 0
125 0 my $best_d2 = 0;
126 0 my @best_vs = ($u, $u);
127 0 while (1) {
128 # print "u: $u, l: $l\n";
129 # $im->line(map($_ * $size, @$u, @$l), $yellow);
130 0 my $d2 = Math::Vector::Real::dist2($u, $l);
131 0 ▶ if ($d2 > $best_d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d2 > $best_d2) 0 0 0
132 0 $best_d2 = $d2;
133 0 @best_vs = ($u, $l);
134 }
135  
136 0 ▶ if (not @u) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @u) { } 0 0 0
elsif (not @l) { } 0 0 0
137 0 ▶ last unless @l;
Branch: 1 branch
BranchTrueFalseTotal
unless @l 0 0 0
138 0 $l = shift @l;
139 }
140 elsif(not @l) {
141 0 $u = pop @u;
142 }
143 else {
144 0 my $du = Math::Vector::Real::versor($u[-1] - $u);
145 0 my $dl = Math::Vector::Real::versor($l - $l[0]);
146 0 ▶ if ($du * $d > $dl * $d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($du * $d > $dl * $d) { } 0 0 0
147 0 $u = pop @u;
148 0 $d = $du;
149 }
150 else {
151 0 $l = shift @l;
152 0 $d = $dl;
153 }
154 }
155 }
156  
157  
158 #my ($alt_d2, @alt_vs) = _find_brute_force(@_);
159 #if ($alt_d2 != $best_d2) {
160 #$im->filledEllipse($_->[0] * $size, $_->[1] * $size, 8, 8, $gray) for @best_vs;
161 #$im->filledEllipse($_->[0] * $size, $_->[1] * $size, 4, 4, $red) for @alt_vs;
162 #open my $fh, '>', "frame.png";
163 #print $fh $im->png;
164 #exit -1;
165 #}
166 0 ▶ wantarray ? ($best_d2, map V(@$_), @best_vs) : $best_d2;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
167 }
168  
169 100 ▶sub find {
Subroutine find: called CC=34 SCAR=35.3
Pod documented
170 100 shift;
171  
172 100 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 100 100
173 100 my $dim = @{$_[0]};
174  
175 100 my @vs;
176 100 ▶ if (@_ <= 10) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ <= 10) 30 70 100
177 30 ▶ if (@_ <= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ <= 2) 6 24 30
178 # shortcut for sets of 1 and 2 elements
179 6 my @best_vs = @_[0, -1];
180 6 my $best_d2 = Math::Vector::Real::dist2(@best_vs);
181 6 ▶ return (wantarray ? ($best_d2, map V(@$_), @best_vs) : $best_d2);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 0 6
182 }
183 24 ▶ $dim > 1 and goto &_find_brute_force;
Branch: 1 branch
BranchTrueFalseTotal
$dim > 1 and goto \&Math::Vector::Real::Farthest::_find_brute_force 21 3 24
184 }
185  
186 73 ▶ if ($dim <= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dim <= 2) 23 50 73
187 # use specialized algorithm for 1D
188 23 ▶ $dim == 1 and goto &_find_1d;
Branch: 1 branch
BranchTrueFalseTotal
$dim == 1 and goto \&Math::Vector::Real::Farthest::_find_1d 13 10 23
189  
190 # use specialized algorithm for 2D
191 10 ▶ goto &_find_2d_convex_hull if $optimization_convex_hull;
Branch: 1 branch
BranchTrueFalseTotal
if $Math::Vector::Real::Farthest::optimization_convex_hull 0 10 10
192 }
193  
194 60 my ($best_d2, @best_vs);
195 ### $O++;
196 60 my ($c0, $c1) = Math::Vector::Real->box(@_);
197 60 my $diag = $c1 - $c0;
198 60 my $max_comp = $diag->max_component;
199 60 $best_d2 = $max_comp * $max_comp;
200 60 ▶ if ($best_d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($best_d2) { } 60 0 60
201  
202 60 my $vs0;
203  
204 60 ▶ if ($optimization_core) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Math::Vector::Real::Farthest::optimization_core) { } 60 0 60
205 # There is a place in the center of the box which is
206 # guaranteed to not contain any of the target vectors. We
207 # calculate its aproximate hyper-volumen and if it is at least
208 # 10% of that of the box we filter out the points within. This
209 # heuristic works well when the vectors are evenly
210 # distributed.
211  
212 # Benchmarks show that this optimization can provide a
213 # huge gain in some cases while non impacting performance
214 # on the rest.
215  
216 60 my $nellipsoid_volumen = nsphere_volumen(scalar(@$diag));
217 60 my $ncube_volumen = 1;
218 60 my $half = 0.5 * $diag;
219 60 my $t2 = $half->abs2 - $best_d2;
220 60 for my $ix (0..$#$half) {
221 305 my $y = $half->[$ix];
222 305 my $y2 = $y * $y;
223 305 ▶ if ($t2 + 3 * $y2 > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t2 + 3 * $y2 > 0) 304 1 305
224 304 ▶ if ($y2 > $t2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 > $t2) { } 114 190 304
225 114 $y = sqrt($y2 - $t2) - $y;
226 }
227 else {
228 190 $y = 0;
229 }
230 }
231 305 $nellipsoid_volumen *= $y;
232 305 $ncube_volumen *= $diag->[$ix];
233 }
234  
235 # we don't want to discard points that are at a distance
236 # exactly equal to the bigest box side, so we apply a small
237 # correction factor here:
238 60 $best_d2 *= 0.99999;
239  
240 60 ▶ if ($nellipsoid_volumen > $ncube_volumen * 0.1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nellipsoid_volumen > $ncube_volumen * 0.1) { } 13 47 60
241 # we aim at discarding at least 10% of the points
242 13 my $zero = 0.5 * ($c0 + $c1);
243 13 my $corner = $c0 - $zero;
244 13 $vs0 = [grep { Math::Vector::Real::dist2($corner, ($_ - $zero)->first_orthant_reflection) > $best_d2 } @_];
245 }
246 else {
247 47 $vs0 = \@_;
248 }
249 }
250 else {
251 0 $best_d2 *= 0.99999;
252 0 $vs0 = \@_;
253 }
254  
255 60 my @d2 = $diag->abs2;
256 60 my @a = [$c0, $c1, scalar(@$vs0), $vs0];
257 60 my @b = undef;
258  
259 60 ▶ while (@d2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@d2) 15274 38 15312
260 15274 my $d2 = pop @d2;
261 15274 ▶ $d2 > $best_d2 or last;
Branch: 1 branch
BranchTrueFalseTotal
$d2 > $best_d2 or last 15252 22 15274
262 ### $O++;
263 15252 my $a = pop @a;
264 15252 my $b = pop @b;
265 15252 ▶ ($a, $b) = ($b, $a) if $b and $a->[_n] < $b->[_n];
Branch: 1 branch
BranchTrueFalseTotal
if $b and $a->[2] < $b->[2] 8552 6700 15252
Condition100%3/3
$b and $a->[2] < $b->[2]
!ll&&!rl&&r
42162798552
MC/DC100%2/2
$b and $a->[2] < $b->[2]
$b $a->[2] < $b->[2]
Truth table100%3/3
A and B
A= $bB= $a->[2] < $b->[2]
ABresult
0X0
100
111
266 15252 ▶ if (my $avs = $a->[_vs]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $avs = $a->[3]) 7529 7723 15252
267 7529 ▶ if ($a->[_n] <= $threshold_brute_force) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->[2] <= $Math::Vector::Real::Farthest::threshold_brute_force) 6733 796 7529
268 6733 ▶ if ($b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b) { } 6651 82 6733
269 # brute force
270 ### $O += @$avs * $b->[_n];
271 6651 for my $v0 (@{$b->[_vs]}) {
272 71185 for my $v1 (@$avs) {
273 872385 my $d2 = Math::Vector::Real::dist2($v0, $v1);
274 872385 ▶ if ($best_d2 < $d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($best_d2 < $d2) 411 871974 872385
275 411 $best_d2 = $d2;
276 411 @best_vs = ($v0, $v1);
277 }
278 }
279 }
280 }
281 else {
282 ### $O += ((@$avs - 1) * @$avs) >> 1;
283 82 for my $i (1..$#$avs) {
284 971 my $v0 = $avs->[$i];
285 971 for my $v1 (@$avs[0 .. $i - 1]) {
286 6548 my $d2 = Math::Vector::Real::dist2($v0, $v1);
287 6548 ▶ if ($best_d2 < $d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($best_d2 < $d2) 22 6526 6548
288 22 $best_d2 = $d2;
289 22 @best_vs = ($v0, $v1);
290 }
291 }
292 }
293 }
294 6733 next;
295 }
296  
297 # else part it in two...
298 ### $O += @$avs;
299 796 my $ix = ($a->[_c0] - $a->[_c1])->max_component_index;
300 796 my ($avs0, $avs1) = nslotpartref $ix => @$avs / 2 => @$avs;
301 796 $a->[_s0] = [Math::Vector::Real->box(@$avs0), scalar(@$avs0), $avs0];
302 796 $a->[_s1] = [Math::Vector::Real->box(@$avs1), scalar(@$avs1), $avs1];
303 796 undef $a->[_vs];
304  
305 # and fall-through...
306 }
307  
308 8519 my ($a0, $a1) = @{$a}[_s0, _s1];
309 # If $b is defined we generate the combinations ($a0-$b,
310 # $a1-$b), otherwise it means we want to compare a with
311 # itself and so we generate the trio of pairs ($a0-$a1,
312 # $a0-$a0, $a1-$a1).
313 8519 my (@na, @nb, @nd2);
314 8519 ▶ if ($b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b) { } 8180 339 8519
315 8180 @na = ($a0, $a1);
316 8180 @nb = ($b, $b);
317 8180 @nd2 = (Math::Vector::Real->max_dist2_between_boxes(@{$a0}[_c0, _c1], @{$b}[_c0, _c1]),
318 8180 Math::Vector::Real->max_dist2_between_boxes(@{$a1}[_c0, _c1], @{$b}[_c0, _c1]));
319 }
320 else {
321 339 @na = ($a0, $a0, $a1);
322 339 @nb = ($a1);
323 339 @nd2 = (Math::Vector::Real->max_dist2_between_boxes(@{$a0}[_c0, _c1], @{$a1}[_c0, _c1]),
324 339 Math::Vector::Real::dist2(@{$a0}[_c0, _c1]),
325 339 Math::Vector::Real::dist2(@{$a1}[_c0, _c1]));
326 }
327  
328 8519 ▶ while (@na) {
Branch: 1 branch
BranchTrueFalseTotal
if (@na) 17377 8519 25896
329 17377 my $a = shift @na;
330 17377 my $b = shift @nb;
331 17377 my $d2 = shift @nd2;
332  
333 17377 ▶ if ($d2 > $best_d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d2 > $best_d2) 15324 2053 17377
334 15324 my $p;
335 15324 ▶ for ($p = @d2; $p > 0; $p--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p > 0) 2683950 514 2684464
336 ### $O++;
337 2683950 ▶ last if $d2[$p - 1] <= $d2;
Branch: 1 branch
BranchTrueFalseTotal
if $d2[$p - 1] <= $d2 14810 2669140 2683950
338 }
339 15324 splice @d2, $p, 0, $d2;
340 15324 splice @a, $p, 0, $a;
341 15324 splice @b, $p, 0, $b;
342 }
343 }
344 }
345 }
346 else {
347 0 @best_vs = ($_[0], $_[0]);
348 }
349 60 ▶ wantarray ? ($best_d2, map V(@$_), @best_vs) : $best_d2;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 60 0 60
350 }
351  
352 100 ▶sub find_brute_force {
Subroutine find_brute_force: called CC=1 SCAR=0.0
Pod documented
353 100 shift;
354 100 goto &_find_brute_force;
355 }
356  
357 0 ▶sub find_2d_convex_hull {
Subroutine find_2d_convex_hull: not called CC=3 SCAR=24.8
Pod documented
358 0 shift;
359  
360 0 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
361 0 my $dim = @{$_[0]};
362 0 ▶ $dim == 2 or croak "find_2d_convex_hull called with vectors of dimension $dim";
Branch: 1 branch
BranchTrueFalseTotal
$dim == 2 or croak("find_2d_convex_hull called with vectors of dimension $dim") 0 0 0
363  
364 0 goto &_find_2d_convex_hull;
365 }
366  
367 1 1;
368 __END__
↑ Summary