NumSeq/HafermanCarpet.pm

filter: Statementstmt 90.4% 76 / 84 Branchbran 62.5% 15 / 24 Conditioncond 43.7% 7 / 16 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 100% 16 / 16 Podpod 100% 2 / 2 totaltotal 76.9% 117 / 152 CC 19 SCAR 21.9
CC
19
Cov
79%
ith
16.9
_divrem_mutate
9.9
pred
2.0
CRAP 8.9
« NumSeq/RepdigitRadix.pm ↑ Summary NumSeq/BalancedBinary.pm »
1 # Copyright 2013, 2014, 2016, 2017, 2019, 2020 Kevin Ryde
2  
3 # This file is part of Math-NumSeq.
4 #
5 # Math-NumSeq 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-NumSeq 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-NumSeq. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 package Math::NumSeq::HafermanCarpet;
20 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 $VERSION = 75;
25  
26 2 ▶use Math::NumSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Math::NumSeq::Base::IterateIth;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 @ISA = ('Math::NumSeq::Base::IterateIth',
29 'Math::NumSeq');
30 2 *_is_infinite = \&Math::NumSeq::_is_infinite;
31  
32 2 ▶use Math::NumSeq::Repdigits;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 *_digit_split_lowtohigh = \&Math::NumSeq::Repdigits::_digit_split_lowtohigh;
34  
35 # uncomment this to run the ### lines
36 # use Smart::Comments;
37  
38  
39 2 ▶use constant description => Math::NumSeq::__('Flattened Haferman carpet.');
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use constant default_i_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use constant values_min => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use constant values_max => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use constant characteristic_integer => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 ▶use constant characteristic_smaller => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 # 000 all zeros
47 # 001 infs are box fractal
48 # 010 evens is plain starting from 1
49 # 011 odd=1 even=0 inf=0 is inverse of plain
50 # 100 odds odd=0 even=1 inf=1 is plain starting from 0
51 # 101 inverse of evens
52 # 110 inverse of infs box fractal
53 # 111 all ones
54  
55 # start0 carpet 0
56 # start0 inv carpet 0 inverse
57 # start1 carpet 1
58 # start1 inv carpet 1 inverse
59 # box
60 # box inverse
61  
62 2 ▶use constant parameter_info_array =>
Subroutine BEGIN: called CC=1 SCAR=0.0
63 [
64 # { name => 'haferman_type',
65 # display => Math::NumSeq::__('Type'),
66 # type => 'enum',
67 # default => 'array',
68 # choices => ['array','alt','side'],
69 # },
70 { name => 'initial_value',
71 display => Math::NumSeq::__('Initial'),
72 type => 'integer',
73 default => 0,
74 minimum => 0,
75 maximum => 1,
76 width => 1,
77 },
78 { name => 'inverse',
79 display => Math::NumSeq::__('Inverse'),
80 type => 'boolean',
81 default => 0,
82 },
83 # { name => 'radix',
84 # share_key => 'radix_3',
85 # type => 'integer',
86 # display => Math::NumSeq::__('Radix'),
87 # default => 3,
88 # minimum => 2,
89 # width => 3,
90 # description => Math::NumSeq::__('Radix, ie. base, for the values calculation. Default is base 3.'),
91 # },
92 2 ];
93  
94 #------------------------------------------------------------------------------
95  
96 # 000 all zeros
97 # 001 infs are box fractal
98 # 010 evens inverse of starting from 1
99 # 011 odd=1 even=0 inf=0 is inverse of plain
100 # 100 odds odd=0 even=1 inf=1 is plain starting from 0
101 # 101 is plain starting from 1
102 # 110 inverse of infs box fractal
103 # 111 all ones
104 #
105 # my %odd_by_type = (start0 => 1,
106 # start1 => 1,
107 # box => 0);
108 # my %inf_by_type = (start0 => 0,
109 # start1 => 1,
110 # box => 1);
111 1777 ▶sub ith {
Subroutine ith: called CC=14 SCAR=28.3
Pod documented
112 1777 my ($self, $i) = @_;
113 ### ith(): $i
114  
115 1777 ▶ if ($i < 0 || _is_infinite($i)) { # don't loop forever if $i is +infinity
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 0 or _is_infinite($i)) 9 1768 1777
Condition66%2/3
$i < 0 or _is_infinite($i)
l!l&&r!l&&!r
901768
MC/DC50%1/2
$i < 0 or _is_infinite($i)
$i < 0 _is_infinite($i)
Truth table66%2/3
A or B
A= $i < 0B= _is_infinite($i)
ABresult
1X1
011
000
116 9 return undef;
117 }
118  
119 1768 ▶ my $haferman_type = $self->{'haferman_type'} || 'array';
Condition50%1/2
$self->{'haferman_type'} || 'array'
l!l
01768
MC/DC0%0/1
$self->{'haferman_type'} || 'array'
$self->{'haferman_type'}
Truth table50%1/2
A
A= $self->{'haferman_type'}
Aresult
11
00
120 1768 ▶ my $radix = $self->{'radix'} || 3;
Condition50%1/2
$self->{'radix'} || 3
l!l
01768
MC/DC0%0/1
$self->{'radix'} || 3
$self->{'radix'}
Truth table50%1/2
A
A= $self->{'radix'}
Aresult
11
00
121 1768 my $two_digits;
122 1768 ▶ if ($haferman_type eq 'array') {
Branch: 1 branch
BranchTrueFalseTotal
if ($haferman_type eq 'array') { } 1768 0 1768
123 1768 ▶ if ($radix & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($radix & 1) { } 1768 0 1768
124 1768 $radix *= $radix;
125 } else {
126 0 $two_digits = 1;
127 }
128 } else {
129 0 $two_digits = ($haferman_type eq 'alt');
130 }
131  
132 1768 my $value = 0; # position even or odd
133 1768 for (;;) {
134 3145 ▶ if ($i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i) { } 2609 536 3145
135 2609 my $digit = _divrem_mutate($i,$radix);
136 2609 ▶ if ($two_digits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($two_digits) 0 2609 2609
137 0 my $digit2 = _divrem_mutate($i,$radix);
138 0 ▶ if ($haferman_type eq 'array') {
Branch: 1 branch
BranchTrueFalseTotal
if ($haferman_type eq "array") 0 0 0
139 0 $digit += $digit2;
140 }
141 }
142 2609 ▶ if ($digit & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit & 1) { } 1232 1377 2609
143 # stop at odd digit
144 1232 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 466 766 1232
145 466 $value = 0; # even position value=0 always
146 } else {
147 766 ▶ $value = ($haferman_type eq 'box' ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$haferman_type eq 'box' ? : 0 766 766
148 }
149 1232 last;
150 } else {
151 # step position across even digit
152 1377 $value ^= 1;
153 }
154 } else {
155 # no more digits, all even, no odd
156 536 $value = $self->{'initial_value'};
157 536 last;
158 }
159 }
160 1768 ▶ if ($self->{'inverse'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inverse"}) 622 1146 1768
161 622 $value ^= 1;
162 }
163 1768 return $value;
164 }
165  
166 92 ▶sub pred {
Subroutine pred: called CC=2 SCAR=7.1
Pod documented
167 92 my ($self, $value) = @_;
168 92 ▶ return ($value == 0 || $value == 1);
Condition66%2/3
$value == 0 || $value == 1
l!l&&r!l&&!r
38540
MC/DC0%0/2
$value == 0 || $value == 1
$value == 0 $value == 1
Truth table66%2/3
A || B
A= $value == 0B= $value == 1
ABresult
1X1
011
000
169 }
170  
171 #------------------------------------------------------------------------------
172  
173 # return $remainder, modify $n
174 # the scalar $_[0] is modified, but if it's a BigInt then a new BigInt is made
175 # and stored there, the bigint value is not changed
176 2609 ▶sub _divrem_mutate {
Subroutine _divrem_mutate: called CC=5 SCAR=22.9
177 2609 my $d = $_[1];
178 2609 my $rem;
179 2609 ▶ if (ref $_[0] && $_[0]->isa('Math::BigInt')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and $_[0]->isa('Math::BigInt')) { } 0 2609 2609
Condition33%1/3
ref $_[0] and $_[0]->isa("Math::BigInt")
!ll&&!rl&&r
260900
MC/DC0%0/2
ref $_[0] and $_[0]->isa("Math::BigInt")
ref $_[0] $_[0]->isa("Math::BigInt")
Truth table33%1/3
A and B
A= ref $_[0]B= $_[0]->isa("Math::BigInt")
ABresult
0X0
100
111
180 0 ($_[0], $rem) = $_[0]->copy->bdiv($d); # quot,rem in array context
181 0 ▶ if (! ref $d || $d < 1_000_000) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $d or $d < 1000000) 0 0 0
Condition0%0/3
not ref $d or $d < 1000000
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $d or $d < 1000000
not ref $d $d < 1000000
Truth table0%0/3
not(A) or B
A= not ref $dB= $d < 1000000
ABresult
1X1
011
000
182 0 return $rem->numify; # plain remainder if fits
183 }
184 } else {
185 2609 $rem = $_[0] % $d;
186 2609 $_[0] = int(($_[0]-$rem)/$d); # exact division stays in UV
187 }
188 2609 return $rem;
189 }
190  
191 2 1;
192 __END__
« NumSeq/RepdigitRadix.pm ↑ Summary NumSeq/BalancedBinary.pm »