PlanePath/SierpinskiTriangle.pm

filter: Statementstmt 66.2% 175 / 264 Branchbran 47.1% 65 / 138 Conditioncond 42.3% 22 / 52 MC/DCmcdc 30.7% 8 / 26 Subroutinesub 60.9% 25 / 41 Podpod 100% 23 / 23 totaltotal 58.4% 318 / 544 CC 91 SCAR 29.8
CC
91
Cov
58%
tree_n_num_children
72.0
tree_n_to_subheight
72.0
_EXPERIMENTAL__tree_n_to_leafdist
72.0
CRAP 19.8
« NumSeq/PlanePathDelta.pm ↑ Summary PlanePath/DiagonalsOctant.pm »
1 # Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 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 # Maybe:
20 #
21 # rule 22 includes the midpoint between adjacent leaf points.
22 # math-image --path=CellularRule,rule=22 --all --text
23 #
24 # rule 126 extra cell to the inward side of each
25 # math-image --path=CellularRule,rule=60 --all --text
26 #
27 # cf rule 150 double ups, something base 2 instead
28 # math-image --path=CellularRule,rule=150 --all
29 #
30 # cf rule 182 filled gaps
31 # math-image --path=CellularRule,rule=182 --all
32  
33 # math-image --path=SierpinskiTriangle --all --scale=5
34 # math-image --path=SierpinskiTriangle --all --output=numbers
35 # math-image --path=SierpinskiTriangle --all --text --size=80
36  
37 # Number of cells in a row:
38 # numerator of (2^k)/k!
39 #
40 # cf A067771 vertices of sierpinski graph, joins up replications
41 # so 1 less each giving 3*(3^k-1)/2
42 #
43  
44  
45  
46  
47 package Math::PlanePath::SierpinskiTriangle;
48 5 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 5 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 #use List::Util 'max';
52 5 *max = \&Math::PlanePath::_max;
53  
54 5 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
55 5 $VERSION = 128;
56 5 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 5 @ISA = ('Math::PlanePath');
58 5 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
59  
60 use Math::PlanePath::Base::Generic
61 5 'is_infinite',
62 5 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
63 use Math::PlanePath::Base::Digits
64 5 'round_down_pow',
65 'bit_split_lowtohigh',
66 5 ▶ 'digit_join_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 # uncomment this to run the ### lines
69 # use Smart::Comments;
70  
71 5 use constant parameter_info_array =>
72 [ { name => 'align',
73 share_key => 'align_trld',
74 display => 'Align',
75 type => 'enum',
76 default => 'triangular',
77 choices => ['triangular', 'right', 'left','diagonal'],
78 choices_display => ['Triangular', 'Right', 'Left','Diagonal'],
79 },
80 # { name => 'parts',
81 # share_key => 'parts_alr',
82 # display => 'Parts',
83 # type => 'enum',
84 # default => 'all',
85 # choices => ['all', 'left', 'right'],
86 # choices_display => ['All', 'Left', 'Right'],
87 # },
88 Math::PlanePath::Base::Generic::parameter_info_nstart0(),
89 5 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 5 ▶use constant default_n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 5 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 5 ▶use constant n_frac_discontinuity => .5;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 5 ▶use constant tree_num_children_list => (0,1,2);
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 sub x_negative {
97 11 ▶ my ($self) = @_;
Subroutine x_negative: called CC=3 SCAR=11.0
Pod documented
98 return ($self->{'align'} eq 'left'
99 || ($self->{'align'} eq 'triangular'
100 11 ▶ && $self->{'parts'} ne 'right'));
Condition83%5/6
$self->{'align'} eq 'left' || $self->{'align'} eq 'triangular' && $self->{'parts'} ne 'right'
l!l&&r!l&&!r
191
$self->{"align"} eq "triangular" and $self->{"parts"} ne "right"
!ll&&!rl&&r
109
MC/DC66%2/3
$self->{'align'} eq 'left' || $self->{'align'} eq 'triangular' && $self->{'parts'} ne 'right'
$self->{'align'} eq 'left' $self->{"align"} eq "triangular" $self->{"parts"} ne "right"
Truth table75%3/4
A || B and C
A= $self->{'align'} eq 'left'B= $self->{"align"} eq "triangular"C= $self->{"parts"} ne "right"
ABCresult
1XX1
0111
00X0
0100
101 }
102 sub x_negative_at_n {
103 0 ▶ my ($self) = @_;
Subroutine x_negative_at_n: not called CC=4 SCAR=30.0
Pod documented
104 return ($self->{'align'} eq 'left'
105 || ($self->{'align'} eq 'triangular'
106 0 ▶ && $self->{'parts'} ne 'right')
Branch: 1 branch
BranchTrueFalseTotal
$self->{'align'} eq 'left' || $self->{'align'} eq 'triangular' && $self->{'parts'} ne 'right' ? : 0 0 0
Condition0%0/6
$self->{"align"} eq "left" or $self->{"align"} eq "triangular" and $self->{"parts"} ne "right"
l!l&&r!l&&!r
000
$self->{"align"} eq "triangular" and $self->{"parts"} ne "right"
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"align"} eq "left" or $self->{"align"} eq "triangular" and $self->{"parts"} ne "right"
$self->{"align"} eq "left" $self->{"align"} eq "triangular" $self->{"parts"} ne "right"
Truth table0%0/4
A or B and C
A= $self->{"align"} eq "left"B= $self->{"align"} eq "triangular"C= $self->{"parts"} ne "right"
ABCresult
1XX1
0111
00X0
0100
107 ? $self->n_start + 1
108 : undef);
109 }
110  
111 # Note: this method shared by SierpinskiArrowhead
112 sub x_maximum {
113 0 ▶ my ($self) = @_;
Subroutine x_maximum: not called CC=5 SCAR=34.0
Pod documented
114 return ($self->{'align'} eq 'left'
115 || ($self->{'align'} eq 'triangular'
116 0 ▶ && ($self->{'parts'}||'all') eq 'left')
Branch: 1 branch
BranchTrueFalseTotal
$self->{'align'} eq 'left' || $self->{'align'} eq 'triangular' && ($self->{'parts'} || 'all') eq 'left' ? : 0 0 0
Condition0%0/8
$self->{"align"} eq "left" or $self->{"align"} eq "triangular" and ($self->{"parts"} || "all") eq "left"
l!l&&r!l&&!r
000
$self->{"align"} eq "triangular" and ($self->{"parts"} || "all") eq "left"
!ll&&!rl&&r
000
$self->{'parts'} || 'all'
l!l
00
MC/DC0%0/3
$self->{"align"} eq "left" or $self->{"align"} eq "triangular" and ($self->{"parts"} || "all") eq "left"
$self->{"align"} eq "left" $self->{"align"} eq "triangular" $self->{'parts'}
Truth table0%0/4
A or B and C
A= $self->{"align"} eq "left"B= $self->{"align"} eq "triangular"C= $self->{'parts'}
ABCresult
1XX1
0111
00X0
0100
117 ? 0 # left all X<=0
118 : undef); # others X to +infinity
119 }
120 5 ▶use constant sumxy_minimum => 0; # triangular X>=-Y or all X>=0
Subroutine BEGIN: called CC=1 SCAR=0.0
121  
122 sub diffxy_minimum {
123 0 ▶ my ($self) = @_;
Subroutine diffxy_minimum: not called CC=3 SCAR=24.8
Pod documented
124 return ($self->{'align'} eq 'right'
125 0 ▶ && $self->{'parts'} eq 'right'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'align'} eq 'right' && $self->{'parts'} eq 'right' ? : 0 0 0
Condition0%0/3
$self->{"align"} eq "right" and $self->{"parts"} eq "right"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"align"} eq "right" and $self->{"parts"} eq "right"
$self->{"align"} eq "right" $self->{"parts"} eq "right"
Truth table0%0/3
A and B
A= $self->{"align"} eq "right"B= $self->{"parts"} eq "right"
ABresult
0X0
100
111
126 ? 0 # X>=Y so X-Y>=0
127 : undef);
128 }
129  
130 # Note: this method shared by SierpinskiArrowhead, SierpinskiArrowheadCentres
131 sub diffxy_maximum {
132 0 ▶ my ($self) = @_;
Subroutine diffxy_maximum: not called CC=2 SCAR=17.9
Pod documented
133 0 ▶ return ($self->{'align'} eq 'diagonal'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'align'} eq 'diagonal' ? : 0 0 0
134 ? undef
135 : 0); # triangular X<=Y so X-Y<=0
136 }
137  
138 sub dy_minimum {
139 0 ▶ my ($self) = @_;
Subroutine dy_minimum: not called CC=2 SCAR=17.9
Pod documented
140 0 ▶ return ($self->{'align'} eq 'diagonal' ? undef : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'align'} eq 'diagonal' ? : 0 0 0
141 }
142 sub dy_maximum {
143 0 ▶ my ($self) = @_;
Subroutine dy_maximum: not called CC=2 SCAR=17.9
Pod documented
144 0 ▶ return ($self->{'align'} eq 'diagonal' ? undef : 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'align'} eq 'diagonal' ? : 0 0 0
145 }
146 {
147 5 my %absdx_minimum = (triangular => 1,
148 left => 1,
149 right => 0, # at N=0
150 diagonal => 0); # at N=0
151 sub absdx_minimum {
152 0 ▶ my ($self) = @_;
Subroutine absdx_minimum: not called CC=1 SCAR=6.9
Pod documented
153 0 return $absdx_minimum{$self->{'align'}};
154 }
155 }
156 {
157 5 my %absdy_minimum = (triangular => 0, # rows
158 left => 0, # rows
159 right => 0, # rows
160 diagonal => 1); # diagonal always moves
161 sub absdy_minimum {
162 0 ▶ my ($self) = @_;
Subroutine absdy_minimum: not called CC=1 SCAR=6.9
Pod documented
163 0 return $absdy_minimum{$self->{'align'}};
164 }
165 }
166  
167 5 sub dsumxy_minimum {
168 0 ▶ my ($self) = @_;
Subroutine dsumxy_minimum: not called CC=2 SCAR=17.9
Pod documented
169 0 ▶ return ($self->{'align'} eq 'diagonal'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'align'} eq 'diagonal' ? : 0 0 0
170 ? 0 # X+Y constant along diagonals
171 : undef);
172 }
173 sub dsumxy_maximum {
174 0 ▶ my ($self) = @_;
Subroutine dsumxy_maximum: not called CC=2 SCAR=17.9
Pod documented
175 0 ▶ return ($self->{'align'} eq 'diagonal'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'align'} eq 'diagonal' ? : 0 0 0
176 ? 1 # X+Y increase by 1 to next diagonal
177 : undef);
178 }
179  
180 sub dir_minimum_dxdy {
181 0 ▶ my ($self) = @_;
Subroutine dir_minimum_dxdy: not called CC=2 SCAR=17.9
Pod documented
182 0 ▶ return ($self->{'align'} eq 'diagonal'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'align'} eq 'diagonal' ? : 0 0 0
183 ? (0,1) # North
184 : (1,0)); # East
185 }
186 sub dir_maximum_dxdy {
187 0 ▶ my ($self) = @_;
Subroutine dir_maximum_dxdy: not called CC=2 SCAR=17.9
Pod documented
188 0 ▶ return ($self->{'align'} eq 'diagonal'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'align'} eq 'diagonal' ? : 0 0 0
189 ? (1,-1) # South-Eest
190 : (-1,0)); # supremum, West and 1 up
191 }
192  
193  
194 #------------------------------------------------------------------------------
195 5 my %align_known = (triangular => 1,
196 left => 1,
197 right => 1,
198 diagonal => 1);
199  
200 sub new {
201 23 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=5 SCAR=16.4
Pod documented
202 23 ▶ if (! defined $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"n_start"}) 13 10 23
203 13 $self->{'n_start'} = $self->default_n_start;
204 }
205 23 ▶ $self->{'parts'} ||= 'all';
Condition50%1/2
$self->{"parts"} ||= "all"
l!l
023
MC/DC0%0/1
$self->{"parts"} ||= "all"
$self->{"parts"}
Truth table50%1/2
A
A= $self->{"parts"}
Aresult
11
00
206  
207 23 ▶ my $align = ($self->{'align'} ||= 'triangular');
Condition100%2/2
$self->{"align"} ||= "triangular"
l!l
1013
MC/DC100%1/1
$self->{"align"} ||= "triangular"
$self->{"align"}
Truth table100%2/2
A
A= $self->{"align"}
Aresult
11
00
208 23 ▶ if (! $align_known{$align}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($align_known{$align}) 0 23 23
209 0 croak "Unrecognised align option: ", $align;
210 }
211 ### $align
212  
213 23 return $self;
214 }
215  
216 sub n_to_xy {
217 360 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=11 SCAR=24.2
Pod documented
218 ### SierpinskiTriangle n_to_xy(): $n
219  
220 # written as $n - n_start() rather than "-=" so as to provoke an
221 # uninitialized value warning if $n==undef
222 360 $n = $n - $self->{'n_start'}; # N=0 basis
223  
224 # this frac behaviour slightly unspecified yet
225 360 my $frac;
226 {
227 360 my $int = int($n);
228 360 $frac = $n - $int;
229 360 ▶ if (2*$frac >= 1) { # $frac>=0.5 and BigInt friendly
Branch: 2 branches
BranchTrueFalseTotal
if (2 * $frac >= 1) { } 0 360 360
elsif (2 * $frac < -1) { } 0 360 360
230 0 $frac -= 1;
231 0 $int += 1;
232 } elsif (2*$frac < -1) { # $frac<0.5 and BigInt friendly
233 0 $frac += 1;
234 0 $int -= 1;
235 }
236 360 $n = $int;
237 }
238 ### $n
239 ### $frac
240  
241 360 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 30 330 360
242 30 return;
243 }
244 330 ▶ if ($n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0) 25 305 330
245 25 return ($n,$n);
246 }
247  
248 305 ▶ my ($depthbits, $ndepth) = _n0_to_depthbits($n, $self->{'parts'})
Branch: 1 branch
BranchTrueFalseTotal
() = _n0_to_depthbits($n, $self->{"parts"}) or return $n, $n 0 305 305
249 or return ($n,$n); # infinite
250  
251 ### $depthbits
252 ### $ndepth
253  
254 305 my $zero = $n * 0;
255 305 $n -= $ndepth; # offset into row
256 305 my @nbits = bit_split_lowtohigh($n);
257  
258 # Where there's a 0-bit in the depth remains a 0-bit.
259 # Where there's a 1-bit in the depth takes a bit from Noffset.
260 # Small Noffset has less bits than the depth 1s, hence "|| 0".
261 #
262 305 ▶ my @xbits = map {$_ && (shift @nbits || 0)} @$depthbits;
Branch: 1 branch
BranchTrueFalseTotal
$_ and shift @nbits || 0 529 230 759
Condition100%5/5
$_ && (shift @nbits || 0)
!ll&&!rl&&r
230284245
shift @nbits or 0
l!l
245284
MC/DC100%2/2
$_ && (shift @nbits || 0)
$_ shift @nbits
Truth table100%3/3
A && B
A= $_B= shift @nbits
ABresult
0X0
100
111
263 ### @xbits
264  
265 305 my $x = digit_join_lowtohigh (\@xbits, 2, $zero);
266 305 my $y = digit_join_lowtohigh ($depthbits, 2, $zero);
267  
268 ### n_to_xy as right: "$x,$y"
269  
270 # $x,$y is in the style of align=right, transform to others
271 305 ▶ if ($self->{'align'} eq 'left') {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'align'} eq 'left') { } 30 275 305
elsif ($self->{'align'} eq 'diagonal') { } 10 265 275
elsif ($self->{'align'} eq 'triangular') { } 188 77 265
272 30 $x -= $y;
273 } elsif ($self->{'align'} eq 'diagonal') {
274 10 $y -= $x;
275 } elsif ($self->{'align'} eq 'triangular') {
276 188 $x = 2*$x - $y;
277 }
278  
279 ### n_to_xy final: "$x,$y"
280 305 return ($x, $y);
281 }
282  
283 sub xy_to_n {
284 5336 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=5 SCAR=16.1
Pod documented
285 ### SierpinskiTriangle xy_to_n(): "$x, $y"
286  
287 5336 $y = round_nearest ($y);
288 5336 $x = round_nearest($x);
289  
290 # transform $x,$y to the style of align=right
291 5336 ▶ if ($self->{'align'} eq 'diagonal') {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'align'} eq 'diagonal') { } 17 5319 5336
elsif ($self->{'align'} eq 'left') { } 513 4806 5319
elsif ($self->{'align'} eq 'triangular') { } 4293 513 4806
292 17 $y += $x;
293 } elsif ($self->{'align'} eq 'left') {
294 513 $x += $y;
295 } elsif ($self->{'align'} eq 'triangular') {
296 4293 $x += $y;
297 4293 ▶ if (_divrem_mutate ($x, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_divrem_mutate($x, 2)) 2120 2173 4293
298 # if odd point
299 2120 return undef;
300 }
301 }
302 ### adjusted xy: "$x,$y"
303  
304 3216 return _right_xy_to_n ($self, $x, $y);
305 }
306  
307 sub _right_xy_to_n {
308 3254 ▶ my ($self, $x, $y) = @_;
Subroutine _right_xy_to_n: called CC=9 SCAR=22.0
309 ### _right_xy_to_n(): "$x, $y"
310  
311 3254 ▶ unless ($x >= 0 && $x <= $y && $y >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x >= 0 and $x <= $y and $y >= 0) 812 1614 2426
Condition83%5/6
$x >= 0 and $x <= $y and $y >= 0
!ll&&!rl&&r
164001614
$x >= 0 and $x <= $y
!ll&&!rl&&r
8288121614
MC/DC66%2/3
$x >= 0 and $x <= $y and $y >= 0
$x >= 0 $x <= $y $y >= 0
Truth table75%3/4
A and B and C
A= $x >= 0B= $x <= $yC= $y >= 0
ABCresult
0XX0
10X0
1100
1111
312 ### outside horizontal row range ...
313 1640 return undef;
314 }
315 1614 ▶ if (is_infinite($y)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($y)) 0 1614 1614
316 0 return $y;
317 }
318  
319 1614 my $zero = ($y * 0);
320 1614 my $n = $zero; # inherit bignum 0
321 1614 my $npower = $zero+1; # inherit bignum 1
322  
323 1614 my @xbits = bit_split_lowtohigh($x);
324 1614 my @depthbits = bit_split_lowtohigh($y);
325  
326 1614 my @nbits; # N offset into row
327 1614 foreach my $i (0 .. $#depthbits) { # x,y bits low to high
328 4316 ▶ if ($depthbits[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depthbits[$i]) { } 2747 1569 4316
329 2747 $n = 2*$n + $npower;
330 2747 ▶ push @nbits, $xbits[$i] || 0; # low to high
Condition100%2/2
$xbits[$i] || 0
l!l
13561391
MC/DC100%1/1
$xbits[$i] || 0
$xbits[$i]
Truth table100%2/2
A
A= $xbits[$i]
Aresult
11
00
331 } else {
332 1569 ▶ if ($xbits[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xbits[$i]) 624 945 1569
333 624 return undef;
334 }
335 }
336 3692 $npower *= 3;
337 }
338  
339 ### n at left end of y row: $n
340 ### n offset for x: @nbits
341 ### total: $n + digit_join_lowtohigh(\@nbits,2,$zero) + $self->{'n_start'}
342  
343 990 return $n + digit_join_lowtohigh(\@nbits,2,$zero) + $self->{'n_start'};
344 }
345  
346 # not exact
347 sub rect_to_n_range {
348 19 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=6 SCAR=19.9
Pod documented
349 ### SierpinskiTriangle rect_to_n_range(): "$x1,$y1, $x2,$y2"
350  
351 19 $y1 = round_nearest ($y1);
352 19 $y2 = round_nearest ($y2);
353 19 ▶ if ($y1 > $y2) { ($y1,$y2) = ($y2,$y1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 > $y2) 0 19 19
354  
355 19 $x1 = round_nearest ($x1);
356 19 $x2 = round_nearest ($x2);
357 19 ▶ if ($x1 > $x2) { ($x1,$x2) = ($x2,$x1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 > $x2) 9 10 19
358  
359 # $y1 to $y2 is the depth range for "triangular", "right" and "left".
360 # For "diagonal" must use X+Y to reckon by anti-diagonals.
361 #
362 19 ▶ if ($self->{'align'} eq 'diagonal') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"align"} eq "diagonal") 0 19 19
363 0 $y2 += $x2;
364 0 $y1 += $x1;
365 }
366  
367 19 ▶ if ($y2 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 < 0) 0 19 19
368 0 return (1, 0);
369 }
370 19 ▶ if ($y1 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 < 0) 0 19 19
371 0 $y1 *= 0; # preserve any bignum $y1
372 }
373 19 return ($self->tree_depth_to_n($y1),
374 $self->tree_depth_to_n_end($y2));
375 }
376  
377 # To get N within a triangle row, based on the X range ...
378 #
379 # use Math::PlanePath::CellularRule54;
380 # *_rect_for_V = \&Math::PlanePath::CellularRule54::_rect_for_V;
381 #
382 # if ($self->{'align'} eq 'diagonal') {
383 # if ($x2 < 0 || $y2 < 0) {
384 # return (1,0);
385 # }
386 # if ($x1 < 0) { $x1 *= 0; }
387 # if ($y1 < 0) { $y1 *= 0; }
388 #
389 # return ($self->xy_to_n(0, $x1+$y1),
390 # $self->xy_to_n($x2+$y2, 0));
391 # }
392 #
393 # ($x1,$y1, $x2,$y2) = _rect_for_V ($x1,$y1, $x2,$y2)
394 # or return (1,0); # rect outside pyramid
395 #
396 # return ($self->xy_to_n($self->{'align'} eq 'right' ? 0 : -$y1,
397 # $y1),
398 # $self->xy_to_n($self->{'align'} eq 'left' ? 0 : $y2,
399 # $y2));
400  
401  
402 #------------------------------------------------------------------------------
403 5 ▶use constant tree_num_roots => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
404  
405 sub tree_n_num_children {
406 0 ▶ my ($self, $n) = @_;
Subroutine tree_n_num_children: not called CC=8 SCAR=42.8
Pod documented
407  
408 0 $n = $n - $self->{'n_start'}; # N=0 basis
409 0 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
410 0 return undef;
411 }
412 0 ▶ if ($n == 0 && $self->{'parts'} ne 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0 and $self->{"parts"} ne "all") 0 0 0
Condition0%0/3
$n == 0 and $self->{"parts"} ne "all"
!ll&&!rl&&r
000
MC/DC0%0/2
$n == 0 and $self->{"parts"} ne "all"
$n == 0 $self->{"parts"} ne "all"
Truth table0%0/3
A and B
A= $n == 0B= $self->{"parts"} ne "all"
ABresult
0X0
100
111
413 # parts=left or parts=right have only 1 child under the root n=0
414 0 return 1;
415 }
416 0 ▶ my ($depthbits, $ndepth) = _n0_to_depthbits($n, $self->{'parts'})
Branch: 1 branch
BranchTrueFalseTotal
() = _n0_to_depthbits($n, $self->{"parts"}) or return 1 0 0 0
417 or return 1; # infinite
418  
419 0 ▶ unless (shift @$depthbits) { # low bit
Branch: 1 branch
BranchTrueFalseTotal
unless (shift @$depthbits) 0 0 0
420 # Depth even (incl zero), two children under every point.
421 0 return 2;
422 }
423  
424 # Depth odd, either 0 or 1 child.
425 # If depth==1mod4 then 1-child.
426 # If depth==3mod4 so two or more trailing 1-bits then some 0-child and
427 # some 1-child.
428 #
429 0 $n -= $ndepth; # Noffset into row
430 0 my $repbit = _divrem_mutate($n,2); # low bit of $n
431 0 ▶ while (shift @$depthbits) { # bits of depth low to high
Branch: 1 branch
BranchTrueFalseTotal
if (shift @$depthbits) 0 0 0
432 0 ▶ if (_divrem_mutate($n,2) != $repbit) { # bits of $n offset low to high
Branch: 1 branch
BranchTrueFalseTotal
if (_divrem_mutate($n, 2) != $repbit) 0 0 0
433 0 return 0;
434 }
435 }
436 0 return 1;
437 }
438  
439 sub tree_n_children {
440 44 ▶ my ($self, $n) = @_;
Subroutine tree_n_children: called CC=8 SCAR=21.4
Pod documented
441 ### tree_n_num_children(): $n
442  
443 44 $n = $n - $self->{'n_start'}; # N=0 basis
444 44 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 44 44
445 0 return;
446 }
447 44 ▶ if ($n == 0 && $self->{'parts'} ne 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0 and $self->{"parts"} ne "all") 0 44 44
Condition66%2/3
$n == 0 and $self->{"parts"} ne "all"
!ll&&!rl&&r
4040
MC/DC0%0/2
$n == 0 and $self->{"parts"} ne "all"
$n == 0 $self->{"parts"} ne "all"
Truth table66%2/3
A and B
A= $n == 0B= $self->{"parts"} ne "all"
ABresult
0X0
100
111
448 # parts=left or parts=right have only 1 child under the root n=0
449 0 return ($n+1 + $self->{'n_start'});
450 }
451 44 ▶ my ($depthbits, $ndepth, $nwidth) = _n0_to_depthbits($n, $self->{'parts'})
Branch: 1 branch
BranchTrueFalseTotal
() = _n0_to_depthbits($n, $self->{"parts"}) or return $n 0 44 44
452 or return $n; # infinite
453  
454 44 $n -= $ndepth; # Noffset into row
455  
456 44 ▶ if (shift @$depthbits) {
Branch: 1 branch
BranchTrueFalseTotal
if (shift @$depthbits) { } 24 20 44
457 # Depth odd, single child under some or all points.
458 # When depth==1mod4 it's all points, when depth has more than one
459 # trailing 1-bit then it's only some points.
460 24 ▶ while (shift @$depthbits) { # depth==3mod4 or more low 1s
Branch: 1 branch
BranchTrueFalseTotal
if (shift @$depthbits) 16 16 32
461 16 my $repbit = _divrem_mutate($n,2);
462 16 ▶ if (($n % 2) != $repbit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n % 2 != $repbit) 8 8 16
463 8 return;
464 }
465 }
466 16 return $n + $ndepth+$nwidth + $self->{'n_start'};
467  
468 } else {
469 # Depth even (or zero), two children under every point.
470 20 $n = 2*$n + $ndepth+$nwidth + $self->{'n_start'};
471 20 return ($n,$n+1);
472 }
473 }
474 sub tree_n_parent {
475 44 ▶ my ($self, $n) = @_;
Subroutine tree_n_parent: called CC=5 SCAR=16.1
Pod documented
476  
477 44 ▶ my ($x,$y) = $self->n_to_xy($n)
Branch: 1 branch
BranchTrueFalseTotal
() = $self->n_to_xy($n) or return undef 0 44 44
478 or return undef;
479  
480 44 ▶ if ($self->{'align'} eq 'diagonal') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"align"} eq "diagonal") 11 33 44
481 11 my $n_parent = $self->xy_to_n($x-1, $y);
482 11 ▶ if (defined $n_parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n_parent) { } 5 6 11
483 5 return $n_parent;
484 } else {
485 6 return $self->xy_to_n($x,$y-1);
486 }
487 }
488  
489 33 $y -= 1;
490 33 my $n_parent = $self->xy_to_n($x-($self->{'align'} ne 'left'), $y);
491 33 ▶ if (defined $n_parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n_parent) 15 18 33
492 15 return $n_parent;
493 }
494 18 return $self->xy_to_n($x+($self->{'align'} ne 'right'),$y);
495 }
496  
497 sub tree_n_to_depth {
498 0 ▶ my ($self, $n) = @_;
Subroutine tree_n_to_depth: not called CC=3 SCAR=24.8
Pod documented
499 ### SierpinskiTriangle n_to_depth(): $n
500 0 $n = $n - $self->{'n_start'};
501 0 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
502 0 return undef;
503 }
504 0 ▶ my ($depthbits) = _n0_to_depthbits($n, $self->{'parts'})
Branch: 1 branch
BranchTrueFalseTotal
my($depthbits) = _n0_to_depthbits($n, $self->{"parts"}) or return $n 0 0 0
505 or return $n; # infinite
506 0 return digit_join_lowtohigh ($depthbits, 2, $n*0);
507 }
508 sub tree_depth_to_n {
509 38 ▶ my ($self, $depth) = @_;
Subroutine tree_depth_to_n: called CC=2 SCAR=7.2
Pod documented
510 38 ▶ return ($depth >= 0 ? _right_xy_to_n($self,0,$depth) : undef);
Branch: 1 branch
BranchTrueFalseTotal
$depth >= 0 ? : 38 0 38
511 }
512  
513 # sub _NOTWORKING__tree_depth_to_n_range {
514 # my ($self, $depth) = @_;
515 # if (is_infinite($depth)) {
516 # return $depth;
517 # }
518 # if ($depth < 0) {
519 # return undef;
520 # }
521 #
522 # my $zero = my $n = ($depth * 0); # inherit bignum 0
523 # my $width = my $npower = $zero+1; # inherit bignum 1
524 #
525 # foreach my $dbit (bit_split_lowtohigh($depth)) {
526 # if ($dbit) {
527 # $n = 2*$n + $npower;
528 # $width *= 2;
529 # }
530 # $npower *= 3;
531 # }
532 # $n += $self->{'n_start'};
533 #
534 # return ($n, $n+$width-1);
535 # }
536  
537  
538 #------------------------------------------------------------------------------
539 # In align=diagonal style, height is following a straight line X increment
540 # until hit bit in common with Y, meaning the end of Y low 0s. Or follow
541 # straight line Y until hit bit in common with X, meaning end of X low 0s.
542 #
543 # If X,Y both even then X or Y lines are the same.
544 # If X odd then follow X to limit of Y low 0s.
545 # If Y odd then follow Y to limit of X low 0s.
546 #
547 # | 65 ...
548 # | 57 66
549 # | 49 67
550 # | 45 50 58 68
551 # | 37 69
552 # | 33 38 59 70
553 # | 29 39 51 71
554 # | 27 30 34 40 46 52 60 72
555 # | 19 73
556 # | | |
557 # | 15-20 61-74
558 # | | |
559 # | 11 21 53 75
560 # | | | | |
561 # | 9-12-16-22 47-54-62-76
562 # | | |
563 # | 5 23 41 77
564 # | | | | |
565 # | 3--6 17-24 35-42 63-78
566 # | | | | |
567 # | 1 7 13 25 31 43 55 79
568 # | | | | | | | | |
569 # | 0--2--4--8-10-14-18-26-28-32-36-44-48-56-64-80
570 # +-------------------------------------------------
571 # X=0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
572 #
573 # depthbits 1 0 0 0 1 Y of "right"
574 # nbits n n
575 # xbits n 0 0 0 n
576 # ybits 1-n 1-n of Y-X for "diagonal"
577 #
578 # Y odd when ylow==1,nlow==0
579 # follow its X low 0s by nbit==0 and invert of ybits==1
580 # X odd when ylow==1,nlow==1
581 # follow its Y low 0s by nbit==1 and invert of xbits=nbits==1
582 #
583 # At a given depth<=2^k can go at most to its 2^k-1 limit, which means
584 # height = 2^k-1 - depth which is depth with bits flipped.
585 # Then bits of Noffset may put it in the middle of somewhere which limits
586 # the height to a sub-part 2^j < 2^k.
587 #
588 sub tree_n_to_subheight {
589 0 ▶ my ($self, $n) = @_;
Subroutine tree_n_to_subheight: not called CC=8 SCAR=42.8
Pod documented
590 ### SierpinskiTriangle tree_n_to_subheight(): $n
591  
592 0 $n = $n - $self->{'n_start'};
593 0 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
594 0 return undef;
595 }
596 0 ▶ my ($depthbits, $ndepth) = _n0_to_depthbits($n, $self->{'parts'})
Branch: 1 branch
BranchTrueFalseTotal
() = _n0_to_depthbits($n, $self->{"parts"}) or return $n 0 0 0
597 or return $n; # infinite
598 0 $n -= $ndepth; # offset into row
599 0 my @nbits = bit_split_lowtohigh($n);
600  
601 0 ▶ my $target = $nbits[0] || 0;
Condition0%0/2
$nbits[0] || 0
l!l
00
MC/DC0%0/1
$nbits[0] || 0
$nbits[0]
Truth table0%0/2
A
A= $nbits[0]
Aresult
11
00
602 0 foreach my $i (0 .. $#$depthbits) {
603 0 ▶ unless ($depthbits->[$i] ^= 1) { # flip 0<->1, at original==1 take nbit
Branch: 1 branch
BranchTrueFalseTotal
unless ($depthbits->[$i] ^= 1) 0 0 0
604 0 ▶ if ((shift @nbits || 0) != $target) {
Branch: 1 branch
BranchTrueFalseTotal
if ((shift @nbits || 0) != $target) 0 0 0
Condition0%0/2
shift @nbits || 0
l!l
00
MC/DC0%0/1
shift @nbits || 0
shift @nbits
Truth table0%0/2
A
A= shift @nbits
Aresult
11
00
605 0 $#$depthbits = $i-1;
606 0 return digit_join_lowtohigh($depthbits, 2, $n*0);
607 }
608 }
609 }
610 0 return undef; # first or last of row, infinite
611 }
612  
613  
614 #------------------------------------------------------------------------------
615 # \ /
616 # 4 0 0 0 0 0 0 4
617 # \ / \ / \ / \ /
618 # 1 1 1 1
619 # \ / \ /
620 # 2 2 2 2
621 # \ / \ /
622 # 3 3
623 # \ /
624 # 4 0 0 4
625 # \ / \ /
626 # 1 1
627 # \ /
628 # 2 2
629 # \ /
630 # 3
631  
632 # sub _EXPERIMENTAL__tree_n_to_leafdist {
633 # my ($self, $n) = @_;
634 # ### _EXPERIMENTAL__tree_n_to_leafdist() ...
635 # my $d = $self->tree_n_to_depth($n);
636 # if (defined $d) {
637 # $d = 3 - ($d % 4);
638 # if ($d == 0 && $self->tree_n_num_children($n) != 0) {
639 # $d = 4;
640 # }
641 # }
642 # return $d;
643 # }
644 sub _EXPERIMENTAL__tree_n_to_leafdist {
645 0 ▶ my ($self, $n) = @_;
Subroutine _EXPERIMENTAL__tree_n_to_leafdist: not called CC=8 SCAR=42.8
646 ### _EXPERIMENTAL__tree_n_to_leafdist(): $n
647  
648 0 $n = $n - $self->{'n_start'}; # N=0 basis
649 0 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
650 0 return undef;
651 }
652 0 ▶ my ($depthbits, $ndepth) = _n0_to_depthbits($n, $self->{'parts'}) # low to high
Branch: 1 branch
BranchTrueFalseTotal
() = _n0_to_depthbits($n, $self->{"parts"}) or return 1 0 0 0
653 or return 1; # infinite
654 ### $depthbits
655  
656 # depth bits leafdist
657 # 0 0,0 3
658 # 1 0,1 2
659 # 2 1,0 1
660 # 3 1,1 0 or 4
661 #
662 0 ▶ my $ret = 3 - ((shift @$depthbits)||0);
Condition0%0/2
shift @$depthbits || 0
l!l
00
MC/DC0%0/1
shift @$depthbits || 0
shift @$depthbits
Truth table0%0/2
A
A= shift @$depthbits
Aresult
11
00
663 0 ▶ if (shift @$depthbits) { $ret -= 2; }
Branch: 1 branch
BranchTrueFalseTotal
if (shift @$depthbits) 0 0 0
664 ### $ret
665 0 ▶ if ($ret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret) 0 0 0
666 0 return $ret;
667 }
668  
669 0 $n -= $ndepth;
670 ### Noffset into row: $n
671  
672 # Low bits of Nrem unchanging while trailing 1-bits in @depthbits,
673 # to distinguish between leaf or non-leaf. Same as tree_n_children().
674 #
675 0 my $repbit = _divrem_mutate($n,2); # low bit of $n
676 ### $repbit
677 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
shift @$depthbits and do { do { if (_divrem_mutate($n, 2) != $repbit) { return 0; } } } 0 0 0
678 ### next bit: $n%2
679 0 ▶ if (_divrem_mutate($n,2) != $repbit) { # bits of $n offset low to high
Branch: 1 branch
BranchTrueFalseTotal
if (_divrem_mutate($n, 2) != $repbit) 0 0 0
680 0 return 0; # is a leaf
681 }
682 } while (shift @$depthbits);
683 0 return 4; # is a non-leaf
684 }
685  
686 #------------------------------------------------------------------------------
687 # Return ($depthbits, $ndepth, $nwidth).
688 # $depthbits is an arrayref of bits low to high which are the tree depth of $n.
689 # $ndepth is first N of the row.
690 # $nwidth is the number of points in the row.
691 #
692 sub _n0_to_depthbits {
693 349 ▶ my ($n, $parts) = @_;
Subroutine _n0_to_depthbits: called CC=7 SCAR=19.6
694 ### _n0_to_depthbits(): "$n $parts"
695  
696 349 ▶ if (is_infinite($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 349 349
697 0 return;
698 }
699 349 ▶ if ($n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0) 4 345 349
700 4 return ([], 0, 1);
701 }
702  
703 345 ▶ my ($nwidth, $bitpos) = round_down_pow ($parts eq 'all' ? $n : 2*$n-1,
Branch: 1 branch
BranchTrueFalseTotal
$parts eq 'all' ? : 345 0 345
704 3);
705 ### $nwidth
706 ### $bitpos
707  
708 345 my @depthbits;
709 345 $depthbits[$bitpos] = 1;
710 345 ▶ my $ndepth = ($parts eq 'all' ? $nwidth : ($nwidth + 1)/2);
Branch: 1 branch
BranchTrueFalseTotal
$parts eq 'all' ? : 345 0 345
711 345 $nwidth *= 2;
712  
713 345 ▶ while (--$bitpos >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$bitpos >= 0) 494 345 839
714 494 $nwidth /= 3;
715 ### at: "n=$n nwidth=$nwidth bitpos=$bitpos depthbits=".join(',',map{$_||0}@depthbits)
716  
717 494 ▶ if ($n >= $ndepth + $nwidth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= $ndepth + $nwidth) { } 240 254 494
718 240 $depthbits[$bitpos] = 1;
719 240 $ndepth += $nwidth;
720 240 $nwidth *= 2;
721 } else {
722 254 $depthbits[$bitpos] = 0;
723 }
724 }
725  
726 # Nwidth = 2**count1bits(depth), when parts=all
727 ### @depthbits
728 ### assert: $parts ne 'all' || $nwidth == (1 << scalar(grep{$_}@depthbits))
729  
730 345 return (\@depthbits, $ndepth, $nwidth);
731 }
732  
733 #------------------------------------------------------------------------------
734 # levels
735  
736 5 ▶use Math::PlanePath::SierpinskiArrowheadCentres;
Subroutine BEGIN: called CC=1 SCAR=0.0
737 5 *level_to_n_range = \&Math::PlanePath::SierpinskiArrowheadCentres::level_to_n_range;
738 5 *n_to_level = \&Math::PlanePath::SierpinskiArrowheadCentres::n_to_level;
739  
740 #-----------------------------------------------------------------------------
741 5 1;
742 __END__
« NumSeq/PlanePathDelta.pm ↑ Summary PlanePath/DiagonalsOctant.pm »