NumSeq/GolayRudinShapiro.pm

filter: Statementstmt 87.6% 57 / 65 Branchbran 70.0% 7 / 10 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 82.3% 14 / 17 Podpod 71.4% 5 / 7 totaltotal 72.2% 86 / 119 CC 11 SCAR 11.8
CC
11
Cov
77%
ith
6.6
characteristic_integer
6.0
characteristic_pn1
6.0
CRAP 3.2
« NumSeq/Pell.pm ↑ Summary NumSeq/CullenNumbers.pm »
1 # Copyright 2010, 2011, 2012, 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 # ENHANCE-ME: "alternate" parameter for (-1)^i factor ...
20  
21  
22 package Math::NumSeq::GolayRudinShapiro;
23 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use List::Util 'max','min';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 2 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 $VERSION = 75;
29  
30 2 ▶use Math::NumSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use Math::NumSeq::Base::IterateIth;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 @ISA = ('Math::NumSeq::Base::IterateIth',
33 'Math::NumSeq');
34 2 *_is_infinite = \&Math::NumSeq::_is_infinite;
35  
36 2 ▶use Math::NumSeq::Repdigits;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 2 *_digit_split_lowtohigh = \&Math::NumSeq::Repdigits::_digit_split_lowtohigh;
38  
39 # uncomment this to run the ### lines
40 #use Smart::Comments;
41  
42  
43 # use constant name => Math::NumSeq::__('Golay-Rudin-Shapiro');
44 2 ▶use constant default_i_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 2 ▶use constant parameter_info_array =>
Subroutine BEGIN: called CC=1 SCAR=0.0
47 [ {
48 name => 'values_type',
49 share_key => 'values_type_1-101',
50 type => 'enum', # but the code takes any pair with , between
51 default => '1,-1',
52 choices => ['1,-1',
53 '0,1',
54 ],
55 # TRANSLATORS: "1,-1" is here for translation in case the "," might look like a decimal point, otherwise can be left unchanged
56 choices_display => [Math::NumSeq::__('1,-1'),
57 Math::NumSeq::__('0,1'),
58 ],
59 description => Math::NumSeq::__('The values to give for even or odd parity.'),
60 },
61 2 ];
62  
63 4 ▶sub description {
Subroutine description: called CC=2 SCAR=6.9
Pod documented
64 4 my ($self) = @_;
65 4 ▶ my ($even,$odd) = (ref $self ? @{$self->{'values'}} : (1,-1));
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 2 2 4
66 # ENHANCE-ME: use __x(), maybe
67 4 return sprintf(Math::NumSeq::__('Golay/Rudin/Shapiro parity of adjacent 11 bit pairs, %s if even count %s if odd count.'),
68 $even, $odd);
69 }
70  
71 0 ▶sub characteristic_integer {
Subroutine characteristic_integer: not called CC=2 SCAR=17.9
Pod undocumented
72 0 my ($self) = @_;
73 return (_is_integer($self->{'values_min'}) # the two values
74 0 ▶ && _is_integer($self->{'values_max'}));
Condition0%0/3
_is_integer($self->{'values_min'}) && _is_integer($self->{'values_max'})
!ll&&!rl&&r
000
MC/DC0%0/2
_is_integer($self->{'values_min'}) && _is_integer($self->{'values_max'})
_is_integer($self->{'values_min'}) _is_integer($self->{'values_max'})
Truth table0%0/3
A && B
A= _is_integer($self->{'values_min'})B= _is_integer($self->{'values_max'})
ABresult
0X0
100
111
75 }
76 0 ▶sub characteristic_pn1 {
Subroutine characteristic_pn1: not called CC=2 SCAR=17.9
Pod undocumented
77 0 my ($self) = @_;
78 0 ▶ return ($self->{'values_min'} == -1 && $self->{'values_max'} == 1);
Condition0%0/3
$self->{'values_min'} == -1 && $self->{'values_max'} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{'values_min'} == -1 && $self->{'values_max'} == 1
$self->{'values_min'} == -1 $self->{'values_max'} == 1
Truth table0%0/3
A && B
A= $self->{'values_min'} == -1B= $self->{'values_max'} == 1
ABresult
0X0
100
111
79 }
80 0 ▶sub _is_integer {
Subroutine _is_integer: not called CC=1 SCAR=6.9
81 0 my ($n) = @_;
82 0 return ($n == int($n));
83 }
84  
85 #------------------------------------------------------------------------------
86 # cf A022155 - positions of -1
87 # A203463 - positions of +1
88 # A014081 - count of 11 bit pairs
89 # A020991 - position of last occurrence of n in the partial sums
90 # A005943 - number of subwords length n
91 #
92 2 my %oeis_anum = ('1,-1' => 'A020985', # 1 and -1
93 '0,1' => 'A020987', # 0 and 1
94 # OEIS-Catalogue: A020985
95 # OEIS-Catalogue: A020987 values_type=0,1
96 );
97 2 ▶sub oeis_anum {
Subroutine oeis_anum: called CC=2 SCAR=7.6
Pod documented
98 2 my ($self) = @_;
99 0 ▶ if ($self->{'alternate'}) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"alternate"}) 0 2 2
100 2 return $oeis_anum{$self->{'values_type'}};
101 }
102  
103 #------------------------------------------------------------------------------
104  
105 9 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
106 9 my $self = shift->SUPER::new(@_);
107  
108 9 my @values = split /,/, $self->{'values_type'};
109 9 $self->{'values'} = \@values;
110 9 $self->{'values_min'} = min(@values);
111 9 $self->{'values_max'} = max(@values);
112 ### $self
113 9 return $self;
114 }
115  
116 # ENHANCE-ME: use unpack() checksum 1-bit count as described by
117 # perlfunc.pod, if fit a UV or C "int" or whatever
118 #
119 # # N & Nshift leaves bits with a 1 below them, then parity of bit count
120 # $i &= ($i >> 1);
121 # return (1 & unpack('%32b*', pack('I', $i)));
122 #
123 20516 ▶sub ith {
Subroutine ith: called CC=6 SCAR=18.8
Pod documented
124 20516 my ($self, $i) = @_;
125 20516 ▶ if ($i < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 0) 6 20510 20516
126 6 return undef;
127 }
128 20510 ▶ if (_is_infinite($i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_infinite($i)) 0 20510 20510
129 0 return $i;
130 }
131  
132 20510 my $prev = 0;
133 20510 ▶ my $xor = ($self->{'alternate'} && ($i&1) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'alternate'} && $i & 1 ? : 0 20510 20510
Condition33%1/3
$self->{"alternate"} and $i & 1
!ll&&!rl&&r
2051000
MC/DC0%0/2
$self->{"alternate"} and $i & 1
$self->{"alternate"} $i & 1
Truth table33%1/3
A and B
A= $self->{"alternate"}B= $i & 1
ABresult
0X0
100
111
134 20510 foreach my $bit (_digit_split_lowtohigh($i,2)) {
135 249046 $xor ^= ($prev & $bit);
136 249046 $prev = $bit;
137 }
138 20510 return $self->{'values'}->[$xor];
139 }
140  
141 34 ▶sub pred {
Subroutine pred: called CC=2 SCAR=7.1
Pod documented
142 34 my ($self, $value) = @_;
143 return ($value == $self->{'values'}->[0]
144 34 ▶ || $value == $self->{'values'}->[1]);
Condition66%2/3
$value == $self->{'values'}[0] || $value == $self->{'values'}[1]
l!l&&r!l&&!r
22120
MC/DC0%0/2
$value == $self->{'values'}[0] || $value == $self->{'values'}[1]
$value == $self->{'values'}[0] $value == $self->{'values'}[1]
Truth table66%2/3
A || B
A= $value == $self->{'values'}[0]B= $value == $self->{'values'}[1]
ABresult
1X1
011
000
145 }
146  
147 # Jorg Arndt fxtbook next step by
148 # low 1s 0111 increment to become 1000
149 # if even number of 1s then that's a "11" parity change
150 # and if the 1000 has a 1 above it then that's a parity change too
151 # so flip if 10..00 is an odd bit position XOR the bit above it
152  
153  
154 #------------------------------------------------------------------------------
155  
156 2 1;
157 __END__
« NumSeq/Pell.pm ↑ Summary NumSeq/CullenNumbers.pm »