PlanePath/CfracDigits.pm

filter: Statementstmt 83.2% 129 / 155 Branchbran 60.0% 24 / 40 Conditioncond 20.8% 5 / 24 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 83.3% 25 / 30 Podpod 100% 7 / 7 totaltotal 70.9% 190 / 268 CC 34 SCAR 17.4
CC
34
Cov
72%
xy_is_visited
20.0
xy_to_n
10.7
rect_to_n_range
9.0
CRAP 5.7
« PlanePath/GosperReplicate.pm ↑ Summary PlanePath/DragonRounded.pm »
1 # Copyright 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 package Math::PlanePath::CfracDigits;
20 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 $VERSION = 128;
25 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 @ISA = ('Math::PlanePath');
27  
28 use Math::PlanePath::Base::Generic
29 1 'is_infinite',
30 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 use Math::PlanePath::Base::Digits
32 1 'round_down_pow',
33 'digit_split_lowtohigh',
34 1 ▶ 'digit_join_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 1 ▶use Math::PlanePath::RationalsTree;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 *_xy_to_quotients = \&Math::PlanePath::RationalsTree::_xy_to_quotients;
38  
39 1 ▶use Math::PlanePath::CoprimeColumns;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 *_coprime = \&Math::PlanePath::CoprimeColumns::_coprime;
41  
42 # uncomment this to run the ### lines
43 #use Smart::Comments;
44  
45  
46 1 use constant parameter_info_array =>
47 [ { name => 'radix',
48 share_key => 'radix2_min1',
49 display => 'Radix',
50 type => 'integer',
51 minimum => 1,
52 default => 2,
53 width => 3,
54 },
55 1 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 1 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use constant x_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use constant y_minimum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use constant diffxy_maximum => -1; # upper octant X<=Y-1 so X-Y<=-1
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use constant gcdxy_maximum => 1; # no common factor
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 # FIXME: believe this is right, but check N+1 always changes Y
66 sub absdy_minimum {
67 0 ▶ my ($self) = @_;
Subroutine absdy_minimum: not called CC=2 SCAR=17.9
Pod documented
68 0 ▶ return ($self->{'radix'} < 3 ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'radix'} < 3 ? : 0 0 0
69 }
70  
71 # radix=1 N=1 has dir4=0
72 # radix=2 N=5628 has dir4=0 dx=9,dy=0
73 # radix=3 N=1189140 has dir4=0 dx=1,dy=0
74 # radix=4 N=169405 has dir4=0 dx=2,dy=0
75 # always eventually 0 ?
76 # use constant dir_minimum_dxdy => (1,0); # the default
77  
78 # radix=1 N=4 dX=1,dY=-1 for dir4=3.5
79 # radix=2 N=4413 dX=9,dY=-1
80 # radix=3 N=9492 dX=3,dY=-1
81 # ENHANCE-ME: suspect believe approaches 360 degrees, eventually, but proof?
82 # use constant dir_maximum_dxdy => (0,0); # the default
83  
84 sub turn_any_straight {
85 0 ▶ my ($self) = @_;
Subroutine turn_any_straight: not called CC=1 SCAR=6.9
Pod documented
86 0 return ($self->{'radix'} != 1); # radix=1 never straight
87 }
88  
89 sub _UNDOCUMENTED__turn_any_left_at_n {
90 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__turn_any_left_at_n: not called CC=1 SCAR=6.9
91 0 return $self->{'radix'} + 1;
92 }
93 sub _UNDOCUMENTED__turn_any_right_at_n {
94 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__turn_any_right_at_n: not called CC=1 SCAR=6.9
95 0 return $self->{'radix'};
96 }
97  
98  
99 #------------------------------------------------------------------------------
100  
101 sub new {
102 3 ▶ my $self = shift->SUPER::new (@_);
Subroutine new: called CC=3 SCAR=12.0
Pod documented
103 3 ▶ unless ($self->{'radix'} && $self->{'radix'} >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"radix"} and $self->{"radix"} >= 1) 3 0 3
Condition33%1/3
$self->{"radix"} and $self->{"radix"} >= 1
!ll&&!rl&&r
300
MC/DC0%0/2
$self->{"radix"} and $self->{"radix"} >= 1
$self->{"radix"} $self->{"radix"} >= 1
Truth table33%1/3
A and B
A= $self->{"radix"}B= $self->{"radix"} >= 1
ABresult
0X0
100
111
104 3 $self->{'radix'} = 2;
105 }
106 3 return $self;
107 }
108  
109 sub n_to_xy {
110 1 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=5 SCAR=18.7
Pod documented
111 ### CfracDigits n_to_xy(): "$n"
112  
113 1 ▶ if ($n < 0) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 1 1
114 1 ▶ if (is_infinite($n)) { return ($n,$n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 1 1
115  
116 {
117 1 my $int = int($n);
118 1 ▶ if ($n != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $int) 0 1 1
119 ### frac ...
120 0 my $frac = $n - $int; # inherit possible BigFloat/BigRat
121 0 my ($x1,$y1) = $self->n_to_xy($int);
122 0 my ($x2,$y2) = $self->n_to_xy($int+1);
123 0 my $dx = $x2-$x1;
124 0 my $dy = $y2-$y1;
125 ### x1,y1: "$x1, $y1"
126 ### x2,y2: "$x2, $y2"
127 ### dx,dy: "$dx, $dy"
128 ### result: ($frac*$dx + $x1).', '.($frac*$dy + $y1)
129 0 return ($frac*$dx + $x1, $frac*$dy + $y1);
130 }
131 1 $n = $int;
132 }
133  
134 1 my $radix = $self->{'radix'};
135 1 my $zero = ($n * 0); # inherit bignum 0
136 1 my $x = $zero;
137 1 my $y = 1 + $zero; # inherit bignum 1
138  
139 1 foreach my $q (_n_to_quotients_bottomtotop($n,$radix,$zero)) { # bottom to top
140 ### at: "$x,$y q=$q"
141  
142 # 1/(q + X/Y) = 1/((qY+X)/Y)
143 # = Y/(qY+X)
144 4 ($x,$y) = ($y, $q*$y + $x);
145 }
146  
147 ### return: "$x,$y"
148 1 return ($x,$y);
149 }
150  
151 # Return a list of quotients bottom to top. The base3 digits of N are split
152 # by "3" delimiters and the parts adjusted so the first bottom-most q>=2 and
153 # the rest q>=1. The values are ready to be used as continued fraction
154 # terms.
155 #
156 sub _n_to_quotients_bottomtotop {
157 6 ▶ my ($n, $radix, $zero) = @_;
Subroutine _n_to_quotients_bottomtotop: called CC=3 SCAR=11.0
158 ### _n_to_quotients_bottomtotop(): $n
159  
160 6 my $radix_plus_1 = $radix + 1;
161 6 my @ret;
162 my @group;
163 6 foreach my $digit (_digit_split_1toR_lowtohigh($n,$radix_plus_1)) {
164 21 ▶ if ($digit == $radix_plus_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit == $radix_plus_1) { } 7 14 21
165 ### @group
166 7 push @ret, _digit_join_1toR_destructive(\@group, $radix, $zero) + 1;
167 7 @group = ();
168 } else {
169 14 push @group, $digit;
170 }
171 }
172 ### final group: @group
173 6 push @ret, _digit_join_1toR_destructive(\@group, $radix, $zero) + 1;
174  
175 6 $ret[0] += 1; # bottom-most is +2 rather than +1
176  
177 ### _n_to_quotients_bottomtotop result: @ret
178 6 return @ret;
179 }
180  
181 # Return a list of digits 1 <= d <= R which is $n written in $radix, low to
182 # high digits.
183 sub _digit_split_1toR_lowtohigh {
184 28 ▶ my ($n, $radix) = @_;
Subroutine _digit_split_1toR_lowtohigh: called CC=5 SCAR=16.2
185 ### assert: $radix >= 1
186 ### assert: $n >= 0
187  
188 28 ▶ if ($radix == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($radix == 1) 0 28 28
189 0 return (1) x $n;
190 }
191 28 my @digits = digit_split_lowtohigh($n,$radix);
192  
193 # mangle 0 -> R
194 28 my $borrow = 0;
195 28 foreach my $digit (@digits) { # low to high
196 61 ▶ if ($borrow = (($digit -= $borrow) <= 0)) { # modify array contents
Branch: 1 branch
BranchTrueFalseTotal
if ($borrow = ($digit -= $borrow) <= 0) 26 35 61
197 26 $digit += $radix;
198 }
199 }
200 28 ▶ if ($borrow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($borrow) 6 22 28
201 ### assert: $digits[-1] == $radix
202 6 pop @digits;
203 }
204  
205 28 return @digits;
206 }
207  
208 sub _digit_join_1toR_destructive {
209 18 ▶ my ($aref, $radix, $zero) = @_;
Subroutine _digit_join_1toR_destructive: called CC=5 SCAR=16.2
210 ### assert: $radix >= 1
211  
212 18 ▶ if ($radix == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($radix == 1) 0 18 18
213 0 return scalar(@$aref);
214 }
215  
216 # mangle any digit==$radix down to digit=0
217 18 my $carry = 0;
218 18 foreach my $digit (@$aref) { # low to high
219 34 ▶ if ($carry = (($digit += $carry) >= $radix)) { # modify array contents
Branch: 1 branch
BranchTrueFalseTotal
if ($carry = ($digit += $carry) >= $radix) 16 18 34
220 16 $digit -= $radix;
221 }
222 }
223 18 ▶ if ($carry) {
Branch: 1 branch
BranchTrueFalseTotal
if ($carry) 6 12 18
224 6 push @$aref, 1;
225 }
226  
227 ### _digit_join_1toR_destructive() result: digit_join_lowtohigh($aref, $radix, $zero)
228 18 return digit_join_lowtohigh($aref, $radix, $zero);
229 }
230  
231 sub xy_is_visited {
232 0 ▶ my ($self, $x, $y) = @_;
Subroutine xy_is_visited: not called CC=4 SCAR=30.0
Pod documented
233 0 $x = round_nearest ($x);
234 0 $y = round_nearest ($y);
235 0 ▶ return (! ($x < 1 || $y < 2 || $x >= $y)
Condition0%0/9
!($x < 1 || $y < 2 || $x >= $y) && _coprime($x, $y)
!ll&&!rl&&r
000
$x < 1 || $y < 2 || $x >= $y
l!l&&r!l&&!r
000
$x < 1 or $y < 2
l!l&&r!l&&!r
000
MC/DC0%0/4
!($x < 1 || $y < 2 || $x >= $y) && _coprime($x, $y)
$x < 1 $y < 2 $x >= $y _coprime($x, $y)
Truth table0%0/5
not(A or B || C) && D
A= $x < 1B= $y < 2C= $x >= $yD= _coprime($x, $y)
ABCDresult
1XXX0
01XX0
001X0
00000
00011
236 && _coprime($x,$y));
237 }
238  
239 sub xy_to_n {
240 1 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=7 SCAR=23.7
Pod documented
241 1 $x = round_nearest ($x);
242 1 $y = round_nearest ($y);
243 ### CfracDigits xy_to_n(): "$x,$y"
244  
245 1 ▶ if (is_infinite($x)) { return $x; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($x)) 0 1 1
246 1 ▶ if (is_infinite($y)) { return $y; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($y)) 0 1 1
247 1 ▶ if ($x < 1 || $y < 2 || $x >= $y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 1 or $y < 2 or $x >= $y) 0 1 1
Condition33%2/6
$x < 1 or $y < 2 or $x >= $y
l!l&&r!l&&!r
001
$x < 1 or $y < 2
l!l&&r!l&&!r
001
MC/DC0%0/3
$x < 1 or $y < 2 or $x >= $y
$x < 1 $y < 2 $x >= $y
Truth table25%1/4
A or B or C
A= $x < 1B= $y < 2C= $x >= $y
ABCresult
1XX1
01X1
0011
0000
248 0 return undef;
249 }
250  
251 1 ▶ my @quotients = _xy_to_quotients($x,$y)
Branch: 1 branch
BranchTrueFalseTotal
my(@quotients) = _xy_to_quotients($x, $y) or return undef 0 1 1
252 or return undef; # $x,$y have a common factor
253 ### @quotients
254  
255 # drop initial 0 integer part
256 ### assert: $quotients[0] == 0
257 1 shift @quotients;
258  
259 return _cfrac_join_toptobottom(\@quotients,
260 1 $self->{'radix'},
261 $x * 0 * $y); # inherit bignum 0
262 }
263  
264 # $aref is a list of continued fraction quotients from top-most to
265 # bottom-most. There's no initial integer term in $aref. Each quotient is
266 # q >= 1 except the bottom-most which q-1 and so also >=1.
267 #
268 sub _cfrac_join_toptobottom {
269 5 ▶ my ($aref, $radix, $zero) = @_;
Subroutine _cfrac_join_toptobottom: called CC=2 SCAR=6.9
270 ### _cfrac_join_toptobottom(): $aref
271  
272 5 my @digits;
273 5 foreach my $q (reverse @$aref) {
274 ### assert: $q >= 1
275 12 push @digits, _digit_split_1toR_lowtohigh($q-1, $radix), $radix+1;
276 }
277 5 pop @digits; # no high delimiter
278 ### groups digits 1toR: @digits
279 5 return _digit_join_1toR_destructive(\@digits, $radix+1, $zero);
280 }
281  
282  
283 # X/Y = F[k]/F[k+1] quotients all 1
284 # N = all delimiter digits R,R,...,R
285 # = 1222...2221
286 # = R^k + 2*(R^k+1)/(R-1) - 1
287 # = (RR^k - R^k + 2R^k + 2 - R + 1) / (R-1)
288 # = (RR^k + R^k - R + 3) / (R-1)
289 # = ((R+1)R^k - R + 3) / (R-1)
290 # take high as "12" = R+2
291 # k = log(Y)/log(phi)
292 # N = (R+2) * R ** k
293 # N = Y ** (log(R)/log(phi))
294 #
295 # not exact
296 sub rect_to_n_range {
297 2 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=7 SCAR=22.0
Pod documented
298 ### rect_to_n_range()
299  
300 2 $x1 = round_nearest ($x1);
301 2 $y1 = round_nearest ($y1);
302 2 $x2 = round_nearest ($x2);
303 2 $y2 = round_nearest ($y2);
304  
305 2 ▶ ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
Branch: 1 branch
BranchTrueFalseTotal
if $x1 > $x2 0 2 2
306 2 ▶ ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
Branch: 1 branch
BranchTrueFalseTotal
if $y1 > $y2 0 2 2
307 ### $x2
308 ### $y2
309  
310 # | /
311 # | / x1
312 # | / +-----y2
313 # | / |
314 # |/ +-----
315 #
316 2 ▶ if ($x2 < 1 || $y2 < 2 || $x1 >= $y2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < 1 or $y2 < 2 or $x1 >= $y2) 0 2 2
Condition33%2/6
$x2 < 1 or $y2 < 2 or $x1 >= $y2
l!l&&r!l&&!r
002
$x2 < 1 or $y2 < 2
l!l&&r!l&&!r
002
MC/DC0%0/3
$x2 < 1 or $y2 < 2 or $x1 >= $y2
$x2 < 1 $y2 < 2 $x1 >= $y2
Truth table25%1/4
A or B or C
A= $x2 < 1B= $y2 < 2C= $x1 >= $y2
ABCresult
1XX1
01X1
0011
0000
317 ### no values, rect outside upper octant ...
318 0 return (1,0);
319 }
320  
321 2 my $zero = ($x1 * 0 * $y1 * $x2 * $y2); # inherit bignum
322 2 my $radix = $self->{'radix'};
323  
324 2 ▶ return (0,
Branch: 1 branch
BranchTrueFalseTotal
$radix == 1 ? : 0 2 2
325 ($radix+3)
326 * ($radix+1 + $zero) ** ($radix == 1
327 ? $y2
328 : _log_phi_estimate($y2,$radix)));
329 }
330  
331 # Return an estimate of log base phi of $x, that being log($x)/log(phi),
332 # where phi=(1+sqrt(5))/2 the golden ratio.
333 #
334 sub _log_phi_estimate {
335 2 ▶ my ($x) = @_;
Subroutine _log_phi_estimate: called CC=1 SCAR=0.0
336 2 my ($pow,$exp) = round_down_pow ($x, 2);
337 2 return int ($exp * (log(2) / log((1+sqrt(5))/2)));
338 }
339  
340 1 1;
341 __END__
« PlanePath/GosperReplicate.pm ↑ Summary PlanePath/DragonRounded.pm »