PlanePath/UlamWarburton.pm

filter: Statementstmt 74.6% 215 / 288 Branchbran 59.4% 88 / 148 Conditioncond 44.8% 13 / 29 MC/DCmcdc 46.6% 7 / 15 Subroutinesub 70.5% 24 / 34 Podpod 100% 20 / 20 totaltotal 68.7% 367 / 534 CC 88 SCAR 28.5
CC
88
Cov
68%
tree_n_to_subheight
72.0
xy_to_n
32.2
n_to_xy
19.5
CRAP 17.3
« PlanePath/SquareReplicate.pm ↑ Summary PlanePath/MultipleRings.pm »
1 # Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Kevin Ryde
2  
3 # This file is part of Math-PlanePath.
4 #
5 # Math-PlanePath is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Math-PlanePath is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Math-PlanePath. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 #------------------------------------------------------------------------------
20 # cf
21 # Ulam/Warburton with cells turning off too
22 # A079315 cells OFF -> ON
23 # A079317 cells ON at stage n
24 # A079316 cells ON at stage n, in first quadrant
25 # A151921 net gain ON cells
26  
27  
28 #------------------------------------------------------------------------------
29  
30 package Math::PlanePath::UlamWarburton;
31 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use List::Util 'sum';
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 $VERSION = 128;
38 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 @ISA = ('Math::PlanePath');
40 1 *_divrem = \&Math::PlanePath::_divrem;
41 1 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
42  
43 use Math::PlanePath::Base::Generic
44 1 'is_infinite',
45 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 use Math::PlanePath::Base::Digits
47 1 'round_up_pow',
48 'round_down_pow',
49 1 ▶ 'digit_split_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 1 ▶use Math::PlanePath::UlamWarburtonQuarter;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 # uncomment this to run the ### lines
54 # use Smart::Comments;
55  
56  
57 1 use constant parameter_info_array =>
58 [
59 { name => 'parts',
60 share_key => 'parts_ulamwarburton',
61 display => 'Parts',
62 type => 'enum',
63 default => '4',
64 choices => ['4','2','1','octant','octant_up' ],
65 choices_display => ['4','2','1','Octant','Octant Up' ],
66 description => 'Which parts of the plane to fill.',
67 },
68 Math::PlanePath::Base::Generic::parameter_info_nstart1(),
69 1 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 # octant_up goes up the Y axis spine, dX=0
72 # all others always have dX!=0
73 sub absdx_minimum {
74 0 ▶ my ($self) = @_;
Subroutine absdx_minimum: not called CC=2 SCAR=17.9
Pod documented
75 0 ▶ return ($self->{'parts'} eq 'octant_up' ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'parts'} eq 'octant_up' ? : 0 0 0
76 }
77  
78 # used also to validate $self->{'parts'}
79 1 my %x_negative = (4 => 1,
80 2 => 1,
81 1 => 0,
82 octant => 0,
83 octant_up => 0,
84 );
85 sub x_negative {
86 1 ▶ my ($self) = @_;
Subroutine x_negative: called CC=1 SCAR=0.0
Pod documented
87 1 return $x_negative{$self->{'parts'}};
88 }
89 sub y_negative {
90 1 ▶ my ($self) = @_;
Subroutine y_negative: called CC=1 SCAR=0.0
Pod documented
91 1 return $self->{'parts'} eq '4';
92 }
93  
94 sub x_negative_at_n {
95 0 ▶ my ($self) = @_;
Subroutine x_negative_at_n: not called CC=2 SCAR=17.9
Pod documented
96 0 ▶ return ($x_negative{$self->{'parts'}} ? $self->n_start + 3 : undef);
Branch: 1 branch
BranchTrueFalseTotal
$x_negative{$self->{'parts'}} ? : 0 0 0
97 }
98 sub y_negative_at_n {
99 0 ▶ my ($self) = @_;
Subroutine y_negative_at_n: not called CC=2 SCAR=17.9
Pod documented
100 0 ▶ return ($self->{'parts'} eq '4' ? $self->n_start + 4 : undef);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'parts'} eq '4' ? : 0 0 0
101 }
102  
103 sub diffxy_minimum {
104 0 ▶ my ($self) = @_;
Subroutine diffxy_minimum: not called CC=2 SCAR=17.9
Pod documented
105 0 ▶ return ($self->{'parts'} eq 'octant' ? 0 : undef);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'parts'} eq 'octant' ? : 0 0 0
106 }
107 sub diffxy_maximum {
108 0 ▶ my ($self) = @_;
Subroutine diffxy_maximum: not called CC=2 SCAR=17.9
Pod documented
109 0 ▶ return ($self->{'parts'} eq 'octant_up' ? 0 : undef);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'parts'} eq 'octant_up' ? : 0 0 0
110 }
111  
112 {
113 1 my %dir_maximum_dxdy = (4 => [1,-1], # N=4 South-East
114 2 => [1,-1], # N=44 South-East
115 1 => [2,-1], # N=3 ESE
116 octant => [10,-3], # N=51
117 octant_up => [2,-1], # N=8 ESE
118 );
119 sub dir_maximum_dxdy {
120 0 ▶ my ($self) = @_;
Subroutine dir_maximum_dxdy: not called CC=1 SCAR=6.9
Pod documented
121 0 return @{$dir_maximum_dxdy{$self->{'parts'}}};
122 }
123 }
124  
125 {
126 1 my %_UNDOCUMENTED__turn_any_right_at_n
127 = (
128 4 => 20,
129 2 => 35,
130 1 => 2,
131 octant => 4,
132 octant_up => 2,
133 );
134 sub _UNDOCUMENTED__turn_any_right_at_n {
135 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__turn_any_right_at_n: not called CC=1 SCAR=6.9
136 return $self->n_start
137 0 + $_UNDOCUMENTED__turn_any_right_at_n{$self->{'parts'}};
138 }
139 }
140  
141 1 sub tree_num_children_list {
142 0 ▶ my ($self) = @_;
Subroutine tree_num_children_list: not called CC=2 SCAR=17.9
Pod documented
143 0 ▶ return ($self->{'parts'} eq '4'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'parts'} eq '4' ? : 0 0 0
144 ? (0, 1, 3, 4)
145 : (0, 1, 2, 3 ));
146 }
147  
148 #------------------------------------------------------------------------------
149 sub new {
150 20 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=4 SCAR=14.0
Pod documented
151 20 ▶ if (! defined $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"n_start"}) 9 11 20
152 9 $self->{'n_start'} = $self->default_n_start;
153 }
154 20 ▶ my $parts = ($self->{'parts'} ||= '4');
Condition100%2/2
$self->{"parts"} ||= 4
l!l
911
MC/DC100%1/1
$self->{"parts"} ||= 4
$self->{"parts"}
Truth table100%2/2
A
A= $self->{"parts"}
Aresult
11
00
155 20 ▶ if (! exists $x_negative{$parts}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $x_negative{$parts}) 0 20 20
156 0 croak "Unrecognised parts option: ", $parts;
157 }
158 20 return $self;
159 }
160  
161 sub n_to_xy {
162 370 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=14 SCAR=29.7
Pod documented
163 ### UlamWarburton n_to_xy(): "$n parts=$self->{'parts'}"
164  
165 370 ▶ if ($n < $self->{'n_start'}) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $self->{"n_start"}) 0 370 370
166 370 ▶ if (is_infinite($n)) { return ($n,$n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 370 370
167 {
168 370 my $int = int($n);
169 ### $int
170 ### $n
171 370 ▶ if ($n != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $int) 0 370 370
172 0 my ($x1,$y1) = $self->n_to_xy($int);
173 0 my ($x2,$y2) = $self->n_to_xy($int+1);
174 0 my $frac = $n - $int; # inherit possible BigFloat
175 0 my $dx = $x2-$x1;
176 0 my $dy = $y2-$y1;
177 0 return ($frac*$dx + $x1, $frac*$dy + $y1);
178 }
179 370 $n = $int; # BigFloat int() gives BigInt, use that
180 }
181  
182 370 $n = $n - $self->{'n_start'}; # N=0 basis
183 370 ▶ if ($n == 0) { return (0,0); }
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0) 6 364 370
184  
185 364 my $parts = $self->{'parts'};
186 364 ▶ my ($depthsum, $factor, $nrem) = _n0_to_depthsum_factor_rem($n, $parts)
Branch: 1 branch
BranchTrueFalseTotal
() = _n0_to_depthsum_factor_rem($n, $parts) or return $n 0 364 364
187 or return $n; # N=nan or +inf
188 ### depthsum: join(',',@$depthsum)
189 ### $factor
190 ### n rem within row: $nrem
191  
192 364 ▶ if ($parts eq '4') {
Branch: 3 branches
BranchTrueFalseTotal
if ($parts eq '4') { } 364 0 364
elsif ($parts eq '2') { } 0 0 0
elsif ($parts eq 'octant_up') { } 0 0 0
193 364 $factor /= 4;
194 } elsif ($parts eq '2') {
195 0 $factor /= 2;
196 0 $nrem += ($factor-1)/2;
197 } elsif ($parts eq 'octant_up') {
198 0 $nrem += $factor;
199 } else {
200 0 $nrem += ($factor-1)/2;
201 }
202 364 (my $quad, $nrem) = _divrem ($nrem, $factor);
203  
204 ### factor modulus: $factor
205 ### $quad
206 ### n rem within quad: $nrem
207 ### assert: $quad >= 0
208 ### assert: $quad <= 3
209  
210 364 my $dhigh = shift @$depthsum; # highest term
211 364 my @ndigits = digit_split_lowtohigh($nrem,3);
212 ### $dhigh
213 ### ndigits low to high: join(',',@ndigits)
214  
215 364 my $x = 0;
216 364 my $y = 0;
217 364 foreach my $depthterm (reverse @$depthsum) { # depth terms low to high
218 735 my $ndigit = shift @ndigits; # N digits low to high
219 ### $depthterm
220 ### $ndigit
221  
222 735 $x += $depthterm;
223 ### bit to x: "$x,$y"
224  
225 735 ▶ if ($ndigit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ndigit) { } 459 276 735
226 459 ▶ if ($ndigit == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ndigit == 2) 263 196 459
227 263 ($x,$y) = (-$y,$x); # rotate +90
228 }
229 } else {
230 # $ndigit==0 (or undef when @ndigits shorter than @$depthsum)
231 276 ($x,$y) = ($y,-$x); # rotate -90
232 }
233 ### rotate to: "$x,$y"
234 }
235 364 $x += $dhigh;
236  
237 ### xy before quad: "$x,$y"
238 364 ▶ if ($quad & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quad & 2) 176 188 364
239 176 $x = -$x;
240 176 $y = -$y;
241 }
242 364 ▶ if ($quad & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quad & 1) 180 184 364
243 180 ($x,$y) = (-$y,$x); # rotate +90
244 }
245  
246 ### final: "$x,$y"
247 364 return $x,$y;
248 }
249 # no Smart::Comments;
250  
251 sub xy_to_n {
252 564 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=24 SCAR=34.7
Pod documented
253 ### UlamWarburton xy_to_n(): "$x, $y"
254  
255 564 $x = round_nearest ($x);
256 564 $y = round_nearest ($y);
257 564 ▶ if ($x == 0 && $y == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x == 0 and $y == 0) 13 551 564
Condition100%3/3
$x == 0 and $y == 0
!ll&&!rl&&r
5084313
MC/DC100%2/2
$x == 0 and $y == 0
$x == 0 $y == 0
Truth table100%3/3
A and B
A= $x == 0B= $y == 0
ABresult
0X0
100
111
258 13 return $self->{'n_start'};
259 }
260  
261 551 my $parts = $self->{'parts'};
262 551 ▶ if ($parts ne '4'
Branch: 2 branches
BranchTrueFalseTotal
if ($parts ne 4 and $y < 0 || $parts ne 2 && $x < ($parts eq "octant" ? $y : 0) || $parts eq "octant_up" && $x > $y) 0 551 551
$parts eq 'octant' ? : 0 0 0
Condition6%1/15
$parts ne 4 and $y < 0 || $parts ne 2 && $x < ($parts eq "octant" ? $y : 0) || $parts eq "octant_up" && $x > $y
!ll&&!rl&&r
55100
$y < 0 or $parts ne 2 and $x < ($parts eq "octant" ? $y : 0) or $parts eq "octant_up" and $x > $y
l!l&&r!l&&!r
000
$y < 0 or $parts ne 2 and $x < ($parts eq "octant" ? $y : 0)
l!l&&r!l&&!r
000
$parts ne 2 and $x < ($parts eq "octant" ? $y : 0)
!ll&&!rl&&r
000
$parts eq "octant_up" and $x > $y
!ll&&!rl&&r
000
MC/DC0%0/6
$parts ne 4 and $y < 0 || $parts ne 2 && $x < ($parts eq "octant" ? $y : 0) || $parts eq "octant_up" && $x > $y
$parts ne 4 $y < 0 $parts ne 2 $x < ($parts eq "octant" ? $y : 0) $parts eq "octant_up" $x > $y
Truth table11%1/9
A and B or C and D or E and F
A= $parts ne 4B= $y < 0C= $parts ne 2D= $x < ($parts eq "octant" ? $y : 0)E= $parts eq "octant_up"F= $x > $y
ABCDEFresult
0XXXXX0
100X0X0
100X100
10100X0
1010100
11XXXX1
1011XX1
100X111
1010111
263 && ($y < 0
264 || ($parts ne '2' && $x < ($parts eq 'octant' ? $y : 0))
265 || ($parts eq 'octant_up' && $x > $y))) {
266 0 return undef;
267 }
268  
269 551 my $quad;
270 551 ▶ if ($y > $x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y > $x) { } 261 290 551
271 ### quad above leading diagonal ...
272 # /
273 # above /
274 # /
275 261 ▶ if ($y > -$x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y > -$x) { } 130 131 261
276 ### quad above opposite diagonal, top quarter ...
277 # top
278 # \ /
279 # \/
280 130 $quad = 1;
281 130 ($x,$y) = ($y,-$x); # rotate -90
282 } else {
283 ### quad below opposite diagonal, left quarter ...
284 # \
285 # left \
286 # /
287 # /
288 131 $quad = 2;
289 131 $x = -$x; # rotate -180
290 131 $y = -$y;
291 }
292 } else {
293 ### quad below leading diagonal ...
294 # /
295 # / below
296 # /
297 290 ▶ if ($y > -$x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y > -$x) { } 148 142 290
298 ### quad above opposite diagonal, right quarter ...
299 # /
300 # / right
301 # \
302 # \
303 148 $quad = 0;
304 } else {
305 ### quad below opposite diagonal, bottom quarter ...
306 # /\
307 # / \
308 # bottom
309 142 $quad = 3;
310 142 ($x,$y) = (-$y,$x); # rotate +90
311 }
312 }
313 ### $quad
314 ### quad rotated xy: "$x,$y"
315 ### assert: ! ($y > $x)
316 ### assert: ! ($y < -$x)
317  
318 551 my ($len, $exp) = round_down_pow ($x + abs($y), 2);
319 551 ▶ if (is_infinite($exp)) { return ($exp); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($exp)) 0 551 551
320  
321  
322 551 my $depth =
323 my $ndigits =
324 my $n = ($x * 0 * $y); # inherit bignum 0
325  
326 551 ▶ while ($exp-- >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp-- >= 0) 1713 386 2099
327 ### at: "$x,$y n=$n len=$len"
328  
329 1713 my $abs_y = abs($y);
330 1713 ▶ if ($x && $x == $abs_y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x and $x == $abs_y) 165 1548 1713
Condition100%3/3
$x and $x == $abs_y
!ll&&!rl&&r
1261422165
MC/DC100%2/2
$x and $x == $abs_y
$x $x == $abs_y
Truth table100%3/3
A and B
A= $xB= $x == $abs_y
ABresult
0X0
100
111
331 165 return undef;
332 }
333  
334 # right quarter diamond
335 ### assert: $x >= 0
336 ### assert: $x >= abs($y)
337 ### assert: $x+abs($y) < 2*$len || $x==abs($y)
338  
339 1548 ▶ if ($x + $abs_y >= $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x + $abs_y >= $len) 1275 273 1548
340 # one of the three quarter diamonds away from the origin
341 1275 $x -= $len;
342 ### shift to: "$x,$y"
343  
344 1275 $depth += $len;
345 1275 ▶ if ($x || $y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x or $y) 889 386 1275
Condition100%3/3
$x or $y
l!l&&r!l&&!r
587302386
MC/DC100%2/2
$x or $y
$x $y
Truth table100%3/3
A or B
A= $xB= $y
ABresult
1X1
011
000
346 889 $n *= 3;
347 889 $ndigits++;
348  
349 889 ▶ if ($y < -$x) {
Branch: 2 branches
BranchTrueFalseTotal
if ($y < -$x) { } 330 559 889
elsif ($y > $x) { } 319 240 559
350 ### bottom, digit 0 ...
351 330 ($x,$y) = (-$y,$x); # rotate +90
352  
353 } elsif ($y > $x) {
354 ### top, digit 2 ...
355 319 ($x,$y) = ($y,-$x); # rotate -90
356 319 $n += 2;
357 } else {
358 ### right, digit 1 ...
359 240 $n += 1;
360 }
361 }
362 }
363  
364 1548 $len /= 2;
365 }
366  
367 ### $n
368 ### $depth
369 ### $ndigits
370 ### npower: 3**$ndigits
371 ### $quad
372 ### quad powered: $quad*3**$ndigits
373  
374 386 my $npower = 3**$ndigits;
375 386 ▶ if ($parts eq 'octant_up') {
Branch: 2 branches
BranchTrueFalseTotal
if ($parts eq 'octant_up') { } 0 386 386
elsif ($parts ne '4') { } 0 386 386
376 0 $n -= $npower;
377 } elsif ($parts ne '4') {
378 0 $n -= ($npower-1)/2;
379 }
380  
381 386 return $n + $quad*$npower + $self->tree_depth_to_n($depth);
382 }
383  
384 # not exact
385 sub rect_to_n_range {
386 50 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=2 SCAR=6.9
Pod documented
387 ### UlamWarburton rect_to_n_range(): "$x1,$y1 $x2,$y2"
388  
389 50 my ($dlo, $dhi)
390 = _rect_to_diamond_range (round_nearest($x1), round_nearest($y1),
391 round_nearest($x2), round_nearest($y2));
392 ### $dlo
393 ### $dhi
394  
395 50 ▶ if ($dlo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dlo) 46 4 50
396 46 ($dlo) = round_down_pow ($dlo,2);
397 }
398 50 ($dhi) = round_down_pow ($dhi,2);
399  
400 ### rounded to pow2: "$dlo ".(2*$dhi)
401  
402 50 return ($self->tree_depth_to_n($dlo),
403 $self->tree_depth_to_n(2*$dhi) - 1);
404 }
405  
406 # x1 | x2
407 # +--------|-------+ y2 xzero true, yzero false
408 # | | | diamond min is y1
409 # +--------|-------+ y1
410 # |
411 # ----------O-------------
412 #
413 # | x1 x2
414 # | +--------+ y2 xzero false, yzero true
415 # | | | diamond min is x1
416 # -O--------------------
417 # | | |
418 # | +--------+ y1
419 # |
420 #
421 sub _rect_to_diamond_range {
422 50 ▶ my ($x1,$y1, $x2,$y2) = @_;
Subroutine _rect_to_diamond_range: called CC=5 SCAR=17.4
423  
424 50 my $xzero = ($x1 < 0) != ($x2 < 0); # x range covers x=0
425 50 my $yzero = ($y1 < 0) != ($y2 < 0); # y range covers y=0
426  
427 50 $x1 = abs($x1);
428 50 $y1 = abs($y1);
429 50 $x2 = abs($x2);
430 50 $y2 = abs($y2);
431  
432 50 ▶ if ($x1 > $x2) { ($x1,$x2) = ($x2,$x1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 > $x2) 0 50 50
433 50 ▶ if ($y1 > $y2) { ($y1,$y2) = ($y2,$y1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 > $y2) 0 50 50
434  
435 50 ▶ return (($yzero ? 0 : $y1) + ($xzero ? 0 : $x1),
Branch: 2 branches
BranchTrueFalseTotal
$yzero ? : 0 50 50
$xzero ? : 0 50 50
436 $x2+$y2);
437 }
438  
439  
440 #------------------------------------------------------------------------------
441 1 ▶use constant tree_num_roots => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
442  
443 # ENHANCE-ME: step by the bits, not by X,Y
444 # ENHANCE-ME: tree_n_to_depth() by probe
445 sub tree_n_children {
446 9 ▶ my ($self, $n) = @_;
Subroutine tree_n_children: called CC=5 SCAR=16.1
Pod documented
447 ### UlamWarburton tree_n_children(): $n
448  
449 9 ▶ if ($n < $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $self->{"n_start"}) 0 9 9
450 0 return;
451 }
452 9 my ($x,$y) = $self->n_to_xy($n);
453 9 my @ret;
454 9 my $dx = 1;
455 9 my $dy = 0;
456 9 foreach (1 .. 4) {
457 36 ▶ if (defined (my $n_child = $self->xy_to_n($x+$dx,$y+$dy))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $n_child = $self->xy_to_n($x + $dx, $y + $dy))) 28 8 36
458 28 ▶ if ($n_child > $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_child > $n) 20 8 28
459 20 push @ret, $n_child;
460 }
461 }
462 36 ($dx,$dy) = (-$dy,$dx); # rotate +90
463 }
464 9 return sort {$a<=>$b} @ret;
465 }
466 sub tree_n_parent {
467 15 ▶ my ($self, $n) = @_;
Subroutine tree_n_parent: called CC=5 SCAR=16.1
Pod documented
468 ### UlamWarburton tree_n_parent(): $n
469  
470 15 ▶ if ($n <= $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= $self->{"n_start"}) 1 14 15
471 1 return undef;
472 }
473 14 my ($x,$y) = $self->n_to_xy($n);
474 14 my $dx = 1;
475 14 my $dy = 0;
476 14 foreach (1 .. 4) {
477 37 ▶ if (defined (my $n_parent = $self->xy_to_n($x+$dx,$y+$dy))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $n_parent = $self->xy_to_n($x + $dx, $y + $dy))) 24 13 37
478 24 ▶ if ($n_parent < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_parent < $n) 14 10 24
479 14 return $n_parent;
480 }
481 }
482 23 ($dx,$dy) = (-$dy,$dx); # rotate +90
483 }
484 0 return undef;
485 }
486 # sub tree_n_children {
487 # my ($self, $n) = @_;
488 # my ($power, $exp) = _round_down_pow (3*$n-2, 4);
489 # $exp -= 1;
490 # $power /= 4;
491 #
492 # ### $power
493 # ### $exp
494 # ### pow base: 2 + 4*(4**$exp - 1)/3
495 #
496 # $n -= ($power - 1)/3 * 4 + 2;
497 # ### n less pow base: $n
498 #
499 # my @$depthsum = (2**$exp);
500 # $power = 3**$exp;
501 #
502 # # find the cumulative levelpoints total <= $n, being the start of the
503 # # level containing $n
504 # #
505 # my $factor = 4;
506 # while (--$exp >= 0) {
507 # $power /= 3;
508 # my $sub = 4**$exp * $factor;
509 # ### $sub
510 # # $power*$factor;
511 # my $rem = $n - $sub;
512 #
513 # ### $n
514 # ### $power
515 # ### $factor
516 # ### consider subtract: $sub
517 # ### $rem
518 #
519 # if ($rem >= 0) {
520 # $n = $rem;
521 # push @$depthsum, 2**$exp;
522 # $factor *= 3;
523 # }
524 # }
525 #
526 # $n += $factor;
527 # if (1) {
528 # return ($n,$n+1,$n+2);
529 # } else {
530 # return $n,$n+1,$n+2;
531 # }
532 # }
533  
534 # Converting quarter ...
535 # (N-start)*4+1+start = 4*N-4*start+1+start
536 # = 4*N-3*start+1
537 #
538 sub tree_depth_to_n {
539 624 ▶ my ($self, $depth) = @_;
Subroutine tree_depth_to_n: called CC=7 SCAR=20.2
Pod documented
540 ### UlamWarburton tree_depth_to_n(): $depth
541  
542 624 ▶ if ($depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 0) 12 612 624
543 12 return $self->{'n_start'};
544 }
545 612 my $n = $self->Math::PlanePath::UlamWarburtonQuarter::tree_depth_to_n($depth-1);
546 612 ▶ if (! defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 0 612 612
547 0 return undef;
548 }
549 612 my $parts = $self->{'parts'};
550 612 ▶ if ($parts eq '2') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts eq 2) 16 596 612
551 16 return 2*$n - $self->{'n_start'} + $depth;
552 }
553 596 ▶ if ($parts eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts eq 1) 61 535 596
554 61 return $n + $depth;
555 }
556 535 ▶ if ($parts eq 'octant' || $parts eq 'octant_up') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts eq "octant" or $parts eq "octant_up") 0 535 535
Condition33%1/3
$parts eq "octant" or $parts eq "octant_up"
l!l&&r!l&&!r
00535
MC/DC0%0/2
$parts eq "octant" or $parts eq "octant_up"
$parts eq "octant" $parts eq "octant_up"
Truth table33%1/3
A or B
A= $parts eq "octant"B= $parts eq "octant_up"
ABresult
1X1
011
000
557 0 return ($n + 1);
558 }
559 ### assert: $parts eq '4'
560 535 return 4*$n - 3*$self->{'n_start'} + 1;
561 }
562 # sub _NOTWORKING__tree_depth_to_n_range {
563 # my ($self, $depth) = @_;
564 # my ($nstart, $nend) = $self->Math::PlanePath::UlamWarburtonQuarter::tree_depth_to_n_range($self, $depth)
565 # or return;
566 # return (4*$nstart-3 + $self->{'n_start'}-1,
567 # 4*$nend-3 + $self->{'n_start'}-1);
568 # }
569  
570  
571 sub tree_n_to_depth {
572 168 ▶ my ($self, $n) = @_;
Subroutine tree_n_to_depth: called CC=4 SCAR=14.2
Pod documented
573 ### UlamWarburton tree_n_to_depth(): $n
574  
575 168 $n = $n - $self->{'n_start'}; # N=0 basis
576 168 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 168 168
577 0 return undef;
578 }
579 168 $n = int($n);
580 168 ▶ if ($n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0) 16 152 168
581 16 return 0;
582 }
583 152 ▶ my ($depthsum) = _n0_to_depthsum_factor_rem($n, $self->{'parts'})
Branch: 1 branch
BranchTrueFalseTotal
my($depthsum) = _n0_to_depthsum_factor_rem($n, $self->{"parts"}) or return $n 0 152 152
584 or return $n; # N=nan or +inf
585 152 return sum(@$depthsum);
586 }
587  
588  
589 # 1+3+3+9=16
590 #
591 # 0 +1
592 # 1 +4 <- 0
593 # 5 +4 <- 1
594 # 9 +12
595 # 21 +4 <- 5 + 4+12 = 21 = 5 + 4*(1+3)
596 # 25 +12
597 # 37 +12
598 # 49 +36
599 # 85 +4 <- 21 + 4+12+12+36 = 21 + 4*(1+3+3+9)
600 # 89 +12 <- 8 +64
601 # 101 +12
602 # 113 +36
603 # 149
604 # 161
605 # 197
606 # 233
607 # 341
608 # 345 <- 16 +256
609 # 357
610 # 369
611  
612 # 1+3 = 4 power 2
613 # 1+3+3+9 = 16 power 3
614 # 1+3+3+9+3+9+9+27 = 64 power 4
615 #
616 # 4*(1+4+...+4^(l-1)) = 4*(4^l-1)/3
617 # l=1 total=4*(4-1)/3 = 4
618 # l=2 total=4*(16-1)/3=4*5 = 20
619 # l=3 total=4*(64-1)/3=4*63/3 = 4*21 = 84
620 #
621 # n = 2 + 4*(4^l-1)/3
622 # (n-2) = 4*(4^l-1)/3
623 # 3*(n-2) = 4*(4^l-1)
624 # 3n-6 = 4^(l+1)-4
625 # 3n-2 = 4^(l+1)
626 #
627 # 3^0+3^1+3^1+3^2 = 1+3+3+9=16
628 # x+3x+3x+9x = 16x = 256
629 # 4 quads is 4*16=64
630 #
631 # 1+1+3 = 5
632 # 1+1+3 +1+1+3 +3+3+9 = 25
633  
634 # 1+4 = 5
635 # 1+4+4+12 = 21 = 1 + 4*(1+1+3)
636 # 2 +1
637 # 3 +3
638 # 6 +1
639 # 7 +1
640 # 10 +3
641 # 13
642  
643  
644 # parts=1
645 # 1+4+...+4^(l-1) + 2^l
646 # = (4^l-1)/3 + 2^l
647 # = (4^l-1 + 3*2^l)/3
648 # = (2^l*(2^l + 3) - 1)/3
649 # l=1 total= 3
650 # l=2 total= 9
651 # l=3 total= 29
652 # l=4 total= 101
653 #
654 # N = (4^l-1)/3 + 2^l
655 # 3*(N-2^l)+1 = 4^l
656 # 12*(N-2^l)+1 = 4 * 4^l
657 #
658 # parts=2
659 # N = 2*(4^l-1)/3 + 2^l
660 # 3/2*(N-2^l)+1 = 4^l
661 # 6*(N-2^l)+1 = 4 * 4^l
662 #
663 # parts=4
664 # N = (4^l-1)/3
665 # 3*N+1 = 4 * 4^l
666  
667 # use Smart::Comments;
668  
669 # Return ($aref, $factor, $remaining_n).
670 # sum(@$aref) = depth starting depth=1
671 #
672 sub _n0_to_depthsum_factor_rem {
673 516 ▶ my ($n, $parts) = @_;
Subroutine _n0_to_depthsum_factor_rem: called CC=13 SCAR=27.6
674 ### _n0_to_depthsum_factor_rem(): "$n parts=$parts"
675  
676 516 ▶ my $factor = ($parts eq '4' ? 4 : $parts eq '2' ? 2 : 1);
Branch: 2 branches
BranchTrueFalseTotal
$parts eq '4' ? : 432 84 516
$parts eq '2' ? : 0 84 84
677 516 ▶ if ($n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0) 0 516 516
678 0 return ([], $factor, 0);
679 }
680  
681 516 my $n3 = 3*$n + 1;
682 516 my $ndepth = 0;
683 516 my $power = $n3;
684 516 my $exp;
685 516 ▶ if ($parts eq '4') {
Branch: 3 branches
BranchTrueFalseTotal
if ($parts eq '4') { } 432 84 516
elsif ($parts eq '2') { } 0 84 84
elsif ($parts =~ /octant/) { } 0 84 84
686 432 $power /= 4;
687 } elsif ($parts eq '2') {
688 0 $power /= 2;
689 0 $ndepth = -1;
690 } elsif ($parts =~ /octant/) {
691 0 $power *= 2;
692 0 $ndepth = 2;
693 }
694 516 ($power, $exp) = round_down_pow ($power, 4);
695 ### $n3
696 ### $power
697 ### $exp
698 516 ▶ if (is_infinite($exp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($exp)) 0 516 516
699 0 return;
700 }
701  
702 # ### pow base: ($power - 1)/3 * $factor + 1 + ($parts ne '4' && $exp)
703 # $n -= ($power - 1)/3 * $factor + 1;
704 # if ($parts ne '4') { $n -= $exp; }
705 # ### n less pow base: $n
706  
707 516 my $twopow = 2**$exp;
708 516 my @depthsum;
709  
710 516 ▶ for (;
Branch: 1 branch
BranchTrueFalseTotal
if ($exp-- >= 0) 1816 516 2332
711 $exp-- >= 0;
712 $power /= 4, $twopow /= 2) {
713 ### at: "power=$power twopow=$twopow factor=$factor n3=$n3 ndepth=$ndepth depthsum=".join(',',@depthsum)
714  
715 1816 my $nmore = $power * $factor;
716 1816 ▶ if ($parts ne '4') { $nmore += 3*$twopow; }
Branch: 1 branch
BranchTrueFalseTotal
if ($parts ne 4) 268 1548 1816
717 1816 ▶ if ($parts =~ /octant/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts =~ /octant/) 0 1816 1816
718 ### assert: $nmore % 2 == 0
719 0 $nmore = $nmore/2;
720 }
721  
722 1816 my $ncmp = $ndepth + $nmore;
723 ### $nmore
724 ### $ncmp
725  
726 1816 ▶ if ($n3 >= $ncmp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n3 >= $ncmp) 1367 449 1816
727 ### go to ncmp, remainder: $n3-$ncmp
728 1367 $factor *= 3;
729 1367 $ndepth = $ncmp;
730 1367 push @depthsum, $twopow;
731 }
732 }
733  
734 516 ▶ if ($parts eq '2') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts eq 2) 0 516 516
735 0 $n3 += 1;
736 }
737  
738 # ### assert: ($n3 - $ndepth)%3 == 0
739 516 $n = ($n3 - $ndepth) / 3;
740 516 $factor /= 3;
741  
742 ### $ndepth
743 ### @depthsum
744 ### remaining n: $n
745 ### assert: $n >= 0
746 ### assert: $n < $factor + ($parts ne '4')
747  
748 516 return \@depthsum, $factor, $n;
749 }
750  
751 sub tree_n_to_subheight {
752 0 ▶ my ($self, $n) = @_;
Subroutine tree_n_to_subheight: not called CC=8 SCAR=42.8
Pod documented
753 ### tree_n_to_subheight(): $n
754  
755 0 $n = int($n - $self->{'n_start'}); # N=0 basis
756 0 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
757 0 return undef;
758 }
759 0 ▶ my ($depthsum, $factor, $nrem) = _n0_to_depthsum_factor_rem($n, $self->{'parts'})
Branch: 1 branch
BranchTrueFalseTotal
() = _n0_to_depthsum_factor_rem($n, $self->{"parts"}) or return $n 0 0 0
760 or return $n; # N=nan or +inf
761 ### $depthsum
762 ### $factor
763 ### $nrem
764  
765 0 my $parts = $self->{'parts'};
766 0 ▶ if ($parts eq '4') {
Branch: 3 branches
BranchTrueFalseTotal
if ($parts eq '4') { } 0 0 0
elsif ($parts eq '2') { } 0 0 0
elsif ($parts eq 'octant_up') { } 0 0 0
767 0 $factor /= 4;
768 } elsif ($parts eq '2') {
769 0 $factor /= 2;
770 0 $nrem += ($factor-1)/2;
771 } elsif ($parts eq 'octant_up') {
772 } else {
773 0 $nrem += ($factor-1)/2;
774 }
775 0 (my $quad, $nrem) = _divrem ($nrem, $factor);
776  
777 0 my $sub = pop @$depthsum;
778 0 ▶ while (_divrem_mutate($nrem,3) == 1) { # low "1" ternary digits of Nrem
Branch: 1 branch
BranchTrueFalseTotal
if (_divrem_mutate($nrem, 3) == 1) 0 0 0
779 0 $sub += pop @$depthsum;
780 }
781 0 ▶ if (@$depthsum) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$depthsum) { } 0 0 0
782 0 return $depthsum->[-1] - 1 - $sub;
783 } else {
784 0 return undef; # N all 1-digits, on central infinite spine
785 }
786 }
787  
788 #------------------------------------------------------------------------------
789 # levels
790  
791 sub level_to_n_range {
792 29 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: called CC=1 SCAR=0.0
Pod documented
793 29 return ($self->{'n_start'},
794 $self->tree_depth_to_n_end(2**($level+1)-1));
795 }
796 sub n_to_level {
797 0 ▶ my ($self, $n) = @_;
Subroutine n_to_level: not called CC=2 SCAR=17.9
Pod documented
798 0 my $depth = $self->tree_n_to_depth($n);
799 0 ▶ if (! defined $depth) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $depth) 0 0 0
800 0 my ($pow, $exp) = round_down_pow ($depth, 2);
801 0 return $exp;
802 }
803  
804 # parts=4
805 # Ndepth(2^a) = 2 + 4*(4^a-1)/3
806 # Nend(2^a-1) = 1 + 4*(4^a-1)/3 = (4^(a+1)-1)/3
807 # parts=2
808 #
809 # {
810 # my %factor = (4 => 16,
811 # 2 => 8,
812 # 1 => 4,
813 # octant => 2,
814 # octant_up => 2,
815 # );
816 # my %constant = (4 => -4,
817 # 2 => -5,
818 # 1 => -4,
819 # octant => 0,
820 # octant_up => 0,
821 # );
822 # my %spine = (4 => 0,
823 # 2 => 2,
824 # 1 => 2,
825 # octant => 1,
826 # octant_up => 1,
827 # );
828 # sub level_to_n_range {
829 # my ($self, $level) = @_;
830 # my $parts = $self->{'parts'};
831 # return ($self->{'n_start'},
832 # $self->{'n_start'}
833 # + (4**$level * $factor{$parts} + $constant{$parts}) / 3
834 # + 2**$level * $spine{$parts});
835 # }
836 # }
837  
838 #------------------------------------------------------------------------------
839 1 1;
840 __END__
« PlanePath/SquareReplicate.pm ↑ Summary PlanePath/MultipleRings.pm »