PlanePath/PythagoreanTree.pm

filter: Statementstmt 64.1% 272 / 424 Branchbran 48.9% 93 / 190 Conditioncond 57.6% 49 / 85 MC/DCmcdc 48.9% 23 / 47 Subroutinesub 54.1% 33 / 61 Podpod 100% 25 / 25 totaltotal 59.5% 495 / 832 CC 129 SCAR 30.4
CC
129
Cov
59%
n_to_radius
56.0
xy_to_n
54.1
_mc_to_pq
42.0
CRAP 20.8
« PlanePath/DekkingCentres.pm ↑ Summary NumSeq/PlanePathDelta.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 # math-image --path=PythagoreanTree --all --scale=3
20  
21 # http://sunilchebolu.wordpress.com/pythagorean-triples-and-the-integer-points-on-a-hyperboloid/
22  
23 # http://www.math.uconn.edu/~kconrad/blurbs/ugradnumthy/pythagtriple.pdf
24 #
25 # http://www.math.ou.edu/~dmccullough/teaching/pythagoras1.pdf
26 # http://www.math.ou.edu/~dmccullough/teaching/pythagoras2.pdf
27 #
28 # http://www.microscitech.com/pythag_eigenvectors_invariants.pdf
29 #
30  
31  
32 package Math::PlanePath::PythagoreanTree;
33 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 2 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 $VERSION = 128;
39 2 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 *_divrem = \&Math::PlanePath::_divrem;
41 2 *_sqrtint = \&Math::PlanePath::_sqrtint;
42 2 @ISA = ('Math::PlanePath');
43  
44 #use List::Util 'min','max';
45 2 *min = \&Math::PlanePath::_min;
46 2 *max = \&Math::PlanePath::_max;
47  
48 use Math::PlanePath::Base::Generic
49 2 'is_infinite',
50 2 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 use Math::PlanePath::Base::Digits
52 2 'round_down_pow',
53 'digit_split_lowtohigh',
54 2 ▶ 'digit_join_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶use Math::PlanePath::GrayCode;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 # uncomment this to run the ### lines
58 # use Smart::Comments;
59  
60 2 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 2 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 2 ▶use constant tree_num_children_list => (3); # complete ternary tree
Subroutine BEGIN: called CC=1 SCAR=0.0
63 2 ▶use constant tree_n_to_subheight => undef; # complete tree, all infinity
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 2 use constant parameter_info_array =>
66 [ { name => 'tree_type',
67 share_key => 'tree_type_uadfb',
68 display => 'Tree Type',
69 type => 'enum',
70 default => 'UAD',
71 choices => ['UAD','UArD','FB','UMT'],
72 },
73 { name => 'coordinates',
74 share_key => 'coordinates_abcpqsm',
75 display => 'Coordinates',
76 type => 'enum',
77 default => 'AB',
78 choices => ['AB','AC','BC','PQ', 'SM','SC','MC',
79 # 'BA'
80 # 'UV', # q from x=y diagonal down at 45-deg
81 # 'RS','ST', # experimental
82 ],
83 },
84 { name => 'digit_order',
85 display => 'Digit Order',
86 type => 'enum',
87 default => 'HtoL',
88 choices => ['HtoL','LtoH'],
89 },
90 2 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 {
93 2 my %UAD_coordinates_always_right = (PQ => 1,
94 AB => 1,
95 AC => 1);
96 sub turn_any_left {
97 0 ▶ my ($self) = @_;
Subroutine turn_any_left: not called CC=2 SCAR=17.9
Pod documented
98 return ! ($self->{'tree_type'} eq 'UAD'
99 0 ▶ && $UAD_coordinates_always_right{$self->{'coordinates'}});
Condition0%0/3
$self->{'tree_type'} eq 'UAD' && $UAD_coordinates_always_right{$self->{'coordinates'}}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{'tree_type'} eq 'UAD' && $UAD_coordinates_always_right{$self->{'coordinates'}}
$self->{'tree_type'} eq 'UAD' $UAD_coordinates_always_right{$self->{'coordinates'}}
Truth table0%0/3
A && B
A= $self->{'tree_type'} eq 'UAD'B= $UAD_coordinates_always_right{$self->{'coordinates'}}
ABresult
0X0
100
111
100 }
101 }
102 {
103 2 my %UAD_coordinates_always_left = (BC => 1);
104 sub turn_any_right {
105 0 ▶ my ($self) = @_;
Subroutine turn_any_right: not called CC=2 SCAR=17.9
Pod documented
106 return ! ($self->{'tree_type'} eq 'UAD'
107 0 ▶ && $UAD_coordinates_always_left{$self->{'coordinates'}});
Condition0%0/3
$self->{'tree_type'} eq 'UAD' && $UAD_coordinates_always_left{$self->{'coordinates'}}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{'tree_type'} eq 'UAD' && $UAD_coordinates_always_left{$self->{'coordinates'}}
$self->{'tree_type'} eq 'UAD' $UAD_coordinates_always_left{$self->{'coordinates'}}
Truth table0%0/3
A && B
A= $self->{'tree_type'} eq 'UAD'B= $UAD_coordinates_always_left{$self->{'coordinates'}}
ABresult
0X0
100
111
108 }
109 }
110 {
111 2 my %UMT_coordinates_any_straight = (BC => 1, # UMT at N=5
112 PQ => 1); # UMT at N=5
113 sub turn_any_straight {
114 0 ▶ my ($self) = @_;
Subroutine turn_any_straight: not called CC=2 SCAR=17.9
Pod documented
115 return ($self->{'tree_type'} eq 'UMT'
116 0 ▶ && $UMT_coordinates_any_straight{$self->{'coordinates'}});
Condition0%0/3
$self->{'tree_type'} eq 'UMT' && $UMT_coordinates_any_straight{$self->{'coordinates'}}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{'tree_type'} eq 'UMT' && $UMT_coordinates_any_straight{$self->{'coordinates'}}
$self->{'tree_type'} eq 'UMT' $UMT_coordinates_any_straight{$self->{'coordinates'}}
Truth table0%0/3
A && B
A= $self->{'tree_type'} eq 'UMT'B= $UMT_coordinates_any_straight{$self->{'coordinates'}}
ABresult
0X0
100
111
117 }
118 }
119  
120  
121 #------------------------------------------------------------------------------
122 {
123 2 my %coordinate_minimum = (A => 3,
124 B => 4,
125 C => 5,
126 P => 2,
127 Q => 1,
128 S => 3,
129 M => 4,
130 );
131 sub x_minimum {
132 0 ▶ my ($self) = @_;
Subroutine x_minimum: not called CC=1 SCAR=6.9
Pod documented
133 0 return $coordinate_minimum{substr($self->{'coordinates'},0,1)};
134 }
135 sub y_minimum {
136 0 ▶ my ($self) = @_;
Subroutine y_minimum: not called CC=1 SCAR=6.9
Pod documented
137 0 return $coordinate_minimum{substr($self->{'coordinates'},1)};
138 }
139 }
140 {
141 2 my %diffxy_minimum = (PQ => 1, # octant X>=Y+1 so X-Y>=1
142 );
143 sub diffxy_minimum {
144 0 ▶ my ($self) = @_;
Subroutine diffxy_minimum: not called CC=1 SCAR=6.9
Pod documented
145 0 return $diffxy_minimum{$self->{'coordinates'}};
146 }
147 }
148 {
149 2 my %diffxy_maximum = (AC => -2, # C>=A+2 so X-Y<=-2
150 BC => -1, # C>=B+1 so X-Y<=-1
151 SM => -1, # S<M so X-Y<=-1
152 SC => -2, # S<M<C so S-C<=-2
153 MC => -1, # M<C so M-C<=-1
154 );
155 sub diffxy_maximum {
156 0 ▶ my ($self) = @_;
Subroutine diffxy_maximum: not called CC=1 SCAR=6.9
Pod documented
157 0 return $diffxy_maximum{$self->{'coordinates'}};
158 }
159 }
160 {
161 2 my %absdiffxy_minimum = (PQ => 1,
162 AB => 1, # X=Y never occurs
163 BA => 1, # X=Y never occurs
164 AC => 2, # C>=A+2 so abs(X-Y)>=2
165 BC => 1,
166 SM => 1, # X=Y never occurs
167 SC => 2, # X<=Y-2
168 MC => 1, # X=Y never occurs
169 );
170 sub absdiffxy_minimum {
171 0 ▶ my ($self) = @_;
Subroutine absdiffxy_minimum: not called CC=1 SCAR=6.9
Pod documented
172 0 return $absdiffxy_minimum{$self->{'coordinates'}};
173 }
174 }
175 2 ▶use constant gcdxy_maximum => 1; # no common factor
Subroutine BEGIN: called CC=1 SCAR=0.0
176  
177 {
178 2 my %absdx_minimum = ('AB,UAD' => 2,
179 'AB,FB' => 2,
180 'AB,UMT' => 2,
181  
182 'AC,UAD' => 2,
183 'AC,FB' => 2,
184 'AC,UMT' => 2,
185  
186 'BC,UAD' => 4, # at N=37
187 'BC,FB' => 4, # at N=2 X=12,Y=13
188 'BC,UMT' => 4, # at N=2 X=12,Y=13
189  
190 'PQ,UAD' => 0,
191 'PQ,FB' => 0,
192 'PQ,UMT' => 0,
193  
194 'SM,UAD' => 1,
195 'SM,FB' => 1,
196 'SM,UMT' => 2,
197  
198 'SC,UAD' => 1,
199 'SC,FB' => 1,
200 'SC,UMT' => 1,
201  
202 'MC,UAD' => 3,
203 'MC,FB' => 3,
204 'MC,UMT' => 1,
205 );
206 sub absdx_minimum {
207 0 ▶ my ($self) = @_;
Subroutine absdx_minimum: not called CC=2 SCAR=17.9
Pod documented
208 0 ▶ return $absdx_minimum{"$self->{'coordinates'},$self->{'tree_type'}"} || 0;
Condition0%0/2
$absdx_minimum{"$self->{'coordinates'},$self->{'tree_type'}"} || 0
l!l
00
MC/DC0%0/1
$absdx_minimum{"$self->{'coordinates'},$self->{'tree_type'}"} || 0
$absdx_minimum{"$self->{'coordinates'},$self->{'tree_type'}"}
Truth table0%0/2
A
A= $absdx_minimum{"$self->{'coordinates'},$self->{'tree_type'}"}
Aresult
11
00
209 }
210 }
211 {
212 2 my %absdy_minimum = ('AB,UAD' => 4,
213 'AB,FB' => 4,
214 'AB,UMT' => 4,
215  
216 'AC,UAD' => 4,
217 'AC,FB' => 4,
218 'BC,UAD' => 4,
219 'BC,FB' => 4,
220 'PQ,UAD' => 0,
221 'PQ,FB' => 1,
222  
223 'SM,UAD' => 3,
224 'SM,FB' => 3,
225 'SM,UMT' => 1,
226  
227 'SC,UAD' => 4,
228 'SC,FB' => 4,
229 'MC,UAD' => 4,
230 'MC,FB' => 4,
231 );
232 sub absdy_minimum {
233 0 ▶ my ($self) = @_;
Subroutine absdy_minimum: not called CC=2 SCAR=17.9
Pod documented
234 0 ▶ return $absdy_minimum{"$self->{'coordinates'},$self->{'tree_type'}"} || 0;
Condition0%0/2
$absdy_minimum{"$self->{'coordinates'},$self->{'tree_type'}"} || 0
l!l
00
MC/DC0%0/1
$absdy_minimum{"$self->{'coordinates'},$self->{'tree_type'}"} || 0
$absdy_minimum{"$self->{'coordinates'},$self->{'tree_type'}"}
Truth table0%0/2
A
A= $absdy_minimum{"$self->{'coordinates'},$self->{'tree_type'}"}
Aresult
11
00
235 }
236 }
237  
238 {
239 2 my %dir_minimum_dxdy = (# AB apparent minimum dX=16,dY=8
240 'AB,UAD' => [16,8],
241 'AC,UAD' => [1,1], # it seems
242 # 'BC,UAD' => [1,0], # infimum
243 # 'SM,UAD' => [1,0], # infimum
244 # 'SC,UAD' => [1,0], # N=255 dX=7,dY=0
245 # 'MC,UAD' => [1,0], # infimum
246  
247 # 'SM,FB' => [1,0], # infimum
248 # 'SC,FB' => [1,0], # infimum
249 # 'SM,FB' => [1,0], # infimum
250  
251 'AB,UMT' => [6,12], # it seems
252  
253 # N=ternary 1111111122 dx=118,dy=40
254 # in general dx=3*4k-2 dy=4k
255 'AC,UMT' => [3,1], # infimum
256 #
257 # 'BC,UMT' => [1,0], # N=31 dX=72,dY=0
258 'PQ,UMT' => [1,1], # N=1
259 'SM,UMT' => [1,0], # infiumum dX=big,dY=3
260 'SC,UMT' => [3,1], # like AC
261 # 'MC,UMT' => [1,0], # at N=31
262 );
263 sub dir_minimum_dxdy {
264 0 ▶ my ($self) = @_;
Subroutine dir_minimum_dxdy: not called CC=2 SCAR=17.9
Pod documented
265 0 ▶ return @{$dir_minimum_dxdy{"$self->{'coordinates'},$self->{'tree_type'}"}
Branch: 1 branch
BranchTrueFalseTotal
$dir_minimum_dxdy{qq[$self->{"coordinates"},$self->{"tree_type"}]} or [1, 0] 0 0 0
266 || [1,0] };
267 }
268 }
269 {
270 # AB apparent maximum dX=-6,dY=-12 at N=3
271 # AC apparent maximum dX=-6,dY=-12 at N=3 same
272 # PQ apparent maximum dX=-1,dY=-1
273 2 my %dir_maximum_dxdy = ('AB,UAD' => [-6,-12],
274 'AC,UAD' => [-6,-12],
275 # 'BC,UAD' => [0,0],
276 'PQ,UAD' => [-1,-1],
277 # 'SM,UAD' => [0,0], # supremum
278 # 'SC,UAD' => [0,0], # supremum
279 # 'MC,UAD' => [0,0], # supremum
280  
281 # 'AB,FB' => [0,0],
282 # 'AC,FB' => [0,0],
283 'BC,FB' => [1,-1],
284 # 'PQ,FB' => [0,0],
285 # 'SM,FB' => [0,0], # supremum
286 # 'SC,FB' => [0,0], # supremum
287 # 'MC,FB' => [0,0], # supremum
288  
289 # N=ternary 1111111122 dx=118,dy=-40
290 # in general dx=3*4k-2 dy=-4k
291 'AB,UMT' => [3,-1], # supremum
292 #
293 'AC,UMT' => [-10,-20], # at N=9 apparent maximum
294 # 'BC,UMT' => [0,0], # apparent approach
295 'PQ,UMT' => [1,-1], # N=2
296 # 'SM,UMT' => [0,0], # supremum dX=big,dY=-1
297 'SC,UMT' => [-3,-5], # apparent approach
298 # 'MC,UMT' => [0,0], # supremum dX=big,dY=-small
299 );
300 sub dir_maximum_dxdy {
301 0 ▶ my ($self) = @_;
Subroutine dir_maximum_dxdy: not called CC=2 SCAR=17.9
Pod documented
302 0 ▶ return @{$dir_maximum_dxdy{"$self->{'coordinates'},$self->{'tree_type'}"}
Branch: 1 branch
BranchTrueFalseTotal
$dir_maximum_dxdy{qq[$self->{"coordinates"},$self->{"tree_type"}]} or [0, 0] 0 0 0
303 || [0,0]};
304 }
305 }
306  
307 #------------------------------------------------------------------------------
308  
309 2 sub _noop {
310 1340 ▶ return @_;
Subroutine _noop: called CC=1 SCAR=0.0
311 }
312 2 my %xy_to_pq = (AB => \&_ab_to_pq,
313 AC => \&_ac_to_pq,
314 BC => \&_bc_to_pqa, # ignoring extra $a return
315 PQ => \&_noop,
316 SM => \&_sm_to_pq,
317 SC => \&_sc_to_pq,
318 MC => \&_mc_to_pq,
319 UV => \&_uv_to_pq,
320 RS => \&_rs_to_pq,
321 ST => \&_st_to_pq,
322 );
323 2 my %pq_to_xy = (AB => \&_pq_to_ab,
324 AC => \&_pq_to_ac,
325 BC => \&_pq_to_bc,
326 PQ => \&_noop,
327 SM => \&_pq_to_sm,
328 SC => \&_pq_to_sc,
329 MC => \&_pq_to_mc,
330 UV => \&_pq_to_uv,
331 RS => \&_pq_to_rs,
332 ST => \&_pq_to_st,
333 );
334  
335 2 my %tree_types = (UAD => 1,
336 UArD => 1,
337 FB => 1,
338 UMT => 1);
339 2 my %digit_orders = (HtoL => 1,
340 LtoH => 1);
341 sub new {
342 21 ▶ my $self = shift->SUPER::new (@_);
Subroutine new: called CC=7 SCAR=20.0
Pod documented
343 {
344 21 ▶ my $digit_order = ($self->{'digit_order'} ||= 'HtoL');
Condition50%1/2
$self->{"digit_order"} ||= "HtoL"
l!l
021
MC/DC0%0/1
$self->{"digit_order"} ||= "HtoL"
$self->{"digit_order"}
Truth table50%1/2
A
A= $self->{"digit_order"}
Aresult
11
00
345 21 ▶ $digit_orders{$digit_order}
Branch: 1 branch
BranchTrueFalseTotal
$digit_orders{$digit_order} or croak("Unrecognised digit_order option: ", $digit_order) 0 21 21
346 || croak "Unrecognised digit_order option: ",$digit_order;
347 }
348 {
349 21 ▶ my $tree_type = ($self->{'tree_type'} ||= 'UAD');
Condition100%2/2
$self->{"tree_type"} ||= "UAD"
l!l
1110
MC/DC100%1/1
$self->{"tree_type"} ||= "UAD"
$self->{"tree_type"}
Truth table100%2/2
A
A= $self->{"tree_type"}
Aresult
11
00
350 21 ▶ $tree_types{$tree_type}
Branch: 1 branch
BranchTrueFalseTotal
$tree_types{$tree_type} or croak("Unrecognised tree_type option: ", $tree_type) 0 21 21
351 || croak "Unrecognised tree_type option: ",$tree_type;
352 }
353 {
354 21 ▶ my $coordinates = ($self->{'coordinates'} ||= 'AB');
Condition100%2/2
$self->{"coordinates"} ||= "AB"
l!l
147
MC/DC100%1/1
$self->{"coordinates"} ||= "AB"
$self->{"coordinates"}
Truth table100%2/2
A
A= $self->{"coordinates"}
Aresult
11
00
355 21 ▶ $self->{'xy_to_pq'} = $xy_to_pq{$coordinates}
Condition33%1/3
$xy_to_pq{$coordinates} || croak('Unrecognised coordinates option: ', $coordinates)
l!l&&r!l&&!r
2100
MC/DC0%0/2
$xy_to_pq{$coordinates} || croak('Unrecognised coordinates option: ', $coordinates)
$xy_to_pq{$coordinates} croak('Unrecognised coordinates option: ', $coordinates)
Truth table33%1/3
A || B
A= $xy_to_pq{$coordinates}B= croak('Unrecognised coordinates option: ', $coordinates)
ABresult
1X1
011
000
356 || croak "Unrecognised coordinates option: ",$coordinates;
357 21 $self->{'pq_to_xy'} = $pq_to_xy{$coordinates};
358 }
359 21 return $self;
360 }
361  
362 sub n_to_xy {
363 56 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=4 SCAR=17.9
Pod documented
364 ### PythagoreanTree n_to_xy(): $n
365  
366 56 ▶ if ($n < 1) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1) 0 56 56
367 56 ▶ if (is_infinite($n)) { return ($n,$n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 56 56
368  
369 {
370 56 my $int = int($n);
371 56 ▶ if ($n != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $int) 0 56 56
372 0 my $frac = $n - $int; # inherit possible BigFloat/BigRat
373 0 my ($x1,$y1) = $self->n_to_xy($int);
374 0 my ($x2,$y2) = $self->n_to_xy($int+1);
375 0 my $dx = $x2-$x1;
376 0 my $dy = $y2-$y1;
377 0 return ($frac*$dx + $x1, $frac*$dy + $y1);
378 }
379 }
380  
381 56 return &{$self->{'pq_to_xy'}}(_n_to_pq($self,$n));
382 }
383  
384 # maybe similar n_to_rsquared() as C^2=(P^2+Q^2)^2
385 sub n_to_radius {
386 0 ▶ my ($self, $n) = @_;
Subroutine n_to_radius: not called CC=7 SCAR=40.3
Pod documented
387  
388 0 ▶ if (($self->{'coordinates'} eq 'AB'
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"coordinates"} eq "AB" || $self->{"coordinates"} eq "BA" || $self->{"coordinates"} eq "SM" and $n == int $n) 0 0 0
Condition0%0/9
$self->{"coordinates"} eq "AB" || $self->{"coordinates"} eq "BA" || $self->{"coordinates"} eq "SM" and $n == int $n
!ll&&!rl&&r
000
$self->{"coordinates"} eq "AB" or $self->{"coordinates"} eq "BA" or $self->{"coordinates"} eq "SM"
l!l&&r!l&&!r
000
$self->{"coordinates"} eq "AB" or $self->{"coordinates"} eq "BA"
l!l&&r!l&&!r
000
MC/DC0%0/4
$self->{"coordinates"} eq "AB" || $self->{"coordinates"} eq "BA" || $self->{"coordinates"} eq "SM" and $n == int $n
$self->{"coordinates"} eq "AB" $self->{"coordinates"} eq "BA" $self->{"coordinates"} eq "SM" $n == int $n
Truth table0%0/7
A or B or C and D
A= $self->{"coordinates"} eq "AB"B= $self->{"coordinates"} eq "BA"C= $self->{"coordinates"} eq "SM"D= $n == int $n
ABCDresult
000X0
1XX00
01X00
00100
1XX11
01X11
00111
389 || $self->{'coordinates'} eq 'BA'
390 || $self->{'coordinates'} eq 'SM')
391 && $n == int($n)) {
392 0 ▶ if ($n < 1) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1) 0 0 0
393 0 ▶ if (is_infinite($n)) { return $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
394 0 my ($p,$q) = _n_to_pq($self,$n);
395 0 return $p*$p + $q*$q; # C=P^2+Q^2
396 }
397  
398 0 return $self->SUPER::n_to_radius($n);
399 }
400  
401 sub _n_to_pq {
402 56 ▶ my ($self, $n) = @_;
Subroutine _n_to_pq: called CC=14 SCAR=29.5
403  
404 56 my $ndigits = _n_to_digits_lowtohigh($n);
405 ### $ndigits
406  
407 56 ▶ if ($self->{'tree_type'} eq 'UArD') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tree_type"} eq "UArD") 0 56 56
408 0 Math::PlanePath::GrayCode::_digits_to_gray_reflected($ndigits,3);
409 ### gray: $ndigits
410 }
411 56 ▶ if ($self->{'digit_order'} eq 'HtoL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"digit_order"} eq "HtoL") 56 0 56
412 56 @$ndigits = reverse @$ndigits;
413 ### reverse: $ndigits
414 }
415  
416 56 my $zero = $n * 0;
417  
418 56 my $p = 2 + $zero;
419 56 my $q = 1 + $zero;
420  
421 56 ▶ if ($self->{'tree_type'} eq 'FB') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'tree_type'} eq 'FB') { } 26 30 56
elsif ($self->{'tree_type'} eq 'UMT') { } 0 30 30
422 ### FB ...
423  
424 26 foreach my $digit (@$ndigits) { # high to low, possibly $digit=undef
425 ### $p
426 ### $q
427 ### $digit
428  
429 42 ▶ if ($digit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit) { } 28 14 42
430 28 ▶ if ($digit == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit == 1) { } 14 14 28
431 14 $q = $p-$q; # (2p, p-q) M2
432 14 $p *= 2;
433 } else {
434 # ($p,$q) = (2*$p, $p+$q);
435 14 $q += $p; # (p+q, 2q) M3
436 14 $p *= 2;
437 }
438 } else { # $digit == 0
439 # ($p,$q) = ($p+$q, 2*$q);
440 14 $p += $q; # (p+q, 2q) M1
441 14 $q *= 2;
442 }
443 }
444 } elsif ($self->{'tree_type'} eq 'UMT') {
445 ### UMT ...
446  
447 0 foreach my $digit (@$ndigits) { # high to low, possibly $digit=undef
448 ### $p
449 ### $q
450 ### $digit
451  
452 0 ▶ if ($digit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit) { } 0 0 0
453 0 ▶ if ($digit == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit == 1) { } 0 0 0
454 0 $q = $p-$q; # (2p, p-q) M2
455 0 $p *= 2;
456 } else { # $digit == 2
457 0 $p += 3*$q; # T
458 0 $q *= 2;
459 }
460 } else { # $digit == 0
461 # ($p,$q) = ($p+$q, 2*$q);
462 0 ($p,$q) = (2*$p-$q, $p); # "U" = (2p-q, p)
463 }
464 }
465 } else {
466 ### UAD or UArD ...
467 ### assert: $self->{'tree_type'} eq 'UAD' || $self->{'tree_type'} eq 'UArD'
468  
469 # # Could optimize high zeros as repeated U
470 # # high zeros as repeated U: $depth-scalar(@$ndigits)
471 # # U^0 = p, q
472 # # U^1 = 2p-q, p eg. P=2,Q=1 is 2*2-1,2 = 3,2
473 # # U^2 = 3p-2q, 2p-q eg. P=2,Q=1 is 3*2-2*1,2*2-1 = 4,3
474 # # U^3 = 4p-3q, 3p-2q
475 # # U^k = (k+1)p-kq, kp-(k-1)q for k>=2
476 # # = p + k*(p-q), k*(p-q)+q
477 # # and with initial p=2,q=1
478 # # U^k = 2+k, 1+k
479 # #
480 # $q = $depth - $#ndigits + $zero; # count high zeros + 1
481 # $p = $q + 1 + $zero;
482  
483 30 foreach my $digit (@$ndigits) { # high to low, possibly $digit=undef
484 ### $p
485 ### $q
486 ### $digit
487  
488 52 ▶ if ($digit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit) { } 34 18 52
489 34 ▶ if ($digit == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit == 1) { } 18 16 34
490 18 ($p,$q) = (2*$p+$q, $p); # "A" = (2p+q, p)
491 } else {
492 16 $p += 2*$q; # "D" = (p+2q, q)
493 }
494 } else { # $digit==0
495 18 ($p,$q) = (2*$p-$q, $p); # "U" = (2p-q, p)
496 }
497 }
498  
499 }
500  
501 ### final pq: "$p, $q"
502  
503 56 return ($p, $q);
504 }
505  
506 # _n_to_digits_lowtohigh() returns an arrayref $ndigits which is a list of
507 # ternary digits 0,1,2 from low to high which are the position of $n within
508 # its row of the tree.
509 # The length of the array is the depth.
510 #
511 # depth N N%3 2*N-1 (N-2)/3*2+1
512 # 0 1 1 1 1/3
513 # 1 2 2 3 1
514 # 2 5 2 9 3
515 # 3 14 2 27 9
516 # 4 41 2 81 27 28 + (28/2-1) = 41
517 #
518 # (N-2)/3*2+1 rounded down to pow=3^k gives depth=k+1 and base=pow+(pow+1)/2
519 # is the start of the row base=1,2,5,14,41 etc.
520 #
521 # An easier calculation is 2*N-1 rounded down to pow=3^d gives depth=d and
522 # base=2*pow-1, but 2*N-1 and 2*pow-1 might overflow an integer. Though
523 # just yet round_down_pow() goes into floats and so doesn't preserve 64-bit
524 # integer. So the technique here helps 53-bit float integers, but not right
525 # up to 64-bits.
526 #
527 sub _n_to_digits_lowtohigh {
528 76 ▶ my ($n) = @_;
Subroutine _n_to_digits_lowtohigh: called CC=2 SCAR=6.9
529 ### _n_to_digits_lowtohigh(): $n
530  
531 76 my @ndigits;
532 76 ▶ if ($n >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 2) 69 7 76
533 69 my ($pow) = _divrem($n-2, 3);
534 69 ($pow, my $depth) = round_down_pow (2*$pow+1, 3);
535 ### $depth
536 ### base: $pow + ($pow+1)/2
537 ### offset: $n - $pow - ($pow+1)/2
538 69 @ndigits = digit_split_lowtohigh ($n - $pow - ($pow+1)/2, 3);
539 69 push @ndigits, (0) x ($depth - $#ndigits); # pad to $depth with 0s
540 }
541 ### @ndigits
542 76 return \@ndigits;
543  
544  
545 # {
546 # my ($pow, $depth) = round_down_pow (2*$n-1, 3);
547 #
548 # ### h: 2*$n-1
549 # ### $depth
550 # ### $pow
551 # ### base: ($pow + 1)/2
552 # ### rem n: $n - ($pow + 1)/2
553 #
554 # my @ndigits = digit_split_lowtohigh ($n - ($pow+1)/2, 3);
555 # $#ndigits = $depth-1; # pad to $depth with undefs
556 # ### @ndigits
557 #
558 # return \@ndigits;
559 # }
560 }
561  
562 #------------------------------------------------------------------------------
563 # xy_to_n()
564  
565 # Nrow(depth+1) - Nrow(depth)
566 # = (3*pow+1)/2 - (pow+1)/2
567 # = (3*pow + 1 - pow - 1)/2
568 # = (2*pow)/2
569 # = pow
570 #
571 sub xy_to_n {
572 5213 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=30 SCAR=39.9
Pod documented
573 5213 $x = round_nearest ($x);
574 5213 $y = round_nearest ($y);
575 ### PythagoreanTree xy_to_n(): "$x, $y"
576  
577 5213 ▶ my ($p,$q) = &{$self->{'xy_to_pq'}}($x,$y)
Branch: 1 branch
BranchTrueFalseTotal
() = &{$self->{"xy_to_pq"};}($x, $y) or return undef 3844 1369 5213
578 or return undef; # not a primitive A,B,C
579  
580 1369 ▶ unless ($p >= 2 && $q >= 1) { # must be P > Q >= 1
Branch: 1 branch
BranchTrueFalseTotal
unless ($p >= 2 and $q >= 1) 328 1041 1369
Condition100%3/3
$p >= 2 and $q >= 1
!ll&&!rl&&r
1841441041
MC/DC100%2/2
$p >= 2 and $q >= 1
$p >= 2 $q >= 1
Truth table100%3/3
A and B
A= $p >= 2B= $q >= 1
ABresult
0X0
100
111
581 328 return undef;
582 }
583 1041 ▶ if (is_infinite($p)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($p)) 0 1041 1041
584 0 return $p; # infinity
585 }
586 1041 ▶ if (is_infinite($q)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($q)) 0 1041 1041
587 0 return $q; # infinity
588 }
589 1041 ▶ if ($p%2 == $q%2) { # must be opposite parity, not same parity
Branch: 1 branch
BranchTrueFalseTotal
if ($p % 2 == $q % 2) 480 561 1041
590 480 return undef;
591 }
592  
593 561 my @ndigits; # low to high
594 561 ▶ if ($self->{'tree_type'} eq 'FB') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'tree_type'} eq 'FB') { } 276 285 561
elsif ($self->{'tree_type'} eq 'UMT') { } 0 285 285
595 276 for (;;) {
596 885 ▶ unless ($p > $q && $q >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p > $q and $q >= 1) 114 771 885
Condition100%3/3
$p > $q and $q >= 1
!ll&&!rl&&r
9024771
MC/DC100%2/2
$p > $q and $q >= 1
$p > $q $q >= 1
Truth table100%3/3
A and B
A= $p > $qB= $q >= 1
ABresult
0X0
100
111
597 114 return undef;
598 }
599 771 ▶ last if $q <= 1 && $p <= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $q <= 1 and $p <= 2 162 609 771
Condition100%3/3
$q <= 1 and $p <= 2
!ll&&!rl&&r
487122162
MC/DC100%2/2
$q <= 1 and $p <= 2
$q <= 1 $p <= 2
Truth table100%3/3
A and B
A= $q <= 1B= $p <= 2
ABresult
0X0
100
111
600  
601 609 ▶ if ($q % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q % 2) { } 323 286 609
602 ### q odd, p even, digit 1 or 2 ...
603 323 $p /= 2;
604 323 ▶ if ($q > $p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q > $p) { } 119 204 323
605 ### digit 2, M3 ...
606 119 push @ndigits, 2;
607 119 $q -= $p; # opp parity of p, and < new p
608 } else {
609 ### digit 1, M2 ...
610 204 push @ndigits, 1;
611 204 $q = $p - $q; # opp parity of p, and < p
612 }
613 } else {
614 ### q even, p odd, digit 0, M1 ...
615 286 push @ndigits, 0;
616 286 $q /= 2;
617 286 $p -= $q; # opp parity of q
618 }
619 ### descend: "$q / $p"
620 }
621  
622 } elsif ($self->{'tree_type'} eq 'UMT') {
623 0 for (;;) {
624 ### at: "p=$p q=$q"
625 0 my $qmod2 = $q % 2;
626 0 ▶ unless ($p > $q && $q >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p > $q and $q >= 1) 0 0 0
Condition0%0/3
$p > $q and $q >= 1
!ll&&!rl&&r
000
MC/DC0%0/2
$p > $q and $q >= 1
$p > $q $q >= 1
Truth table0%0/3
A and B
A= $p > $qB= $q >= 1
ABresult
0X0
100
111
627 0 return undef;
628 }
629 0 ▶ last if $q <= 1 && $p <= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $q <= 1 and $p <= 2 0 0 0
Condition0%0/3
$q <= 1 and $p <= 2
!ll&&!rl&&r
000
MC/DC0%0/2
$q <= 1 and $p <= 2
$q <= 1 $p <= 2
Truth table0%0/3
A and B
A= $q <= 1B= $p <= 2
ABresult
0X0
100
111
630  
631 0 ▶ if ($p < 2*$q) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p < 2 * $q) { } 0 0 0
elsif ($qmod2) { } 0 0 0
632 0 ($p,$q) = ($q, 2*$q-$p); # U
633 0 push @ndigits, 0;
634 } elsif ($qmod2) {
635 0 $p /= 2; # M2
636 0 $q = $p - $q;
637 0 push @ndigits, 1;
638 } else {
639 0 $q /= 2; # T
640 0 $p -= 3*$q;
641 0 push @ndigits, 2;
642 }
643 }
644  
645 } else {
646 ### UAD or UArD ...
647 ### assert: $self->{'tree_type'} eq 'UAD' || $self->{'tree_type'} eq 'UArD'
648 285 for (;;) {
649 ### $p
650 ### $q
651 1065 ▶ if ($q <= 0 || $p <= 0 || $p <= $q) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q <= 0 or $p <= 0 or $p <= $q) 90 946 1036
Condition83%5/6
$q <= 0 or $p <= 0 or $p <= $q
l!l&&r!l&&!r
2990946
$q <= 0 or $p <= 0
l!l&&r!l&&!r
2901036
MC/DC66%2/3
$q <= 0 or $p <= 0 or $p <= $q
$q <= 0 $p <= 0 $p <= $q
Truth table75%3/4
A or B or C
A= $q <= 0B= $p <= 0C= $p <= $q
ABCresult
1XX1
01X1
0011
0000
652 119 return undef;
653 }
654 946 ▶ last if $q <= 1 && $p <= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $q <= 1 and $p <= 2 166 780 946
Condition100%3/3
$q <= 1 and $p <= 2
!ll&&!rl&&r
643137166
MC/DC100%2/2
$q <= 1 and $p <= 2
$q <= 1 $p <= 2
Truth table100%3/3
A and B
A= $q <= 1B= $p <= 2
ABresult
0X0
100
111
655  
656 780 ▶ if ($p > 2*$q) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p > 2 * $q) { } 317 463 780
657 317 ▶ if ($p > 3*$q) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p > 3 * $q) { } 230 87 317
658 ### digit 2 ...
659 230 push @ndigits, 2;
660 230 $p -= 2*$q;
661 } else {
662 ### digit 1
663 87 push @ndigits, 1;
664 87 ($p,$q) = ($q, $p - 2*$q);
665 }
666  
667 } else {
668 ### digit 0 ...
669 463 push @ndigits, 0;
670 463 ($p,$q) = ($q, 2*$q-$p);
671 }
672 ### descend: "$q / $p"
673 }
674 }
675 ### @ndigits
676  
677 328 ▶ if ($self->{'digit_order'} eq 'LtoH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"digit_order"} eq "LtoH") 0 328 328
678 0 @ndigits = reverse @ndigits;
679 ### unreverse: @ndigits
680 }
681 328 ▶ if ($self->{'tree_type'} eq 'UArD') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tree_type"} eq "UArD") 0 328 328
682 0 Math::PlanePath::GrayCode::_digits_from_gray_reflected(\@ndigits,3);
683 ### ungray: @ndigits
684 }
685  
686 328 my $zero = $x*0*$y;
687 ### offset: digit_join_lowtohigh(\@ndigits,3,$zero)
688 ### depth: scalar(@ndigits)
689 ### Nrow: $self->tree_depth_to_n($zero + scalar(@ndigits))
690  
691 328 return ($self->tree_depth_to_n($zero + scalar(@ndigits))
692 + digit_join_lowtohigh(\@ndigits,3,$zero)); # offset into row
693 }
694  
695 # numprims(H) = how many with hypot < H
696 # limit H->inf numprims(H) / H -> 1/2pi
697 #
698 # not exact
699 sub rect_to_n_range {
700 64 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=16 SCAR=32.2
Pod documented
701 ### PythagoreanTree rect_to_n_range(): "$x1,$y1 $x2,$y2"
702  
703 64 $x1 = round_nearest ($x1);
704 64 $y1 = round_nearest ($y1);
705 64 $x2 = round_nearest ($x2);
706 64 $y2 = round_nearest ($y2);
707  
708 64 my $zero = ($x1 * 0 * $y1 * $x2 * $y2); # inherit bignum
709  
710 64 ▶ ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
Branch: 1 branch
BranchTrueFalseTotal
if $x1 > $x2 0 64 64
711 64 ▶ ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
Branch: 1 branch
BranchTrueFalseTotal
if $y1 > $y2 0 64 64
712 ### x2: "$x2"
713 ### y2: "$y2"
714  
715 64 ▶ if ($self->{'coordinates'} eq 'BA') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"coordinates"} eq "BA") 0 64 64
716 0 ($x2,$y2) = ($y2,$x2);
717 }
718 64 ▶ if ($self->{'coordinates'} eq 'SM') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"coordinates"} eq "SM") 0 64 64
719 0 ▶ if ($x2 > $y2) { # both max
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 > $y2) { } 0 0 0
720 0 $y2 = $x2;
721 } else {
722 0 $x2 = $y2;
723 }
724 }
725  
726 64 ▶ if ($self->{'coordinates'} eq 'PQ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'coordinates'} eq 'PQ') { } 28 36 64
727 28 ▶ if ($x2 < 2 || $y2 < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < 2 or $y2 < 1) 0 28 28
Condition33%1/3
$x2 < 2 or $y2 < 1
l!l&&r!l&&!r
0028
MC/DC0%0/2
$x2 < 2 or $y2 < 1
$x2 < 2 $y2 < 1
Truth table33%1/3
A or B
A= $x2 < 2B= $y2 < 1
ABresult
1X1
011
000
728 0 return (1,0);
729 }
730 # P > Q so reduce y2 to at most x2-1
731 28 ▶ if ($y2 >= $x2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 >= $x2) 0 28 28
732 0 $y2 = $x2-1; # $y2 = min ($y2, $x2-1);
733 }
734  
735 28 ▶ if ($y2 < $y1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 < $y1) 0 28 28
736 ### PQ y range all above X=Y diagonal ...
737 0 return (1,0);
738 }
739 } else {
740 # AB,AC,BC, SM,SC,MC
741 36 ▶ if ($x2 < 3 || $y2 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < 3 or $y2 < 0) 0 36 36
Condition33%1/3
$x2 < 3 or $y2 < 0
l!l&&r!l&&!r
0036
MC/DC0%0/2
$x2 < 3 or $y2 < 0
$x2 < 3 $y2 < 0
Truth table33%1/3
A or B
A= $x2 < 3B= $y2 < 0
ABresult
1X1
011
000
742 0 return (1,0);
743 }
744 }
745  
746 64 my $depth;
747 64 ▶ if ($self->{'tree_type'} eq 'FB') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'tree_type'} eq 'FB') { } 30 34 64
748 ### FB ...
749 30 ▶ if ($self->{'coordinates'} eq 'PQ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"coordinates"} eq "PQ") 14 16 30
750 14 $x2 *= 3;
751 }
752 30 my ($pow, $exp) = round_down_pow ($x2, 2);
753 30 $depth = 2*$exp;
754 } else {
755 ### UAD or UArD, and UMT ...
756 34 ▶ if ($self->{'coordinates'} eq 'PQ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'coordinates'} eq 'PQ') { } 14 20 34
757 ### PQ ...
758 # P=k+1,Q=k diagonal N=100..000 first of row is depth=P-2
759 # anything else in that X=P column is smaller depth
760 14 $depth = $x2 - 2;
761 } else {
762 20 my $xdepth = int (($x2+1) / 2);
763 20 my $ydepth = int (($y2+31) / 4);
764 20 $depth = min($xdepth,$ydepth);
765 }
766 }
767 ### depth: "$depth"
768 64 return (1, $self->tree_depth_to_n_end($zero+$depth));
769 }
770  
771 #------------------------------------------------------------------------------
772 2 ▶use constant tree_num_roots => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
773  
774 sub tree_n_children {
775 7 ▶ my ($self, $n) = @_;
Subroutine tree_n_children: called CC=2 SCAR=7.4
Pod documented
776 7 ▶ unless ($n >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n >= 1) 0 7 7
777 0 return;
778 }
779 7 $n *= 3;
780 7 return ($n-1, $n, $n+1);
781 }
782 sub tree_n_num_children {
783 0 ▶ my ($self, $n) = @_;
Subroutine tree_n_num_children: not called CC=2 SCAR=17.9
Pod documented
784 0 ▶ return ($n >= 1 ? 3 : undef);
Branch: 1 branch
BranchTrueFalseTotal
$n >= 1 ? : 0 0 0
785 }
786 sub tree_n_parent {
787 13 ▶ my ($self, $n) = @_;
Subroutine tree_n_parent: called CC=2 SCAR=6.9
Pod documented
788 13 ▶ unless ($n >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n >= 2) 1 12 13
789 1 return undef;
790 }
791 12 return int(($n+1)/3);
792 }
793 sub tree_n_to_depth {
794 0 ▶ my ($self, $n) = @_;
Subroutine tree_n_to_depth: not called CC=2 SCAR=17.9
Pod documented
795 ### PythagoreanTree tree_n_to_depth(): $n
796 0 ▶ unless ($n >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n >= 1) 0 0 0
797 0 return undef;
798 }
799 0 my ($pow, $depth) = round_down_pow (2*$n-1, 3);
800 0 return $depth;
801 }
802  
803 sub tree_depth_to_n {
804 328 ▶ my ($self, $depth) = @_;
Subroutine tree_depth_to_n: called CC=2 SCAR=7.2
Pod documented
805 328 ▶ return ($depth >= 0
Branch: 1 branch
BranchTrueFalseTotal
$depth >= 0 ? : 328 0 328
806 ? (3**$depth + 1)/2
807 : undef);
808 }
809 # (3^(d+1)+1)/2-1 = (3^(d+1)-1)/2
810 sub tree_depth_to_n_end {
811 64 ▶ my ($self, $depth) = @_;
Subroutine tree_depth_to_n_end: called CC=2 SCAR=7.2
Pod documented
812 64 ▶ return ($depth >= 0
Branch: 1 branch
BranchTrueFalseTotal
$depth >= 0 ? : 64 0 64
813 ? (3**($depth+1) - 1)/2
814 : undef);
815 }
816 sub tree_depth_to_n_range {
817 0 ▶ my ($self, $depth) = @_;
Subroutine tree_depth_to_n_range: not called CC=2 SCAR=17.9
Pod documented
818 0 ▶ if ($depth >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth >= 0) { } 0 0 0
819 0 my $n_lo = (3**$depth + 1) / 2; # same as tree_depth_to_n()
820 0 return ($n_lo, 3*$n_lo-2);
821 } else {
822 0 return;
823 }
824 }
825 sub tree_depth_to_width {
826 0 ▶ my ($self, $depth) = @_;
Subroutine tree_depth_to_width: not called CC=2 SCAR=17.9
Pod documented
827 0 ▶ return ($depth >= 0
Branch: 1 branch
BranchTrueFalseTotal
$depth >= 0 ? : 0 0 0
828 ? 3**$depth
829 : undef);
830 }
831  
832 #------------------------------------------------------------------------------
833  
834 # Maybe, or abc_to_pq() perhaps with two of three values.
835 #
836 # @EXPORT_OK = ('ab_to_pq','pq_to_ab');
837 #
838 # =item C<($p,$q) = Math::PlanePath::PythagoreanTree::ab_to_pq($a,$b)>
839 #
840 # Return the P,Q coordinates for C<$a,$b>. As described above this is
841 #
842 # P = sqrt((C+A)/2) where C=sqrt(A^2+B^2)
843 # Q = sqrt((C-A)/2)
844 #
845 # The returned P,Q are integers PE<gt>=0,QE<gt>=0, but the further
846 # conditions for the path (namely PE<gt>QE<gt>=1 and no common factor) are
847 # not enforced.
848 #
849 # If P,Q are not integers or if BE<lt>0 then return an empty list. This
850 # ensures A,B is a Pythagorean triple, ie. that C=sqrt(A^2+B^2) is an
851 # integer, but it might not be a primitive triple and might not have A odd B
852 # even.
853 #
854 # =item C<($a,$b) = Math::PlanePath::PythagoreanTree::pq_to_ab($p,$q)>
855 #
856 # Return the A,B coordinates for C<$p,$q>. This is simply
857 #
858 # $a = $p*$p - $q*$q
859 # $b = 2*$p*$q
860 #
861 # This is intended for use with C<$p,$q> satisfying PE<gt>QE<gt>=1 and no
862 # common factor, but that's not enforced.
863  
864  
865 # a=p^2-q^2, b=2pq, c=p^2+q^2
866 # Done as a=(p-q)*(p+q) for one multiply instead of two squares, and to work
867 # close to a=UINT_MAX.
868 #
869 sub _pq_to_ab {
870 27 ▶ my ($p, $q) = @_;
Subroutine _pq_to_ab: called CC=1 SCAR=0.0
871 27 return (($p-$q)*($p+$q), 2*$p*$q);
872 }
873  
874 # C=(p-q)^2+B for one squaring instead of two.
875 # Also possible is C=(p+q)^2-B, but prefer "+B" so as not to round-off in
876 # floating point if (p+q)^2 overflows an integer.
877 sub _pq_to_bc {
878 1 ▶ my ($p, $q) = @_;
Subroutine _pq_to_bc: called CC=1 SCAR=0.0
879 1 my $b = 2*$p*$q;
880 1 $p -= $q;
881 1 return ($b, $p*$p+$b);
882 }
883  
884 # a=p^2-q^2, b=2pq, c=p^2+q^2
885 # Could a=(p-q)*(p+q) to avoid overflow if p^2 exceeds an integer as per
886 # _pq_to_ab(), but c overflows in that case anyway.
887 sub _pq_to_ac {
888 2 ▶ my ($p, $q) = @_;
Subroutine _pq_to_ac: called CC=1 SCAR=0.0
889 2 $p *= $p;
890 2 $q *= $q;
891 2 return ($p-$q, $p+$q);
892 }
893  
894 # a=p^2-q^2, b=2pq, c=p^2+q^2
895 # a<b
896 # p^2-q^2 < 2pq
897 # p^2 + 2pq - q^2 < 0
898 # (p+q)^2 - 2*q^2 < 0
899 # (p+q + sqrt(2)*q)*(p+q - sqrt(2)*q) < 0
900 # (p+q - sqrt(2)*q) < 0
901 # p + (1-sqrt(2))*q < 0
902 # p < (sqrt(2)-1)*q
903 #
904 sub _pq_to_sc {
905 0 ▶ my ($p, $q) = @_;
Subroutine _pq_to_sc: not called CC=1 SCAR=6.9
906 0 my $b = 2*$p*$q;
907 0 my $p_plus_q = $p + $q;
908 0 $p -= $q;
909 0 return (min($p_plus_q*$p, $b), # A = P^2-Q^2 = (P+Q)*(P-Q)
910 $p*$p+$b); # C = P^2+Q^2 = (P-Q)^2 + 2*P*Q
911 }
912 sub _pq_to_mc {
913 0 ▶ my ($p, $q) = @_;
Subroutine _pq_to_mc: not called CC=1 SCAR=6.9
914 0 my $b = 2*$p*$q;
915 0 my $p_plus_q = $p + $q;
916 0 $p -= $q;
917 0 return (max($p_plus_q*$p, $b), # A = P^2-Q^2 = (P+Q)*(P-Q)
918 $p*$p+$b); # C = P^2+Q^2 = (P-Q)^2 + 2*P*Q
919 }
920 sub _pq_to_sm {
921 0 ▶ my ($p, $q) = @_;
Subroutine _pq_to_sm: not called CC=2 SCAR=17.9
922 0 my ($a, $b) = _pq_to_ab($p,$q);
923 0 ▶ return ($a < $b ? ($a, $b) : ($b, $a));
Branch: 1 branch
BranchTrueFalseTotal
$a < $b ? : 0 0 0
924 }
925  
926 # u = p+q, v=p-q
927 # at given p, vertical q
928 # u=p,v=p on diagonal then p+q,p-q is diagonal down
929 # so mirror p axis to x=y diagonal and measure down diagonal from there
930 sub _pq_to_uv {
931 0 ▶ my ($p, $q) = @_;
Subroutine _pq_to_uv: not called CC=1 SCAR=6.9
932 0 return ($p+$q, $p-$q);
933 }
934  
935 # r = b+c = 2pq+p^2+q^2 = (p+q)^2
936 # s = c-a = p^2+q^2 - (p^2-q^2) = 2*q^2
937 sub _pq_to_rs {
938 0 ▶ my ($p, $q) = @_;
Subroutine _pq_to_rs: not called CC=1 SCAR=6.9
939 0 return (($p+$q)**2, 2*$q*$q);
940 }
941  
942 # s = c-a = p^2+q^2 - (p^2-q^2) = 2*q^2
943 # t = a+b-c = p^2-q^2 + 2pq - (p^2+q^2) = 2pq-2q^2 = 2(p-q)q
944 sub _pq_to_st {
945 0 ▶ my ($p, $q) = @_;
Subroutine _pq_to_st: not called CC=1 SCAR=6.9
946 0 my $q2 = 2*$q;
947 0 return ($q2*$q, ($p-$q)*$q2);
948 }
949  
950 #------------------------------------------------------------------------------
951  
952 # a = p^2 - q^2
953 # b = 2pq
954 # c = p^2 + q^2
955 #
956 # q = b/2p
957 # a = p^2 - (b/2p)^2
958 # = p^2 - b^2/4p^2
959 # 4ap^2 = 4p^4 - b^2
960 # 4(p^2)^2 - 4a*p^2 - b^2 = 0
961 # p^2 = [ 4a +/- sqrt(16a^2 + 16*b^2) ] / 2*4
962 # = [ a +/- sqrt(a^2 + b^2) ] / 2
963 # = (a +/- c) / 2 where c=sqrt(a^2+b^2)
964 # p = sqrt((a+c)/2) since c>a
965 #
966 # a = (a+c)/2 - q^2
967 # q^2 = (a+c)/2 - a
968 # = (c-a)/2
969 # q = sqrt((c-a)/2)
970 #
971 # if c^2 = a^2+b^2 is a perfect square then a,b,c is a pythagorean triple
972 # p^2 = (a+c)/2
973 # = (a + sqrt(a^2+b^2))/2
974 # 2p^2 = a + sqrt(a^2+b^2)
975 #
976 # p>q so a>0
977 # a+c even is a odd, c odd or a even, c even
978 # if a odd then c=a^2+b^2 is opp of b parity, must have b even to make c+a even
979 # if a even then c=a^2+b^2 is same as b parity, must have b even to c+a even
980 #
981 # a=6,b=8 is c=sqrt(6^2+8^2)=10
982 # a=0,b=4 is c=sqrt(0+4^4)=4 p^2=(a+c)/2 = 2 not a square
983 # a+c even, then (a+c)/2 == 0,1 mod 4 so a+c==0,2 mod 4
984 #
985 sub _ab_to_pq {
986 5009 ▶ my ($a, $b) = @_;
Subroutine _ab_to_pq: called CC=5 SCAR=16.1
987 ### _ab_to_pq(): "A=$a, B=$b"
988  
989 5009 ▶ unless ($b >= 4 && ($a%2) && !($b%2)) { # A odd, B even
Branch: 1 branch
BranchTrueFalseTotal
unless ($b >= 4 and $a % 2 and not $b % 2) 3059 1078 4137
Condition100%6/6
$b >= 4 and $a % 2 and not $b % 2
!ll&&!rl&&r
29479841078
$b >= 4 and $a % 2
!ll&&!rl&&r
87220752062
MC/DC100%3/3
$b >= 4 and $a % 2 and not $b % 2
$b >= 4 $a % 2 not $b % 2
Truth table100%4/4
A and B and not(C)
A= $b >= 4B= $a % 2C= not $b % 2
ABCresult
0XX0
10X0
1100
1111
990 3931 return;
991 }
992  
993 # This used to be $c=hypot($a,$b) and check $c==int($c), but libm hypot()
994 # on Darwin 8.11.0 is somehow a couple of bits off being an integer, for
995 # example hypot(57,176)==185 but a couple of bits out so $c!=int($c).
996 # Would have thought hypot() ought to be exact on integer inputs and a
997 # perfect square sum :-(. Check for a perfect square by multiplying back
998 # instead.
999 #
1000 # The condition is "$csquared != $c*$c" with operands that way around
1001 # since the other way is bad for Math::BigInt::Lite 0.14.
1002 #
1003 1078 my $c;
1004 {
1005 1078 my $csquared = $a*$a + $b*$b;
1006 1078 $c = _sqrtint($csquared);
1007 ### $csquared
1008 ### $c
1009 # since A odd and B even should have C odd, but floating point rounding
1010 # might prevent that
1011 1078 ▶ unless ($csquared == $c*$c) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($csquared == $c * $c) 1010 68 1078
1012 ### A^2+B^2 not a perfect square ...
1013 1010 return;
1014 }
1015 }
1016 68 return _ac_to_pq($a,$c);
1017 }
1018  
1019 sub _bc_to_pqa {
1020 1290 ▶ my ($b, $c) = @_;
Subroutine _bc_to_pqa: called CC=8 SCAR=20.8
1021 ### _bc_to_pqa(): "B=$b C=$c"
1022  
1023 1290 ▶ unless ($c > $b && $b >= 4 && !($b%2) && ($c%2)) { # B even, C odd
Branch: 1 branch
BranchTrueFalseTotal
unless ($c > $b and $b >= 4 and not $b % 2 and $c % 2) 200 74 274
Condition100%9/9
$c > $b and $b >= 4 and not $b % 2 and $c % 2
!ll&&!rl&&r
11447274
$c > $b and $b >= 4 and not $b % 2
!ll&&!rl&&r
1016128146
$c > $b and $b >= 4
!ll&&!rl&&r
782234274
MC/DC100%4/4
$c > $b and $b >= 4 and not $b % 2 and $c % 2
$c > $b $b >= 4 not $b % 2 $c % 2
Truth table100%5/5
A and B and not(C) and D
A= $c > $bB= $b >= 4C= not $b % 2D= $c % 2
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1024 1216 return;
1025 }
1026  
1027 74 my $a;
1028 {
1029 74 my $asquared = $c*$c - $b*$b;
1030 74 ▶ unless ($asquared > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($asquared > 0) 0 74 74
1031 0 return;
1032 }
1033 74 $a = _sqrtint($asquared);
1034 ### $asquared
1035 ### $a
1036 74 ▶ unless ($asquared == $a*$a) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($asquared == $a * $a) 64 10 74
1037 64 return;
1038 }
1039 }
1040  
1041 # If $c is near DBL_MAX can have $a overflow to infinity, leaving A>C.
1042 # _ac_to_pq() will detect that.
1043 10 ▶ my ($p,$q) = _ac_to_pq($a,$c) or return;
Branch: 1 branch
BranchTrueFalseTotal
() = _ac_to_pq($a, $c) or return 2 8 10
1044 8 return ($p,$q,$a);
1045 }
1046  
1047 sub _ac_to_pq {
1048 1369 ▶ my ($a, $c) = @_;
Subroutine _ac_to_pq: called CC=7 SCAR=19.5
1049 ### _ac_to_pq(): "A=$a C=$c"
1050  
1051 1369 ▶ unless ($c > $a && $a >= 3 && ($a%2) && ($c%2)) { # A odd, C odd
Branch: 1 branch
BranchTrueFalseTotal
unless ($c > $a and $a >= 3 and $a % 2 and $c % 2) 234 145 379
Condition100%9/9
$c > $a and $a >= 3 and $a % 2 and $c % 2
!ll&&!rl&&r
113490145
$c > $a and $a >= 3 and $a % 2
!ll&&!rl&&r
990144235
$c > $a and $a >= 3
!ll&&!rl&&r
782208379
MC/DC100%4/4
$c > $a and $a >= 3 and $a % 2 and $c % 2
$c > $a $a >= 3 $a % 2 $c % 2
Truth table100%5/5
A and B and C and D
A= $c > $aB= $a >= 3C= $a % 2D= $c % 2
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1052 1224 return;
1053 }
1054 145 $a = ($a-1)/2;
1055 145 $c = ($c-1)/2;
1056 ### halved to: "a=$a c=$c"
1057  
1058 145 my $p;
1059 {
1060 # If a,b,c is a triple but not primitive then can have psquared not an
1061 # integer. Eg. a=9,b=12 has c=15 giving psquared=(9+15)/2=12 is not a
1062 # perfect square. So notice that here.
1063 #
1064 145 my $psquared = $c+$a+1;
1065 145 $p = _sqrtint($psquared);
1066 ### $psquared
1067 ### $p
1068 145 ▶ unless ($psquared == $p*$p) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($psquared == $p * $p) 72 73 145
1069 ### P^2=A+C not a perfect square ...
1070 72 return;
1071 }
1072 }
1073  
1074 73 my $q;
1075 {
1076 # If a,b,c is a triple but not primitive then can have qsquared not an
1077 # integer. Eg. a=15,b=36 has c=39 giving qsquared=(39-15)/2=12 is not a
1078 # perfect square. So notice that here.
1079 #
1080 73 my $qsquared = $c-$a;
1081 73 $q = _sqrtint($qsquared);
1082 ### $qsquared
1083 ### $q
1084 73 ▶ unless ($qsquared == $q*$q) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($qsquared == $q * $q) 6 67 73
1085 6 return;
1086 }
1087 }
1088  
1089 # Might have a common factor between P,Q here. Eg.
1090 # A=27 = 3*3*3, B=36 = 4*3*3
1091 # A=45 = 3*3*5, B=108 = 4*3*3*3
1092 # A=63, B=216
1093 # A=75 =3*5*5 B=100 = 4*5*5
1094 # A=81, B=360
1095 #
1096 67 return ($p, $q);
1097 }
1098  
1099 sub _sm_to_pq {
1100 0 ▶ my ($s, $m) = @_;
Subroutine _sm_to_pq: not called CC=3 SCAR=24.8
1101 0 ▶ unless ($s < $m) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s < $m) 0 0 0
1102 0 return;
1103 }
1104 0 ▶ return _ab_to_pq($s % 2
Branch: 1 branch
BranchTrueFalseTotal
$s % 2 ? : 0 0 0
1105 ? ($s,$m) # s odd is A
1106 : ($m,$s)); # s even is B
1107 }
1108  
1109  
1110 # s^2+m^2=c^2
1111 # if s odd then a=s
1112 # ac_to_pq
1113 # b = 2pq check isn't smaller than s
1114 #
1115 # p^2=(c+a)/2
1116 # q^2=(c-a)/2
1117  
1118 sub _sc_to_pq {
1119 2 ▶ my ($s, $c) = @_;
Subroutine _sc_to_pq: called CC=6 SCAR=18.8
1120 2 my ($p,$q);
1121 2 ▶ if ($s % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s % 2) { } 1 1 2
1122 1 ▶ ($p,$q) = _ac_to_pq($s,$c) # s odd is A
Branch: 1 branch
BranchTrueFalseTotal
() = _ac_to_pq($s, $c) or return 0 1 1
1123 or return;
1124 1 ▶ if ($s > 2*$p*$q) { return; } # if s>B then s is not the smaller one
Branch: 1 branch
BranchTrueFalseTotal
if ($s > 2 * $p * $q) 0 1 1
1125 } else {
1126 1 ▶ ($p,$q,$a) = _bc_to_pqa($s,$c) # s even is B
Branch: 1 branch
BranchTrueFalseTotal
() = _bc_to_pqa($s, $c) or return 0 1 1
1127 or return;
1128 1 ▶ if ($s > $a) { return; } # if s>A then s is not the smaller one
Branch: 1 branch
BranchTrueFalseTotal
if ($s > $a) 1 0 1
1129 }
1130 1 return ($p,$q);
1131 }
1132  
1133 sub _mc_to_pq {
1134 0 ▶ my ($m, $c) = @_;
Subroutine _mc_to_pq: not called CC=6 SCAR=37.4
1135 ### _mc_to_pq() ...
1136 0 my ($p,$q);
1137 0 ▶ if ($m % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m % 2) { } 0 0 0
1138 ### m odd is A ...
1139 0 ▶ ($p,$q) = _ac_to_pq($m,$c)
Branch: 1 branch
BranchTrueFalseTotal
() = _ac_to_pq($m, $c) or return 0 0 0
1140 or return;
1141 0 ▶ if ($m < 2*$p*$q) { return; } # if m<B then m is not the bigger one
Branch: 1 branch
BranchTrueFalseTotal
if ($m < 2 * $p * $q) 0 0 0
1142 } else {
1143 ### m even is B ...
1144 0 ▶ ($p,$q,$a) = _bc_to_pqa($m,$c)
Branch: 1 branch
BranchTrueFalseTotal
() = _bc_to_pqa($m, $c) or return 0 0 0
1145 or return;
1146 ### $a
1147 0 ▶ if ($m < $a) { return; } # if m<A then m is not the bigger one
Branch: 1 branch
BranchTrueFalseTotal
if ($m < $a) 0 0 0
1148 }
1149 0 return ($p,$q);
1150 }
1151  
1152 # u = p+q, v=p-q
1153 # u+v=2p p = (u+v)/2
1154 # u-v=2q q = (u-v)/2
1155 sub _uv_to_pq {
1156 0 ▶ my ($u, $v) = @_;
Subroutine _uv_to_pq: not called CC=1 SCAR=6.9
1157 0 return (($u+$v)/2, ($u-$v)/2);
1158 }
1159  
1160 # r = (p+q)^2
1161 # s = 2*q^2 so q = sqrt(r/2)
1162 sub _rs_to_pq {
1163 0 ▶ my ($r, $s) = @_;
Subroutine _rs_to_pq: not called CC=6 SCAR=37.4
1164  
1165 0 ▶ return if $s % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $s % 2 0 0 0
1166 0 $s /= 2;
1167 0 ▶ return unless $s >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $s >= 1 0 0 0
1168 0 my $q = _sqrtint($s);
1169 0 ▶ return unless $q*$q == $s;
Branch: 1 branch
BranchTrueFalseTotal
unless $q * $q == $s 0 0 0
1170  
1171 0 ▶ return unless $r >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $r >= 1 0 0 0
1172 0 my $p_plus_q = _sqrtint($r);
1173 0 ▶ return unless $p_plus_q*$p_plus_q == $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $p_plus_q * $p_plus_q == $r 0 0 0
1174  
1175 0 return ($p_plus_q - $q, $q);
1176 }
1177  
1178 # s = 2*q^2
1179 # t = a+b-c = p^2-q^2 + 2pq - (p^2+q^2) = 2pq-2q^2 = 2(p-q)q
1180 #
1181 # p=2,q=1 s=2 t=2.1.1=2
1182 #
1183 sub _st_to_pq {
1184 0 ▶ my ($s, $t) = @_;
Subroutine _st_to_pq: not called CC=6 SCAR=37.4
1185  
1186 ### _st_to_pq(): "$s, $t"
1187 0 ▶ return if $s % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $s % 2 0 0 0
1188 0 $s /= 2;
1189 0 ▶ return unless $s >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $s >= 1 0 0 0
1190 0 my $q = _sqrtint($s);
1191 ### $q
1192 0 ▶ return unless $q*$q == $s;
Branch: 1 branch
BranchTrueFalseTotal
unless $q * $q == $s 0 0 0
1193  
1194 0 ▶ return if $t % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $t % 2 0 0 0
1195 0 $t /= 2;
1196 ### rem: $t % $q
1197 0 ▶ return if $t % $q;
Branch: 1 branch
BranchTrueFalseTotal
if $t % $q 0 0 0
1198 0 $t /= $q; # p-q
1199  
1200 ### pq: ($t+$q).", $q"
1201  
1202 0 return ($t+$q, $q);
1203 }
1204  
1205 2 1;
1206 __END__
« PlanePath/DekkingCentres.pm ↑ Summary NumSeq/PlanePathDelta.pm »