| 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 | # math-image --path=ImaginaryBase --lines --scale=10 | ||||||||||||||||||||||||||||
| 20 | # math-image --path=ImaginaryBase --all --output=numbers_dash --size=80x50 | ||||||||||||||||||||||||||||
| 21 | # | ||||||||||||||||||||||||||||
| 22 | # cf A005351 positives as negabinary index | ||||||||||||||||||||||||||||
| 23 | # A005352 negatives as negabinary index | ||||||||||||||||||||||||||||
| 24 | # A039724 positives as negabinary index, in binary | ||||||||||||||||||||||||||||
| 25 | # A027615 negabinary bit count | ||||||||||||||||||||||||||||
| 26 | # = 3 * A072894(n+1) - 2n - 3 | ||||||||||||||||||||||||||||
| 27 | # A098725 first diffs of A072894 | ||||||||||||||||||||||||||||
| 28 | # A000695 same value binary and negabinary, being base 4 digits 0,1 | ||||||||||||||||||||||||||||
| 29 | # A001045 abs(negabinary) of 0b11111 all ones (2^n-(-1)^n)/3 | ||||||||||||||||||||||||||||
| 30 | # A185269 negabinary primes | ||||||||||||||||||||||||||||
| 31 | # | ||||||||||||||||||||||||||||
| 32 | # A073785 positives as -3 index | ||||||||||||||||||||||||||||
| 33 | # A007608 positives as -4 index | ||||||||||||||||||||||||||||
| 34 | # A073786 -5 | ||||||||||||||||||||||||||||
| 35 | # A073787 -6 | ||||||||||||||||||||||||||||
| 36 | # A073788 -7 | ||||||||||||||||||||||||||||
| 37 | # A073789 -8 | ||||||||||||||||||||||||||||
| 38 | # A073790 -9 | ||||||||||||||||||||||||||||
| 39 | # A039723 positives as negadecimal index | ||||||||||||||||||||||||||||
| 40 | # A051022 same value integer and negadecimal, 0s between digits | ||||||||||||||||||||||||||||
| 41 | # | ||||||||||||||||||||||||||||
| 42 | # http://mathworld.wolfram.com/Negabinary.html | ||||||||||||||||||||||||||||
| 43 | # http://mathworld.wolfram.com/Negadecimal.html | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | package Math::PlanePath::ImaginaryBase; | ||||||||||||||||||||||||||||
| 46 | 4 | ▶ | use 5.004; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | #use List::Util 'min','max'; | ||||||||||||||||||||||||||||
| 49 | 4 | *min = \&Math::PlanePath::_min; | |||||||||||||||||||||||||||
| 50 | 4 | *max = \&Math::PlanePath::_max; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 4 | ▶ | use vars '$VERSION', '@ISA'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 4 | $VERSION = 128; | |||||||||||||||||||||||||||
| 54 | 4 | ▶ | use Math::PlanePath; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 4 | @ISA = ('Math::PlanePath'); | |||||||||||||||||||||||||||
| 56 | 4 | *_divrem_mutate = \&Math::PlanePath::_divrem_mutate; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | use Math::PlanePath::Base::Generic | ||||||||||||||||||||||||||||
| 59 | 4 | 'is_infinite', | |||||||||||||||||||||||||||
| 60 | 4 | ▶ | 'round_nearest'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | use Math::PlanePath::Base::Digits | ||||||||||||||||||||||||||||
| 62 | 4 | 'parameter_info_array', # radix parameter | |||||||||||||||||||||||||||
| 63 | 'round_down_pow', | ||||||||||||||||||||||||||||
| 64 | 'round_up_pow', | ||||||||||||||||||||||||||||
| 65 | 'digit_split_lowtohigh', | ||||||||||||||||||||||||||||
| 66 | 4 | ▶ | 'digit_join_lowtohigh'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 4 | ▶ | use Math::PlanePath::ZOrderCurve; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | 4 | *_digit_interleave = \&Math::PlanePath::ZOrderCurve::_digit_interleave; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||
| 72 | #use Smart::Comments; | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 4 | ▶ | use constant n_start => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 76 | 4 | ▶ | use constant xy_is_visited => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 77 | 4 | ▶ | use constant absdx_minimum => 1; # X coord always changes | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | sub x_negative_at_n { | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
x_negative_at_n: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 0 | return $self->{'radix'}**2; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | sub y_negative_at_n { | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
y_negative_at_n: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 0 | return $self->{'radix'}**3; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub dir_maximum_dxdy { | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
dir_maximum_dxdy: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 0 | return ($self->{'radix'}-1, -2); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub turn_any_straight { | ||||||||||||||||||||||||||||
| 94 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
turn_any_straight: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 0 | return ($self->{'radix'} != 2); # radix=2 never straight | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | sub _UNDOCUMENTED__turn_any_left_at_n { | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_UNDOCUMENTED__turn_any_left_at_n: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 99 | 0 | return $self->{'radix'} - 1; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | sub _UNDOCUMENTED__turn_any_right_at_n { | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_UNDOCUMENTED__turn_any_right_at_n: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 103 | 0 | return $self->{'radix'}; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 109 | sub new { | ||||||||||||||||||||||||||||
| 110 | 9 | ▶ | my $self = shift->SUPER::new(@_); | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 9 | my $radix = $self->{'radix'}; | |||||||||||||||||||||||||||
| 113 | 9 | ▶ | if (! defined $radix || $radix <= 2) { $radix = 2; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $radix or $radix <= 2
MC/DC100%2/2
not defined $radix or $radix <= 2
not defined $radix
$radix <= 2
Truth table100%3/3
not(A) or B
A= not defined $radixB= $radix <= 2
| |||||||||||||||||||||||||||||
| 114 | 9 | $self->{'radix'} = $radix; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 9 | return $self; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | sub n_to_xy { | ||||||||||||||||||||||||||||
| 120 | 100 | ▶ | my ($self, $n) = @_; | ||||||||||||||||||||||||||
|
Subroutine
n_to_xy: called CC=7 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | ### ImaginaryBase n_to_xy(): $n | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 100 | ▶ | if ($n < 0) { return; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 100 | ▶ | if (is_infinite($n)) { return ($n,$n); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # ENHANCE-ME: lowest non-(r-1) digit determines direction to next, or | ||||||||||||||||||||||||||||
| 127 | # something like that | ||||||||||||||||||||||||||||
| 128 | { | ||||||||||||||||||||||||||||
| 129 | 100 | my $int = int($n); | |||||||||||||||||||||||||||
| 130 | ### $int | ||||||||||||||||||||||||||||
| 131 | ### $n | ||||||||||||||||||||||||||||
| 132 | 100 | ▶ | if ($n != $int) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | my ($x1,$y1) = $self->n_to_xy($int); | |||||||||||||||||||||||||||
| 134 | 0 | my ($x2,$y2) = $self->n_to_xy($int+1); | |||||||||||||||||||||||||||
| 135 | 0 | my $frac = $n - $int; # inherit possible BigFloat | |||||||||||||||||||||||||||
| 136 | 0 | my $dx = $x2-$x1; | |||||||||||||||||||||||||||
| 137 | 0 | my $dy = $y2-$y1; | |||||||||||||||||||||||||||
| 138 | 0 | return ($frac*$dx + $x1, $frac*$dy + $y1); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 100 | $n = $int; # BigFloat int() gives BigInt, use that | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 100 | my $radix = $self->{'radix'}; | |||||||||||||||||||||||||||
| 144 | 100 | my $x = 0; | |||||||||||||||||||||||||||
| 145 | 100 | my $y = 0; | |||||||||||||||||||||||||||
| 146 | 100 | my $len = ($n*0)+1; # inherit bignum 1 | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 100 | ▶ | if (my @digits = digit_split_lowtohigh($n, $radix)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 100 | $radix = -$radix; | |||||||||||||||||||||||||||
| 150 | 100 | for (;;) { | |||||||||||||||||||||||||||
| 151 | 217 | $x += (shift @digits) * $len; # digits low to high | |||||||||||||||||||||||||||
| 152 | 217 | ▶ | @digits || last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 158 | $y += (shift @digits) * $len; # digits low to high | |||||||||||||||||||||||||||
| 155 | 158 | ▶ | @digits || last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 117 | $len *= $radix; # $radix negative negates each time | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | ### final: "$x,$y" | ||||||||||||||||||||||||||||
| 162 | 100 | return ($x,$y); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # ($x-$digit) and ($y-$digit) are multiples of $radix, but apply int() in | ||||||||||||||||||||||||||||
| 166 | # case floating point rounding | ||||||||||||||||||||||||||||
| 167 | # | ||||||||||||||||||||||||||||
| 168 | sub xy_to_n { | ||||||||||||||||||||||||||||
| 169 | 1385 | ▶ | my ($self, $x, $y) = @_; | ||||||||||||||||||||||||||
|
Subroutine
xy_to_n: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 170 | ### ImaginaryBase xy_to_n(): "$x, $y" | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 1385 | $x = round_nearest ($x); | |||||||||||||||||||||||||||
| 173 | 1385 | ▶ | if (is_infinite($x)) { return ($x); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 1385 | $y = round_nearest ($y); | |||||||||||||||||||||||||||
| 176 | 1385 | ▶ | if (is_infinite($y)) { return ($y); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 1385 | my $radix = $self->{'radix'}; | |||||||||||||||||||||||||||
| 179 | 1385 | my $zero = ($x * 0 * $y); # inherit bignum 0 | |||||||||||||||||||||||||||
| 180 | 1385 | my @n; # digits low to high | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 1385 | ▶ | while ($x || $y) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$x or $y
MC/DC100%2/2
$x or $y
$x
$y
Truth table100%3/3
A or B
A= $xB= $y
| |||||||||||||||||||||||||||||
| 183 | ### at: "x=$x,y=$y n=".join(',',@n) | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 2999 | push @n, _divrem_mutate ($x, $radix); | |||||||||||||||||||||||||||
| 186 | 2999 | $x = -$x; | |||||||||||||||||||||||||||
| 187 | 2999 | push @n, _divrem_mutate ($y, $radix); | |||||||||||||||||||||||||||
| 188 | 2999 | $y = -$y; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | 1385 | return digit_join_lowtohigh (\@n,$radix, $zero); | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | # left xmax = (r-1) + (r^2 -r) + (r^3-r^2) + ... + (r^k - r^(k-1)) | ||||||||||||||||||||||||||||
| 194 | # = r^(k-1) - 1 | ||||||||||||||||||||||||||||
| 195 | # | ||||||||||||||||||||||||||||
| 196 | # right xmin = - (r + r^3 + ... + r^(2k+1)) | ||||||||||||||||||||||||||||
| 197 | # = -r * (1 + r^2 + ... + r^2k) | ||||||||||||||||||||||||||||
| 198 | # = -r * ((r^2)^(k+1) -1) / (r^2 - 1) | ||||||||||||||||||||||||||||
| 199 | # | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | # exact | ||||||||||||||||||||||||||||
| 202 | sub rect_to_n_range { | ||||||||||||||||||||||||||||
| 203 | 104 | ▶ | my ($self, $x1,$y1, $x2,$y2) = @_; | ||||||||||||||||||||||||||
|
Subroutine
rect_to_n_range: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 204 | ### ImaginaryBase rect_to_n_range(): "$x1,$y1 $x2,$y2" | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 104 | $x1 = round_nearest($x1); | |||||||||||||||||||||||||||
| 207 | 104 | $y1 = round_nearest($y1); | |||||||||||||||||||||||||||
| 208 | 104 | $x2 = round_nearest($x2); | |||||||||||||||||||||||||||
| 209 | 104 | $y2 = round_nearest($y2); | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 104 | my $zero = $x1 * 0 * $y1 * $x2 * $y2; | |||||||||||||||||||||||||||
| 212 | 104 | my $radix = $self->{'radix'}; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 104 | my ($min_xdigits, $max_xdigits) | |||||||||||||||||||||||||||
| 215 | = _negaradix_range_digits_lowtohigh($x1,$x2, $radix); | ||||||||||||||||||||||||||||
| 216 | 104 | ▶ | unless (defined $min_xdigits) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | 2 | return (0, $max_xdigits); # infinity | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 102 | my ($min_ydigits, $max_ydigits) | |||||||||||||||||||||||||||
| 221 | = _negaradix_range_digits_lowtohigh($y1,$y2, $radix); | ||||||||||||||||||||||||||||
| 222 | 102 | ▶ | unless (defined $min_ydigits) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | 2 | return (0, $max_ydigits); # infinity | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | ### $min_xdigits | ||||||||||||||||||||||||||||
| 227 | ### $max_xdigits | ||||||||||||||||||||||||||||
| 228 | ### min_x: digit_join_lowtohigh ($min_xdigits, $radix, $zero) | ||||||||||||||||||||||||||||
| 229 | ### max_x: digit_join_lowtohigh ($max_xdigits, $radix, $zero) | ||||||||||||||||||||||||||||
| 230 | ### $min_ydigits | ||||||||||||||||||||||||||||
| 231 | ### $max_ydigits | ||||||||||||||||||||||||||||
| 232 | ### min_y: digit_join_lowtohigh ($min_ydigits, $radix, $zero) | ||||||||||||||||||||||||||||
| 233 | ### max_y: digit_join_lowtohigh ($max_ydigits, $radix, $zero) | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 100 | my @min_digits = _digit_interleave ($min_xdigits, $min_ydigits); | |||||||||||||||||||||||||||
| 236 | 100 | my @max_digits = _digit_interleave ($max_xdigits, $max_ydigits); | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | ### final ... | ||||||||||||||||||||||||||||
| 239 | ### @min_digits | ||||||||||||||||||||||||||||
| 240 | ### @max_digits | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 100 | return (digit_join_lowtohigh (\@min_digits, $radix, $zero), | |||||||||||||||||||||||||||
| 243 | digit_join_lowtohigh (\@max_digits, $radix, $zero)); | ||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | # Return arrayrefs ($min_digits, $max_digits) which are the digits making | ||||||||||||||||||||||||||||
| 249 | # up the index range for negaradix values $x1 to $x2 inclusive. | ||||||||||||||||||||||||||||
| 250 | # The arrays are lowtohigh, so $min_digits->[0] is the least significant digit. | ||||||||||||||||||||||||||||
| 251 | # | ||||||||||||||||||||||||||||
| 252 | sub _negaradix_range_digits_lowtohigh { | ||||||||||||||||||||||||||||
| 253 | 302 | ▶ | my ($x1,$x2, $radix) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_negaradix_range_digits_lowtohigh: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 254 | ### _negaradix_range_digits(): "$x1,$x2 radix=$radix" | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 302 | ▶ | if ($x1 > $x2) { ($x1,$x2) = ($x2,$x1); } # make x1 <= x2 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 302 | my $radix_minus_1 = $radix - 1; | |||||||||||||||||||||||||||
| 259 | ### $radix | ||||||||||||||||||||||||||||
| 260 | ### $radix_minus_1 | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 302 | my ($len, $level, $min_base) = _negaradix_range_level ($x1,$x2, $radix); | |||||||||||||||||||||||||||
| 264 | ### $len | ||||||||||||||||||||||||||||
| 265 | ### $level | ||||||||||||||||||||||||||||
| 266 | 302 | ▶ | if (is_infinite($level)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | 4 | return (undef, $level); | |||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | 298 | my $max_base = $min_base; | |||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | ### assert: $min_base <= $x1 | ||||||||||||||||||||||||||||
| 272 | ### assert: $min_base + $len > $x2 | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 298 | my @min_digits; # digits formed high to low, stored low to high | |||||||||||||||||||||||||||
| 275 | my @max_digits; | ||||||||||||||||||||||||||||
| 276 | 298 | ▶ | while (--$level > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | 376 | $len /= $radix; | |||||||||||||||||||||||||||
| 278 | ### at: "len=$len reverse" | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | # reversed digits, x1 low end for max, x2 high end for min | ||||||||||||||||||||||||||||
| 281 | { | ||||||||||||||||||||||||||||
| 282 | 376 | my $digit = max (0, | |||||||||||||||||||||||||||
| 283 | min ($radix_minus_1, | ||||||||||||||||||||||||||||
| 284 | int (($x2 - $min_base) / $len))); | ||||||||||||||||||||||||||||
| 285 | ### min base: $min_base | ||||||||||||||||||||||||||||
| 286 | ### min diff: $x2-$min_base | ||||||||||||||||||||||||||||
| 287 | ### min digit raw: $digit | ||||||||||||||||||||||||||||
| 288 | ### min digit reversed: $radix_minus_1 - $digit | ||||||||||||||||||||||||||||
| 289 | 376 | $min_base += $digit * $len; | |||||||||||||||||||||||||||
| 290 | 376 | $min_digits[$level] = $radix_minus_1 - $digit; | |||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | { | ||||||||||||||||||||||||||||
| 293 | 376 | my $digit = max (0, | |||||||||||||||||||||||||||
| 294 | min ($radix_minus_1, | ||||||||||||||||||||||||||||
| 295 | int (($x1 - $max_base) / $len))); | ||||||||||||||||||||||||||||
| 296 | ### max base: $max_base | ||||||||||||||||||||||||||||
| 297 | ### max diff: $x1-$max_base | ||||||||||||||||||||||||||||
| 298 | ### max digit raw: $digit | ||||||||||||||||||||||||||||
| 299 | ### max digit reversed: $radix_minus_1 - $digit | ||||||||||||||||||||||||||||
| 300 | 376 | $max_base += $digit * $len; | |||||||||||||||||||||||||||
| 301 | 376 | $max_digits[$level--] = $radix_minus_1 - $digit; | |||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 376 | $len /= $radix; | |||||||||||||||||||||||||||
| 305 | ### at: "len=$len plain" | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | # plain digits, x1 low end for min, x2 high end for max | ||||||||||||||||||||||||||||
| 308 | { | ||||||||||||||||||||||||||||
| 309 | 376 | my $digit = max (0, | |||||||||||||||||||||||||||
| 310 | min ($radix_minus_1, | ||||||||||||||||||||||||||||
| 311 | int (($x1 - $min_base) / $len))); | ||||||||||||||||||||||||||||
| 312 | ### min base: $min_base | ||||||||||||||||||||||||||||
| 313 | ### min diff: $x1-$min_base | ||||||||||||||||||||||||||||
| 314 | ### min digit: $digit | ||||||||||||||||||||||||||||
| 315 | 376 | $min_base += $digit * $len; | |||||||||||||||||||||||||||
| 316 | 376 | $min_digits[$level] = $digit; | |||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||
| 318 | { | ||||||||||||||||||||||||||||
| 319 | 376 | my $digit = max (0, | |||||||||||||||||||||||||||
| 320 | min ($radix_minus_1, | ||||||||||||||||||||||||||||
| 321 | int (($x2 - $max_base) / $len))); | ||||||||||||||||||||||||||||
| 322 | ### max base: $max_base | ||||||||||||||||||||||||||||
| 323 | ### max diff: $x2-$max_base | ||||||||||||||||||||||||||||
| 324 | ### max digit: $digit | ||||||||||||||||||||||||||||
| 325 | 376 | $max_base += $digit * $len; | |||||||||||||||||||||||||||
| 326 | 376 | $max_digits[$level] = $digit; | |||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | ### @min_digits | ||||||||||||||||||||||||||||
| 330 | ### @max_digits | ||||||||||||||||||||||||||||
| 331 | 298 | return (\@min_digits, \@max_digits); | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | # return ($len,$level,$base) | ||||||||||||||||||||||||||||
| 335 | # $level = number of digits in the bigest integer in negaradix $x1..$x2, | ||||||||||||||||||||||||||||
| 336 | # rounded up to be $level even | ||||||||||||||||||||||||||||
| 337 | # $len = $radix**$level | ||||||||||||||||||||||||||||
| 338 | # $base = lowest negaradix reached by indexes from 0 to $len-1 | ||||||||||||||||||||||||||||
| 339 | # | ||||||||||||||||||||||||||||
| 340 | # have $base <= $x1, $x2 < $base+$len | ||||||||||||||||||||||||||||
| 341 | # and $level is the smallest even number with that coverage | ||||||||||||||||||||||||||||
| 342 | # | ||||||||||||||||||||||||||||
| 343 | # negabinary | ||||||||||||||||||||||||||||
| 344 | # 0,1,5,21 | ||||||||||||||||||||||||||||
| 345 | # | ||||||||||||||||||||||||||||
| 346 | # negaternary | ||||||||||||||||||||||||||||
| 347 | # 1 3 9 27 81 243 | ||||||||||||||||||||||||||||
| 348 | # 0,2, 20 182 | ||||||||||||||||||||||||||||
| 349 | # -6 -60 -546 | ||||||||||||||||||||||||||||
| 350 | # | ||||||||||||||||||||||||||||
| 351 | sub _negaradix_range_level { | ||||||||||||||||||||||||||||
| 352 | 302 | ▶ | my ($x1,$x2, $radix) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_negaradix_range_level: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 353 | ### _negaradix_range_level(): "$x1,$x2 radix=$radix" | ||||||||||||||||||||||||||||
| 354 | ### assert: $x1 <= $x2 | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 302 | my ($len, $level) | |||||||||||||||||||||||||||
| 357 | = round_down_pow (max($radix - $x1*($radix + 1), | ||||||||||||||||||||||||||||
| 358 | (($radix+1)*$x2 - 1) * $radix), | ||||||||||||||||||||||||||||
| 359 | $radix); | ||||||||||||||||||||||||||||
| 360 | 302 | ▶ | if ($level & 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 361 | ### increase level to even ... | ||||||||||||||||||||||||||||
| 362 | 98 | $len *= $radix; | |||||||||||||||||||||||||||
| 363 | 98 | $level += 1; | |||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||
| 365 | ### $len | ||||||||||||||||||||||||||||
| 366 | ### $level | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | # because level is even r^2k-1 is a multiple of r^2-1 and therefore of r+1 | ||||||||||||||||||||||||||||
| 369 | ### assert: ($len-1) % ($radix+1) == 0 | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 302 | return ($len, | |||||||||||||||||||||||||||
| 372 | $level, | ||||||||||||||||||||||||||||
| 373 | ((1-$len) / ($radix+1)) * $radix); # base | ||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 378 | # levels | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | # shared by ImaginaryHalf and CubicBase | ||||||||||||||||||||||||||||
| 381 | sub level_to_n_range { | ||||||||||||||||||||||||||||
| 382 | 8 | ▶ | my ($self, $level) = @_; | ||||||||||||||||||||||||||
|
Subroutine
level_to_n_range: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 383 | 8 | return (0, $self->{'radix'}**$level - 1); | |||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||
| 385 | sub n_to_level { | ||||||||||||||||||||||||||||
| 386 | 0 | ▶ | my ($self, $n) = @_; | ||||||||||||||||||||||||||
|
Subroutine
n_to_level: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 387 | 0 | ▶ | if ($n < 0) { return undef; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 388 | 0 | ▶ | if (is_infinite($n)) { return $n; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 389 | 0 | $n = round_nearest($n); | |||||||||||||||||||||||||||
| 390 | 0 | my ($pow, $exp) = round_up_pow ($n+1, $self->{'radix'}); | |||||||||||||||||||||||||||
| 391 | 0 | return $exp; | |||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 395 | 4 | 1; | |||||||||||||||||||||||||||
| 396 | __END__ | ||||||||||||||||||||||||||||