| 1 | package Math::BigInt::Pari; | ||
| 2 | |||
| 3 | use 5.006002; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Math::BigInt::Lib 1.999801; | ||
| 8 | |||
| 9 | our @ISA = qw< Math::BigInt::Lib >; | ||
| 10 | |||
| 11 | our $VERSION = '1.3012'; | ||
| 12 | |||
| 13 | use Math::Pari qw(PARI pari2pv gdivent bittest | ||
| 14 | gcmp gcmp0 gcmp1 gcd ifact gpui gmul | ||
| 15 | binomial lcm | ||
| 16 | ); | ||
| 17 | |||
| 18 | # MBI will call this, so catch it and throw it away | ||
| 19 | sub import { } | ||
| 20 | sub api_version() { 2; } # we are compatible with MBI v1.83 and up | ||
| 21 | |||
| 22 | my $zero = PARI(0); # for _copy | ||
| 23 | my $one = PARI(1); # for _inc and _dec | ||
| 24 | my $two = PARI(2); # for _is_two | ||
| 25 | my $ten = PARI(10); # for _digit | ||
| 26 | |||
| 27 | sub _new { | ||
| 28 | # the . '' is because new($2) will give a magical scalar to us, and PARI | ||
| 29 | # does not like this at all :/ | ||
| 30 | # use Devel::Peek; print Dump($_[1]); | ||
| 31 | PARI($_[1] . '') | ||
| 32 | } | ||
| 33 | |||
| 34 | sub _from_hex { | ||
| 35 | my $hex = $_[1]; | ||
| 36 | $hex = '0x' . $hex unless substr($hex, 0, 2) eq '0x'; | ||
| 37 | Math::Pari::_hex_cvt($hex); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub _from_oct { | ||
| 41 | Math::Pari::_hex_cvt('0' . $_[1]); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub _from_bin { | ||
| 45 | my $bin = $_[1]; | ||
| 46 | $bin = '0b' . $bin unless substr($bin, 0, 2) eq '0b'; | ||
| 47 | Math::Pari::_hex_cvt($bin); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub _as_bin { | ||
| 51 | my $v = unpack('B*', _mp2os($_[1])); | ||
| 52 | return "0b0" if $v eq ''; | ||
| 53 | $v =~ s/^0*/0b/; | ||
| 54 | $v; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub _as_oct { | ||
| 58 | my $v = _mp2oct($_[1]); | ||
| 59 | return "00" if $v eq ''; | ||
| 60 | $v =~ s/^0*/0/; | ||
| 61 | $v; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _as_hex { | ||
| 65 | my $v = unpack('H*', _mp2os($_[1])); | ||
| 66 | return "0x0" if $v eq ''; | ||
| 67 | $v =~ s/^0*/0x/; | ||
| 68 | $v; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub _to_bin { | ||
| 72 | my $v = unpack('B*', _mp2os($_[1])); | ||
| 73 | $v =~ s/^0+//; | ||
| 74 | return "0" if $v eq ''; | ||
| 75 | $v; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub _to_oct { | ||
| 79 | my $v = _mp2oct($_[1]); | ||
| 80 | $v =~ s/^0+//; | ||
| 81 | return "0" if $v eq ''; | ||
| 82 | $v; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub _to_hex { | ||
| 86 | my $v = unpack('H*', _mp2os($_[1])); | ||
| 87 | $v =~ s/^0+//; | ||
| 88 | return "0" if $v eq ''; | ||
| 89 | $v; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub _to_bytes { | ||
| 93 | my $bytes = _mp2os($_[1]); | ||
| 94 | return $bytes eq '' ? "\x00" : $bytes; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub _mp2os { | ||
| 98 | my($p) = @_; | ||
| 99 | $p = PARI($p); | ||
| 100 | my $base = PARI(1) << PARI(4*8); | ||
| 101 | my $res = ''; | ||
| 102 | while ($p != 0) { | ||
| 103 | my $r = $p % $base; | ||
| 104 | $p = ($p - $r) / $base; | ||
| 105 | my $buf = pack 'V', $r; | ||
| 106 | if ($p == 0) { | ||
| 107 | $buf = $r >= 16777216 ? $buf | ||
| 108 | : $r >= 65536 ? substr($buf, 0, 3) | ||
| 109 | : $r >= 256 ? substr($buf, 0, 2) | ||
| 110 | : substr($buf, 0, 1); | ||
| 111 | } | ||
| 112 | $res .= $buf; | ||
| 113 | } | ||
| 114 | scalar reverse $res; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub _mp2oct { | ||
| 118 | my($p) = @_; | ||
| 119 | $p = PARI($p); | ||
| 120 | my $base = PARI(8); | ||
| 121 | my $res = ''; | ||
| 122 | while ($p != 0) { | ||
| 123 | my $r = $p % $base; | ||
| 124 | $p = ($p - $r) / $base; | ||
| 125 | $res .= $r; | ||
| 126 | } | ||
| 127 | scalar reverse $res; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub _zero { PARI(0) } | ||
| 131 | sub _one { PARI(1) } | ||
| 132 | sub _two { PARI(2) } | ||
| 133 | sub _ten { PARI(10) } | ||
| 134 | |||
| 135 | sub _1ex { gpui(PARI(10), $_[1]) } | ||
| 136 | |||
| 137 | sub _copy { $_[1] + $zero; } | ||
| 138 | |||
| 139 | sub _str { pari2pv($_[1]) } | ||
| 140 | |||
| 141 | sub _num { 0 + pari2pv($_[1]) } | ||
| 142 | |||
| 143 | sub _add { $_[1] += $_[2] } | ||
| 144 | |||
| 145 | sub _sub { | ||
| 146 | if ($_[3]) { | ||
| 147 | $_[2] = $_[1] - $_[2]; | ||
| 148 | return $_[2]; | ||
| 149 | } | ||
| 150 | $_[1] -= $_[2]; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub _mul { $_[1] = gmul($_[1], $_[2]) } | ||
| 154 | |||
| 155 | sub _div { | ||
| 156 | if (wantarray) { | ||
| 157 | my $r = $_[1] % $_[2]; | ||
| 158 | $_[1] = gdivent($_[1], $_[2]); | ||
| 159 | return ($_[1], $r); | ||
| 160 | } | ||
| 161 | $_[1] = gdivent($_[1], $_[2]); | ||
| 162 | } | ||
| 163 | |||
| 164 | sub _mod { $_[1] %= $_[2]; } | ||
| 165 | |||
| 166 | sub _nok { | ||
| 167 | my ($class, $n, $k) = @_; | ||
| 168 | |||
| 169 | # Math::Pari doesn't seem to be able to handle the case when n is large and | ||
| 170 | # k is almost as big as n. For instance, the following returns zero (at | ||
| 171 | # least for certain versions and configurations): | ||
| 172 | # | ||
| 173 | # $n = PARI("10000000000000000000"); | ||
| 174 | # $k = PARI("9999999999999999999"); | ||
| 175 | # print Math::Pari::binomial($n, $k);' | ||
| 176 | |||
| 177 | # If k > n/2, or, equivalently, 2*k > n, compute nok(n, k) as nok(n, n-k). | ||
| 178 | |||
| 179 | { | ||
| 180 | my $twok = $class -> _mul($class -> _two(), $class -> _copy($k)); | ||
| 181 | if ($class -> _acmp($twok, $n) > 0) { | ||
| 182 | $k = $class -> _sub($class -> _copy($n), $k); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | binomial($n, $k); | ||
| 187 | } | ||
| 188 | |||
| 189 | sub _inc { ++$_[1]; } | ||
| 190 | sub _dec { --$_[1]; } | ||
| 191 | |||
| 192 | sub _and { $_[1] &= $_[2] } | ||
| 193 | |||
| 194 | sub _xor { $_[1] ^= $_[2] } | ||
| 195 | |||
| 196 | sub _or { $_[1] |= $_[2] } | ||
| 197 | |||
| 198 | sub _pow { gpui($_[1], $_[2]) } | ||
| 199 | |||
| 200 | sub _gcd { gcd($_[1], $_[2]) } | ||
| 201 | |||
| 202 | sub _lcm { lcm($_[1], $_[2]) } | ||
| 203 | |||
| 204 | sub _len { length(pari2pv($_[1])) } # costly! | ||
| 205 | |||
| 206 | # XXX TODO: calc len in base 2 then appr. in base 10 | ||
| 207 | sub _alen { length(pari2pv($_[1])) } | ||
| 208 | |||
| 209 | sub _zeros { | ||
| 210 | my ($class, $x) = @_; | ||
| 211 | return 0 if gcmp0($x); # 0 has no trailing zeros | ||
| 212 | |||
| 213 | my $u = $class -> _str($x); | ||
| 214 | $u =~ /(0*)\z/; | ||
| 215 | return length($1); | ||
| 216 | |||
| 217 | #my $s = pari2pv($_[1]); | ||
| 218 | #my $i = length($s); | ||
| 219 | #my $zeros = 0; | ||
| 220 | #while (--$i >= 0) { | ||
| 221 | # substr($s, $i, 1) eq '0' ? $zeros ++ : last; | ||
| 222 | #} | ||
| 223 | #$zeros; | ||
| 224 | } | ||
| 225 | |||
| 226 | sub _digit { | ||
| 227 | # if $n < 0, we need to count from left and thus can't use the other method: | ||
| 228 | if ($_[2] < 0) { | ||
| 229 | return substr(pari2pv($_[1]), -1 - $_[2], 1); | ||
| 230 | } | ||
| 231 | # else this is faster (except for very short numbers) | ||
| 232 | # shift the number right by $n digits, then extract last digit via % 10 | ||
| 233 | pari2pv(gdivent($_[1], $ten ** $_[2]) % $ten); | ||
| 234 | } | ||
| 235 | |||
| 236 | sub _is_zero { gcmp0($_[1]) } | ||
| 237 | |||
| 238 | sub _is_one { gcmp1($_[1]) } | ||
| 239 | |||
| 240 | sub _is_two { gcmp($_[1], $two) ? 0 : 1 } | ||
| 241 | |||
| 242 | sub _is_ten { gcmp($_[1], $ten) ? 0 : 1 } | ||
| 243 | |||
| 244 | sub _is_even { bittest($_[1], 0) ? 0 : 1 } | ||
| 245 | |||
| 246 | sub _is_odd { bittest($_[1], 0) ? 1 : 0 } | ||
| 247 | |||
| 248 | sub _acmp { | ||
| 249 | my $i = gcmp($_[1], $_[2]) || 0; | ||
| 250 | # work around bug in Pari (on 64bit systems?) | ||
| 251 | $i = -1 if $i == 4294967295; | ||
| 252 | $i; | ||
| 253 | } | ||
| 254 | |||
| 255 | sub _check { | ||
| 256 | my ($class, $x) = @_; | ||
| 257 | return "Undefined" unless defined $x; | ||
| 258 | return "$x is not a reference to Math::Pari" | ||
| 259 | unless ref($x) eq 'Math::Pari'; | ||
| 260 | return 0; | ||
| 261 | } | ||
| 262 | |||
| 263 | sub _sqrt { | ||
| 264 | # square root of $x | ||
| 265 | my ($class, $x) = @_; | ||
| 266 | my $y = Math::Pari::sqrtint($x); | ||
| 267 | return $y * $y > $x ? $y - $one : $y; # bug in sqrtint()? | ||
| 268 | } | ||
| 269 | |||
| 270 | sub _root { | ||
| 271 | # n'th root | ||
| 272 | |||
| 273 | # Native version: | ||
| 274 | return $_[1] = int(Math::Pari::sqrtn($_[1] + 0.5, $_[2])); | ||
| 275 | } | ||
| 276 | |||
| 277 | sub _modpow { | ||
| 278 | # modulus of power ($x ** $y) % $z | ||
| 279 | my ($c, $num, $exp, $mod) = @_; | ||
| 280 | |||
| 281 | # in the trivial case, | ||
| 282 | if (gcmp1($mod)) { | ||
| 283 | $num = PARI(0); | ||
| 284 | return $num; | ||
| 285 | } | ||
| 286 | |||
| 287 | if (gcmp1($num)) { | ||
| 288 | $num = PARI(1); | ||
| 289 | return $num; | ||
| 290 | } | ||
| 291 | |||
| 292 | if (gcmp0($num)) { | ||
| 293 | if (gcmp0($exp)) { | ||
| 294 | return PARI(1); | ||
| 295 | } else { | ||
| 296 | return PARI(0); | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | my $acc = $c -> _copy($num); | ||
| 301 | my $t = $c -> _one(); | ||
| 302 | |||
| 303 | my $expbin = $c -> _to_bin($exp); | ||
| 304 | my $len = length($expbin); | ||
| 305 | while (--$len >= 0) { | ||
| 306 | if (substr($expbin, $len, 1) eq '1') { # is_odd | ||
| 307 | $c -> _mul($t, $acc); | ||
| 308 | $t = $c -> _mod($t, $mod); | ||
| 309 | } | ||
| 310 | $c -> _mul($acc, $acc); | ||
| 311 | $acc = $c -> _mod($acc, $mod); | ||
| 312 | } | ||
| 313 | $num = $t; | ||
| 314 | $num; | ||
| 315 | } | ||
| 316 | |||
| 317 | sub _rsft { | ||
| 318 | # (X, Y, N) = @_; means X >> Y in base N | ||
| 319 | |||
| 320 | if ($_[3] != 2) { | ||
| 321 | return $_[1] = gdivent($_[1], PARI($_[3]) ** $_[2]); | ||
| 322 | } | ||
| 323 | $_[1] >>= $_[2]; | ||
| 324 | } | ||
| 325 | |||
| 326 | sub _lsft { | ||
| 327 | # (X, Y, N) = @_; means X >> Y in base N | ||
| 328 | |||
| 329 | if ($_[3] != 2) { | ||
| 330 | return $_[1] *= PARI($_[3]) ** $_[2]; | ||
| 331 | } | ||
| 332 | $_[1] <<= $_[2]; | ||
| 333 | } | ||
| 334 | |||
| 335 | sub _fac { | ||
| 336 | # factorial of argument | ||
| 337 | $_[1] = ifact($_[1]); | ||
| 338 | } | ||
| 339 | |||
| 340 | # _set() - set an already existing object to the given scalar value | ||
| 341 | |||
| 342 | sub _set { | ||
| 343 | my ($c, $x, $y) = @_; | ||
| 344 | *x = \PARI($y . ''); | ||
| 345 | } | ||
| 346 | |||
| 347 | 1; | ||
| 348 | |||
| 349 | __END__ |