PlanePath/CellularRule190.pm

filter: Statementstmt 92.1% 106 / 115 Branchbran 80.9% 34 / 42 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 95.6% 22 / 23 Podpod 100% 5 / 5 totaltotal 89.4% 170 / 190 CC 23 SCAR 18.2
CC
23
Cov
89%
rect_to_n_range
14.8
xy_to_n
9.0
n_to_xy
5.0
CRAP 6.2
« PlanePath/CellularRule54.pm ↑ Summary PlanePath/GosperReplicate.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 # http://mathworld.wolfram.com/ElementaryCellularAutomaton.html
20 #
21 # Loeschian numbers strips on the right ...
22  
23  
24 package Math::PlanePath::CellularRule190;
25 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 2 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 $VERSION = 128;
30 2 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 *_sqrtint = \&Math::PlanePath::_sqrtint;
32 2 @ISA = ('Math::PlanePath');
33  
34 use Math::PlanePath::Base::Generic
35 2 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 2 ▶use Math::PlanePath::CellularRule54;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 *_rect_for_V = \&Math::PlanePath::CellularRule54::_rect_for_V;
39  
40 # uncomment this to run the ### lines
41 # use Smart::Comments;
42  
43  
44 2 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 2 ▶use constant n_frac_discontinuity => .5;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 2 use constant parameter_info_array =>
48 [ { name => 'mirror',
49 display => 'Mirror',
50 type => 'boolean',
51 default => 0,
52 description => 'Mirror to "rule 246" instead.',
53 },
54 Math::PlanePath::Base::Generic::parameter_info_nstart1(),
55 2 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 sub x_negative_at_n {
58 0 ▶ my ($self) = @_;
Subroutine x_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
59 0 return $self->n_start + 1;
60 }
61 2 ▶use constant sumxy_minimum => 0; # triangular X>=-Y so X+Y>=0
Subroutine BEGIN: called CC=1 SCAR=0.0
62 2 ▶use constant diffxy_maximum => 0; # triangular X<=Y so X-Y<=0
Subroutine BEGIN: called CC=1 SCAR=0.0
63 2 ▶use constant dx_maximum => 2; # across gap
Subroutine BEGIN: called CC=1 SCAR=0.0
64 2 ▶use constant dy_minimum => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 2 ▶use constant dy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 2 ▶use constant absdx_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 2 ▶use constant dsumxy_maximum => 2; # straight East dX=+2
Subroutine BEGIN: called CC=1 SCAR=0.0
68 2 ▶use constant ddiffxy_maximum => 2; # straight East dX=+2
Subroutine BEGIN: called CC=1 SCAR=0.0
69 2 ▶use constant dir_maximum_dxdy => (-1,0); # supremum, West except dY=+1
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71  
72 #------------------------------------------------------------------------------
73  
74 sub new {
75 10 ▶ my $self = shift->SUPER::new (@_);
Subroutine new: called CC=2 SCAR=6.9
Pod documented
76 10 ▶ if (! defined $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"n_start"}) 7 3 10
77 7 $self->{'n_start'} = $self->default_n_start;
78 }
79 10 return $self;
80 }
81  
82 # 31 32 33 34 35 36 37 38 39 40
83 # 22 23 24 25 26 27 28 29 30
84 # 15 6 17 18 19 20 21
85 # 9 10 11 12 13 14
86 # 5 6 7 8
87 # 2 3 4
88 # 1
89 #
90 # even y = [ 0, 2, 4, 6 ]
91 # N = [ 1, 5, 15, 31 ]
92 # Neven = (3/4 y^2 + 1/2 y + 1)
93 # = (3y + 2)*y/4 + 1
94 # = ((3y + 2)*y + 4) /4
95 # = (3 (y/2)^2 + (y/2) + 1)
96 # = (3*(y/2) + 1)*(y/2) + 1
97 #
98 # odd y = [ 1, 3, 5,7 ]
99 # N = [ 2,9,22,41 ]
100 # Nodd = (3/4 y^2 + 1/2 y + 3/4)
101 # = ((3y+2)*y+ 3) / 4
102 #
103 # pair even d = [0,1,2,3]
104 # N = [ 1, 5, 15, 31 ]
105 # Npair = (3 d^2 + d + 1)
106 # d = -1/6 + sqrt(1/3 * $n + -11/36)
107 # = [ -1 + sqrt(1/3 * $n + -11/36)*6 ] / 6
108 # = [ -1 + sqrt(1/3 * $n*36 + -11/36*36) ] / 6
109 # = [ -1 + sqrt(12n-11) ] / 6
110 #
111 sub n_to_xy {
112 140 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=5 SCAR=16.2
Pod documented
113 ### CellularRule190 n_to_xy(): $n
114  
115 140 $n = $n - $self->{'n_start'} + 1; # to N=1 basis, and warn if $n undef
116 140 my $frac;
117 {
118 140 my $int = int($n);
119 140 $frac = $n - $int;
120 140 $n = $int; # BigFloat int() gives BigInt, use that
121 140 ▶ if (2*$frac >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (2 * $frac >= 1) 0 140 140
122 0 $frac -= 1;
123 0 $n += 1;
124 }
125 # now -0.5 <= $frac < 0.5
126 ### assert: 2*$frac >= -1 || $n!=$n || $n+1==$n
127 ### assert: 2*$frac < 1 || $n!=$n || $n+1==$n
128 }
129  
130 140 ▶ if ($n < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1) 6 134 140
131 6 return;
132 }
133  
134 # d is the two-row number, ie. d=2*y, where n belongs
135 # start of the two-row group is nbase = 3 d^2 + d + 1
136 #
137 134 my $d = int ((_sqrtint(12*$n-11) - 1) / 6);
138 134 $n -= ((3*$d + 1)*$d + 1); # remainder within two-row
139 ### $d
140 ### remainder: $n
141 134 ▶ if ($n <= 3*$d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= 3 * $d) { } 80 54 134
142 # 3d+1 many points in the Y=0,2,4,6 etc even row
143 80 $d *= 2; # y=2*d
144 80 ▶ return ($frac + $n + int(($n + ($self->{'mirror'} ? 2 : 0))/3) - $d,
Branch: 1 branch
BranchTrueFalseTotal
$self->{'mirror'} ? : 40 40 80
145 $d);
146 } else {
147 # 3*d many points in the Y=1,3,5,7 etc odd row, using 3 in 4 cells
148 54 $n -= 3*$d+1; # remainder 0 upwards into odd row
149 54 $d = 2*$d+1; # y=2*d+1
150 54 return ($frac + $n + int($n/3) - $d,
151 $d);
152 }
153 }
154  
155 sub xy_to_n {
156 1628 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=9 SCAR=22.0
Pod documented
157 1628 $x = round_nearest ($x);
158 1628 $y = round_nearest ($y);
159 ### CellularRule190 xy_to_n(): "$x,$y"
160  
161 1628 ▶ if ($y < 0 || $x > $y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < 0 or $x > $y) 240 1388 1628
Condition66%2/3
$y < 0 or $x > $y
l!l&&r!l&&!r
02401388
MC/DC50%1/2
$y < 0 or $x > $y
$y < 0 $x > $y
Truth table66%2/3
A or B
A= $y < 0B= $x > $y
ABresult
1X1
011
000
162 240 return undef;
163 }
164  
165 1388 $x += $y; # move to have x=0 the start of the row
166 1388 ▶ if ($x < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0) 240 1148 1388
167 240 return undef;
168 }
169  
170 ### x centred: $x
171 1148 ▶ if ($y % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y % 2) { } 505 643 1148
172 ### odd row, 3s from the start ...
173 505 ▶ if (($x % 4) == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x % 4 == 3) 56 449 505
174 56 return undef;
175 }
176 # 3y^2+2y-1 = (3y-1)*(y+1)
177 return ($x
178 - int($x/4)
179 + ((3*$y+2)*$y-1)/4
180 449 + $self->{'n_start'});
181 } else {
182 ### even row, 3s then 1 sep, or mirror 1 sep start ...
183 643 my $mirror = $self->{'mirror'};
184 643 ▶ if (($x % 4) == ($mirror ? 1 : 3)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x % 4 == ($mirror ? 1 : 3)) 56 587 643
$mirror ? : 231 412 643
185 56 return undef;
186 }
187 return ($x
188 - int(($x+($mirror ? 2 : 1))/4)
189 + (3*$y+2)*$y/4
190 587 ▶ + $self->{'n_start'});
Branch: 1 branch
BranchTrueFalseTotal
$mirror ? : 203 384 587
191 }
192 }
193  
194 # exact
195 sub rect_to_n_range {
196 222 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=10 SCAR=27.0
Pod documented
197 ### CellularRule190 rect_to_n_range(): "$x1,$y1, $x2,$y2"
198  
199 222 ▶ ($x1,$y1, $x2,$y2) = _rect_for_V ($x1,$y1, $x2,$y2)
Branch: 1 branch
BranchTrueFalseTotal
() = _rect_for_V($x1, $y1, $x2, $y2) or return 1, 0 0 222 222
200 or return (1,0); # rect outside pyramid
201  
202 # # inherit bignum (before collapsing some y1 to x1 etc)
203 # my $zero = ($x1 * 0 * $y1 * $x2 * $y2);
204  
205 222 my $mirror = $self->{'mirror'};
206 222 my $unincremented_x1 = $x1;
207  
208 # \+------+
209 # | | /
210 # |\ | /
211 # | \ | /
212 # | \ | /
213 # y1 +------+ /
214 # x1 \ /
215 # \/
216 #
217 222 ▶ if ($x1 < (my $neg_y1 = -$y1)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x1 < (my $neg_y1 = -$y1)) { } 11 211 222
elsif (($mirror ? $y1 - $x1 : $x1 + $y1) % 4 == 3) { } 0 211 211
218 ### bottom-left outside, move across to: "$neg_y1,$y1"
219 11 $x1 = $neg_y1;
220  
221 # For the following blank checks a blank doesn't occur at the ends of a
222 # row, so when on a blank it's always possible to increment or decrement
223 # X to go to a non-blank -- as long as that adjacent space is within the
224 # rectangle.
225 #
226 ▶ } elsif ((($mirror ? $y1-$x1 : $x1+$y1) % 4) == 3) {
Branch: 1 branch
BranchTrueFalseTotal
$mirror ? : 55 156 211
227 ### x1,y1 bottom left is on a blank: "x1+y1=".($x1+$y1)
228 0 ▶ if ($x1 < $x2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 < $x2) { } 0 0 0
229 ### rect width >= 2, so increase x1 ...
230 0 $x1 += 1;
231 } else {
232 ### rect is a single column width==1, increase y1 ...
233 0 ▶ if (($y1 += 1) > $y2) {
Branch: 1 branch
BranchTrueFalseTotal
if (($y1 += 1) > $y2) 0 0 0
234 ### rect was a single blank square, contains no N ...
235 0 return (1,0);
236 }
237 }
238 }
239  
240 222 ▶ if ((($mirror ? $y2-$x2 : $x2+$y2) % 4) == 3) {
Branch: 2 branches
BranchTrueFalseTotal
if (($mirror ? $y2 - $x2 : $x2 + $y2) % 4 == 3) 12 210 222
$mirror ? : 60 162 222
241 ### x2,y2 top right is on a blank, decrement ...
242 12 ▶ if ($x2 > $unincremented_x1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 > $unincremented_x1) { } 12 0 12
243 ### rect width >= 2, so decrease x2 ...
244 12 $x2 -= 1;
245 } else {
246 ### rect is a single column width==1, decrease y2 ...
247 0 $y2 -= 1;
248  
249 # Can decrement without checking whether the rect is a single square.
250 # If the rect was a single blank square then the x1+y1 bottom left
251 # above detects and returns. And the bottom left blank check
252 # incremented y1 to leave a single square then that's a non-blank
253 # because there's no vertical blank pairs (they go on the diagonal).
254 ### assert $y2 >= $y1
255 }
256 }
257  
258 # At this point $x1,$y1 is a non-blank bottom left corner, and $x2,$y2
259 # is a non-blank top right corner, being the N lo to hi range.
260  
261 ### range: "bottom-right $x1,$y1 top-left $x2,$y2"
262 222 return ($self->xy_to_n ($x1,$y1),
263 $self->xy_to_n ($x2,$y2));
264 }
265  
266  
267 # old rect_to_n_range() of row endpoints
268 #
269 # # even right y = [ 0, 2, 4, 6 ]
270 # # N = [ 1,8,21,40 ]
271 # # Nright = (3/4 y^2 + 2 y + 1)
272 # # = (3 y^2 + 8 y + 4) / 4
273 # # = ((3y + 8)y + 4) / 4
274 # #
275 # # odd right y = [ 1, 3, 5, 7 ]
276 # # N = [ 4,14,30, 52 ]
277 # # Nright = (3/4 y^2 + 2 y + 5/4)
278 # # = (3 y^2 + 8 y + 5) / 4
279 # # = ((3y + 8)y + 5) / 4
280 # #
281 # # Nleft y even ((3y+2)*y + 4)/4
282 # # Nleft y odd ((3y+2)*y + 3)/4
283 # # Nright even ((3(y+1)+2)*(y+1) + 3)/4 - 1
284 # # = ((3y+3+2)*(y+1) + 3 - 4)/4
285 # # = ((3y+5)*(y+1) - 1)/4
286 # # = ((3y^2 + 8y + 5 - 1)/4
287 # # = ((3y^2 + 8y + 4)/4
288 # # = ((3y+8)y + 4)/4
289 # # = ((3y+2)(y+2)/4
290 # #
291 # ### $y1
292 # ### $y2
293 # $y2 += $zero;
294 # $y1 += $zero;
295 # return (((3*$y1 + 2)*$y1 + 4 - ($y1%2)) / 4, # even/odd Nleft
296 # ((3*$y2 + 8)*$y2 + 4 + ($y2%2)) / 4); # even/odd Nright
297  
298 2 1;
299 __END__
« PlanePath/CellularRule54.pm ↑ Summary PlanePath/GosperReplicate.pm »