PlanePath/ComplexPlus.pm

filter: Statementstmt 29.2% 40 / 137 Branchbran 11.9% 5 / 42 Conditioncond 40.0% 6 / 15 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 52.6% 10 / 19 Podpod 100% 10 / 10 totaltotal 31.4% 73 / 232 CC 29 SCAR 36.8
CC
29
Cov
26%
xy_to_n
132.0
n_to_xy
42.0
n_to_level
12.0
CRAP 39.6
« PlanePath/WunderlichSerpentine.pm ↑ Summary PlanePath/CornerReplicate.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  
20 # math-image --path=ComplexPlus --all --scale=5
21 #
22 # math-image --path=ComplexPlus --expression='i<128?i:0' --output=numbers --size=132x40
23 #
24 # Realpart:
25 # math-image --path=ComplexPlus,realpart=2 --expression='i<50?i:0' --output=numbers --size=180
26 #
27 # Arms:
28 # math-image --path=ComplexPlus,arms=2 --expression='i<64?i:0' --output=numbers --size=79
29  
30  
31  
32 package Math::PlanePath::ComplexPlus;
33 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 #use List::Util 'max';
36 1 *max = \&Math::PlanePath::_max;
37  
38 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 $VERSION = 128;
40  
41 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 @ISA = ('Math::PlanePath');
43 1 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
44  
45 use Math::PlanePath::Base::Generic
46 1 'is_infinite',
47 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 use Math::PlanePath::Base::Digits
49 1 'round_up_pow',
50 'digit_split_lowtohigh',
51 1 ▶ 'digit_join_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 # uncomment this to run the ### lines
54 #use Smart::Comments;
55  
56  
57 1 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 use constant parameter_info_array =>
59 [ { name => 'realpart',
60 display => 'Real Part',
61 type => 'integer',
62 default => 1,
63 minimum => 1,
64 width => 2,
65 description => 'Real part r in the i+r complex base.',
66 },
67 { name => 'arms',
68 share_key => 'arms_2',
69 display => 'Arms',
70 type => 'integer',
71 minimum => 1,
72 maximum => 2,
73 default => 1,
74 width => 1,
75 description => 'Arms',
76 when_name => 'realpart',
77 when_value => '1',
78 },
79 1 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 # b=i+r
82 # theta = atan(1/r)
83 sub x_negative_at_n {
84 0 ▶ my ($self) = @_;
Subroutine x_negative_at_n: not called CC=2 SCAR=17.9
Pod documented
85 0 ▶ if ($self->{'realpart'} == 1) { return 8; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"realpart"} == 1) 0 0 0
86 0 return $self->{'norm'} ** _ceil((2*atan2(1,1)) / atan2(1,$self->{'realpart'}));
87 }
88 sub y_negative_at_n {
89 0 ▶ my ($self) = @_;
Subroutine y_negative_at_n: not called CC=2 SCAR=17.9
Pod documented
90 0 ▶ if ($self->{'realpart'} == 1) { return 32; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"realpart"} == 1) 0 0 0
91 0 return $self->{'norm'} ** _ceil((4*atan2(1,1)) / atan2(1,$self->{'realpart'}));
92 }
93 sub _ceil {
94 0 ▶ my ($x) = @_;
Subroutine _ceil: not called CC=2 SCAR=17.9
95 0 my $int = int($x);
96 0 ▶ return ($x > $int ? $int+1 : $int);
Branch: 1 branch
BranchTrueFalseTotal
$x > $int ? : 0 0 0
97 }
98  
99 sub absdx_minimum {
100 0 ▶ my ($self) = @_;
Subroutine absdx_minimum: not called CC=2 SCAR=17.9
Pod documented
101 0 ▶ return ($self->{'realpart'} == 1
Branch: 1 branch
BranchTrueFalseTotal
$self->{'realpart'} == 1 ? : 0 0 0
102 ? 0 # i+1 N=1 dX=0,dY=1
103 : 1); # i+r otherwise always diff
104 }
105 # use constant dir_maximum_dxdy => (0,0); # supremum, almost full way
106  
107 sub turn_any_straight {
108 0 ▶ my ($self) = @_;
Subroutine turn_any_straight: not called CC=1 SCAR=6.9
Pod documented
109 0 return ($self->{'realpart'} != 1); # realpart=1 never straight
110 }
111  
112  
113 #------------------------------------------------------------------------------
114 sub new {
115 5 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=7 SCAR=19.9
Pod documented
116  
117 5 my $realpart = $self->{'realpart'};
118 5 ▶ if (! defined $realpart || $realpart < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $realpart or $realpart < 1) 4 1 5
Condition66%2/3
not defined $realpart or $realpart < 1
l!l&&r!l&&!r
401
MC/DC50%1/2
not defined $realpart or $realpart < 1
not defined $realpart $realpart < 1
Truth table66%2/3
not(A) or B
A= not defined $realpartB= $realpart < 1
ABresult
1X1
011
000
119 4 $self->{'realpart'} = $realpart = 1;
120 }
121 5 $self->{'norm'} = $realpart*$realpart + 1;
122  
123 5 my $arms = $self->{'arms'};
124 5 ▶ if (! defined $arms || $arms <= 0 || $realpart != 1) { $arms = 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $arms or $arms <= 0 or $realpart != 1) { } 4 1 5
elsif ($arms > 2) { } 0 1 1
Condition66%4/6
not defined $arms or $arms <= 0 or $realpart != 1
l!l&&r!l&&!r
401
not defined $arms or $arms <= 0
l!l&&r!l&&!r
401
MC/DC33%1/3
not defined $arms or $arms <= 0 or $realpart != 1
not defined $arms $arms <= 0 $realpart != 1
Truth table50%2/4
not(A) or B or C
A= not defined $armsB= $arms <= 0C= $realpart != 1
ABCresult
1XX1
01X1
0011
0000
125 0 elsif ($arms > 2) { $arms = 2; }
126 5 $self->{'arms'} = $arms;
127  
128 5 return $self;
129 }
130  
131 sub n_to_xy {
132 0 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: not called CC=6 SCAR=37.4
Pod documented
133 ### ComplexPlus n_to_xy(): $n
134  
135 0 ▶ if ($n < 0) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
136 0 ▶ if (is_infinite($n)) { return ($n,$n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
137  
138 {
139 0 my $int = int($n);
140 ### $int
141 ### $n
142 0 ▶ if ($n != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $int) 0 0 0
143 0 my ($x1,$y1) = $self->n_to_xy($int);
144 0 my ($x2,$y2) = $self->n_to_xy($int+$self->{'arms'});
145 0 my $frac = $n - $int; # inherit possible BigFloat
146 0 my $dx = $x2-$x1;
147 0 my $dy = $y2-$y1;
148 0 return ($frac*$dx + $x1, $frac*$dy + $y1);
149 }
150 0 $n = $int; # BigFloat int() gives BigInt, use that
151 }
152  
153 0 my $realpart = $self->{'realpart'};
154 0 my $norm = $self->{'norm'};
155 ### $norm
156 ### $realpart
157  
158 # for i+1, arm=0 start X=0,Y=0, arm=1 start X=0,Y=1
159 0 my $x = 0;
160 0 my $y = _divrem_mutate ($n, $self->{'arms'});
161  
162 # for i+1, arm=0 start dX=1,dY=0, arm=1 start dX=-1,dY=0
163 0 my $dy = ($n * 0); # 0, inheriting bignum from $n
164 0 ▶ my $dx = ($y ? -1 : 1) + $dy; # inheriting bignum from $n
Branch: 1 branch
BranchTrueFalseTotal
$y ? : 0 0 0
165  
166 0 foreach my $digit (digit_split_lowtohigh($n,$norm)) {
167 ### at: "$x,$y digit=$digit dxdy=$dx,$dy"
168  
169 0 $x += $dx * $digit;
170 0 $y += $dy * $digit;
171  
172 # multiply i+r, ie. (dx,dy) = (dx + i*dy)*(i+$realpart)
173 0 ($dx,$dy) = ($realpart*$dx - $dy, $dx + $realpart*$dy);
174 }
175  
176 ### final: "$x,$y"
177 0 return ($x,$y);
178 }
179  
180 sub xy_to_n {
181 0 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: not called CC=11 SCAR=48.8
Pod documented
182 ### ComplexPlus xy_to_n(): "$x, $y"
183  
184 0 $x = round_nearest ($x);
185 0 $y = round_nearest ($y);
186  
187 0 my $realpart = $self->{'realpart'};
188 {
189 0 my $rx = $realpart*$x;
190 0 my $ry = $realpart*$y;
191 0 foreach my $overflow ($rx+$ry, $rx-$ry) {
192 0 ▶ if (is_infinite($overflow)) { return $overflow; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($overflow)) 0 0 0
193 }
194 }
195  
196 0 my $orig_x = $x;
197 0 my $orig_y = $y;
198  
199 0 my $norm = $self->{'norm'};
200 0 my $zero = ($x * 0 * $y); # inherit bignum 0
201 0 my @n; # digits low to high
202  
203 0 my $prev_x = 0;
204 0 my $prev_y = 0;
205 0 ▶ while ($x || $y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x or $y) 0 0 0
Condition0%0/3
$x or $y
l!l&&r!l&&!r
000
MC/DC0%0/2
$x or $y
$x $y
Truth table0%0/3
A or B
A= $xB= $y
ABresult
1X1
011
000
206 0 my $neg_y = $x - $y*$realpart;
207 0 my $digit = $neg_y % $norm;
208 ### at: "$x,$y n=$n digit $digit"
209  
210 0 push @n, $digit;
211 0 $x -= $digit;
212 0 $neg_y -= $digit;
213  
214 ### assert: ($neg_y % $norm) == 0
215 ### assert: (($x * $realpart + $y) % $norm) == 0
216  
217 # divide i+r = mul (i-r)/(i^2 - r^2)
218 # = mul (i-r)/-norm
219 # is (i*y + x) * (i-realpart)/-norm
220 # x = [ x*-realpart - y ] / -norm
221 # = [ x*realpart + y ] / norm
222 # y = [ y*-realpart + x ] / -norm
223 # = [ y*realpart - x ] / norm
224 #
225 0 ($x,$y) = (($x*$realpart+$y)/$norm, -$neg_y/$norm);
226  
227 0 ▶ if ($x == $prev_x && $y == $prev_y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x == $prev_x and $y == $prev_y) 0 0 0
Condition0%0/3
$x == $prev_x and $y == $prev_y
!ll&&!rl&&r
000
MC/DC0%0/2
$x == $prev_x and $y == $prev_y
$x == $prev_x $y == $prev_y
Truth table0%0/3
A and B
A= $x == $prev_xB= $y == $prev_y
ABresult
0X0
100
111
228 0 last;
229 }
230 0 $prev_x = $x;
231 0 $prev_y = $y;
232 }
233  
234 ### final: "$x,$y n=$n cf arms $self->{'arms'}"
235  
236 0 ▶ if ($y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y) 0 0 0
237 0 ▶ if ($self->{'arms'} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"arms"} > 1) 0 0 0
238 ### not on first arm, re-run as: -$orig_x, 1-$orig_y
239 0 local $self->{'arms'} = 1;
240 0 my $n = $self->xy_to_n(-$orig_x,1-$orig_y);
241 0 ▶ if (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n) 0 0 0
242 0 return 1 + 2*$n; # 180 degrees
243 }
244 }
245 ### X,Y not visited
246 0 return undef;
247 }
248  
249 0 my $n = digit_join_lowtohigh (\@n, $norm, $zero);
250 0 ▶ if ($self->{'arms'} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"arms"} > 1) 0 0 0
251 0 $n *= 2;
252 }
253 0 return $n;
254 }
255  
256 # not exact
257 sub rect_to_n_range {
258 0 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: not called CC=2 SCAR=17.9
Pod documented
259 ### ComplexPlus rect_to_n_range(): "$x1,$y1 $x2,$y2"
260  
261 0 my $xm = max(abs($x1),abs($x2));
262 0 my $ym = max(abs($y1),abs($y2));
263 0 my $n_hi = ($xm*$xm + $ym*$ym) * $self->{'arms'};
264 0 ▶ if ($self->{'realpart'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"realpart"} == 1) 0 0 0
265 0 $n_hi *= 16; # 2**4
266 }
267 0 return (0, int($n_hi));
268 }
269  
270 #------------------------------------------------------------------------------
271 # levels
272  
273 sub level_to_n_range {
274 9 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: called CC=1 SCAR=0.0
Pod documented
275 9 return (0, $self->{'norm'}**$level * $self->{'arms'} - 1);
276 }
277 sub n_to_level {
278 0 ▶ my ($self, $n) = @_;
Subroutine n_to_level: not called CC=3 SCAR=24.8
Pod documented
279 0 ▶ if ($n < 0) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
280 0 ▶ if (is_infinite($n)) { return $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
281 0 $n = round_nearest($n);
282 0 _divrem_mutate ($n, $self->{'arms'});
283 0 my ($pow, $exp) = round_up_pow ($n+1, $self->{'norm'});
284 0 return $exp;
285 }
286  
287 #------------------------------------------------------------------------------
288 1 1;
289 __END__
« PlanePath/WunderlichSerpentine.pm ↑ Summary PlanePath/CornerReplicate.pm »