| 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 | # PowerPart has mostly square-free for X/Y > 1/2, then wedge of mostly | ||
| 20 | # multiple of 4, then mostly multiple of 16, then wedge of higher powers | ||
| 21 | # of 2. Similar in AYT. | ||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | package Math::PlanePath::FractionsTree; | ||
| 26 | use 5.004; | ||
| 27 | use strict; | ||
| 28 | |||
| 29 | use vars '$VERSION', '@ISA'; | ||
| 30 | $VERSION = 128; | ||
| 31 | use Math::PlanePath; | ||
| 32 | @ISA = ('Math::PlanePath'); | ||
| 33 | |||
| 34 | use Math::PlanePath::Base::Generic | ||
| 35 | 'is_infinite', | ||
| 36 | 'round_nearest'; | ||
| 37 | use Math::PlanePath::Base::Digits | ||
| 38 | 'bit_split_lowtohigh', | ||
| 39 | 'digit_join_lowtohigh'; | ||
| 40 | use Math::PlanePath::RationalsTree; | ||
| 41 | |||
| 42 | use Math::PlanePath::CoprimeColumns; | ||
| 43 | *_coprime = \&Math::PlanePath::CoprimeColumns::_coprime; | ||
| 44 | |||
| 45 | # uncomment this to run the ### lines | ||
| 46 | #use Smart::Comments; | ||
| 47 | |||
| 48 | |||
| 49 | use constant class_x_negative => 0; | ||
| 50 | use constant class_y_negative => 0; | ||
| 51 | use constant x_minimum => 1; | ||
| 52 | use constant y_minimum => 2; | ||
| 53 | use constant diffxy_maximum => -1; # upper octant X<=Y-1 so X-Y<=-1 | ||
| 54 | use constant gcdxy_maximum => 1; # no common factor | ||
| 55 | use constant tree_num_children_list => (2); # complete binary tree | ||
| 56 | use constant tree_n_to_subheight => undef; # complete tree, all infinity | ||
| 57 | |||
| 58 | use constant parameter_info_array => | ||
| 59 | [ | ||
| 60 | { name => 'tree_type', | ||
| 61 | share_key => 'tree_type_fractionstree', | ||
| 62 | display => 'Tree Type', | ||
| 63 | type => 'enum', | ||
| 64 | default => 'Kepler', | ||
| 65 | choices => ['Kepler'], | ||
| 66 | }, | ||
| 67 | ]; | ||
| 68 | |||
| 69 | use constant dir_maximum_dxdy => (-2, -(sqrt(5)+1)); # phi | ||
| 70 | |||
| 71 | |||
| 72 | #------------------------------------------------------------------------------ | ||
| 73 | |||
| 74 | sub new { | ||
| 75 | my $self = shift->SUPER::new(@_); | ||
| 76 | $self->{'tree_type'} ||= 'Kepler'; | ||
| 77 | $self->{'n_start'} = 1; # for RationalsTree sharing | ||
| 78 | return $self; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub n_to_xy { | ||
| 82 | my ($self, $n) = @_; | ||
| 83 | ### FractionsTree n_to_xy(): "$n" | ||
| 84 | |||
| 85 | if ($n < 1) { return; } | ||
| 86 | if (is_infinite($n)) { return ($n,$n); } | ||
| 87 | |||
| 88 | # what to do for fractional $n? | ||
| 89 | { | ||
| 90 | my $int = int($n); | ||
| 91 | if ($n != $int) { | ||
| 92 | ### frac ... | ||
| 93 | my $frac = $n - $int; # inherit possible BigFloat/BigRat | ||
| 94 | my ($x1,$y1) = $self->n_to_xy($int); | ||
| 95 | my ($x2,$y2) = $self->n_to_xy($int+1); | ||
| 96 | my $dx = $x2-$x1; | ||
| 97 | my $dy = $y2-$y1; | ||
| 98 | ### x1,y1: "$x1, $y1" | ||
| 99 | ### x2,y2: "$x2, $y2" | ||
| 100 | ### dx,dy: "$dx, $dy" | ||
| 101 | ### result: ($frac*$dx + $x1).', '.($frac*$dy + $y1) | ||
| 102 | return ($frac*$dx + $x1, $frac*$dy + $y1); | ||
| 103 | } | ||
| 104 | $n = $int; | ||
| 105 | } | ||
| 106 | |||
| 107 | my $zero = ($n * 0); # inherit bignum 0 | ||
| 108 | my $one = $zero + 1; # inherit bignum 1 | ||
| 109 | |||
| 110 | # my $tree_type = $self->{'tree_type'}; | ||
| 111 | # if ($tree_type eq 'Kepler') | ||
| 112 | |||
| 113 | { | ||
| 114 | ### Kepler tree ... | ||
| 115 | |||
| 116 | # X/Y | ||
| 117 | # / \ | ||
| 118 | # X/(X+Y) Y/(X+Y) | ||
| 119 | # | ||
| 120 | # (1 0) (x) = ( x ) (a b) (1 0) = (a+b b) digit 0 | ||
| 121 | # (1 1) (y) (x+y) (c d) (1 1) (c+d d) | ||
| 122 | # | ||
| 123 | # (0 1) (x) = ( y ) (a b) (0 1) = (b a+b) digit 1 | ||
| 124 | # (1 1) (y) (x+y) (c d) (1 1) (d c+d) | ||
| 125 | |||
| 126 | my @bits = bit_split_lowtohigh($n); | ||
| 127 | pop @bits; # drop high 1 bit | ||
| 128 | |||
| 129 | my $a = $one; # initial (1 0) | ||
| 130 | my $b = $zero; # (0 1) | ||
| 131 | my $c = $zero; | ||
| 132 | my $d = $one; | ||
| 133 | while (@bits) { | ||
| 134 | ### at: "($a $b)" | ||
| 135 | ### at: "($c $d)" | ||
| 136 | ### $digit | ||
| 137 | |||
| 138 | if (shift @bits) { # low to high | ||
| 139 | ($a,$b) = ($b, $a+$b); | ||
| 140 | ($c,$d) = ($d, $c+$d); | ||
| 141 | } else { | ||
| 142 | $a += $b; | ||
| 143 | $c += $d; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | ### final: "($a $b)" | ||
| 147 | ### final: "($c $d)" | ||
| 148 | |||
| 149 | # (a b) (1) = (a+b) | ||
| 150 | # (c d) (2) (c+d) | ||
| 151 | return ($a+2*$b, $c+2*$d); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | sub xy_is_visited { | ||
| 156 | my ($self, $x, $y) = @_; | ||
| 157 | $x = round_nearest ($x); | ||
| 158 | $y = round_nearest ($y); | ||
| 159 | if ($x < 1 || $y < 2 || $x >= $y || ! _coprime($x,$y)) { | ||
| 160 | return 0; | ||
| 161 | } | ||
| 162 | return 1; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub xy_to_n { | ||
| 166 | my ($self, $x, $y) = @_; | ||
| 167 | $x = round_nearest ($x); | ||
| 168 | $y = round_nearest ($y); | ||
| 169 | ### FractionsTree xy_to_n(): "$x,$y $self->{'tree_type'}" | ||
| 170 | |||
| 171 | if ($x < 1 || $y < 2 || $x >= $y) { | ||
| 172 | return undef; | ||
| 173 | } | ||
| 174 | if (is_infinite($x)) { return $x; } | ||
| 175 | if (is_infinite($y)) { return $y; } | ||
| 176 | |||
| 177 | my $zero = $x * 0 * $y; # inherit bignum 0 | ||
| 178 | |||
| 179 | # X/Y | ||
| 180 | # / \ | ||
| 181 | # X/(X+Y) Y/(X+Y) | ||
| 182 | # | ||
| 183 | # (x,y) <- (x, y-x) nbit 0 | ||
| 184 | # (x,y) <- (y-x, x) nbit 1 | ||
| 185 | # | ||
| 186 | my @nbits; # low to high | ||
| 187 | for (;;) { | ||
| 188 | ### at: "$x,$y n=$n" | ||
| 189 | |||
| 190 | if ($y <= 2) { | ||
| 191 | if ($x == 1 && $y == 2) { | ||
| 192 | push @nbits, 1; # high bit | ||
| 193 | return digit_join_lowtohigh(\@nbits, 2, $zero); | ||
| 194 | } else { | ||
| 195 | return undef; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | ($y -= $x) # (X,Y) <- (X, Y-X) | ||
| 200 | || return undef; # common factor if had X==Y | ||
| 201 | if ($x > $y) { | ||
| 202 | ($x,$y) = ($y,$x); | ||
| 203 | push @nbits, 1; | ||
| 204 | } else { | ||
| 205 | push @nbits, 0; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | |||
| 211 | # not exact | ||
| 212 | sub rect_to_n_range { | ||
| 213 | my ($self, $x1,$y1, $x2,$y2) = @_; | ||
| 214 | ### rect_to_n_range() | ||
| 215 | |||
| 216 | $x1 = round_nearest ($x1); | ||
| 217 | $y1 = round_nearest ($y1); | ||
| 218 | $x2 = round_nearest ($x2); | ||
| 219 | $y2 = round_nearest ($y2); | ||
| 220 | |||
| 221 | ($x1,$x2) = ($x2,$x1) if $x1 > $x2; | ||
| 222 | ($y1,$y2) = ($y2,$y1) if $y1 > $y2; | ||
| 223 | ### $x2 | ||
| 224 | ### $y2 | ||
| 225 | |||
| 226 | |||
| 227 | # | / | ||
| 228 | # | / x1 | ||
| 229 | # | / +-----y2 | ||
| 230 | # | / | | ||
| 231 | # |/ +----- | ||
| 232 | # | ||
| 233 | if ($x2 < 1 || $y2 < 2 || $x1 >= $y2) { | ||
| 234 | ### no values, rect outside upper octant ... | ||
| 235 | return (1,0); | ||
| 236 | } | ||
| 237 | |||
| 238 | my $zero = ($x1 * 0 * $y1 * $x2 * $y2); # inherit bignum | ||
| 239 | ### $zero | ||
| 240 | |||
| 241 | if ($x2 >= $y2) { $x2 = $y2-1; } | ||
| 242 | if ($x1 < 1) { $x1 = 1; } | ||
| 243 | if ($y1 < 2) { $y1 = 2; } | ||
| 244 | |||
| 245 | # big x2, small y1 | ||
| 246 | # big y2, small x1 | ||
| 247 | # my $level = _bingcd_max ($y2,$x1); | ||
| 248 | ### $level | ||
| 249 | |||
| 250 | my $level = $y2; | ||
| 251 | return (1, ($zero+2) ** $level); | ||
| 252 | } | ||
| 253 | |||
| 254 | sub _bingcd_max { | ||
| 255 | my ($x,$y) = @_; | ||
| 256 | ### _bingcd_max(): "$x,$y" | ||
| 257 | |||
| 258 | if ($x < $y) { ($x,$y) = ($y,$x) } | ||
| 259 | |||
| 260 | ### div: int($x/$y) | ||
| 261 | ### bingcd: int($x/$y) + $y | ||
| 262 | |||
| 263 | return int($x/$y) + $y + 1; | ||
| 264 | } | ||
| 265 | |||
| 266 | #------------------------------------------------------------------------------ | ||
| 267 | use constant tree_num_roots => 1; | ||
| 268 | |||
| 269 | # Same structure as RationalsTree | ||
| 270 | *tree_n_children = \&Math::PlanePath::RationalsTree::tree_n_children; | ||
| 271 | *tree_n_num_children = \&Math::PlanePath::RationalsTree::tree_n_num_children; | ||
| 272 | *tree_n_parent = \&Math::PlanePath::RationalsTree::tree_n_parent; | ||
| 273 | *tree_n_to_depth = \&Math::PlanePath::RationalsTree::tree_n_to_depth; | ||
| 274 | *tree_depth_to_n = \&Math::PlanePath::RationalsTree::tree_depth_to_n; | ||
| 275 | *tree_depth_to_n_end = \&Math::PlanePath::RationalsTree::tree_depth_to_n_end; | ||
| 276 | *tree_depth_to_n_range=\&Math::PlanePath::RationalsTree::tree_depth_to_n_range; | ||
| 277 | *tree_depth_to_width = \&Math::PlanePath::RationalsTree::tree_depth_to_width; | ||
| 278 | |||
| 279 | |||
| 280 | 1; | ||
| 281 | __END__ |