blib/lib/Math/AnyNum.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Build ↑ Summary
1 package Math::AnyNum;
2  
3 1 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶no warnings qw(numeric uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use List::Util qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Math::MPFR qw();
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use Math::GMPq qw();
12 use Math::GMPz qw();
13 use Math::MPC qw();
14  
15 use constant {
16 ULONG_MAX => Math::GMPq::_ulong_max(),
17 LONG_MIN => Math::GMPq::_long_min(),
18 };
19  
20 our $VERSION = '0.41';
21 our ($ROUND, $PREC);
22  
23 BEGIN {
24 $ROUND = Math::MPFR::MPFR_RNDN();
25 $PREC = 192;
26 }
27  
28 use overload
29 '""' => \&stringify,
30 '0+' => \&numify,
31 bool => \&boolify,
32  
33 '+' => \&add,
34 '*' => \&mul,
35  
36 '==' => \&eq,
37 '!=' => \&ne,
38  
39 '&' => \&and,
40 '|' => \&or,
41 '^' => \&xor,
42 '~' => \&not,
43  
44 '>' => sub { $_[2] ? (goto &lt) : (goto &gt) },
45 '>=' => sub { $_[2] ? (goto &le) : (goto &ge) },
46 '<' => sub { $_[2] ? (goto &gt) : (goto &lt) },
47 '<=' => sub { $_[2] ? (goto &ge) : (goto &le) },
48  
49 '<=>' => sub { $_[2] ? -(&cmp($_[0], $_[1]) // return undef) : &cmp($_[0], $_[1]) },
50  
51 '>>' => sub { @_ = ($_[1], $_[0]) if $_[2]; goto &rsft },
52 '<<' => sub { @_ = ($_[1], $_[0]) if $_[2]; goto &lsft },
53 '/' => sub { @_ = ($_[1], $_[0]) if $_[2]; goto &div },
54 '-' => sub { @_ = ($_[1], $_[0]) if $_[2]; goto &sub },
55  
56 '**' => sub { @_ = $_[2] ? @_[1, 0] : @_[0, 1]; goto &pow },
57 '%' => sub { @_ = $_[2] ? @_[1, 0] : @_[0, 1]; goto &mod },
58  
59 atan2 => sub { @_ = $_[2] ? @_[1, 0] : @_[0, 1]; goto &atan2 },
60  
61 eq => sub { "$_[0]" eq "$_[1]" },
62 ne => sub { "$_[0]" ne "$_[1]" },
63  
64 cmp => sub { $_[2] ? ("$_[1]" cmp $_[0]->stringify) : ($_[0]->stringify cmp "$_[1]") },
65  
66 neg => \&neg,
67 sin => \&sin,
68 cos => \&cos,
69 exp => \&exp,
70 log => \&ln,
71 int => \&int,
72 abs => \&abs,
73 sqrt => \&sqrt;
74  
75 {
76  
77 my %const = ( # prototypes are assigned in import()
78 e => \&e,
79 phi => \&phi,
80 tau => \&tau,
81 pi => \&pi,
82 ln2 => \&ln2,
83 i => \&i,
84 CatalanG => \&CatalanG,
85 EulerGamma => \&EulerGamma,
86 Inf => \&inf,
87 NaN => \&nan,
88 );
89  
90 my %trig = (
91 sin => sub (_) { goto &sin }, # built-in function
92 sinh => \&sinh,
93 asin => \&asin,
94 asinh => \&asinh,
95  
96 cos => sub (_) { goto &cos }, # built-in function
97 cosh => \&cosh,
98 acos => \&acos,
99 acosh => \&acosh,
100  
101 tan => \&tan,
102 tanh => \&tanh,
103 atan => \&atan,
104 atanh => \&atanh,
105  
106 cot => \&cot,
107 coth => \&coth,
108 acot => \&acot,
109 acoth => \&acoth,
110  
111 sec => \&sec,
112 sech => \&sech,
113 asec => \&asec,
114 asech => \&asech,
115  
116 csc => \&csc,
117 csch => \&csch,
118 acsc => \&acsc,
119 acsch => \&acsch,
120  
121 atan2 => \&atan2,
122 deg2rad => \&deg2rad,
123 rad2deg => \&rad2deg,
124 );
125  
126 my %special = (
127 beta => \&beta,
128 zeta => \&zeta,
129 eta => \&eta,
130  
131 gamma => \&gamma,
132 lgamma => \&lgamma,
133 lngamma => \&lngamma,
134 digamma => \&digamma,
135  
136 Ai => \&Ai,
137 Ei => \&Ei,
138 Li => \&Li,
139 Li2 => \&Li2,
140  
141 lgrt => \&lgrt,
142 LambertW => \&LambertW,
143  
144 BesselJ => \&BesselJ,
145 BesselY => \&BesselY,
146  
147 pow => \&pow,
148 sqr => \&sqr,
149 norm => \&norm,
150 sqrt => sub (_) { goto &sqrt }, # built-in function
151 cbrt => \&cbrt,
152 root => \&root,
153  
154 exp => sub (_) { goto &exp }, # built-in function
155 exp2 => \&exp2,
156 exp10 => \&exp10,
157  
158 ln => sub ($) { goto &ln }, # used in overloading
159 log => \&log, # built-in function
160 log2 => \&log2,
161 log10 => \&log10,
162  
163 mod => \&mod,
164 polymod => \&polymod,
165  
166 abs => sub (_) { goto &abs }, # built-in function
167  
168 erf => \&erf,
169 erfc => \&erfc,
170  
171 hypot => \&hypot,
172 agm => \&agm,
173  
174 lnbern => \&lnbern,
175 bernreal => \&bernreal,
176 harmreal => \&harmreal,
177  
178 lnsuperfactorial => \&lnsuperfactorial,
179 lnhyperfactorial => \&lnhyperfactorial,
180  
181 polygonal_root => \&polygonal_root,
182 polygonal_root2 => \&polygonal_root2,
183 );
184  
185 my %ntheory = (
186 factorial => \&factorial,
187 dfactorial => \&dfactorial,
188 mfactorial => \&mfactorial,
189 subfactorial => \&subfactorial,
190 primorial => \&primorial,
191 bell => \&bell,
192 catalan => \&catalan,
193 binomial => \&binomial,
194 multinomial => \&multinomial,
195  
196 superfactorial => \&superfactorial,
197 hyperfactorial => \&hyperfactorial,
198  
199 rising_factorial => \&rising_factorial,
200 falling_factorial => \&falling_factorial,
201  
202 lucas => \&lucas,
203 fibonacci => \&fibonacci,
204  
205 lucasU => \&lucasU,
206 lucasV => \&lucasV,
207  
208 lucasUmod => \&lucasUmod,
209 lucasVmod => \&lucasVmod,
210  
211 fibmod => \&fibmod,
212 lucasmod => \&lucasmod,
213  
214 chebyshevT => \&chebyshevT,
215 chebyshevU => \&chebyshevU,
216  
217 chebyshevTmod => \&chebyshevTmod,
218 chebyshevUmod => \&chebyshevUmod,
219  
220 laguerreL => \&laguerreL,
221 legendreP => \&legendreP,
222  
223 hermiteH => \&hermiteH,
224 hermiteHe => \&hermiteHe,
225  
226 faulhaber_sum => \&faulhaber_sum,
227 geometric_sum => \&geometric_sum,
228 dirichlet_sum => \&dirichlet_sum,
229  
230 bernfrac => \&bernfrac,
231 harmfrac => \&harmfrac,
232 harmonic => \&harmfrac,
233  
234 secant_number => \&secant_number,
235 tangent_number => \&tangent_number,
236  
237 euler => \&euler,
238 bernoulli => \&bernfrac,
239 faulhaber => \&faulhaber_polynomial,
240  
241 euler_polynomial => \&euler_polynomial,
242 bernoulli_polynomial => \&bernoulli_polynomial,
243 faulhaber_polynomial => \&faulhaber_polynomial,
244  
245 lcm => \&lcm,
246 gcd => \&gcd,
247 gcdext => \&gcdext,
248  
249 valuation => \&valuation,
250 kronecker => \&kronecker,
251  
252 remdiv => \&remdiv,
253  
254 addmod => \&addmod,
255 submod => \&submod,
256 mulmod => \&mulmod,
257 divmod => \&divmod,
258  
259 iadd => \&iadd,
260 isub => \&isub,
261 imul => \&imul,
262 imod => \&imod,
263  
264 idiv => \&idiv,
265 idiv_ceil => \&idiv_ceil,
266 idiv_round => \&idiv_round,
267 idiv_trunc => \&idiv_trunc,
268  
269 ipow => \&ipow,
270 ipow2 => \&ipow2,
271 ipow10 => \&ipow10,
272  
273 iroot => \&iroot,
274 isqrt => \&isqrt,
275 icbrt => \&icbrt,
276  
277 ilog => \&ilog,
278 ilog2 => \&ilog2,
279 ilog10 => \&ilog10,
280  
281 isqrtrem => \&isqrtrem,
282 irootrem => \&irootrem,
283  
284 polygonal => \&polygonal,
285 ipolygonal_root => \&ipolygonal_root,
286 ipolygonal_root2 => \&ipolygonal_root2,
287  
288 powmod => \&powmod,
289 invmod => \&invmod,
290  
291 quadratic_powmod => \&quadratic_powmod,
292  
293 is_power => \&is_power,
294 is_power_of => \&is_power_of,
295 is_square => \&is_square,
296 is_polygonal => \&is_polygonal,
297 is_polygonal2 => \&is_polygonal2,
298  
299 is_prime => \&is_prime,
300 is_coprime => \&is_coprime,
301 next_prime => \&next_prime,
302  
303 is_smooth => \&is_smooth,
304 is_rough => \&is_rough,
305 is_smooth_over_prod => \&is_smooth_over_prod,
306  
307 smooth_part => \&smooth_part,
308 rough_part => \&rough_part,
309  
310 make_coprime => \&make_coprime,
311 );
312  
313 my %misc = (
314 rand => \&rand,
315 irand => \&irand,
316  
317 min => \&min,
318 max => \&max,
319  
320 sum => \&sum,
321 prod => \&prod,
322  
323 seed => \&seed,
324 iseed => \&iseed,
325  
326 floor => \&floor,
327 ceil => \&ceil,
328 round => \&round,
329 sgn => \&sgn,
330  
331 acmp => \&acmp,
332 approx_cmp => \&approx_cmp,
333  
334 popcount => \&popcount,
335 hamdist => \&hamdist,
336  
337 neg => sub ($) { goto &neg }, # used in overloading
338 inv => \&inv,
339 conj => \&conj,
340 real => \&real,
341 imag => \&imag,
342 reals => \&reals,
343  
344 int => sub (_) { goto &int }, # built-in function
345 rat => \&rat,
346 float => \&float,
347 complex => \&complex,
348  
349 numerator => \&numerator,
350 denominator => \&denominator,
351 nude => \&nude,
352  
353 digits => \&digits,
354 digits2num => \&digits2num,
355 sumdigits => \&sumdigits,
356  
357 bsearch => \&bsearch,
358 bsearch_le => \&bsearch_le,
359 bsearch_ge => \&bsearch_ge,
360  
361 base => \&base,
362 as_bin => \&as_bin,
363 as_hex => \&as_hex,
364 as_oct => \&as_oct,
365 as_int => \&as_int,
366 as_rat => \&as_rat,
367 as_frac => \&as_frac,
368 as_dec => \&as_dec,
369  
370 setbit => \&setbit,
371 getbit => \&getbit,
372 flipbit => \&flipbit,
373 clearbit => \&clearbit,
374  
375 bit_scan0 => \&bit_scan0,
376 bit_scan1 => \&bit_scan1,
377  
378 rat_approx => \&rat_approx,
379 ratmod => \&ratmod,
380  
381 is_inf => \&is_inf,
382 is_ninf => \&is_ninf,
383 is_neg => \&is_neg,
384 is_pos => \&is_pos,
385 is_nan => \&is_nan,
386 is_rat => \&is_rat,
387 is_real => \&is_real,
388 is_imag => \&is_imag,
389 is_int => \&is_int,
390 is_complex => \&is_complex,
391 is_zero => \&is_zero,
392 is_one => \&is_one,
393 is_mone => \&is_mone,
394  
395 is_odd => \&is_odd,
396 is_even => \&is_even,
397 is_div => \&is_div,
398 is_congruent => \&is_congruent,
399 );
400  
401 sub import {
402 shift;
403  
404 my $caller = caller(0);
405  
406 while (@_) {
407 my $name = shift(@_);
408  
409 if ($name eq ':overload') {
410 overload::constant integer => sub {
411 ($_[0] < ULONG_MAX)
412 ? (bless \Math::GMPz::Rmpz_init_set_ui($_[0]))
413 : (bless \Math::GMPz::Rmpz_init_set_str("$_[0]", 10));
414 },
415 float => sub { bless \_str2obj($_[0]) },
416 binary => sub {
417 my $const = ($_[0] =~ tr/_//dr);
418 my $prefix = substr($const, 0, 2);
419 bless \(
420 $prefix eq '0x' ? Math::GMPz::Rmpz_init_set_str(substr($const, 2) || 0, 16)
421 : $prefix eq '0b' ? Math::GMPz::Rmpz_init_set_str(substr($const, 2) || 0, 2)
422 : Math::GMPz::Rmpz_init_set_str(substr($const, 1) || 0, 8)
423 );
424 };
425  
426 # Export 'Inf', 'NaN' and 'i' as constants
427 foreach my $pair (['Inf', inf()], ['NaN', nan()], ['i', i()]) {
428 my $sub = $caller . '::' . $pair->[0];
429 no strict 'refs';
430 no warnings 'redefine';
431 my $value = $pair->[1];
432 *$sub = sub () { $value };
433 }
434 }
435 elsif (exists $const{$name}) {
436 no strict 'refs';
437 no warnings 'redefine';
438 my $caller_sub = $caller . '::' . $name;
439 my $sub = $const{$name};
440 my $value = $sub->();
441 *$caller_sub = sub() { $value }
442 }
443 elsif ( exists($special{$name})
444 or exists($trig{$name})
445 or exists($ntheory{$name})
446 or exists($misc{$name})) {
447 no strict 'refs';
448 no warnings 'redefine';
449 my $caller_sub = $caller . '::' . $name;
450 *$caller_sub = $ntheory{$name} // $special{$name} // $trig{$name} // $misc{$name};
451 }
452 elsif ($name eq ':trig') {
453 push @_, keys(%trig);
454 }
455 elsif ($name eq ':ntheory') {
456 push @_, keys(%ntheory);
457 }
458 elsif ($name eq ':special') {
459 push @_, keys(%special);
460 }
461 elsif ($name eq ':misc') {
462 push @_, keys(%misc);
463 }
464 elsif ($name eq ':all') {
465 push @_, keys(%const), keys(%trig), keys(%special), keys(%ntheory), keys(%misc);
466 }
467 elsif ($name eq 'PREC') {
468 my $prec = CORE::int(shift(@_));
469 if ( $prec < Math::MPFR::RMPFR_PREC_MIN()
470 or $prec > Math::MPFR::RMPFR_PREC_MAX()) {
471 die "invalid value for <<PREC>>: must be between "
472 . Math::MPFR::RMPFR_PREC_MIN() . " and "
473 . Math::MPFR::RMPFR_PREC_MAX()
474 . ", but got <<$prec>>";
475 }
476 $PREC = $prec;
477 }
478 else {
479 die "unknown import: <<$name>>";
480 }
481 }
482 return;
483 }
484  
485 sub unimport {
486 overload::remove_constant(
487 binary => '',
488 float => '',
489 integer => '',
490 );
491 }
492 }
493  
494 # Convert a given pair (real, imag) into an MPC object
495 sub _reals2mpc {
496 my ($re, $im) = @_;
497  
498 my $r = Math::MPC::Rmpc_init2($PREC);
499  
500 $re = ref($re) ? _star2obj($re) : _str2obj($re);
501 $im = ref($im) ? _star2obj($im) : _str2obj($im);
502  
503 my $sig = join(' ', ref($re), ref($im));
504  
505 if ($sig eq q{Math::MPFR Math::MPFR}) {
506 Math::MPC::Rmpc_set_fr_fr($r, $re, $im, $ROUND);
507 }
508 elsif ($sig eq q{Math::GMPz Math::GMPz}) {
509 Math::MPC::Rmpc_set_z_z($r, $re, $im, $ROUND);
510 }
511 elsif ($sig eq q{Math::GMPz Math::MPFR}) {
512 Math::MPC::Rmpc_set_z_fr($r, $re, $im, $ROUND);
513 }
514 elsif ($sig eq q{Math::MPFR Math::GMPz}) {
515 Math::MPC::Rmpc_set_fr_z($r, $re, $im, $ROUND);
516 }
517 elsif ($sig eq q{Math::GMPz Math::GMPq}) {
518 Math::MPC::Rmpc_set_z_q($r, $re, $im, $ROUND);
519 }
520 elsif ($sig eq q{Math::GMPq Math::GMPz}) {
521 Math::MPC::Rmpc_set_q_z($r, $re, $im, $ROUND);
522 }
523 elsif ($sig eq q{Math::GMPq Math::GMPq}) {
524 Math::MPC::Rmpc_set_q_q($r, $re, $im, $ROUND);
525 }
526 elsif ($sig eq q{Math::GMPq Math::MPFR}) {
527 Math::MPC::Rmpc_set_q_fr($r, $re, $im, $ROUND);
528 }
529 elsif ($sig eq q{Math::MPFR Math::GMPq}) {
530 Math::MPC::Rmpc_set_fr_q($r, $re, $im, $ROUND);
531 }
532 elsif (ref($re) eq 'Math::MPC') {
533 Math::MPC::Rmpc_set($r, _any2mpc($im), $ROUND);
534 Math::MPC::Rmpc_mul_i($r, $r, 1, $ROUND);
535 Math::MPC::Rmpc_add($r, $r, $re, $ROUND);
536 }
537 elsif (ref($im) eq 'Math::MPC') {
538 Math::MPC::Rmpc_set($r, $im, $ROUND);
539 Math::MPC::Rmpc_mul_i($r, $r, 1, $ROUND);
540 Math::MPC::Rmpc_add($r, $r, _any2mpc($re), $ROUND);
541 }
542 else { # this should never happen
543 $re = _any2mpfr($re);
544 $im = _any2mpfr($im);
545 Math::MPC::Rmpc_set_fr_fr($r, $re, $im, $ROUND);
546 }
547  
548 return $r;
549 }
550  
551 # Create and return a new {GMP*, MPFR, MPC} object, given a base-10 numerical string
552 sub _str2obj {
553 my ($s) = @_;
554  
555 #<<<
556 #~ $s // do {
557 #~ require Carp;
558 #~ Carp::carp("Use of uninitialized value");
559 #~ };
560 #>>>
561  
562 $s || goto &_zero;
563  
564 $s = lc($s);
565  
566 if ($s eq 'inf' or $s eq '+inf') {
567 goto &_inf;
568 }
569 elsif ($s eq '-inf') {
570 goto &_ninf;
571 }
572 elsif ($s eq 'nan') {
573 goto &_nan;
574 }
575  
576 # Remove underscores
577 $s =~ tr/_//d;
578  
579 # Performance improvement for Perl integers
580 if (CORE::int($s) eq $s and $s > LONG_MIN and $s < ULONG_MAX) {
581 return (
582 ($s < 0)
583 ? Math::GMPz::Rmpz_init_set_si($s)
584 : Math::GMPz::Rmpz_init_set_ui($s)
585 );
586 }
587  
588 # Complex number (form: "(3 4)")
589 if (substr($s, 0, 1) eq '(' and substr($s, -1) eq ')') {
590 my ($re, $im) = split(' ', substr($s, 1, -1));
591  
592 if (defined($re) and defined($im)) {
593 return _reals2mpc($re, $im);
594 }
595 }
596  
597 # Complex number (form: "3+4i")
598 if (substr($s, -1) eq 'i') {
599  
600 if ($s eq 'i' or $s eq '+i') {
601 my $r = Math::MPC::Rmpc_init2($PREC);
602 Math::MPC::Rmpc_set_ui_ui($r, 0, 1, $ROUND);
603 return $r;
604 }
605 elsif ($s eq '-i') {
606 my $r = Math::MPC::Rmpc_init2($PREC);
607 Math::MPC::Rmpc_set_si_si($r, 0, -1, $ROUND);
608 return $r;
609 }
610  
611 my ($re, $im);
612  
613 state $numeric_re = qr/[+-]?+(?=\.?[0-9])[0-9]*+(?:\.[0-9]++)?(?:[Ee](?:[+-]?+[0-9]+))?/;
614 state $unsigned_re = qr/(?=\.?[0-9])[0-9]*+(?:\.[0-9]++)?(?:[Ee](?:[+-]?+[0-9]+))?/;
615  
616 if ($s =~ /^($numeric_re)\s*([-+])\s*($unsigned_re)i\z/o) {
617 ($re, $im) = ($1, $3);
618 $im = "-$im" if $2 eq '-';
619 }
620 elsif ($s =~ /^($numeric_re)i\z/o) {
621 ($re, $im) = (0, $1);
622 }
623 elsif ($s =~ /^($numeric_re)\s*([-+])\s*i\z/o) {
624 ($re, $im) = ($1, 1);
625 $im = -1 if $2 eq '-';
626 }
627  
628 if (defined($re) and defined($im)) {
629 return _reals2mpc($re, $im);
630 }
631 }
632  
633 # Floating-point
634 if ($s =~ tr/e.//) {
635 my $r = Math::MPFR::Rmpfr_init2($PREC);
636 if (Math::MPFR::Rmpfr_set_str($r, "$s", 10, $ROUND)) {
637 Math::MPFR::Rmpfr_set_nan($r);
638 }
639 return $r;
640 }
641  
642 # Remove the leading plus sign (if any)
643 $s =~ s/^\+// if substr($s, 0, 1) eq '+';
644  
645 # Fraction
646 if (index($s, '/') != -1) {
647  
648 if ($s =~ m{^\s*-?[0-9]+\s*/\s*-?[1-9]+[0-9]*\s*\z}) {
649 my $r = Math::GMPq::Rmpq_init();
650 Math::GMPq::Rmpq_set_str($r, $s, 10);
651 Math::GMPq::Rmpq_canonicalize($r);
652 return $r;
653 }
654  
655 return ${Math::AnyNum->new($s, 10)};
656 }
657  
658 # Integer
659 eval { Math::GMPz::Rmpz_init_set_str("$s", 10) } // goto &_nan;
660 }
661  
662 # Parse a given decimal expansion string as a base-10 fraction
663 sub _str2frac {
664 my ($str) = @_;
665  
666 my $sign = substr($str, 0, 1);
667 if ($sign eq '-') {
668 substr($str, 0, 1, '');
669 $sign = '-';
670 }
671 else {
672 substr($str, 0, 1, '') if ($sign eq '+');
673 $sign = '';
674 }
675  
676 if ((my $i = index($str, 'e')) != -1) {
677  
678 my $exp = substr($str, $i + 1);
679  
680 my ($before, $after) = split(/\./, substr($str, 0, $i));
681  
682 if (!defined($after)) { # return faster for numbers like "13e2"
683 if ($exp >= 0) {
684 return ("$sign$before" . ('0' x $exp));
685 }
686 else {
687 $after = '';
688 }
689 }
690  
691 my $numerator = "$sign$before$after";
692  
693 if ($exp < 0) {
694 return ("$numerator/1" . ('0' x (CORE::abs($exp) + CORE::length($after))));
695 }
696  
697 my $diff = ($exp - CORE::length($after));
698  
699 if ($diff >= 0) {
700 return ($numerator . ('0' x $diff));
701 }
702  
703 my $s = "$before$after";
704 substr($s, $exp + CORE::length($before), 0, '.');
705 return _str2frac("$sign$s");
706 }
707  
708 if ((my $i = index($str, '.')) != -1) {
709 my ($before, $after) = (substr($str, 0, $i), substr($str, $i + 1));
710  
711 if ($after == 0) {
712 return "$sign$before";
713 }
714  
715 return ($sign . "$before$after/1" . ('0' x CORE::length($after)));
716 }
717  
718 return "$sign$str";
719 }
720  
721 #
722 ## MPZ
723 #
724  
725 sub _mpz2mpq {
726 my $r = Math::GMPq::Rmpq_init();
727 Math::GMPq::Rmpq_set_z($r, $_[0]);
728 $r;
729 }
730  
731 sub _mpz2mpfr {
732 my $r = Math::MPFR::Rmpfr_init2($PREC);
733 Math::MPFR::Rmpfr_set_z($r, $_[0], $ROUND);
734 $r;
735 }
736  
737 sub _mpz2mpc {
738 my $r = Math::MPC::Rmpc_init2($PREC);
739 Math::MPC::Rmpc_set_z($r, $_[0], $ROUND);
740 $r;
741 }
742  
743 #
744 ## MPQ
745 #
746  
747 sub _mpq2mpz {
748 my $z = Math::GMPz::Rmpz_init();
749 Math::GMPz::Rmpz_set_q($z, $_[0]);
750 $z;
751 }
752  
753 sub _mpq2mpfr {
754 my $r = Math::MPFR::Rmpfr_init2($PREC);
755 Math::MPFR::Rmpfr_set_q($r, $_[0], $ROUND);
756 $r;
757 }
758  
759 sub _mpq2mpc {
760 my $r = Math::MPC::Rmpc_init2($PREC);
761 Math::MPC::Rmpc_set_q($r, $_[0], $ROUND);
762 $r;
763 }
764  
765 #
766 ## MPFR
767 #
768  
769 sub _mpfr2mpc {
770 my $r = Math::MPC::Rmpc_init2($PREC);
771 Math::MPC::Rmpc_set_fr($r, $_[0], $ROUND);
772 $r;
773 }
774  
775 #
776 ## Any
777 #
778  
779 sub _any2mpc {
780 my ($x) = @_;
781  
782 ref($x) eq 'Math::MPC' && return $x;
783 ref($x) eq 'Math::GMPq' && goto &_mpq2mpc;
784 ref($x) eq 'Math::GMPz' && goto &_mpz2mpc;
785  
786 goto &_mpfr2mpc;
787 }
788  
789 sub _any2mpfr {
790 my ($x) = @_;
791  
792 ref($x) eq 'Math::MPFR' && return $x;
793 ref($x) eq 'Math::GMPq' && goto &_mpq2mpfr;
794 ref($x) eq 'Math::GMPz' && goto &_mpz2mpfr;
795  
796 my $fr = Math::MPFR::Rmpfr_init2($PREC);
797 Math::MPC::RMPC_IM($fr, $x);
798  
799 Math::MPFR::Rmpfr_zero_p($fr)
800 ? Math::MPC::RMPC_RE($fr, $x)
801 : Math::MPFR::Rmpfr_set_nan($fr);
802  
803 $fr;
804 }
805  
806 sub _any2mpz {
807 my ($x) = @_;
808  
809 ref($x) eq 'Math::GMPz' && return $x;
810 ref($x) eq 'Math::GMPq' && goto &_mpq2mpz;
811  
812 if (ref($x) eq 'Math::MPFR') {
813 if (Math::MPFR::Rmpfr_number_p($x)) {
814 my $z = Math::GMPz::Rmpz_init();
815 Math::MPFR::Rmpfr_get_z($z, $x, Math::MPFR::MPFR_RNDZ);
816 return $z;
817 }
818 return;
819 }
820  
821 @_ = _any2mpfr($x);
822 goto &_any2mpz;
823 }
824  
825 sub _any2mpq {
826 my ($x) = @_;
827  
828 ref($x) eq 'Math::GMPq' && return $x;
829 ref($x) eq 'Math::GMPz' && goto &_mpz2mpq;
830  
831 if (ref($x) eq 'Math::MPFR') {
832 if (Math::MPFR::Rmpfr_number_p($x)) {
833 my $q = Math::GMPq::Rmpq_init();
834 Math::MPFR::Rmpfr_get_q($q, $x);
835 return $q;
836 }
837 return;
838 }
839  
840 @_ = _any2mpfr($x);
841 goto &_any2mpq;
842 }
843  
844 sub _any2ui {
845 my ($x) = @_;
846 goto(ref($x) =~ tr/:/_/rs);
847  
848 Math_GMPz: {
849  
850 if (Math::GMPz::Rmpz_fits_ulong_p($x)) {
851 return Math::GMPz::Rmpz_get_ui($x);
852 }
853  
854 return;
855 }
856  
857 Math_GMPq: {
858  
859 if (Math::GMPq::Rmpq_integer_p($x)) {
860 $x = _mpq2mpz($x);
861 goto Math_GMPz;
862 }
863  
864 my $d = CORE::int(Math::GMPq::Rmpq_get_d($x));
865 return (($d < 0 or $d > ULONG_MAX) ? undef : $d);
866 }
867  
868 Math_MPFR: {
869  
870 if (Math::MPFR::Rmpfr_integer_p($x) and Math::MPFR::Rmpfr_fits_ulong_p($x, $ROUND)) {
871 return Math::MPFR::Rmpfr_get_ui($x, $ROUND);
872 }
873  
874 if (Math::MPFR::Rmpfr_number_p($x)) {
875 my $d = CORE::int(Math::MPFR::Rmpfr_get_d($x, $ROUND));
876 return (($d < 0 or $d > ULONG_MAX) ? undef : $d);
877 }
878  
879 return;
880 }
881  
882 Math_MPC: {
883 $x = _any2mpfr($x);
884 goto Math_MPFR;
885 }
886 }
887  
888 sub _any2si {
889 my ($x) = @_;
890 goto(ref($x) =~ tr/:/_/rs);
891  
892 Math_GMPz: {
893  
894 if (Math::GMPz::Rmpz_fits_slong_p($x)) {
895 return Math::GMPz::Rmpz_get_si($x);
896 }
897  
898 if (Math::GMPz::Rmpz_fits_ulong_p($x)) {
899 return Math::GMPz::Rmpz_get_ui($x);
900 }
901  
902 return;
903 }
904  
905 Math_GMPq: {
906  
907 if (Math::GMPq::Rmpq_integer_p($x)) {
908 $x = _mpq2mpz($x);
909 goto Math_GMPz;
910 }
911  
912 my $d = CORE::int(Math::GMPq::Rmpq_get_d($x));
913 return (($d < LONG_MIN or $d > ULONG_MAX) ? undef : $d);
914 }
915  
916 Math_MPFR: {
917  
918 if (Math::MPFR::Rmpfr_integer_p($x)) {
919 if (Math::MPFR::Rmpfr_fits_slong_p($x, $ROUND)) {
920 return Math::MPFR::Rmpfr_get_si($x, $ROUND);
921 }
922  
923 if (Math::MPFR::Rmpfr_fits_ulong_p($x, $ROUND)) {
924 return Math::MPFR::Rmpfr_get_ui($x, $ROUND);
925 }
926 }
927  
928 if (Math::MPFR::Rmpfr_number_p($x)) {
929 my $d = CORE::int(Math::MPFR::Rmpfr_get_d($x, $ROUND));
930 return (($d < LONG_MIN or $d > ULONG_MAX) ? undef : $d);
931 }
932  
933 return;
934 }
935  
936 Math_MPC: {
937 $x = _any2mpfr($x);
938 goto Math_MPFR;
939 }
940 }
941  
942 #
943 ## Anything to MPFR (including strings)
944 #
945 sub _star2mpfr {
946 my ($x) = @_;
947  
948 $x =
949 ref($x) eq __PACKAGE__ ? $$x
950 : ref($x) ? _star2obj($x)
951 : _str2obj($x);
952  
953 ref($x) eq 'Math::MPFR' and return $x;
954  
955 @_ = $x;
956 ref($x) eq 'Math::GMPz' && goto &_mpz2mpfr;
957 ref($x) eq 'Math::GMPq' && goto &_mpq2mpfr;
958 goto &_any2mpfr;
959 }
960  
961 #
962 ## Anything to GMPz (including strings)
963 #
964 sub _star2mpz {
965 my ($x) = @_;
966  
967 # Performance improvement for Perl integers
968 if (!ref($x) and CORE::int($x) eq $x and $x > LONG_MIN and $x < ULONG_MAX) {
969 return (
970 ($x < 0)
971 ? Math::GMPz::Rmpz_init_set_si($x)
972 : Math::GMPz::Rmpz_init_set_ui($x)
973 );
974 }
975  
976 $x =
977 ref($x) eq __PACKAGE__ ? $$x
978 : ref($x) ? _star2obj($x)
979 : _str2obj($x);
980  
981 ref($x) eq 'Math::GMPz' and return $x;
982  
983 @_ = $x;
984 ref($x) eq 'Math::GMPq' and goto &_mpq2mpz;
985 goto &_any2mpz;
986 }
987  
988 #
989 ## Anything to a native unsigned integer (including strings)
990 #
991  
992 sub _star2ui {
993 my ($k) = @_;
994  
995 if (ref($k) eq __PACKAGE__) {
996  
997 $k = $$k;
998  
999 if (ref($k) eq 'Math::GMPz' and Math::GMPz::Rmpz_fits_ulong_p($k)) {
1000 $k = Math::GMPz::Rmpz_get_ui($k);
1001 }
1002 else {
1003 $k = _any2ui($k) // return;
1004 }
1005 }
1006 elsif (!ref($k) and CORE::int($k) eq $k and $k >= 0 and $k < ULONG_MAX) {
1007 $k = CORE::int($k);
1008 }
1009 else {
1010 $k = _any2ui(_star2obj($k)) // return;
1011 }
1012  
1013 $k;
1014 }
1015  
1016 #
1017 ## Anything to a native signed integer (including strings)
1018 #
1019  
1020 sub _star2si {
1021 my ($k) = @_;
1022  
1023 if (ref($k) eq __PACKAGE__) {
1024  
1025 $k = $$k;
1026  
1027 if (ref($k) eq 'Math::GMPz' and Math::GMPz::Rmpz_fits_ulong_p($k)) {
1028 $k = Math::GMPz::Rmpz_get_ui($k);
1029 }
1030 else {
1031 $k = _any2si($k) // return;
1032 }
1033 }
1034 elsif (!ref($k) and CORE::int($k) eq $k and $k > LONG_MIN and $k < ULONG_MAX) {
1035 $k = CORE::int($k);
1036 }
1037 else {
1038 $k = _any2si(_star2obj($k)) // return;
1039 }
1040  
1041 $k;
1042 }
1043  
1044 #
1045 ## Anything to MPFR or MPC, in this order (including strings)
1046 #
1047 sub _star2mpfr_mpc {
1048 my ($x) = @_;
1049  
1050 $x =
1051 ref($x) eq __PACKAGE__ ? $$x
1052 : ref($x) ? _star2obj($x)
1053 : _str2obj($x);
1054  
1055 if ( ref($x) eq 'Math::MPFR'
1056 or ref($x) eq 'Math::MPC') {
1057 return $x;
1058 }
1059  
1060 @_ = $x;
1061 ref($x) eq 'Math::GMPz' && goto &_mpz2mpfr;
1062 ref($x) eq 'Math::GMPq' && goto &_mpq2mpfr;
1063 goto &_any2mpfr; # this should not happen
1064 }
1065  
1066 #
1067 ## Anything to a {GMP*, MPFR or MPC} object
1068 #
1069 sub _star2obj {
1070 my ($x) = @_;
1071  
1072 # Performance improvement for Perl integers
1073 if (!ref($x)) {
1074 if (CORE::int($x) eq $x and $x > LONG_MIN and $x < ULONG_MAX) {
1075 return (
1076 ($x < 0)
1077 ? Math::GMPz::Rmpz_init_set_si($x)
1078 : Math::GMPz::Rmpz_init_set_ui($x)
1079 );
1080 }
1081 goto &_str2obj;
1082 }
1083  
1084 if (ref($x) eq __PACKAGE__) {
1085 $$x;
1086 }
1087 elsif ( ref($x) eq 'Math::GMPz'
1088 or ref($x) eq 'Math::GMPq'
1089 or ref($x) eq 'Math::MPFR'
1090 or ref($x) eq 'Math::MPC') {
1091 $x;
1092 }
1093 elsif (ref($x) eq 'Math::GComplex') {
1094 _reals2mpc($x->reals);
1095 }
1096 else {
1097 @_ = "$x";
1098 goto &_str2obj;
1099 }
1100 }
1101  
1102 #
1103 ## Binary splitting
1104 #
1105  
1106 sub _binsplit {
1107 my ($arr, $func) = @_;
1108  
1109 while ($#$arr > 0) {
1110 push(@$arr, $func->(shift(@$arr), shift(@$arr)));
1111 }
1112  
1113 $arr->[0];
1114 }
1115  
1116 # Cached primorial of k
1117 sub _cached_primorial {
1118 my ($k, $limit) = @_;
1119  
1120 state %cache;
1121  
1122 if (exists $cache{$k}) {
1123 return $cache{$k};
1124 }
1125  
1126 $limit //= 100;
1127  
1128 # Clear the cache when there are too many values cached
1129 if (scalar(keys(%cache)) > $limit) {
1130 Math::GMPz::Rmpz_clear($_) for values(%cache);
1131 undef %cache;
1132 }
1133  
1134 $cache{$k} //= do {
1135  
1136 state $GMP_V_MAJOR = Math::GMPz::__GNU_MP_VERSION();
1137 state $GMP_V_MINOR = Math::GMPz::__GNU_MP_VERSION_MINOR();
1138 state $OLD_GMP = ($GMP_V_MAJOR < 5 or ($GMP_V_MAJOR == 5 and $GMP_V_MINOR < 1));
1139  
1140 my $t = Math::GMPz::Rmpz_init_nobless();
1141  
1142 if ($OLD_GMP) {
1143 Math::GMPz::Rmpz_set_ui($t, 1);
1144 for (my $p = Math::GMPz::Rmpz_init_set_ui(2) ; Math::GMPz::Rmpz_cmp_ui($p, $k) <= 0 ; Math::GMPz::Rmpz_nextprime($p, $p)) {
1145 Math::GMPz::Rmpz_mul($t, $t, $p);
1146 }
1147 }
1148 else {
1149 Math::GMPz::Rmpz_primorial_ui($t, $k);
1150 }
1151  
1152 $t;
1153 };
1154 }
1155  
1156 sub new {
1157 my ($class, $num, $base) = @_;
1158  
1159 if (ref($base)) {
1160 if (ref($base) eq __PACKAGE__) {
1161 $base = _any2ui($$base) // 0;
1162 }
1163 else {
1164 $base = CORE::int($base);
1165 }
1166 }
1167  
1168 #<<<
1169 #~ $num // do {
1170 #~ require Carp;
1171 #~ Carp::carp("Use of uninitialized value");
1172 #~ };
1173 #>>>
1174  
1175 my $ref = ref($num);
1176  
1177 # Number with base
1178 if (defined($base)) {
1179  
1180 my $int_base = CORE::int($base);
1181  
1182 if ($int_base < 2 or $int_base > 62) {
1183 require Carp;
1184 Carp::croak("base must be between 2 and 62, got $base");
1185 }
1186  
1187 $num = defined($num) ? "$num" : '0';
1188  
1189 # Remove the leading plus sign (if any)
1190 $num =~ s/^\+// if substr($num, 0, 1) eq '+';
1191  
1192 if (index($num, '/') != -1) {
1193  
1194 my ($nu, $de) = split(/\//, $num);
1195  
1196 my $nu_obj = $class->new($nu, $base);
1197 my $de_obj = $class->new($de, $base);
1198  
1199 if (ref($$nu_obj) ne 'Math::GMPz') {
1200 goto &nan;
1201 }
1202  
1203 if (ref($$de_obj) ne 'Math::GMPz') {
1204 goto &nan;
1205 }
1206  
1207 if (Math::GMPz::Rmpz_sgn($$de_obj) == 0) {
1208 if (Math::GMPz::Rmpz_sgn($$nu_obj) == 0) {
1209 goto &nan; # 0/0
1210 }
1211  
1212 if (Math::GMPz::Rmpz_sgn($$nu_obj) < 0) {
1213 goto &ninf; # -x/0
1214 }
1215 else {
1216 goto &inf; # +x/0
1217 }
1218 }
1219  
1220 my $r = Math::GMPq::Rmpq_init();
1221  
1222 Math::GMPq::Rmpq_set_num($r, $$nu_obj);
1223 Math::GMPq::Rmpq_set_den($r, $$de_obj);
1224  
1225 Math::GMPq::Rmpq_canonicalize($r);
1226  
1227 return bless(\$r, $class);
1228 }
1229 elsif (substr($num, 0, 1) eq '(' and substr($num, -1) eq ')') {
1230 my $r = Math::MPC::Rmpc_init2($PREC);
1231 eval { Math::MPC::Rmpc_set_str($r, $num, $int_base, $ROUND); 1 } // goto &nan;
1232 return bless \$r;
1233 }
1234 elsif (index($num, '.') != -1) {
1235 my $r = Math::MPFR::Rmpfr_init2($PREC);
1236 if (Math::MPFR::Rmpfr_set_str($r, $num, $int_base, $ROUND) < 0) {
1237 goto &nan;
1238 }
1239 return bless \$r, $class;
1240 }
1241 else {
1242 return bless \(eval { Math::GMPz::Rmpz_init_set_str($num, $int_base) } // goto &nan), $class;
1243 }
1244 }
1245  
1246 # Special string values
1247 if (!$ref) {
1248 return bless \_str2obj($num), $class;
1249 }
1250  
1251 # Already a __PACKAGE__ object
1252 if ($ref eq __PACKAGE__) {
1253 return $num;
1254 }
1255  
1256 # GMPz
1257 if ($ref eq 'Math::GMPz') {
1258 return bless \Math::GMPz::Rmpz_init_set($num);
1259 }
1260  
1261 # MPFR
1262 if ($ref eq 'Math::MPFR') {
1263 my $r = Math::MPFR::Rmpfr_init2(CORE::int($PREC));
1264 Math::MPFR::Rmpfr_set($r, $num, $ROUND);
1265 return bless \$r;
1266 }
1267  
1268 # MPC
1269 if ($ref eq 'Math::MPC') {
1270 my $r = Math::MPC::Rmpc_init2(CORE::int($PREC));
1271 Math::MPC::Rmpc_set($r, $num, $ROUND);
1272 return bless \$r;
1273 }
1274  
1275 # GMPq
1276 if ($ref eq 'Math::GMPq') {
1277 my $r = Math::GMPq::Rmpq_init();
1278 Math::GMPq::Rmpq_set($r, $num);
1279 return bless \$r;
1280 }
1281  
1282 bless \_star2obj($num), $class;
1283 }
1284  
1285 sub new_si {
1286 my ($class, $si) = @_;
1287 bless \Math::GMPz::Rmpz_init_set_si($si), $class;
1288 }
1289  
1290 sub new_ui {
1291 my ($class, $ui) = @_;
1292 bless \Math::GMPz::Rmpz_init_set_ui($ui), $class;
1293 }
1294  
1295 sub new_z {
1296 my ($class, $str, $base) = @_;
1297 bless \Math::GMPz::Rmpz_init_set_str("$str", $base // 10), $class;
1298 }
1299  
1300 sub new_q {
1301 my ($class, $num, $den, $base) = @_;
1302 my $r = Math::GMPq::Rmpq_init();
1303  
1304 if (defined($den)) {
1305 Math::GMPq::Rmpq_set_str($r, "$num/$den", $base // 10);
1306 }
1307 else {
1308 Math::GMPq::Rmpq_set_str($r, "$num", $base // 10);
1309 }
1310  
1311 Math::GMPq::Rmpq_canonicalize($r);
1312 bless \$r, $class;
1313 }
1314  
1315 sub new_f {
1316 my ($class, $str, $base) = @_;
1317 my $r = Math::MPFR::Rmpfr_init2($PREC);
1318 Math::MPFR::Rmpfr_set_str($r, "$str", $base // 10, $ROUND);
1319 bless \$r, $class;
1320 }
1321  
1322 sub new_c {
1323 my ($class, $real, $imag, $base) = @_;
1324  
1325 my $c = Math::MPC::Rmpc_init2($PREC);
1326  
1327 if (defined($imag)) {
1328 my $re = Math::MPFR::Rmpfr_init2($PREC);
1329 my $im = Math::MPFR::Rmpfr_init2($PREC);
1330  
1331 Math::MPFR::Rmpfr_set_str($re, "$real", $base // 10, $ROUND);
1332 Math::MPFR::Rmpfr_set_str($im, "$imag", $base // 10, $ROUND);
1333  
1334 Math::MPC::Rmpc_set_fr_fr($c, $re, $im, $ROUND);
1335 }
1336 else {
1337 Math::MPC::Rmpc_set_str($c, "$real", $base // 10, $ROUND);
1338 }
1339  
1340 bless \$c, $class;
1341 }
1342  
1343 sub _nan {
1344 state $nan = do {
1345 my $r = Math::MPFR::Rmpfr_init2($PREC);
1346 Math::MPFR::Rmpfr_set_nan($r);
1347 $r;
1348 };
1349 }
1350  
1351 sub nan {
1352 state $nan = do {
1353 my $r = Math::MPFR::Rmpfr_init2($PREC);
1354 Math::MPFR::Rmpfr_set_nan($r);
1355 bless \$r;
1356 };
1357 }
1358  
1359 sub _inf {
1360 state $inf = do {
1361 my $r = Math::MPFR::Rmpfr_init2($PREC);
1362 Math::MPFR::Rmpfr_set_inf($r, 1);
1363 $r;
1364 };
1365 }
1366  
1367 sub inf {
1368 state $inf = do {
1369 my $r = Math::MPFR::Rmpfr_init2($PREC);
1370 Math::MPFR::Rmpfr_set_inf($r, 1);
1371 bless \$r;
1372 };
1373 }
1374  
1375 sub _ninf {
1376 state $ninf = do {
1377 my $r = Math::MPFR::Rmpfr_init2($PREC);
1378 Math::MPFR::Rmpfr_set_inf($r, -1);
1379 $r;
1380 };
1381 }
1382  
1383 sub ninf {
1384 state $ninf = do {
1385 my $r = Math::MPFR::Rmpfr_init2($PREC);
1386 Math::MPFR::Rmpfr_set_inf($r, -1);
1387 bless \$r;
1388 };
1389 }
1390  
1391 sub _zero {
1392 state $zero = Math::GMPz::Rmpz_init_set_ui(0);
1393 }
1394  
1395 sub zero {
1396 state $zero = do {
1397 my $r = Math::GMPz::Rmpz_init_set_ui(0);
1398 bless \$r;
1399 };
1400 }
1401  
1402 sub _one {
1403 state $one = Math::GMPz::Rmpz_init_set_ui(1);
1404 }
1405  
1406 sub one {
1407 state $one = do {
1408 my $r = Math::GMPz::Rmpz_init_set_ui(1);
1409 bless \$r;
1410 };
1411 }
1412  
1413 sub _mone {
1414 state $mone = Math::GMPz::Rmpz_init_set_si(-1);
1415 }
1416  
1417 sub mone {
1418 state $mone = do {
1419 my $r = Math::GMPz::Rmpz_init_set_si(-1);
1420 bless \$r;
1421 };
1422 }
1423  
1424 #
1425 ## CONSTANTS
1426 #
1427  
1428 sub pi {
1429 my $pi = Math::MPFR::Rmpfr_init2($PREC);
1430 Math::MPFR::Rmpfr_const_pi($pi, $ROUND);
1431 bless \$pi;
1432 }
1433  
1434 sub tau {
1435 my $tau = Math::MPFR::Rmpfr_init2($PREC);
1436 Math::MPFR::Rmpfr_const_pi($tau, $ROUND);
1437 Math::MPFR::Rmpfr_mul_2ui($tau, $tau, 1, $ROUND);
1438 bless \$tau;
1439 }
1440  
1441 sub ln2 {
1442 my $ln2 = Math::MPFR::Rmpfr_init2($PREC);
1443 Math::MPFR::Rmpfr_const_log2($ln2, $ROUND);
1444 bless \$ln2;
1445 }
1446  
1447 sub EulerGamma {
1448 my $euler = Math::MPFR::Rmpfr_init2($PREC);
1449 Math::MPFR::Rmpfr_const_euler($euler, $ROUND);
1450 bless \$euler;
1451 }
1452  
1453 sub CatalanG {
1454 my $catalan = Math::MPFR::Rmpfr_init2($PREC);
1455 Math::MPFR::Rmpfr_const_catalan($catalan, $ROUND);
1456 bless \$catalan;
1457 }
1458  
1459 sub i {
1460 my $i = Math::MPC::Rmpc_init2($PREC);
1461 Math::MPC::Rmpc_set_ui_ui($i, 0, 1, $ROUND);
1462 bless \$i;
1463 }
1464  
1465 sub e {
1466 state $one_f = (Math::MPFR::Rmpfr_init_set_ui_nobless(1, $ROUND))[0];
1467 my $e = Math::MPFR::Rmpfr_init2($PREC);
1468 Math::MPFR::Rmpfr_exp($e, $one_f, $ROUND);
1469 bless \$e;
1470 }
1471  
1472 sub phi {
1473 state $five4_f = (Math::MPFR::Rmpfr_init_set_d_nobless(1.25, $ROUND))[0];
1474  
1475 my $phi = Math::MPFR::Rmpfr_init2($PREC);
1476 Math::MPFR::Rmpfr_sqrt($phi, $five4_f, $ROUND);
1477 Math::MPFR::Rmpfr_add_d($phi, $phi, 0.5, $ROUND);
1478  
1479 bless \$phi;
1480 }
1481  
1482 #
1483 ## Stringification
1484 #
1485  
1486 sub __stringify__ {
1487 my ($x) = @_;
1488 goto(ref($x) =~ tr/:/_/rs);
1489  
1490 Math_GMPz: {
1491 return Math::GMPz::Rmpz_get_str($x, 10);
1492 }
1493  
1494 Math_GMPq: {
1495 return Math::GMPq::Rmpq_get_str($x, 10);
1496 }
1497  
1498 Math_MPFR: {
1499  
1500 Math::MPFR::Rmpfr_number_p($x)
1501 || return (
1502 Math::MPFR::Rmpfr_nan_p($x) ? 'NaN'
1503 : Math::MPFR::Rmpfr_sgn($x) < 0 ? '-Inf'
1504 : 'Inf'
1505 );
1506  
1507 # log(10)/log(2) =~ 3.3219280948873623
1508 my $digits = $PREC >> 2;
1509 my ($mantissa, $exponent) = Math::MPFR::Rmpfr_deref2($x, 10, $digits, $ROUND);
1510  
1511 my $sgn = '';
1512 if (substr($mantissa, 0, 1) eq '-') {
1513 $sgn = substr($mantissa, 0, 1, '');
1514 }
1515  
1516 $mantissa == 0 and return '0';
1517  
1518 if (CORE::abs($exponent) < CORE::length($mantissa)) {
1519  
1520 if ($exponent > 0) {
1521 substr($mantissa, $exponent, 0, '.');
1522 }
1523 else {
1524 substr($mantissa, 0, 0, '0.' . ('0' x CORE::abs($exponent)));
1525 }
1526  
1527 $mantissa = reverse($mantissa);
1528 $mantissa =~ s/^0+//;
1529 $mantissa =~ s/^\.//;
1530 $mantissa = reverse($mantissa);
1531  
1532 return ($sgn . $mantissa);
1533 }
1534  
1535 substr($mantissa, 1, 0, '.');
1536 return ($sgn . $mantissa . 'e' . ($exponent - 1));
1537 }
1538  
1539 Math_MPC: {
1540 my $fr = Math::MPFR::Rmpfr_init2($PREC);
1541  
1542 Math::MPC::RMPC_RE($fr, $x);
1543 my $re = __stringify__($fr);
1544  
1545 Math::MPC::RMPC_IM($fr, $x);
1546 my $im = __stringify__($fr);
1547  
1548 if ($im eq '0' or $im eq '-0') {
1549 return $re;
1550 }
1551  
1552 my $sign = '+';
1553  
1554 if (substr($im, 0, 1) eq '-') {
1555 $sign = '-';
1556 substr($im, 0, 1, '');
1557 }
1558  
1559 $im = '' if $im eq '1';
1560 return ($re eq '0' ? $sign eq '+' ? "${im}i" : "$sign${im}i" : "$re$sign${im}i");
1561 }
1562 }
1563  
1564 sub stringify { # used in overloading
1565 @_ = (${$_[0]});
1566 goto &__stringify__;
1567 }
1568  
1569 #
1570 ## Numification (object to a native integer or a double)
1571 #
1572  
1573 sub numify { # used in overloading
1574 my $x = ${$_[0]};
1575  
1576 goto(ref($x) =~ tr/:/_/rs);
1577  
1578 Math_MPFR: {
1579 if (Math::MPFR::Rmpfr_integer_p($x)) {
1580  
1581 if (Math::MPFR::Rmpfr_fits_ulong_p($x, $ROUND)) {
1582 return Math::MPFR::Rmpfr_get_ui($x, $ROUND);
1583 }
1584  
1585 if (Math::MPFR::Rmpfr_fits_slong_p($x, $ROUND)) {
1586 return Math::MPFR::Rmpfr_get_si($x, $ROUND);
1587 }
1588 }
1589  
1590 return Math::MPFR::Rmpfr_get_d($x, $ROUND);
1591 }
1592  
1593 Math_GMPq: {
1594  
1595 if (Math::GMPq::Rmpq_integer_p($x)) {
1596 $x = _mpq2mpz($x);
1597 goto Math_GMPz;
1598 }
1599  
1600 return Math::GMPq::Rmpq_get_d($x);
1601 }
1602  
1603 Math_GMPz: {
1604  
1605 if (Math::GMPz::Rmpz_fits_ulong_p($x)) {
1606 return Math::GMPz::Rmpz_get_ui($x);
1607 }
1608  
1609 if (Math::GMPz::Rmpz_fits_slong_p($x)) {
1610 return Math::GMPz::Rmpz_get_si($x);
1611 }
1612  
1613 return Math::GMPz::Rmpz_get_str($x, 10);
1614 }
1615  
1616 Math_MPC: {
1617 my $r = Math::MPFR::Rmpfr_init2($PREC);
1618 Math::MPC::RMPC_RE($r, $x);
1619 $x = $r;
1620 goto Math_MPFR;
1621 }
1622 }
1623  
1624 sub boolify { # used in overloading
1625 my $x = ${$_[0]};
1626  
1627 goto(ref($x) =~ tr/:/_/rs);
1628  
1629 Math_MPFR: {
1630 return !!Math::MPFR::Rmpfr_sgn($x);
1631 }
1632  
1633 Math_GMPq: {
1634 return !!Math::GMPq::Rmpq_sgn($x);
1635 }
1636  
1637 Math_GMPz: {
1638 return !!Math::GMPz::Rmpz_sgn($x);
1639 }
1640  
1641 Math_MPC: {
1642 my $r = Math::MPFR::Rmpfr_init2($PREC);
1643 Math::MPC::RMPC_RE($r, $x);
1644 Math::MPFR::Rmpfr_sgn($r) && return 1;
1645 Math::MPFR::Rmpfr_nan_p($r) && return 0;
1646 Math::MPC::RMPC_IM($r, $x);
1647 return !!Math::MPFR::Rmpfr_sgn($r);
1648 }
1649 }
1650  
1651 #
1652 ## EQUALITY
1653 #
1654  
1655 sub __eq__ {
1656 my ($x, $y) = @_;
1657 goto(join('__', ref($x), ref($y) || 'Scalar') =~ tr/:/_/rs);
1658  
1659 #
1660 ## MPFR
1661 #
1662 Math_MPFR__Math_MPFR: {
1663 return Math::MPFR::Rmpfr_equal_p($x, $y);
1664 }
1665  
1666 Math_MPFR__Math_GMPz: {
1667 return (Math::MPFR::Rmpfr_integer_p($x) and Math::MPFR::Rmpfr_cmp_z($x, $y) == 0);
1668 }
1669  
1670 Math_MPFR__Math_GMPq: {
1671 return (Math::MPFR::Rmpfr_number_p($x) and Math::MPFR::Rmpfr_cmp_q($x, $y) == 0);
1672 }
1673  
1674 Math_MPFR__Math_MPC: {
1675 $x = _mpfr2mpc($x);
1676 goto Math_MPC__Math_MPC;
1677 }
1678  
1679 Math_MPFR__Scalar: {
1680 return (
1681 Math::MPFR::Rmpfr_integer_p($x)
1682 and (
1683 ($y || return !Math::MPFR::Rmpfr_sgn($x)) < 0
1684 ? Math::MPFR::Rmpfr_cmp_si($x, $y)
1685 : Math::MPFR::Rmpfr_cmp_ui($x, $y)
1686 ) == 0
1687 );
1688 }
1689  
1690 #
1691 ## GMPq
1692 #
1693 Math_GMPq__Math_GMPq: {
1694 return Math::GMPq::Rmpq_equal($x, $y);
1695 }
1696  
1697 Math_GMPq__Math_GMPz: {
1698 return (Math::GMPq::Rmpq_integer_p($x) and Math::GMPq::Rmpq_cmp_z($x, $y) == 0);
1699 }
1700  
1701 Math_GMPq__Math_MPFR: {
1702 return (Math::MPFR::Rmpfr_number_p($y) and Math::MPFR::Rmpfr_cmp_q($y, $x) == 0);
1703 }
1704  
1705 Math_GMPq__Math_MPC: {
1706 $x = _mpq2mpc($x);
1707 goto Math_MPC__Math_MPC;
1708 }
1709  
1710 Math_GMPq__Scalar: {
1711 return (
1712 Math::GMPq::Rmpq_integer_p($x)
1713 and (
1714 ($y || return !Math::GMPq::Rmpq_sgn($x)) < 0
1715 ? Math::GMPq::Rmpq_cmp_si($x, $y, 1)
1716 : Math::GMPq::Rmpq_cmp_ui($x, $y, 1)
1717 ) == 0
1718 );
1719 }
1720  
1721 #
1722 ## GMPz
1723 #
1724 Math_GMPz__Math_GMPz: {
1725 return (Math::GMPz::Rmpz_cmp($x, $y) == 0);
1726 }
1727  
1728 Math_GMPz__Math_GMPq: {
1729 return (Math::GMPq::Rmpq_integer_p($y) and Math::GMPq::Rmpq_cmp_z($y, $x) == 0);
1730 }
1731  
1732 Math_GMPz__Math_MPFR: {
1733 return (Math::MPFR::Rmpfr_integer_p($y) and Math::MPFR::Rmpfr_cmp_z($y, $x) == 0);
1734 }
1735  
1736 Math_GMPz__Math_MPC: {
1737 $x = _mpz2mpc($x);
1738 goto Math_MPC__Math_MPC;
1739 }
1740  
1741 Math_GMPz__Scalar: {
1742 return (
1743 (
1744 ($y || return !Math::GMPz::Rmpz_sgn($x)) < 0
1745 ? Math::GMPz::Rmpz_cmp_si($x, $y)
1746 : Math::GMPz::Rmpz_cmp_ui($x, $y)
1747 ) == 0
1748 );
1749 }
1750  
1751 #
1752 ## MPC
1753 #
1754 Math_MPC__Math_MPC: {
1755  
1756 my $f1 = Math::MPFR::Rmpfr_init2($PREC);
1757 my $f2 = Math::MPFR::Rmpfr_init2($PREC);
1758  
1759 Math::MPC::RMPC_RE($f1, $x);
1760 Math::MPC::RMPC_RE($f2, $y);
1761  
1762 Math::MPFR::Rmpfr_equal_p($f1, $f2) || return 0;
1763  
1764 Math::MPC::RMPC_IM($f1, $x);
1765 Math::MPC::RMPC_IM($f2, $y);
1766  
1767 return Math::MPFR::Rmpfr_equal_p($f1, $f2);
1768 }
1769  
1770 Math_MPC__Math_GMPz: {
1771 $y = _mpz2mpc($y);
1772 goto Math_MPC__Math_MPC;
1773 }
1774  
1775 Math_MPC__Math_GMPq: {
1776 $y = _mpq2mpc($y);
1777 goto Math_MPC__Math_MPC;
1778 }
1779  
1780 Math_MPC__Math_MPFR: {
1781 $y = _mpfr2mpc($y);
1782 goto Math_MPC__Math_MPC;
1783 }
1784  
1785 Math_MPC__Scalar: {
1786 my $f = Math::MPFR::Rmpfr_init2($PREC);
1787 Math::MPC::RMPC_IM($f, $x);
1788 Math::MPFR::Rmpfr_zero_p($f) || return 0;
1789 Math::MPC::RMPC_RE($f, $x);
1790 $x = $f;
1791 goto Math_MPFR__Scalar;
1792 }
1793 }
1794  
1795 sub eq { # used in overloading
1796 my ($x, $y) = @_;
1797  
1798 if (ref($y) eq __PACKAGE__) {
1799 @_ = ($$x, $$y);
1800 goto &__eq__;
1801 }
1802  
1803 if (!ref($y)) {
1804 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
1805 @_ = ($$x, $y);
1806 }
1807 else {
1808 @_ = ($$x, _str2obj($y));
1809 }
1810 goto &__eq__;
1811 }
1812  
1813 @_ = ($$x, _star2obj($y));
1814 goto &__eq__;
1815 }
1816  
1817 #
1818 ## INEQUALITY
1819 #
1820  
1821 sub __ne__ {
1822 my ($x, $y) = @_;
1823 goto(join('__', ref($x), ref($y) || 'Scalar') =~ tr/:/_/rs);
1824  
1825 #
1826 ## MPFR
1827 #
1828 Math_MPFR__Math_MPFR: {
1829 return !Math::MPFR::Rmpfr_equal_p($x, $y);
1830 }
1831  
1832 Math_MPFR__Math_GMPz: {
1833 return (!Math::MPFR::Rmpfr_integer_p($x) or Math::MPFR::Rmpfr_cmp_z($x, $y) != 0);
1834 }
1835  
1836 Math_MPFR__Math_GMPq: {
1837 return (!Math::MPFR::Rmpfr_number_p($x) or Math::MPFR::Rmpfr_cmp_q($x, $y) != 0);
1838 }
1839  
1840 Math_MPFR__Math_MPC: {
1841 $x = _mpfr2mpc($x);
1842 goto Math_MPC__Math_MPC;
1843 }
1844  
1845 Math_MPFR__Scalar: {
1846 return (
1847 !Math::MPFR::Rmpfr_integer_p($x)
1848 or (
1849 ($y || return !!Math::MPFR::Rmpfr_sgn($x)) < 0
1850 ? Math::MPFR::Rmpfr_cmp_si($x, $y)
1851 : Math::MPFR::Rmpfr_cmp_ui($x, $y)
1852 ) != 0
1853 );
1854 }
1855  
1856 #
1857 ## GMPq
1858 #
1859 Math_GMPq__Math_GMPq: {
1860 return !Math::GMPq::Rmpq_equal($x, $y);
1861 }
1862  
1863 Math_GMPq__Math_GMPz: {
1864 return (!Math::GMPq::Rmpq_integer_p($x) or Math::GMPq::Rmpq_cmp_z($x, $y) != 0);
1865 }
1866  
1867 Math_GMPq__Math_MPFR: {
1868 return (!Math::MPFR::Rmpfr_number_p($y) or Math::MPFR::Rmpfr_cmp_q($y, $x) != 0);
1869 }
1870  
1871 Math_GMPq__Math_MPC: {
1872 $x = _mpq2mpc($x);
1873 goto Math_MPC__Math_MPC;
1874 }
1875  
1876 Math_GMPq__Scalar: {
1877 return (
1878 !Math::GMPq::Rmpq_integer_p($x)
1879 or (
1880 ($y || return !!Math::GMPq::Rmpq_sgn($x)) < 0
1881 ? Math::GMPq::Rmpq_cmp_si($x, $y, 1)
1882 : Math::GMPq::Rmpq_cmp_ui($x, $y, 1)
1883 ) != 0
1884 );
1885 }
1886  
1887 #
1888 ## GMPz
1889 #
1890 Math_GMPz__Math_GMPz: {
1891 return (Math::GMPz::Rmpz_cmp($x, $y) != 0);
1892 }
1893  
1894 Math_GMPz__Math_GMPq: {
1895 return (!Math::GMPq::Rmpq_integer_p($y) or Math::GMPq::Rmpq_cmp_z($y, $x) != 0);
1896 }
1897  
1898 Math_GMPz__Math_MPFR: {
1899 return (!Math::MPFR::Rmpfr_integer_p($y) or Math::MPFR::Rmpfr_cmp_z($y, $x) != 0);
1900 }
1901  
1902 Math_GMPz__Math_MPC: {
1903 $x = _mpz2mpc($x);
1904 goto Math_MPC__Math_MPC;
1905 }
1906  
1907 Math_GMPz__Scalar: {
1908 return (
1909 (
1910 ($y || return !!Math::GMPz::Rmpz_sgn($x)) < 0
1911 ? Math::GMPz::Rmpz_cmp_si($x, $y)
1912 : Math::GMPz::Rmpz_cmp_ui($x, $y)
1913 ) != 0
1914 );
1915 }
1916  
1917 #
1918 ## MPC
1919 #
1920 Math_MPC__Math_MPC: {
1921  
1922 my $f1 = Math::MPFR::Rmpfr_init2($PREC);
1923 my $f2 = Math::MPFR::Rmpfr_init2($PREC);
1924  
1925 Math::MPC::RMPC_RE($f1, $x);
1926 Math::MPC::RMPC_RE($f2, $y);
1927  
1928 Math::MPFR::Rmpfr_equal_p($f1, $f2) || return 1;
1929  
1930 Math::MPC::RMPC_IM($f1, $x);
1931 Math::MPC::RMPC_IM($f2, $y);
1932  
1933 return !Math::MPFR::Rmpfr_equal_p($f1, $f2);
1934 }
1935  
1936 Math_MPC__Math_GMPz: {
1937 $y = _mpz2mpc($y);
1938 goto Math_MPC__Math_MPC;
1939 }
1940  
1941 Math_MPC__Math_GMPq: {
1942 $y = _mpq2mpc($y);
1943 goto Math_MPC__Math_MPC;
1944 }
1945  
1946 Math_MPC__Math_MPFR: {
1947 $y = _mpfr2mpc($y);
1948 goto Math_MPC__Math_MPC;
1949 }
1950  
1951 Math_MPC__Scalar: {
1952 my $f = Math::MPFR::Rmpfr_init2($PREC);
1953 Math::MPC::RMPC_IM($f, $x);
1954 Math::MPFR::Rmpfr_zero_p($f) || return 1;
1955 Math::MPC::RMPC_RE($f, $x);
1956 $x = $f;
1957 goto Math_MPFR__Scalar;
1958 }
1959 }
1960  
1961 sub ne { # used in overloading
1962 my ($x, $y) = @_;
1963  
1964 if (ref($y) eq __PACKAGE__) {
1965 @_ = ($$x, $$y);
1966 goto &__ne__;
1967 }
1968  
1969 if (!ref($y)) {
1970 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
1971 @_ = ($$x, $y);
1972 }
1973 else {
1974 @_ = ($$x, _str2obj($y));
1975 }
1976 goto &__ne__;
1977 }
1978  
1979 @_ = ($$x, _star2obj($y));
1980 goto &__ne__;
1981 }
1982  
1983 #
1984 ## COMPARISON
1985 #
1986  
1987 sub __cmp__ {
1988 my ($x, $y) = @_;
1989 goto(join('__', ref($x), ref($y) || 'Scalar') =~ tr/:/_/rs);
1990  
1991 #
1992 ## MPFR
1993 #
1994 Math_MPFR__Math_MPFR: {
1995  
1996 if ( Math::MPFR::Rmpfr_nan_p($x)
1997 or Math::MPFR::Rmpfr_nan_p($y)) {
1998 return undef;
1999 }
2000  
2001 return Math::MPFR::Rmpfr_cmp($x, $y);
2002 }
2003  
2004 Math_MPFR__Math_GMPz: {
2005 return (
2006 Math::MPFR::Rmpfr_nan_p($x)
2007 ? undef
2008 : Math::MPFR::Rmpfr_cmp_z($x, $y)
2009 );
2010 }
2011  
2012 Math_MPFR__Math_GMPq: {
2013 return (
2014 Math::MPFR::Rmpfr_nan_p($x)
2015 ? undef
2016 : Math::MPFR::Rmpfr_cmp_q($x, $y)
2017 );
2018 }
2019  
2020 Math_MPFR__Math_MPC: {
2021 $x = _mpfr2mpc($x);
2022 goto Math_MPC__Math_MPC;
2023 }
2024  
2025 Math_MPFR__Scalar: {
2026 return (
2027 Math::MPFR::Rmpfr_nan_p($x) ? undef
2028 : ($y || return Math::MPFR::Rmpfr_sgn($x)) < 0 ? Math::MPFR::Rmpfr_cmp_si($x, $y)
2029 : Math::MPFR::Rmpfr_cmp_ui($x, $y)
2030 );
2031 }
2032  
2033 #
2034 ## GMPq
2035 #
2036 Math_GMPq__Math_GMPq: {
2037 return Math::GMPq::Rmpq_cmp($x, $y);
2038 }
2039  
2040 Math_GMPq__Math_GMPz: {
2041 return Math::GMPq::Rmpq_cmp_z($x, $y);
2042 }
2043  
2044 Math_GMPq__Math_MPFR: {
2045 return (
2046 Math::MPFR::Rmpfr_nan_p($y)
2047 ? undef
2048 : -Math::MPFR::Rmpfr_cmp_q($y, $x)
2049 );
2050 }
2051  
2052 Math_GMPq__Math_MPC: {
2053 $x = _mpq2mpc($x);
2054 goto Math_MPC__Math_MPC;
2055 }
2056  
2057 Math_GMPq__Scalar: {
2058 return (
2059 ($y || return Math::GMPq::Rmpq_sgn($x)) < 0
2060 ? Math::GMPq::Rmpq_cmp_si($x, $y, 1)
2061 : Math::GMPq::Rmpq_cmp_ui($x, $y, 1)
2062 );
2063 }
2064  
2065 #
2066 ## GMPz
2067 #
2068 Math_GMPz__Math_GMPz: {
2069 return Math::GMPz::Rmpz_cmp($x, $y);
2070 }
2071  
2072 Math_GMPz__Math_GMPq: {
2073 return -Math::GMPq::Rmpq_cmp_z($y, $x);
2074 }
2075  
2076 Math_GMPz__Math_MPFR: {
2077 return (
2078 Math::MPFR::Rmpfr_nan_p($y)
2079 ? undef
2080 : -Math::MPFR::Rmpfr_cmp_z($y, $x)
2081 );
2082 }
2083  
2084 Math_GMPz__Math_MPC: {
2085 $x = _mpz2mpc($x);
2086 goto Math_MPC__Math_MPC;
2087 }
2088  
2089 Math_GMPz__Scalar: {
2090 return (
2091 ($y || return Math::GMPz::Rmpz_sgn($x)) < 0
2092 ? Math::GMPz::Rmpz_cmp_si($x, $y)
2093 : Math::GMPz::Rmpz_cmp_ui($x, $y)
2094 );
2095 }
2096  
2097 #
2098 ## MPC
2099 #
2100 Math_MPC__Math_MPC: {
2101 my $f = Math::MPFR::Rmpfr_init2($PREC);
2102  
2103 Math::MPC::RMPC_RE($f, $x);
2104 Math::MPFR::Rmpfr_nan_p($f) && return undef;
2105  
2106 Math::MPC::RMPC_RE($f, $y);
2107 Math::MPFR::Rmpfr_nan_p($f) && return undef;
2108  
2109 Math::MPC::RMPC_IM($f, $x);
2110 Math::MPFR::Rmpfr_nan_p($f) && return undef;
2111  
2112 Math::MPC::RMPC_IM($f, $y);
2113 Math::MPFR::Rmpfr_nan_p($f) && return undef;
2114  
2115 my $si = Math::MPC::Rmpc_cmp($x, $y);
2116 my $re_cmp = Math::MPC::RMPC_INEX_RE($si);
2117  
2118 return (
2119 ($re_cmp == 0)
2120 ? Math::MPC::RMPC_INEX_IM($si)
2121 : $re_cmp
2122 );
2123 }
2124  
2125 Math_MPC__Math_GMPz: {
2126 $y = _mpz2mpc($y);
2127 goto Math_MPC__Math_MPC;
2128 }
2129  
2130 Math_MPC__Math_GMPq: {
2131 $y = _mpq2mpc($y);
2132 goto Math_MPC__Math_MPC;
2133 }
2134  
2135 Math_MPC__Math_MPFR: {
2136 $y = _mpfr2mpc($y);
2137 goto Math_MPC__Math_MPC;
2138 }
2139  
2140 Math_MPC__Scalar: {
2141 $y = _any2mpc(_str2obj($y));
2142 goto Math_MPC__Math_MPC;
2143 }
2144 }
2145  
2146 sub cmp ($$) {
2147 my ($x, $y) = @_;
2148  
2149 if (ref($y) eq __PACKAGE__) {
2150 @_ = ($$x, $$y);
2151 goto &__cmp__;
2152 }
2153  
2154 if (!ref($y)) {
2155 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
2156 @_ = ($$x, $y);
2157 }
2158 else {
2159 @_ = ($$x, _str2obj($y));
2160 }
2161 goto &__cmp__;
2162 }
2163  
2164 @_ = ($$x, _star2obj($y));
2165 goto &__cmp__;
2166 }
2167  
2168 # Absolute comparison
2169  
2170 sub acmp ($$) {
2171 my ($x, $y) = @_;
2172  
2173 if (!ref($y) and CORE::int($y) eq $y and $y >= 0 and $y < ULONG_MAX) {
2174 ## `y` is a native unsigned integer
2175 }
2176 else {
2177 $y = __abs__(ref($y) eq __PACKAGE__ ? $$y : _star2obj($y));
2178 }
2179  
2180 __cmp__(__abs__(ref($x) eq __PACKAGE__ ? $$x : _star2obj($x)), $y);
2181 }
2182  
2183 # Approximate comparison
2184  
2185 sub approx_cmp ($$;$) {
2186 my ($x, $y, $places) = @_;
2187  
2188 if (defined($places)) {
2189 if (!ref($places) and CORE::int($places) eq $places and $places > LONG_MIN and $places < ULONG_MAX) {
2190 ## places is a native integer
2191 }
2192 else {
2193 $places = _any2si(_star2obj($places)) // return undef;
2194 }
2195 }
2196 else {
2197 $places = -(($PREC >> 2) - 1);
2198 }
2199  
2200 $x = _star2obj($x);
2201 $y = _star2obj($y);
2202  
2203 if ( ref($x) eq 'Math::MPFR'
2204 or ref($y) eq 'Math::MPFR'
2205 or ref($x) eq 'Math::MPC'
2206 or ref($y) eq 'Math::MPC') {
2207 $x = _star2mpfr_mpc($x);
2208 $y = _star2mpfr_mpc($y);
2209 }
2210  
2211 $x = __round__($x, $places);
2212 $y = __round__($y, $places);
2213  
2214 __cmp__($x, $y);
2215 }
2216  
2217 #
2218 ## GREATER THAN
2219 #
2220  
2221 sub gt { # used in overloading
2222 my ($x, $y) = @_;
2223  
2224 if (ref($y) eq __PACKAGE__) {
2225 return ((__cmp__($$x, $$y) // return undef) > 0);
2226 }
2227  
2228 if (!ref($y)) {
2229 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
2230 return ((__cmp__($$x, $y) // return undef) > 0);
2231 }
2232 return ((__cmp__($$x, _str2obj($y)) // return undef) > 0);
2233 }
2234  
2235 (__cmp__($$x, _star2obj($y)) // return undef) > 0;
2236 }
2237  
2238 #
2239 ## EQUAL OR GREATER THAN
2240 #
2241  
2242 sub ge { # used in overloading
2243 my ($x, $y) = @_;
2244  
2245 if (ref($y) eq __PACKAGE__) {
2246 return ((__cmp__($$x, $$y) // return undef) >= 0);
2247 }
2248  
2249 if (!ref($y)) {
2250 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
2251 return ((__cmp__($$x, $y) // return undef) >= 0);
2252 }
2253 return ((__cmp__($$x, _str2obj($y)) // return undef) >= 0);
2254 }
2255  
2256 (__cmp__($$x, _star2obj($y)) // return undef) >= 0;
2257 }
2258  
2259 #
2260 ## LESS THAN
2261 #
2262  
2263 sub lt { # used in overloading
2264 my ($x, $y) = @_;
2265  
2266 if (ref($y) eq __PACKAGE__) {
2267 return ((__cmp__($$x, $$y) // return undef) < 0);
2268 }
2269  
2270 if (!ref($y)) {
2271 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
2272 return ((__cmp__($$x, $y) // return undef) < 0);
2273 }
2274 return ((__cmp__($$x, _str2obj($y)) // return undef) < 0);
2275 }
2276  
2277 (__cmp__($$x, _star2obj($y)) // return undef) < 0;
2278 }
2279  
2280 #
2281 ## EQUAL OR LESS THAN
2282 #
2283  
2284 sub le { # used in overloading
2285 my ($x, $y) = @_;
2286  
2287 if (ref($y) eq __PACKAGE__) {
2288 return ((__cmp__($$x, $$y) // return undef) <= 0);
2289 }
2290  
2291 if (!ref($y)) {
2292 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
2293 return ((__cmp__($$x, $y) // return undef) <= 0);
2294 }
2295 return ((__cmp__($$x, _str2obj($y)) // return undef) <= 0);
2296 }
2297  
2298 (__cmp__($$x, _star2obj($y)) // return undef) <= 0;
2299 }
2300  
2301 #
2302 ## COPY
2303 #
2304  
2305 sub _copy {
2306 my ($x) = @_;
2307 my $ref = ref($x);
2308  
2309 if ($ref eq 'Math::GMPz') {
2310 Math::GMPz::Rmpz_init_set($x);
2311 }
2312 elsif ($ref eq 'Math::MPFR') {
2313 my $r = Math::MPFR::Rmpfr_init2($PREC);
2314 Math::MPFR::Rmpfr_set($r, $x, $ROUND);
2315 $r;
2316 }
2317 elsif ($ref eq 'Math::GMPq') {
2318 my $r = Math::GMPq::Rmpq_init();
2319 Math::GMPq::Rmpq_set($r, $x);
2320 $r;
2321 }
2322 elsif ($ref eq 'Math::MPC') {
2323 my $r = Math::MPC::Rmpc_init2($PREC);
2324 Math::MPC::Rmpc_set($r, $x, $ROUND);
2325 $r;
2326 }
2327 else {
2328 _str2obj("$x"); # this should not happen
2329 }
2330 }
2331  
2332 sub copy ($) {
2333 my ($x) = @_;
2334 bless \_copy($$x);
2335 }
2336  
2337 #
2338 ## CONVERSION TO INTEGER
2339 #
2340  
2341 sub int { # used in overloading
2342 my ($x) = @_;
2343  
2344 bless \(
2345 (
2346 ref($x) eq __PACKAGE__
2347 ? ref($$x) eq 'Math::GMPz'
2348 ? (return $x)
2349 : _any2mpz($$x)
2350 : _star2mpz($x)
2351 ) // goto &nan
2352 );
2353 }
2354  
2355 #
2356 ## CONVERSION TO RATIONAL
2357 #
2358  
2359 sub rat ($) {
2360 my ($x) = @_;
2361  
2362 if (ref($x) eq __PACKAGE__) {
2363 ref($$x) eq 'Math::GMPq' && return $x;
2364 return bless \(_any2mpq($$x) // (goto &nan));
2365 }
2366  
2367 # Parse a decimal number as an exact fraction
2368 if ("$x" =~ /^([+-]?+(?=\.?[0-9])[0-9_]*+(?:\.[0-9_]++)?(?:[Ee](?:[+-]?+[0-9_]+))?)\z/) {
2369 my $frac = _str2frac(lc($1) =~ tr/_//dr);
2370 my $q = Math::GMPq::Rmpq_init();
2371 Math::GMPq::Rmpq_set_str($q, $frac, 10);
2372 Math::GMPq::Rmpq_canonicalize($q) if (index($frac, '/') != -1);
2373 return bless \$q;
2374 }
2375  
2376 my $r = _star2obj($x);
2377 ref($r) eq 'Math::GMPq' && return bless \$r;
2378 bless \(_any2mpq($r) // (goto &nan));
2379 }
2380  
2381 #
2382 ## CONVERSION TO FLOATING-POINT
2383 #
2384  
2385 sub float ($) {
2386 my ($x) = @_;
2387  
2388 ref($x) eq __PACKAGE__
2389 && (ref($$x) eq 'Math::MPFR' || ref($$x) eq 'Math::MPC')
2390 && return $x;
2391  
2392 bless \_star2mpfr_mpc($x);
2393 }
2394  
2395 #
2396 ## CONVERSION TO COMPLEX
2397 #
2398  
2399 sub complex ($;$) {
2400 my ($x, $y) = @_;
2401  
2402 if (defined $y) {
2403 return bless \_reals2mpc($x, $y);
2404 }
2405  
2406 bless \(
2407 ref($x) eq __PACKAGE__
2408 ? ref($$x) eq 'Math::MPC'
2409 ? (return $x)
2410 : _any2mpc($$x)
2411 : _any2mpc(_star2obj($x))
2412 );
2413 }
2414  
2415 #
2416 ## ABSOLUTE VALUE
2417 #
2418  
2419 sub __abs__ {
2420 my ($x) = @_;
2421 goto(ref($x) =~ tr/:/_/rs);
2422  
2423 Math_MPFR: {
2424 Math::MPFR::Rmpfr_sgn($x) >= 0 and return $x;
2425 my $r = Math::MPFR::Rmpfr_init2($PREC);
2426 Math::MPFR::Rmpfr_abs($r, $x, $ROUND);
2427 return $r;
2428 }
2429  
2430 Math_GMPq: {
2431 Math::GMPq::Rmpq_sgn($x) >= 0 and return $x;
2432 my $r = Math::GMPq::Rmpq_init();
2433 Math::GMPq::Rmpq_abs($r, $x);
2434 return $r;
2435 }
2436  
2437 Math_GMPz: {
2438 Math::GMPz::Rmpz_sgn($x) >= 0 and return $x;
2439 my $r = Math::GMPz::Rmpz_init();
2440 Math::GMPz::Rmpz_abs($r, $x);
2441 return $r;
2442 }
2443  
2444 Math_MPC: {
2445 my $r = Math::MPFR::Rmpfr_init2($PREC);
2446 Math::MPC::Rmpc_abs($r, $x, $ROUND);
2447 return $r;
2448 }
2449 }
2450  
2451 sub abs { # used in overloading
2452 my ($x) = @_;
2453 bless \__abs__(ref($x) eq __PACKAGE__ ? $$x : _star2obj($x));
2454 }
2455  
2456 #
2457 ## ADDITIVE INVERSE (-x)
2458 #
2459  
2460 sub __neg__ {
2461 my ($x) = @_;
2462 goto(ref($x) =~ tr/:/_/rs);
2463  
2464 Math_MPFR: {
2465 my $r = Math::MPFR::Rmpfr_init2($PREC);
2466 Math::MPFR::Rmpfr_neg($r, $x, $ROUND);
2467 return $r;
2468 }
2469  
2470 Math_GMPq: {
2471 my $r = Math::GMPq::Rmpq_init();
2472 Math::GMPq::Rmpq_neg($r, $x);
2473 return $r;
2474 }
2475  
2476 Math_GMPz: {
2477 my $r = Math::GMPz::Rmpz_init();
2478 Math::GMPz::Rmpz_neg($r, $x);
2479 return $r;
2480 }
2481  
2482 Math_MPC: {
2483 my $r = Math::MPC::Rmpc_init2($PREC);
2484 Math::MPC::Rmpc_neg($r, $x, $ROUND);
2485 return $r;
2486 }
2487 }
2488  
2489 sub neg { # used in overloading
2490 my ($x) = @_;
2491 bless \__neg__(ref($x) eq __PACKAGE__ ? $$x : _star2obj($x));
2492 }
2493  
2494 #
2495 ## MULTIPLICATIVE INVERSE (1/x)
2496 #
2497  
2498 sub __inv__ {
2499 my ($x) = @_;
2500 goto(ref($x) =~ tr/:/_/rs);
2501  
2502 Math_MPFR: {
2503 my $r = Math::MPFR::Rmpfr_init2($PREC);
2504 Math::MPFR::Rmpfr_ui_div($r, 1, $x, $ROUND);
2505 return $r;
2506 }
2507  
2508 Math_GMPq: {
2509  
2510 # Check for division by zero
2511 Math::GMPq::Rmpq_sgn($x) || do {
2512 $x = _mpq2mpfr($x);
2513 goto Math_MPFR;
2514 };
2515  
2516 my $r = Math::GMPq::Rmpq_init();
2517 Math::GMPq::Rmpq_inv($r, $x);
2518 return $r;
2519 }
2520  
2521 Math_GMPz: {
2522  
2523 # Check for division by zero
2524 Math::GMPz::Rmpz_sgn($x) || do {
2525 $x = _mpz2mpfr($x);
2526 goto Math_MPFR;
2527 };
2528  
2529 my $r = Math::GMPq::Rmpq_init();
2530 Math::GMPq::Rmpq_set_z($r, $x);
2531 Math::GMPq::Rmpq_inv($r, $r);
2532 return $r;
2533 }
2534  
2535 Math_MPC: {
2536 my $r = Math::MPC::Rmpc_init2($PREC);
2537 Math::MPC::Rmpc_ui_div($r, 1, $x, $ROUND);
2538 return $r;
2539 }
2540 }
2541  
2542 sub inv ($) {
2543 my ($x) = @_;
2544 bless \__inv__(ref($x) eq __PACKAGE__ ? $$x : _star2obj($x));
2545 }
2546  
2547 #
2548 ## INCREMENTATION BY ONE
2549 #
2550  
2551 sub __inc__ {
2552 my ($x) = @_;
2553 goto(ref($x) =~ tr/:/_/rs);
2554  
2555 Math_MPFR: {
2556 my $r = Math::MPFR::Rmpfr_init2($PREC);
2557 Math::MPFR::Rmpfr_add_ui($r, $x, 1, $ROUND);
2558 return $r;
2559 }
2560  
2561 Math_GMPq: {
2562 state $one = Math::GMPz::Rmpz_init_set_ui_nobless(1);
2563 my $r = Math::GMPq::Rmpq_init();
2564 Math::GMPq::Rmpq_add_z($r, $x, $one);
2565 return $r;
2566 }
2567  
2568 Math_GMPz: {
2569 my $r = Math::GMPz::Rmpz_init();
2570 Math::GMPz::Rmpz_add_ui($r, $x, 1);
2571 return $r;
2572 }
2573  
2574 Math_MPC: {
2575 my $r = Math::MPC::Rmpc_init2($PREC);
2576 Math::MPC::Rmpc_add_ui($r, $x, 1, $ROUND);
2577 return $r;
2578 }
2579 }
2580  
2581 sub inc ($) {
2582 my ($x) = @_;
2583 bless \__inc__($$x);
2584 }
2585  
2586 #
2587 ## DECREMENTATION BY ONE
2588 #
2589  
2590 sub __dec__ {
2591 my ($x) = @_;
2592 goto(ref($x) =~ tr/:/_/rs);
2593  
2594 Math_MPFR: {
2595 my $r = Math::MPFR::Rmpfr_init2($PREC);
2596 Math::MPFR::Rmpfr_sub_ui($r, $x, 1, $ROUND);
2597 return $r;
2598 }
2599  
2600 Math_GMPq: {
2601 state $mone = Math::GMPz::Rmpz_init_set_si_nobless(-1);
2602 my $r = Math::GMPq::Rmpq_init();
2603 Math::GMPq::Rmpq_add_z($r, $x, $mone);
2604 return $r;
2605 }
2606  
2607 Math_GMPz: {
2608 my $r = Math::GMPz::Rmpz_init();
2609 Math::GMPz::Rmpz_sub_ui($r, $x, 1);
2610 return $r;
2611 }
2612  
2613 Math_MPC: {
2614 my $r = Math::MPC::Rmpc_init2($PREC);
2615 Math::MPC::Rmpc_sub_ui($r, $x, 1, $ROUND);
2616 return $r;
2617 }
2618 }
2619  
2620 sub dec ($) {
2621 my ($x) = @_;
2622 bless \__dec__($$x);
2623 }
2624  
2625 sub conj ($) {
2626 my ($x) = @_;
2627  
2628 if (ref($x) ne __PACKAGE__) {
2629 $x = __PACKAGE__->new($x);
2630 }
2631  
2632 if (ref($$x) eq 'Math::MPC') {
2633 my $r = Math::MPC::Rmpc_init2($PREC);
2634 Math::MPC::Rmpc_conj($r, $$x, $ROUND);
2635 bless \$r;
2636 }
2637 else {
2638 $x;
2639 }
2640 }
2641  
2642 sub real ($) {
2643 my ($x) = @_;
2644  
2645 if (ref($x) ne __PACKAGE__) {
2646 $x = __PACKAGE__->new($x);
2647 }
2648  
2649 if (ref($$x) eq 'Math::MPC') {
2650 my $r = Math::MPFR::Rmpfr_init2($PREC);
2651 Math::MPC::RMPC_RE($r, $$x);
2652 bless \$r;
2653 }
2654 else {
2655 $x;
2656 }
2657 }
2658  
2659 sub imag ($) {
2660 my ($x) = @_;
2661  
2662 if (ref($x) ne __PACKAGE__) {
2663 $x = __PACKAGE__->new($x);
2664 }
2665  
2666 if (ref($$x) eq 'Math::MPC') {
2667 my $r = Math::MPFR::Rmpfr_init2($PREC);
2668 Math::MPC::RMPC_IM($r, $$x);
2669 bless \$r;
2670 }
2671 else {
2672 goto &zero;
2673 }
2674 }
2675  
2676 sub reals ($) {
2677 my ($x) = @_;
2678  
2679 if (ref($x) ne __PACKAGE__) {
2680 $x = __PACKAGE__->new($x);
2681 }
2682  
2683 ($x->real, $x->imag);
2684 }
2685  
2686 #
2687 ## ADDITION
2688 #
2689  
2690 sub __add__ {
2691 my ($x, $y) = @_;
2692 goto(join('__', ref($x), ref($y) || 'Scalar') =~ tr/:/_/rs);
2693  
2694 #
2695 ## GMPq
2696 #
2697 Math_GMPq__Math_GMPq: {
2698 my $r = Math::GMPq::Rmpq_init();
2699 Math::GMPq::Rmpq_add($r, $x, $y);
2700 return $r;
2701 }
2702  
2703 Math_GMPq__Math_GMPz: {
2704 my $r = Math::GMPq::Rmpq_init();
2705 Math::GMPq::Rmpq_add_z($r, $x, $y);
2706 return $r;
2707 }
2708  
2709 Math_GMPq__Math_MPFR: {
2710 my $r = Math::MPFR::Rmpfr_init2($PREC);
2711 Math::MPFR::Rmpfr_add_q($r, $y, $x, $ROUND);
2712 return $r;
2713 }
2714  
2715 Math_GMPq__Math_MPC: {
2716 my $c = Math::MPC::Rmpc_init2($PREC);
2717 Math::MPC::Rmpc_set_q($c, $x, $ROUND);
2718 Math::MPC::Rmpc_add($c, $c, $y, $ROUND);
2719 return $c;
2720 }
2721  
2722 Math_GMPq__Scalar: {
2723 my $r = Math::GMPq::Rmpq_init();
2724 $y < 0
2725 ? Math::GMPq::Rmpq_set_si($r, $y, 1)
2726 : Math::GMPq::Rmpq_set_ui($r, $y, 1);
2727 Math::GMPq::Rmpq_add($r, $r, $x);
2728 return $r;
2729 }
2730  
2731 #
2732 ## GMPz
2733 #
2734 Math_GMPz__Math_GMPz: {
2735 my $r = Math::GMPz::Rmpz_init();
2736 Math::GMPz::Rmpz_add($r, $x, $y);
2737 return $r;
2738 }
2739  
2740 Math_GMPz__Scalar: {
2741 my $r = Math::GMPz::Rmpz_init();
2742 $y < 0
2743 ? Math::GMPz::Rmpz_sub_ui($r, $x, -$y)
2744 : Math::GMPz::Rmpz_add_ui($r, $x, $y);
2745 return $r;
2746 }
2747  
2748 Math_GMPz__Math_GMPq: {
2749 my $r = Math::GMPq::Rmpq_init();
2750 Math::GMPq::Rmpq_add_z($r, $y, $x);
2751 return $r;
2752 }
2753  
2754 Math_GMPz__Math_MPFR: {
2755 my $f = Math::MPFR::Rmpfr_init2($PREC);
2756 Math::MPFR::Rmpfr_add_z($f, $y, $x, $ROUND);
2757 return $f;
2758 }
2759  
2760 Math_GMPz__Math_MPC: {
2761 my $c = Math::MPC::Rmpc_init2($PREC);
2762 Math::MPC::Rmpc_set_z($c, $x, $ROUND);
2763 Math::MPC::Rmpc_add($c, $c, $y, $ROUND);
2764 return $c;
2765 }
2766  
2767 #
2768 ## MPFR
2769 #
2770 Math_MPFR__Math_MPFR: {
2771 my $r = Math::MPFR::Rmpfr_init2($PREC);
2772 Math::MPFR::Rmpfr_add($r, $x, $y, $ROUND);
2773 return $r;
2774 }
2775  
2776 Math_MPFR__Scalar: {
2777 my $r = Math::MPFR::Rmpfr_init2($PREC);
2778 $y < 0
2779 ? Math::MPFR::Rmpfr_sub_ui($r, $x, -$y, $ROUND)
2780 : Math::MPFR::Rmpfr_add_ui($r, $x, $y, $ROUND);
2781 return $r;
2782 }
2783  
2784 Math_MPFR__Math_GMPq: {
2785 my $r = Math::MPFR::Rmpfr_init2($PREC);
2786 Math::MPFR::Rmpfr_add_q($r, $x, $y, $ROUND);
2787 return $r;
2788 }
2789  
2790 Math_MPFR__Math_GMPz: {
2791 my $r = Math::MPFR::Rmpfr_init2($PREC);
2792 Math::MPFR::Rmpfr_add_z($r, $x, $y, $ROUND);
2793 return $r;
2794 }
2795  
2796 Math_MPFR__Math_MPC: {
2797 my $c = Math::MPC::Rmpc_init2($PREC);
2798 Math::MPC::Rmpc_add_fr($c, $y, $x, $ROUND);
2799 return $c;
2800 }
2801  
2802 #
2803 ## MPC
2804 #
2805 Math_MPC__Math_MPC: {
2806 my $r = Math::MPC::Rmpc_init2($PREC);
2807 Math::MPC::Rmpc_add($r, $x, $y, $ROUND);
2808 return $r;
2809 }
2810  
2811 Math_MPC__Scalar: {
2812 my $r = Math::MPC::Rmpc_init2($PREC);
2813 $y < 0
2814 ? Math::MPC::Rmpc_sub_ui($r, $x, -$y, $ROUND)
2815 : Math::MPC::Rmpc_add_ui($r, $x, $y, $ROUND);
2816 return $r;
2817 }
2818  
2819 Math_MPC__Math_MPFR: {
2820 my $r = Math::MPC::Rmpc_init2($PREC);
2821 Math::MPC::Rmpc_add_fr($r, $x, $y, $ROUND);
2822 return $r;
2823 }
2824  
2825 Math_MPC__Math_GMPz: {
2826 my $r = Math::MPC::Rmpc_init2($PREC);
2827 Math::MPC::Rmpc_set_z($r, $y, $ROUND);
2828 Math::MPC::Rmpc_add($r, $r, $x, $ROUND);
2829 return $r;
2830 }
2831  
2832 Math_MPC__Math_GMPq: {
2833 my $r = Math::MPC::Rmpc_init2($PREC);
2834 Math::MPC::Rmpc_set_q($r, $y, $ROUND);
2835 Math::MPC::Rmpc_add($r, $r, $x, $ROUND);
2836 return $r;
2837 }
2838 }
2839  
2840 sub add { # used in overloading
2841 my ($x, $y) = @_;
2842  
2843 if (ref($y) eq __PACKAGE__) {
2844 return bless \__add__($$x, $$y);
2845 }
2846  
2847 $x = $$x;
2848  
2849 if (!ref($y)) {
2850 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
2851 return bless \__add__($x, $y);
2852 }
2853  
2854 return bless \__add__($x, _str2obj($y));
2855 }
2856  
2857 bless \__add__($x, _star2obj($y));
2858 }
2859  
2860 #
2861 ## SUBTRACTION
2862 #
2863  
2864 sub __sub__ {
2865 my ($x, $y) = @_;
2866 goto(join('__', ref($x) || 'Scalar', ref($y) || 'Scalar') =~ tr/:/_/rs);
2867  
2868 #
2869 ## GMPq
2870 #
2871 Math_GMPq__Math_GMPq: {
2872 my $r = Math::GMPq::Rmpq_init();
2873 Math::GMPq::Rmpq_sub($r, $x, $y);
2874 return $r;
2875 }
2876  
2877 Math_GMPq__Math_GMPz: {
2878 my $r = Math::GMPq::Rmpq_init();
2879 Math::GMPq::Rmpq_sub_z($r, $x, $y);
2880 return $r;
2881 }
2882  
2883 Math_GMPq__Math_MPFR: {
2884 my $r = Math::MPFR::Rmpfr_init2($PREC);
2885 Math::MPFR::Rmpfr_sub_q($r, $y, $x, $ROUND);
2886 Math::MPFR::Rmpfr_neg($r, $r, $ROUND);
2887 return $r;
2888 }
2889  
2890 Math_GMPq__Math_MPC: {
2891 my $r = Math::MPC::Rmpc_init2($PREC);
2892 Math::MPC::Rmpc_set_q($r, $x, $ROUND);
2893 Math::MPC::Rmpc_sub($r, $r, $y, $ROUND);
2894 return $r;
2895 }
2896  
2897 Math_GMPq__Scalar: {
2898 my $r = Math::GMPq::Rmpq_init();
2899 $y < 0
2900 ? Math::GMPq::Rmpq_set_si($r, $y, 1)
2901 : Math::GMPq::Rmpq_set_ui($r, $y, 1);
2902 Math::GMPq::Rmpq_sub($r, $x, $r);
2903 return $r;
2904 }
2905  
2906 Scalar__Math_GMPq: {
2907 my $r = Math::GMPq::Rmpq_init();
2908 $x < 0
2909 ? Math::GMPq::Rmpq_set_si($r, $x, 1)
2910 : Math::GMPq::Rmpq_set_ui($r, $x, 1);
2911 Math::GMPq::Rmpq_sub($r, $r, $y);
2912 return $r;
2913 }
2914  
2915 #
2916 ## GMPz
2917 #
2918 Math_GMPz__Math_GMPz: {
2919 my $r = Math::GMPz::Rmpz_init();
2920 Math::GMPz::Rmpz_sub($r, $x, $y);
2921 return $r;
2922 }
2923  
2924 Math_GMPz__Scalar: {
2925 my $r = Math::GMPz::Rmpz_init();
2926 $y < 0
2927 ? Math::GMPz::Rmpz_add_ui($r, $x, -$y)
2928 : Math::GMPz::Rmpz_sub_ui($r, $x, $y);
2929 return $r;
2930 }
2931  
2932 Scalar__Math_GMPz: {
2933 my $r = Math::GMPz::Rmpz_init();
2934 $x < 0
2935 ? do {
2936 Math::GMPz::Rmpz_add_ui($r, $y, -$x);
2937 Math::GMPz::Rmpz_neg($r, $r);
2938 }
2939 : Math::GMPz::Rmpz_ui_sub($r, $x, $y);
2940 return $r;
2941 }
2942  
2943 Math_GMPz__Math_GMPq: {
2944 my $r = Math::GMPq::Rmpq_init();
2945 Math::GMPq::Rmpq_z_sub($r, $x, $y);
2946 return $r;
2947 }
2948  
2949 Math_GMPz__Math_MPFR: {
2950 my $r = Math::MPFR::Rmpfr_init2($PREC);
2951  
2952 #<<<
2953 state $has_z_sub = (Math::MPFR::MPFR_VERSION_MAJOR() > 3)
2954 || (Math::MPFR::MPFR_VERSION_MAJOR() == 3
2955 && Math::MPFR::MPFR_VERSION_MINOR() >= 1);
2956 #>>>
2957  
2958 $has_z_sub
2959 ? Math::MPFR::Rmpfr_z_sub($r, $x, $y, $ROUND)
2960 : do {
2961 Math::MPFR::Rmpfr_sub_z($r, $y, $x, $ROUND);
2962 Math::MPFR::Rmpfr_neg($r, $r, $ROUND);
2963 };
2964  
2965 return $r;
2966 }
2967  
2968 Math_GMPz__Math_MPC: {
2969 my $r = Math::MPC::Rmpc_init2($PREC);
2970 Math::MPC::Rmpc_set_z($r, $x, $ROUND);
2971 Math::MPC::Rmpc_sub($r, $r, $y, $ROUND);
2972 return $r;
2973 }
2974  
2975 #
2976 ## MPFR
2977 #
2978 Math_MPFR__Math_MPFR: {
2979 my $r = Math::MPFR::Rmpfr_init2($PREC);
2980 Math::MPFR::Rmpfr_sub($r, $x, $y, $ROUND);
2981 return $r;
2982 }
2983  
2984 Math_MPFR__Scalar: {
2985 my $r = Math::MPFR::Rmpfr_init2($PREC);
2986 $y < 0
2987 ? Math::MPFR::Rmpfr_add_ui($r, $x, -$y, $ROUND)
2988 : Math::MPFR::Rmpfr_sub_ui($r, $x, $y, $ROUND);
2989 return $r;
2990 }
2991  
2992 Scalar__Math_MPFR: {
2993 my $r = Math::MPFR::Rmpfr_init2($PREC);
2994 $x < 0
2995 ? Math::MPFR::Rmpfr_si_sub($r, $x, $y, $ROUND)
2996 : Math::MPFR::Rmpfr_ui_sub($r, $x, $y, $ROUND);
2997 return $r;
2998 }
2999  
3000 Math_MPFR__Math_GMPq: {
3001 my $r = Math::MPFR::Rmpfr_init2($PREC);
3002 Math::MPFR::Rmpfr_sub_q($r, $x, $y, $ROUND);
3003 return $r;
3004 }
3005  
3006 Math_MPFR__Math_GMPz: {
3007 my $r = Math::MPFR::Rmpfr_init2($PREC);
3008 Math::MPFR::Rmpfr_sub_z($r, $x, $y, $ROUND);
3009 return $r;
3010 }
3011  
3012 Math_MPFR__Math_MPC: {
3013 my $r = Math::MPC::Rmpc_init2($PREC);
3014 Math::MPC::Rmpc_set_fr($r, $x, $ROUND);
3015 Math::MPC::Rmpc_sub($r, $r, $y, $ROUND);
3016 return $r;
3017 }
3018  
3019 #
3020 ## MPC
3021 #
3022 Math_MPC__Math_MPC: {
3023 my $r = Math::MPC::Rmpc_init2($PREC);
3024 Math::MPC::Rmpc_sub($r, $x, $y, $ROUND);
3025 return $r;
3026 }
3027  
3028 Math_MPC__Scalar: {
3029 my $r = Math::MPC::Rmpc_init2($PREC);
3030 $y < 0
3031 ? Math::MPC::Rmpc_add_ui($r, $x, -$y, $ROUND)
3032 : Math::MPC::Rmpc_sub_ui($r, $x, $y, $ROUND);
3033 return $r;
3034 }
3035  
3036 Scalar__Math_MPC: {
3037 my $r = Math::MPC::Rmpc_init2($PREC);
3038 $x < 0
3039 ? do {
3040 Math::MPC::Rmpc_add_ui($r, $y, -$x, $ROUND);
3041 Math::MPC::Rmpc_neg($r, $r, $ROUND);
3042 }
3043 : Math::MPC::Rmpc_ui_sub($r, $x, $y, $ROUND);
3044 return $r;
3045 }
3046  
3047 Math_MPC__Math_MPFR: {
3048 my $r = Math::MPC::Rmpc_init2($PREC);
3049 Math::MPC::Rmpc_set_fr($r, $y, $ROUND);
3050 Math::MPC::Rmpc_sub($r, $x, $r, $ROUND);
3051 return $r;
3052 }
3053  
3054 Math_MPC__Math_GMPz: {
3055 my $r = Math::MPC::Rmpc_init2($PREC);
3056 Math::MPC::Rmpc_set_z($r, $y, $ROUND);
3057 Math::MPC::Rmpc_sub($r, $x, $r, $ROUND);
3058 return $r;
3059 }
3060  
3061 Math_MPC__Math_GMPq: {
3062 my $r = Math::MPC::Rmpc_init2($PREC);
3063 Math::MPC::Rmpc_set_q($r, $y, $ROUND);
3064 Math::MPC::Rmpc_sub($r, $x, $r, $ROUND);
3065 return $r;
3066 }
3067 }
3068  
3069 sub sub { # used in overloading
3070 my ($x, $y) = @_;
3071  
3072 if (!ref($x) and CORE::int($x) eq $x and $x < ULONG_MAX and $x > LONG_MIN) {
3073  
3074 if (ref($y) eq __PACKAGE__) {
3075 return bless \__sub__($x, $$y);
3076 }
3077  
3078 return bless \__sub__($x, ref($y) ? _star2obj($y) : _str2obj($y));
3079 }
3080  
3081 $x =
3082 ref($x) eq __PACKAGE__ ? $$x
3083 : ref($x) ? _star2obj($x)
3084 : _str2obj($x);
3085  
3086 if (ref($y) eq __PACKAGE__) {
3087 return bless \__sub__($x, $$y);
3088 }
3089  
3090 if (!ref($y)) {
3091 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
3092 return bless \__sub__($x, $y);
3093 }
3094  
3095 return bless \__sub__($x, _str2obj($y));
3096 }
3097  
3098 bless \__sub__($x, _star2obj($y));
3099 }
3100  
3101 #
3102 ## MULTIPLICATION
3103 #
3104  
3105 sub __mul__ {
3106 my ($x, $y) = @_;
3107 goto(join('__', ref($x), ref($y) || 'Scalar') =~ tr/:/_/rs);
3108  
3109 #
3110 ## GMPq
3111 #
3112 Math_GMPq__Math_GMPq: {
3113 my $r = Math::GMPq::Rmpq_init();
3114 Math::GMPq::Rmpq_mul($r, $x, $y);
3115 return $r;
3116 }
3117  
3118 Math_GMPq__Math_GMPz: {
3119 my $r = Math::GMPq::Rmpq_init();
3120 Math::GMPq::Rmpq_mul_z($r, $x, $y);
3121 return $r;
3122 }
3123  
3124 Math_GMPq__Math_MPFR: {
3125 my $r = Math::MPFR::Rmpfr_init2($PREC);
3126 Math::MPFR::Rmpfr_mul_q($r, $y, $x, $ROUND);
3127 return $r;
3128 }
3129  
3130 Math_GMPq__Math_MPC: {
3131 my $r = Math::MPC::Rmpc_init2($PREC);
3132 Math::MPC::Rmpc_set_q($r, $x, $ROUND);
3133 Math::MPC::Rmpc_mul($r, $r, $y, $ROUND);
3134 return $r;
3135 }
3136  
3137 Math_GMPq__Scalar: {
3138 my $r = Math::GMPq::Rmpq_init();
3139 $y < 0
3140 ? Math::GMPq::Rmpq_set_si($r, $y, 1)
3141 : Math::GMPq::Rmpq_set_ui($r, $y, 1);
3142 Math::GMPq::Rmpq_mul($r, $r, $x);
3143 return $r;
3144 }
3145  
3146 #
3147 ## GMPz
3148 #
3149 Math_GMPz__Math_GMPz: {
3150 my $r = Math::GMPz::Rmpz_init();
3151 Math::GMPz::Rmpz_mul($r, $x, $y);
3152 return $r;
3153 }
3154  
3155 Math_GMPz__Scalar: {
3156 my $r = Math::GMPz::Rmpz_init();
3157 $y < 0
3158 ? Math::GMPz::Rmpz_mul_si($r, $x, $y)
3159 : Math::GMPz::Rmpz_mul_ui($r, $x, $y);
3160 return $r;
3161 }
3162  
3163 Math_GMPz__Math_GMPq: {
3164 my $r = Math::GMPq::Rmpq_init();
3165 Math::GMPq::Rmpq_mul_z($r, $y, $x);
3166 return $r;
3167 }
3168  
3169 Math_GMPz__Math_MPFR: {
3170 my $f = Math::MPFR::Rmpfr_init2($PREC);
3171 Math::MPFR::Rmpfr_mul_z($f, $y, $x, $ROUND);
3172 return $f;
3173 }
3174  
3175 Math_GMPz__Math_MPC: {
3176 my $r = Math::MPC::Rmpc_init2($PREC);
3177 Math::MPC::Rmpc_set_z($r, $x, $ROUND);
3178 Math::MPC::Rmpc_mul($r, $r, $y, $ROUND);
3179 return $r;
3180 }
3181  
3182 #
3183 ## MPFR
3184 #
3185 Math_MPFR__Math_MPFR: {
3186 my $r = Math::MPFR::Rmpfr_init2($PREC);
3187 Math::MPFR::Rmpfr_mul($r, $x, $y, $ROUND);
3188 return $r;
3189 }
3190  
3191 Math_MPFR__Scalar: {
3192 my $r = Math::MPFR::Rmpfr_init2($PREC);
3193 $y < 0
3194 ? Math::MPFR::Rmpfr_mul_si($r, $x, $y, $ROUND)
3195 : Math::MPFR::Rmpfr_mul_ui($r, $x, $y, $ROUND);
3196 return $r;
3197 }
3198  
3199 Math_MPFR__Math_GMPq: {
3200 my $r = Math::MPFR::Rmpfr_init2($PREC);
3201 Math::MPFR::Rmpfr_mul_q($r, $x, $y, $ROUND);
3202 return $r;
3203 }
3204  
3205 Math_MPFR__Math_GMPz: {
3206 my $r = Math::MPFR::Rmpfr_init2($PREC);
3207 Math::MPFR::Rmpfr_mul_z($r, $x, $y, $ROUND);
3208 return $r;
3209 }
3210  
3211 Math_MPFR__Math_MPC: {
3212 my $r = Math::MPC::Rmpc_init2($PREC);
3213 Math::MPC::Rmpc_mul_fr($r, $y, $x, $ROUND);
3214 return $r;
3215 }
3216  
3217 #
3218 ## MPC
3219 #
3220 Math_MPC__Math_MPC: {
3221 my $r = Math::MPC::Rmpc_init2($PREC);
3222 Math::MPC::Rmpc_mul($r, $x, $y, $ROUND);
3223 return $r;
3224 }
3225  
3226 Math_MPC__Scalar: {
3227 my $r = Math::MPC::Rmpc_init2($PREC);
3228 $y < 0
3229 ? Math::MPC::Rmpc_mul_si($r, $x, $y, $ROUND)
3230 : Math::MPC::Rmpc_mul_ui($r, $x, $y, $ROUND);
3231 return $r;
3232 }
3233  
3234 Math_MPC__Math_MPFR: {
3235 my $r = Math::MPC::Rmpc_init2($PREC);
3236 Math::MPC::Rmpc_mul_fr($r, $x, $y, $ROUND);
3237 return $r;
3238 }
3239  
3240 Math_MPC__Math_GMPz: {
3241 my $r = Math::MPC::Rmpc_init2($PREC);
3242 Math::MPC::Rmpc_set_z($r, $y, $ROUND);
3243 Math::MPC::Rmpc_mul($r, $r, $x, $ROUND);
3244 return $r;
3245 }
3246  
3247 Math_MPC__Math_GMPq: {
3248 my $r = Math::MPC::Rmpc_init2($PREC);
3249 Math::MPC::Rmpc_set_q($r, $y, $ROUND);
3250 Math::MPC::Rmpc_mul($r, $r, $x, $ROUND);
3251 return $r;
3252 }
3253 }
3254  
3255 sub mul { # used in overloading
3256 my ($x, $y) = @_;
3257  
3258 if (ref($y) eq __PACKAGE__) {
3259 return bless \__mul__($$x, $$y);
3260 }
3261  
3262 $x = $$x;
3263  
3264 if (!ref($y)) {
3265 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
3266 return bless \__mul__($x, $y);
3267 }
3268  
3269 return bless \__mul__($x, _str2obj($y));
3270 }
3271  
3272 bless \__mul__($x, _star2obj($y));
3273 }
3274  
3275 #
3276 ## DIVISION
3277 #
3278  
3279 sub __div__ {
3280 my ($x, $y) = @_;
3281 goto(join('__', ref($x) || 'Scalar', ref($y) || 'Scalar') =~ tr/:/_/rs);
3282  
3283 #
3284 ## GMPq
3285 #
3286 Math_GMPq__Math_GMPq: {
3287  
3288 # Check for division by zero
3289 Math::GMPq::Rmpq_sgn($y) || do {
3290 $x = _mpq2mpfr($x);
3291 goto Math_MPFR__Math_GMPq;
3292 };
3293  
3294 my $r = Math::GMPq::Rmpq_init();
3295 Math::GMPq::Rmpq_div($r, $x, $y);
3296 return $r;
3297 }
3298  
3299 Math_GMPq__Math_GMPz: {
3300  
3301 # Check for division by zero
3302 Math::GMPz::Rmpz_sgn($y) || do {
3303 $x = _mpq2mpfr($x);
3304 goto Math_MPFR__Math_GMPz;
3305 };
3306  
3307 my $r = Math::GMPq::Rmpq_init();
3308 Math::GMPq::Rmpq_div_z($r, $x, $y);
3309 return $r;
3310 }
3311  
3312 Math_GMPq__Math_MPFR: {
3313 my $r = Math::MPFR::Rmpfr_init2($PREC);
3314 Math::MPFR::Rmpfr_q_div($r, $x, $y, $ROUND);
3315 return $r;
3316 }
3317  
3318 Math_GMPq__Math_MPC: {
3319 my $r = Math::MPC::Rmpc_init2($PREC);
3320 Math::MPC::Rmpc_set_q($r, $x, $ROUND);
3321 Math::MPC::Rmpc_div($r, $r, $y, $ROUND);
3322 return $r;
3323 }
3324  
3325 Math_GMPq__Scalar: {
3326 my $r = Math::GMPq::Rmpq_init();
3327 $y < 0
3328 ? Math::GMPq::Rmpq_set_si($r, -1, -$y)
3329 : Math::GMPq::Rmpq_set_ui($r, 1, $y);
3330 Math::GMPq::Rmpq_mul($r, $r, $x);
3331 return $r;
3332 }
3333  
3334 Scalar__Math_GMPq: {
3335  
3336 # Check for division by zero
3337 Math::GMPq::Rmpq_sgn($y) || do {
3338 $y = _mpq2mpfr($y);
3339 goto Scalar__Math_MPFR;
3340 };
3341  
3342 my $r = Math::GMPq::Rmpq_init();
3343  
3344 if ($x == 1 or $x == -1) {
3345 Math::GMPq::Rmpq_inv($r, $y);
3346 Math::GMPq::Rmpq_neg($r, $r) if $x < 0;
3347 return $r;
3348 }
3349  
3350 $x < 0
3351 ? Math::GMPq::Rmpq_set_si($r, $x, 1)
3352 : Math::GMPq::Rmpq_set_ui($r, $x, 1);
3353  
3354 Math::GMPq::Rmpq_div($r, $r, $y);
3355  
3356 return $r;
3357 }
3358  
3359 #
3360 ## GMPz
3361 #
3362 Math_GMPz__Math_GMPz: {
3363  
3364 # Check for division by zero
3365 Math::GMPz::Rmpz_sgn($y) || do {
3366 $x = _mpz2mpfr($x);
3367 goto Math_MPFR__Math_GMPz;
3368 };
3369  
3370 # Check for exact divisibility
3371 if (Math::GMPz::Rmpz_divisible_p($x, $y)) {
3372 my $r = Math::GMPz::Rmpz_init();
3373 Math::GMPz::Rmpz_divexact($r, $x, $y);
3374 return $r;
3375 }
3376  
3377 my $r = Math::GMPq::Rmpq_init();
3378 Math::GMPq::Rmpq_set_num($r, $x);
3379 Math::GMPq::Rmpq_set_den($r, $y);
3380 Math::GMPq::Rmpq_canonicalize($r);
3381 return $r;
3382 }
3383  
3384 Math_GMPz__Scalar: {
3385  
3386 # Check for exact divisibility
3387 if (Math::GMPz::Rmpz_divisible_ui_p($x, CORE::abs($y))) {
3388 my $r = Math::GMPz::Rmpz_init();
3389 Math::GMPz::Rmpz_divexact_ui($r, $x, CORE::abs($y));
3390 Math::GMPz::Rmpz_neg($r, $r) if $y < 0;
3391 return $r;
3392 }
3393  
3394 my $r = Math::GMPq::Rmpq_init();
3395 Math::GMPq::Rmpq_set_ui($r, 1, CORE::abs($y));
3396 Math::GMPq::Rmpq_set_num($r, $x);
3397 Math::GMPq::Rmpq_neg($r, $r) if $y < 0;
3398 Math::GMPq::Rmpq_canonicalize($r);
3399 return $r;
3400 }
3401  
3402 Scalar__Math_GMPz: {
3403  
3404 # Check for division by zero
3405 Math::GMPz::Rmpz_sgn($y) || do {
3406 $y = _mpz2mpfr($y);
3407 goto Scalar__Math_MPFR;
3408 };
3409  
3410 my $r = Math::GMPq::Rmpq_init();
3411  
3412 if ($x == 1 or $x == -1) {
3413 Math::GMPq::Rmpq_set_z($r, $y);
3414 Math::GMPq::Rmpq_inv($r, $r);
3415 Math::GMPq::Rmpq_neg($r, $r) if $x < 0;
3416 return $r;
3417 }
3418  
3419 $x < 0
3420 ? Math::GMPq::Rmpq_set_si($r, $x, 1)
3421 : Math::GMPq::Rmpq_set_ui($r, $x, 1);
3422  
3423 Math::GMPq::Rmpq_set_den($r, $y);
3424 Math::GMPq::Rmpq_canonicalize($r);
3425  
3426 # If the result is an integer, return a GMPz object
3427 if (Math::GMPq::Rmpq_integer_p($r)) {
3428 my $z = Math::GMPz::Rmpz_init();
3429 Math::GMPq::Rmpq_get_num($z, $r);
3430 return $z;
3431 }
3432  
3433 return $r;
3434 }
3435  
3436 Math_GMPz__Math_GMPq: {
3437  
3438 # Check for division by zero
3439 Math::GMPq::Rmpq_sgn($y) || do {
3440 $x = _mpz2mpfr($x);
3441 goto Math_MPFR__Math_GMPq;
3442 };
3443  
3444 my $q = Math::GMPq::Rmpq_init();
3445 Math::GMPq::Rmpq_z_div($q, $x, $y);
3446 return $q;
3447 }
3448  
3449 Math_GMPz__Math_MPFR: {
3450 my $r = Math::MPFR::Rmpfr_init2($PREC);
3451 Math::MPFR::Rmpfr_z_div($r, $x, $y, $ROUND);
3452 return $r;
3453 }
3454  
3455 Math_GMPz__Math_MPC: {
3456 my $r = Math::MPC::Rmpc_init2($PREC);
3457 Math::MPC::Rmpc_set_z($r, $x, $ROUND);
3458 Math::MPC::Rmpc_div($r, $r, $y, $ROUND);
3459 return $r;
3460 }
3461  
3462 #
3463 ## MPFR
3464 #
3465 Math_MPFR__Math_MPFR: {
3466 my $r = Math::MPFR::Rmpfr_init2($PREC);
3467 Math::MPFR::Rmpfr_div($r, $x, $y, $ROUND);
3468 return $r;
3469 }
3470  
3471 Math_MPFR__Scalar: {
3472 my $r = Math::MPFR::Rmpfr_init2($PREC);
3473 $y < 0
3474 ? Math::MPFR::Rmpfr_div_si($r, $x, $y, $ROUND)
3475 : Math::MPFR::Rmpfr_div_ui($r, $x, $y, $ROUND);
3476 return $r;
3477 }
3478  
3479 Scalar__Math_MPFR: {
3480 my $r = Math::MPFR::Rmpfr_init2($PREC);
3481 $x < 0
3482 ? Math::MPFR::Rmpfr_si_div($r, $x, $y, $ROUND)
3483 : Math::MPFR::Rmpfr_ui_div($r, $x, $y, $ROUND);
3484 return $r;
3485 }
3486  
3487 Math_MPFR__Math_GMPq: {
3488 my $r = Math::MPFR::Rmpfr_init2($PREC);
3489 Math::MPFR::Rmpfr_div_q($r, $x, $y, $ROUND);
3490 return $r;
3491 }
3492  
3493 Math_MPFR__Math_GMPz: {
3494 my $r = Math::MPFR::Rmpfr_init2($PREC);
3495 Math::MPFR::Rmpfr_div_z($r, $x, $y, $ROUND);
3496 return $r;
3497 }
3498  
3499 Math_MPFR__Math_MPC: {
3500 my $r = Math::MPC::Rmpc_init2($PREC);
3501 Math::MPC::Rmpc_set_fr($r, $x, $ROUND);
3502 Math::MPC::Rmpc_div($r, $r, $y, $ROUND);
3503 return $r;
3504 }
3505  
3506 #
3507 ## MPC
3508 #
3509 Math_MPC__Math_MPC: {
3510 my $r = Math::MPC::Rmpc_init2($PREC);
3511 Math::MPC::Rmpc_div($r, $x, $y, $ROUND);
3512 return $r;
3513 }
3514  
3515 Math_MPC__Scalar: {
3516 my $r = Math::MPC::Rmpc_init2($PREC);
3517 if ($y < 0) {
3518 Math::MPC::Rmpc_div_ui($r, $x, -$y, $ROUND);
3519 Math::MPC::Rmpc_neg($r, $r, $ROUND);
3520 }
3521 else {
3522 Math::MPC::Rmpc_div_ui($r, $x, $y, $ROUND);
3523 }
3524 return $r;
3525 }
3526  
3527 Scalar__Math_MPC: {
3528 my $r = Math::MPC::Rmpc_init2($PREC);
3529 if ($x < 0) {
3530 Math::MPC::Rmpc_ui_div($r, -$x, $y, $ROUND);
3531 Math::MPC::Rmpc_neg($r, $r, $ROUND);
3532 }
3533 else {
3534 Math::MPC::Rmpc_ui_div($r, $x, $y, $ROUND);
3535 }
3536 return $r;
3537 }
3538  
3539 Math_MPC__Math_MPFR: {
3540 my $r = Math::MPC::Rmpc_init2($PREC);
3541 Math::MPC::Rmpc_div_fr($r, $x, $y, $ROUND);
3542 return $r;
3543 }
3544  
3545 Math_MPC__Math_GMPz: {
3546 my $r = Math::MPC::Rmpc_init2($PREC);
3547 Math::MPC::Rmpc_set_z($r, $y, $ROUND);
3548 Math::MPC::Rmpc_div($r, $x, $r, $ROUND);
3549 return $r;
3550 }
3551  
3552 Math_MPC__Math_GMPq: {
3553 my $r = Math::MPC::Rmpc_init2($PREC);
3554 Math::MPC::Rmpc_set_q($r, $y, $ROUND);
3555 Math::MPC::Rmpc_div($r, $x, $r, $ROUND);
3556 return $r;
3557 }
3558 }
3559  
3560 sub div { # used in overloading
3561 my ($x, $y) = @_;
3562  
3563 if (!ref($x) and CORE::int($x) eq $x and $x < ULONG_MAX and $x > LONG_MIN) {
3564  
3565 if (ref($y) eq __PACKAGE__) {
3566 return bless \__div__($x, $$y);
3567 }
3568  
3569 return bless \__div__($x, ref($y) ? _star2obj($y) : _str2obj($y));
3570 }
3571  
3572 $x =
3573 ref($x) eq __PACKAGE__ ? $$x
3574 : ref($x) ? _star2obj($x)
3575 : _str2obj($x);
3576  
3577 if (ref($y) eq __PACKAGE__) {
3578 return bless \__div__($x, $$y);
3579 }
3580  
3581 if (!ref($y)) {
3582 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN and CORE::int($y)) {
3583 return bless \__div__($x, $y);
3584 }
3585  
3586 return bless \__div__($x, _str2obj($y));
3587 }
3588  
3589 bless \__div__($x, _star2obj($y));
3590 }
3591  
3592 #
3593 ## INTEGER ADDITION
3594 #
3595  
3596 sub iadd ($$) {
3597 my ($x, $y) = @_;
3598  
3599 if (!ref($x) and ref($y)) {
3600 ($x, $y) = ($y, $x);
3601 }
3602  
3603 $x = _star2mpz($x) // goto &nan;
3604  
3605 if (!ref($y) and CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
3606 my $r = Math::GMPz::Rmpz_init();
3607 $y < 0
3608 ? Math::GMPz::Rmpz_sub_ui($r, $x, -$y)
3609 : Math::GMPz::Rmpz_add_ui($r, $x, $y);
3610 return bless \$r;
3611 }
3612  
3613 $y = _star2mpz($y) // goto &nan;
3614  
3615 my $r = Math::GMPz::Rmpz_init();
3616 Math::GMPz::Rmpz_add($r, $x, $y);
3617 bless \$r;
3618 }
3619  
3620 #
3621 ## INTEGER SUBTRACTION
3622 #
3623  
3624 sub isub ($$) {
3625 my ($x, $y) = @_;
3626  
3627 $x = _star2mpz($x) // goto &nan;
3628  
3629 if (!ref($y) and CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
3630 my $r = Math::GMPz::Rmpz_init();
3631 $y < 0
3632 ? Math::GMPz::Rmpz_add_ui($r, $x, -$y)
3633 : Math::GMPz::Rmpz_sub_ui($r, $x, $y);
3634 return bless \$r;
3635 }
3636  
3637 $y = _star2mpz($y) // goto &nan;
3638  
3639 my $r = Math::GMPz::Rmpz_init();
3640 Math::GMPz::Rmpz_sub($r, $x, $y);
3641 bless \$r;
3642 }
3643  
3644 #
3645 ## INTEGER MULTIPLICATION
3646 #
3647  
3648 sub imul ($$) {
3649 my ($x, $y) = @_;
3650  
3651 if (!ref($x) and ref($y)) {
3652 ($x, $y) = ($y, $x);
3653 }
3654  
3655 $x = _star2mpz($x) // goto &nan;
3656  
3657 if (!ref($y) and CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
3658 my $r = Math::GMPz::Rmpz_init();
3659 Math::GMPz::Rmpz_mul_ui($r, $x, $y < 0 ? -$y : $y);
3660 Math::GMPz::Rmpz_neg($r, $r) if $y < 0;
3661 return bless \$r;
3662 }
3663  
3664 $y = _star2mpz($y) // goto &nan;
3665  
3666 my $r = Math::GMPz::Rmpz_init();
3667 Math::GMPz::Rmpz_mul($r, $x, $y);
3668 bless \$r;
3669 }
3670  
3671 #
3672 ## INTEGER DIVISION
3673 #
3674  
3675 sub idiv ($$) {
3676 my ($x, $y) = @_;
3677  
3678 $x = _star2mpz($x) // goto &nan;
3679  
3680 if (!ref($y) and CORE::int($y) eq $y and CORE::int($y) > 0 and $y < ULONG_MAX) {
3681 my $r = Math::GMPz::Rmpz_init();
3682 Math::GMPz::Rmpz_div_ui($r, $x, $y);
3683 return bless \$r;
3684 }
3685  
3686 $y = _star2mpz($y) // goto &nan;
3687  
3688 # Detect division by zero
3689 Math::GMPz::Rmpz_sgn($y) || do {
3690 my $sign = Math::GMPz::Rmpz_sgn($x);
3691  
3692 if ($sign == 0) { # 0/0
3693 goto &nan;
3694 }
3695 elsif ($sign > 0) { # x/0 where: x > 0
3696 goto &inf;
3697 }
3698 else { # x/0 where: x < 0
3699 goto &ninf;
3700 }
3701 };
3702  
3703 my $r = Math::GMPz::Rmpz_init();
3704 Math::GMPz::Rmpz_div($r, $x, $y);
3705 bless \$r;
3706 }
3707  
3708 sub idiv_ceil ($$) {
3709 my ($x, $y) = @_;
3710  
3711 $x = _star2mpz($x) // (goto &nan);
3712  
3713 if (!ref($y) and CORE::int($y) eq $y and CORE::int($y) > 0 and $y < ULONG_MAX) {
3714 my $r = Math::GMPz::Rmpz_init();
3715 Math::GMPz::Rmpz_cdiv_q_ui($r, $x, $y);
3716 return bless \$r;
3717 }
3718  
3719 $y = _star2mpz($y) // (goto &nan);
3720  
3721 # Detect division by zero
3722 Math::GMPz::Rmpz_sgn($y) || do {
3723 my $sign = Math::GMPz::Rmpz_sgn($x);
3724  
3725 if ($sign == 0) { # 0/0
3726 goto &nan;
3727 }
3728 elsif ($sign > 0) { # x/0 where: x > 0
3729 goto &inf;
3730 }
3731 else { # x/0 where: x < 0
3732 goto &ninf;
3733 }
3734 };
3735  
3736 my $r = Math::GMPz::Rmpz_init();
3737 Math::GMPz::Rmpz_cdiv_q($r, $x, $y);
3738 bless \$r;
3739 }
3740  
3741 sub idiv_trunc ($$) {
3742 my ($x, $y) = @_;
3743  
3744 $x = _star2mpz($x) // (goto &nan);
3745  
3746 if (!ref($y) and CORE::int($y) eq $y and CORE::int($y) > 0 and $y < ULONG_MAX) {
3747 my $r = Math::GMPz::Rmpz_init();
3748 Math::GMPz::Rmpz_tdiv_q_ui($r, $x, $y);
3749 return bless \$r;
3750 }
3751  
3752 $y = _star2mpz($y) // (goto &nan);
3753  
3754 # Detect division by zero
3755 Math::GMPz::Rmpz_sgn($y) || do {
3756 my $sign = Math::GMPz::Rmpz_sgn($x);
3757  
3758 if ($sign == 0) { # 0/0
3759 goto &nan;
3760 }
3761 elsif ($sign > 0) { # x/0 where: x > 0
3762 goto &inf;
3763 }
3764 else { # x/0 where: x < 0
3765 goto &ninf;
3766 }
3767 };
3768  
3769 my $r = Math::GMPz::Rmpz_init();
3770 Math::GMPz::Rmpz_tdiv_q($r, $x, $y);
3771 bless \$r;
3772 }
3773  
3774 sub idiv_round ($$) {
3775 my ($x, $y) = @_;
3776  
3777 $x = _star2mpz($x) // (goto &nan);
3778 $y = _star2mpz($y) // (goto &nan);
3779  
3780 # Detect division by zero
3781 Math::GMPz::Rmpz_sgn($y) || do {
3782 my $sign = Math::GMPz::Rmpz_sgn($x);
3783  
3784 if ($sign == 0) { # 0/0
3785 goto &nan;
3786 }
3787 elsif ($sign > 0) { # x/0 where: x > 0
3788 goto &inf;
3789 }
3790 else { # x/0 where: x < 0
3791 goto &ninf;
3792 }
3793 };
3794  
3795 my $r = Math::GMPz::Rmpz_init();
3796 Math::GMPz::Rmpz_set($r, $y);
3797 Math::GMPz::Rmpz_addmul_ui($r, $x, 2);
3798 Math::GMPz::Rmpz_div($r, $r, $y);
3799 Math::GMPz::Rmpz_div_2exp($r, $r, 1);
3800 bless \$r;
3801 }
3802  
3803 #
3804 ## POWER (x^y)
3805 #
3806  
3807 sub __pow__ {
3808 my ($x, $y) = @_;
3809 goto(join('__', ref($x), ref($y) || 'Scalar') =~ tr/:/_/rs);
3810  
3811 #
3812 ## GMPq
3813 #
3814 Math_GMPq__Scalar: {
3815  
3816 my $r = Math::GMPq::Rmpq_init();
3817 Math::GMPq::Rmpq_pow_ui($r, $x, CORE::abs($y));
3818  
3819 if ($y < 0) {
3820 Math::GMPq::Rmpq_sgn($r) || goto &_inf;
3821 Math::GMPq::Rmpq_inv($r, $r);
3822 }
3823  
3824 return $r;
3825 }
3826  
3827 Math_GMPq__Math_GMPq: {
3828  
3829 # Integer power
3830 if (Math::GMPq::Rmpq_integer_p($y)) {
3831 $y = Math::GMPq::Rmpq_get_d($y);
3832 goto Math_GMPq__Scalar;
3833 }
3834  
3835 # (-x)^(a/b) is a complex number
3836 if (Math::GMPq::Rmpq_sgn($x) < 0) {
3837 $x = _mpq2mpc($x);
3838 $y = _mpq2mpc($y);
3839 goto Math_MPC__Math_MPC;
3840 }
3841  
3842 $x = _mpq2mpfr($x);
3843 $y = _mpq2mpfr($y);
3844  
3845 goto Math_MPFR__Math_MPFR;
3846 }
3847  
3848 Math_GMPq__Math_GMPz: {
3849 $y = Math::GMPz::Rmpz_get_d($y);
3850 goto Math_GMPq__Scalar;
3851 }
3852  
3853 Math_GMPq__Math_MPFR: {
3854 $x = _mpq2mpfr($x);
3855 goto Math_MPFR__Math_MPFR;
3856 }
3857  
3858 Math_GMPq__Math_MPC: {
3859 $x = _mpq2mpc($x);
3860 goto Math_MPC__Math_MPC;
3861 }
3862  
3863 #
3864 ## GMPz
3865 #
3866  
3867 Math_GMPz__Scalar: {
3868  
3869 my $r = Math::GMPz::Rmpz_init();
3870 Math::GMPz::Rmpz_pow_ui($r, $x, CORE::abs($y));
3871  
3872 if ($y < 0) {
3873 Math::GMPz::Rmpz_sgn($r) || goto &_inf;
3874  
3875 my $q = Math::GMPq::Rmpq_init();
3876 Math::GMPq::Rmpq_set_z($q, $r);
3877 Math::GMPq::Rmpq_inv($q, $q);
3878 return $q;
3879 }
3880  
3881 return $r;
3882 }
3883  
3884 Math_GMPz__Math_GMPz: {
3885 $y = Math::GMPz::Rmpz_get_d($y);
3886 goto Math_GMPz__Scalar;
3887 }
3888  
3889 Math_GMPz__Math_GMPq: {
3890  
3891 if (Math::GMPq::Rmpq_integer_p($y)) {
3892 $y = Math::GMPq::Rmpq_get_d($y);
3893 goto Math_GMPz__Scalar;
3894 }
3895  
3896 $x = _mpz2mpfr($x);
3897 $y = _mpq2mpfr($y);
3898  
3899 goto Math_MPFR__Math_MPFR;
3900 }
3901  
3902 Math_GMPz__Math_MPFR: {
3903 $x = _mpz2mpfr($x);
3904 goto Math_MPFR__Math_MPFR;
3905 }
3906  
3907 Math_GMPz__Math_MPC: {
3908 $x = _mpz2mpc($x);
3909 goto Math_MPC__Math_MPC;
3910 }
3911  
3912 #
3913 ## MPFR
3914 #
3915 Math_MPFR__Math_MPFR: {
3916  
3917 if ( Math::MPFR::Rmpfr_sgn($x) < 0
3918 and !Math::MPFR::Rmpfr_integer_p($y)
3919 and Math::MPFR::Rmpfr_number_p($y)) {
3920 $x = _mpfr2mpc($x);
3921 goto Math_MPC__Math_MPFR;
3922 }
3923  
3924 my $r = Math::MPFR::Rmpfr_init2($PREC);
3925 Math::MPFR::Rmpfr_pow($r, $x, $y, $ROUND);
3926 return $r;
3927 }
3928  
3929 Math_MPFR__Scalar: {
3930 my $r = Math::MPFR::Rmpfr_init2($PREC);
3931 $y < 0
3932 ? Math::MPFR::Rmpfr_pow_si($r, $x, $y, $ROUND)
3933 : Math::MPFR::Rmpfr_pow_ui($r, $x, $y, $ROUND);
3934 return $r;
3935 }
3936  
3937 Math_MPFR__Math_GMPq: {
3938 $y = _mpq2mpfr($y);
3939 goto Math_MPFR__Math_MPFR;
3940 }
3941  
3942 Math_MPFR__Math_GMPz: {
3943 my $r = Math::MPFR::Rmpfr_init2($PREC);
3944 Math::MPFR::Rmpfr_pow_z($r, $x, $y, $ROUND);
3945 return $r;
3946 }
3947  
3948 Math_MPFR__Math_MPC: {
3949 $x = _mpfr2mpc($x);
3950 goto Math_MPC__Math_MPC;
3951 }
3952  
3953 #
3954 ## MPC
3955 #
3956 Math_MPC__Math_MPC: {
3957 my $r = Math::MPC::Rmpc_init2($PREC);
3958 Math::MPC::Rmpc_pow($r, $x, $y, $ROUND);
3959 return $r;
3960 }
3961  
3962 Math_MPC__Scalar: {
3963 my $r = Math::MPC::Rmpc_init2($PREC);
3964 $y < 0
3965 ? Math::MPC::Rmpc_pow_si($r, $x, $y, $ROUND)
3966 : Math::MPC::Rmpc_pow_ui($r, $x, $y, $ROUND);
3967 return $r;
3968 }
3969  
3970 Math_MPC__Math_MPFR: {
3971 my $r = Math::MPC::Rmpc_init2($PREC);
3972 Math::MPC::Rmpc_pow_fr($r, $x, $y, $ROUND);
3973 return $r;
3974 }
3975  
3976 Math_MPC__Math_GMPz: {
3977 my $r = Math::MPC::Rmpc_init2($PREC);
3978 Math::MPC::Rmpc_pow_z($r, $x, $y, $ROUND);
3979 return $r;
3980 }
3981  
3982 Math_MPC__Math_GMPq: {
3983 $y = _mpq2mpc($y);
3984 goto Math_MPC__Math_MPC;
3985 }
3986 }
3987  
3988 sub pow ($$) {
3989 my ($x, $y) = @_;
3990  
3991 $x =
3992 ref($x) eq __PACKAGE__ ? $$x
3993 : ref($x) ? _star2obj($x)
3994 : _str2obj($x);
3995  
3996 if (ref($y) eq __PACKAGE__) {
3997 return bless \__pow__($x, $$y);
3998 }
3999  
4000 if (!ref($y)) {
4001 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
4002 return bless \__pow__($x, $y);
4003 }
4004  
4005 return bless \__pow__($x, _str2obj($y));
4006 }
4007  
4008 bless \__pow__($x, _star2obj($y));
4009 }
4010  
4011 #
4012 ## INTEGER POWER
4013 #
4014  
4015 sub ipow ($$) {
4016 my ($x, $y) = @_;
4017  
4018 $y = _star2si($y) // goto &nan;
4019  
4020 # Both `x` and `y` are unsigned native integers
4021 if ( !ref($x)
4022 and CORE::int($x) eq $x
4023 and $x >= 0
4024 and $x < ULONG_MAX
4025 and $y >= 0) {
4026 my $r = Math::GMPz::Rmpz_init();
4027 Math::GMPz::Rmpz_ui_pow_ui($r, $x, $y);
4028 return bless \$r;
4029 }
4030  
4031 $x = _star2mpz($x) // goto &nan;
4032  
4033 my $r = Math::GMPz::Rmpz_init();
4034 Math::GMPz::Rmpz_pow_ui($r, $x, $y < 0 ? -$y : $y);
4035  
4036 if ($y < 0) {
4037 Math::GMPz::Rmpz_sgn($r) || goto &inf; # 0^(-y) = Inf
4038 state $ONE_Z = Math::GMPz::Rmpz_init_set_ui_nobless(1);
4039 Math::GMPz::Rmpz_div($r, $ONE_Z, $r);
4040 }
4041  
4042 bless \$r;
4043 }
4044  
4045 #
4046 ## IPOW2
4047 #
4048  
4049 sub ipow2 ($) {
4050 my ($n) = @_;
4051  
4052 $n = _star2si($n) // goto &nan;
4053  
4054 goto &zero if $n < 0;
4055  
4056 my $r = Math::GMPz::Rmpz_init();
4057 Math::GMPz::Rmpz_setbit($r, $n);
4058 bless \$r;
4059 }
4060  
4061 #
4062 ## IPOW10
4063 #
4064  
4065 sub ipow10 ($) {
4066 my ($n) = @_;
4067  
4068 $n = _star2si($n) // goto &nan;
4069  
4070 goto &zero if $n < 0;
4071  
4072 my $r = Math::GMPz::Rmpz_init();
4073 Math::GMPz::Rmpz_ui_pow_ui($r, 10, $n);
4074 bless \$r;
4075 }
4076  
4077 #
4078 ## ROOT
4079 #
4080  
4081 sub root ($$) {
4082 my ($x, $y) = @_;
4083  
4084 $x =
4085 ref($x) eq __PACKAGE__ ? $$x
4086 : ref($x) ? _star2obj($x)
4087 : _str2obj($x);
4088  
4089 $y =
4090 ref($y) eq __PACKAGE__ ? $$y
4091 : ref($y) ? _star2obj($y)
4092 : _str2obj($y);
4093  
4094 bless \__pow__($x, __inv__($y));
4095 }
4096  
4097 #
4098 ## Polygonal root
4099 #
4100  
4101 sub __polygonal_root__ {
4102 my ($n, $k, $second) = @_;
4103 goto(join('__', ref($n), ref($k)) =~ tr/:/_/rs);
4104  
4105 # polygonal_root(n, k)
4106 # = ((k - 4) ± sqrt(8 * (k - 2) * n + (k - 4)^2)) / (2 * (k - 2))
4107  
4108 Math_MPFR__Math_MPFR: {
4109 my $t = Math::MPFR::Rmpfr_init2($PREC);
4110 my $u = Math::MPFR::Rmpfr_init2($PREC);
4111  
4112 Math::MPFR::Rmpfr_sub_ui($u, $k, 2, $ROUND); # u = k-2
4113 Math::MPFR::Rmpfr_mul($t, $n, $u, $ROUND); # t = n*u
4114 Math::MPFR::Rmpfr_mul_2ui($t, $t, 3, $ROUND); # t = t*8
4115  
4116 Math::MPFR::Rmpfr_sub_ui($u, $u, 2, $ROUND); # u = u-2
4117 Math::MPFR::Rmpfr_sqr($u, $u, $ROUND); # u = u^2
4118 Math::MPFR::Rmpfr_add($t, $t, $u, $ROUND); # t = t+u
4119  
4120 # Return a complex number for `t < 0`
4121 if (Math::MPFR::Rmpfr_sgn($t) < 0) {
4122 $n = _mpfr2mpc($n);
4123 $k = _mpfr2mpc($k);
4124 goto Math_MPC__Math_MPC;
4125 }
4126  
4127 Math::MPFR::Rmpfr_sqrt($t, $t, $ROUND); # t = sqrt(t)
4128 Math::MPFR::Rmpfr_sub_ui($u, $k, 4, $ROUND); # u = k-4
4129  
4130 $second
4131 ? Math::MPFR::Rmpfr_sub($t, $u, $t, $ROUND) # t = u-t
4132 : Math::MPFR::Rmpfr_add($t, $t, $u, $ROUND); # t = t+u
4133  
4134 Math::MPFR::Rmpfr_add_ui($u, $u, 2, $ROUND); # u = u+2
4135 Math::MPFR::Rmpfr_mul_2ui($u, $u, 1, $ROUND); # u = u*2
4136  
4137 Math::MPFR::Rmpfr_zero_p($u) && return $n; # `u` is zero
4138 Math::MPFR::Rmpfr_div($t, $t, $u, $ROUND); # t = t/u
4139 return $t;
4140 }
4141  
4142 Math_MPFR__Math_MPC: {
4143 $n = _mpfr2mpc($n);
4144 goto Math_MPC__Math_MPC;
4145 }
4146  
4147 Math_MPC__Math_MPFR: {
4148 $k = _mpfr2mpc($k);
4149 goto Math_MPC__Math_MPC;
4150 }
4151  
4152 Math_MPC__Math_MPC: {
4153 my $t = Math::MPC::Rmpc_init2($PREC);
4154 my $u = Math::MPC::Rmpc_init2($PREC);
4155  
4156 Math::MPC::Rmpc_sub_ui($u, $k, 2, $ROUND); # u = k-2
4157 Math::MPC::Rmpc_mul($t, $n, $u, $ROUND); # t = n*u
4158 Math::MPC::Rmpc_mul_2ui($t, $t, 3, $ROUND); # t = t*8
4159  
4160 Math::MPC::Rmpc_sub_ui($u, $u, 2, $ROUND); # u = u-2
4161 Math::MPC::Rmpc_sqr($u, $u, $ROUND); # u = u^2
4162 Math::MPC::Rmpc_add($t, $t, $u, $ROUND); # t = t+u
4163  
4164 Math::MPC::Rmpc_sqrt($t, $t, $ROUND); # t = sqrt(t)
4165 Math::MPC::Rmpc_sub_ui($u, $k, 4, $ROUND); # u = k-4
4166  
4167 $second
4168 ? Math::MPC::Rmpc_sub($t, $u, $t, $ROUND) # t = u-t
4169 : Math::MPC::Rmpc_add($t, $t, $u, $ROUND); # t = t+u
4170  
4171 Math::MPC::Rmpc_add_ui($u, $u, 2, $ROUND); # u = u+2
4172 Math::MPC::Rmpc_mul_2ui($u, $u, 1, $ROUND); # u = u*2
4173  
4174 if (Math::MPC::Rmpc_cmp_si($t, 0) == 0) { # `u` is zero
4175 return $n;
4176 }
4177  
4178 Math::MPC::Rmpc_div($t, $t, $u, $ROUND); # t = t/u
4179 return $t;
4180 }
4181 }
4182  
4183 sub polygonal_root ($$) {
4184 bless \__polygonal_root__(_star2mpfr_mpc($_[0]), _star2mpfr_mpc($_[1]));
4185 }
4186  
4187 #
4188 ## Second polygonal root
4189 #
4190  
4191 sub polygonal_root2 ($$) {
4192 bless \__polygonal_root__(_star2mpfr_mpc($_[0]), _star2mpfr_mpc($_[1]), 1);
4193 }
4194  
4195 #
4196 ## isqrt
4197 #
4198  
4199 sub isqrt ($) {
4200 my ($x) = @_;
4201  
4202 $x = _star2mpz($x) // goto &nan;
4203  
4204 Math::GMPz::Rmpz_sgn($x) < 0 and goto &nan;
4205 my $r = Math::GMPz::Rmpz_init();
4206 Math::GMPz::Rmpz_sqrt($r, $x);
4207 bless \$r;
4208 }
4209  
4210 #
4211 ## icbrt
4212 #
4213  
4214 sub icbrt ($) {
4215 my ($x) = @_;
4216  
4217 $x = _star2mpz($x) // goto &nan;
4218  
4219 my $r = Math::GMPz::Rmpz_init();
4220 Math::GMPz::Rmpz_root($r, $x, 3);
4221 bless \$r;
4222 }
4223  
4224 #
4225 ## IROOT
4226 #
4227  
4228 sub iroot ($$) {
4229 my ($x, $y) = @_;
4230  
4231 $x = _star2mpz($x) // goto &nan;
4232 $y = _star2si($y) // goto &nan;
4233  
4234 if ($y == 0) {
4235 Math::GMPz::Rmpz_sgn($x) || goto &zero; # 0^Inf = 0
4236  
4237 # 1^Inf = 1 ; (-1)^Inf = 1
4238 if (Math::GMPz::Rmpz_cmpabs_ui($x, 1) == 0) {
4239 goto &one;
4240 }
4241  
4242 goto &inf;
4243 }
4244  
4245 if ($y < 0) {
4246 my $sign = Math::GMPz::Rmpz_sgn($x)
4247 || goto &inf; # 1 / 0^k = Inf
4248  
4249 if ($sign < 0) {
4250 goto &nan;
4251 }
4252  
4253 if (Math::GMPz::Rmpz_cmp_ui($x, 1) == 0) { # 1 / 1^k = 1
4254 goto &one;
4255 }
4256  
4257 goto &zero;
4258 }
4259  
4260 if ($y % 2 == 0 and Math::GMPz::Rmpz_sgn($x) < 0) {
4261 goto &nan;
4262 }
4263  
4264 my $r = Math::GMPz::Rmpz_init();
4265 $y == 2
4266 ? Math::GMPz::Rmpz_sqrt($r, $x)
4267 : Math::GMPz::Rmpz_root($r, $x, $y);
4268 bless \$r;
4269 }
4270  
4271 #
4272 ## ISQRTREM
4273 #
4274  
4275 sub isqrtrem ($) {
4276 my ($x) = @_;
4277  
4278 $x = _star2mpz($x) // return (nan(), nan());
4279  
4280 Math::GMPz::Rmpz_sgn($x) < 0
4281 and return (nan(), nan());
4282  
4283 my $r = Math::GMPz::Rmpz_init();
4284 my $s = Math::GMPz::Rmpz_init();
4285  
4286 Math::GMPz::Rmpz_sqrtrem($r, $s, $x);
4287  
4288 ((bless \$r), (bless \$s));
4289 }
4290  
4291 #
4292 ## IROOTREM
4293 #
4294  
4295 sub irootrem ($$) {
4296 my ($x, $y) = @_;
4297  
4298 $x = _star2mpz($x) // return (nan(), nan());
4299 $y = _star2si($y) // return (nan(), nan());
4300  
4301 if ($y == 0) {
4302  
4303 # 0^Inf = 0
4304 if (Math::GMPz::Rmpz_sgn($x) == 0) {
4305 return (zero(), mone());
4306 }
4307  
4308 my $r = Math::GMPz::Rmpz_init();
4309 Math::GMPz::Rmpz_sub_ui($r, $x, 1);
4310  
4311 # 1^Inf = 1 ; (-1)^Inf = 1
4312 if (Math::GMPz::Rmpz_cmpabs_ui($x, 1) == 0) {
4313 return (one(), (bless \$r));
4314 }
4315  
4316 return (inf(), (bless \$r));
4317 }
4318  
4319 if ($y < 0) {
4320 my $sgn = Math::GMPz::Rmpz_sgn($x);
4321  
4322 # 1 / 0^k = Inf
4323 if ($sgn == 0) {
4324 return (inf(), zero());
4325 }
4326  
4327 # 1 / 1^k = 1
4328 if (Math::GMPz::Rmpz_cmp_ui($x, 1) == 0) {
4329 return (one(), zero());
4330 }
4331  
4332 # x is negative
4333 if ($sgn < 0) {
4334 return (nan(), nan());
4335 }
4336  
4337 return (zero(), ninf());
4338 }
4339  
4340 if ($y % 2 == 0 and Math::GMPz::Rmpz_sgn($x) < 0) {
4341 return (nan(), nan());
4342 }
4343  
4344 my $r = Math::GMPz::Rmpz_init();
4345 my $s = Math::GMPz::Rmpz_init();
4346  
4347 $y == 2
4348 ? Math::GMPz::Rmpz_sqrtrem($r, $s, $x)
4349 : Math::GMPz::Rmpz_rootrem($r, $s, $x, $y);
4350  
4351 ((bless \$r), (bless \$s));
4352 }
4353  
4354 #
4355 ## MOD
4356 #
4357  
4358 sub __mod__ {
4359 my ($x, $y) = @_;
4360 goto(join('__', ref($x), ref($y) || 'Scalar') =~ tr/:/_/rs);
4361  
4362 #
4363 ## GMPq
4364 #
4365 Math_GMPq__Math_GMPq: {
4366  
4367 if (Math::GMPq::Rmpq_integer_p($y)) {
4368 $y = _mpq2mpz($y);
4369 goto Math_GMPq__Math_GMPz;
4370 }
4371  
4372 Math::GMPq::Rmpq_sgn($y) || goto &_nan;
4373  
4374 my $quo = Math::GMPq::Rmpq_init();
4375 Math::GMPq::Rmpq_div($quo, $x, $y);
4376  
4377 # Floor
4378 Math::GMPq::Rmpq_integer_p($quo) || do {
4379 my $z = Math::GMPz::Rmpz_init();
4380 Math::GMPz::Rmpz_set_q($z, $quo);
4381 Math::GMPz::Rmpz_sub_ui($z, $z, 1) if Math::GMPq::Rmpq_sgn($quo) < 0;
4382 Math::GMPq::Rmpq_set_z($quo, $z);
4383 };
4384  
4385 Math::GMPq::Rmpq_mul($quo, $quo, $y);
4386 Math::GMPq::Rmpq_sub($quo, $x, $quo);
4387  
4388 return $quo;
4389 }
4390  
4391 Math_GMPq__Math_GMPz: {
4392 Math::GMPz::Rmpz_sgn($y) || goto &_nan;
4393 my $r = _modular_rational($x, $y) // do {
4394  
4395 my $quo = Math::GMPq::Rmpq_init();
4396 Math::GMPq::Rmpq_div_z($quo, $x, $y);
4397  
4398 # Floor
4399 Math::GMPq::Rmpq_integer_p($quo) || do {
4400 my $z = Math::GMPz::Rmpz_init();
4401 Math::GMPz::Rmpz_set_q($z, $quo);
4402 Math::GMPz::Rmpz_sub_ui($z, $z, 1) if Math::GMPq::Rmpq_sgn($quo) < 0;
4403 Math::GMPq::Rmpq_set_z($quo, $z);
4404 };
4405  
4406 Math::GMPq::Rmpq_mul_z($quo, $quo, $y);
4407 Math::GMPq::Rmpq_sub($quo, $x, $quo);
4408  
4409 return $quo;
4410 };
4411 Math::GMPz::Rmpz_mod($r, $r, $y);
4412 return $r;
4413 }
4414  
4415 Math_GMPq__Math_MPFR: {
4416 $x = _mpq2mpfr($x);
4417 goto Math_MPFR__Math_MPFR;
4418 }
4419  
4420 Math_GMPq__Math_MPC: {
4421 $x = _mpq2mpc($x);
4422 goto Math_MPC__Math_MPC;
4423 }
4424  
4425 #
4426 ## GMPz
4427 #
4428 Math_GMPz__Math_GMPz: {
4429  
4430 my $sgn_y = Math::GMPz::Rmpz_sgn($y) || goto &_nan;
4431  
4432 my $r = Math::GMPz::Rmpz_init();
4433 Math::GMPz::Rmpz_mod($r, $x, $y);
4434  
4435 if (!Math::GMPz::Rmpz_sgn($r)) {
4436 ## ok
4437 }
4438 elsif ($sgn_y < 0) {
4439 Math::GMPz::Rmpz_add($r, $r, $y);
4440 }
4441  
4442 return $r;
4443 }
4444  
4445 Math_GMPz__Scalar: {
4446 my $r = Math::GMPz::Rmpz_init();
4447 Math::GMPz::Rmpz_mod_ui($r, $x, $y);
4448 return $r;
4449 }
4450  
4451 Math_GMPz__Math_GMPq: {
4452 $x = _mpz2mpq($x);
4453 goto Math_GMPq__Math_GMPq;
4454 }
4455  
4456 Math_GMPz__Math_MPFR: {
4457 $x = _mpz2mpfr($x);
4458 goto Math_MPFR__Math_MPFR;
4459 }
4460  
4461 Math_GMPz__Math_MPC: {
4462 $x = _mpz2mpc($x);
4463 goto Math_MPC__Math_MPC;
4464 }
4465  
4466 #
4467 ## MPFR
4468 #
4469 Math_MPFR__Math_MPFR: {
4470  
4471 my $quo = Math::MPFR::Rmpfr_init2($PREC);
4472 Math::MPFR::Rmpfr_div($quo, $x, $y, $ROUND);
4473 Math::MPFR::Rmpfr_floor($quo, $quo);
4474 Math::MPFR::Rmpfr_mul($quo, $quo, $y, $ROUND);
4475 Math::MPFR::Rmpfr_sub($quo, $x, $quo, $ROUND);
4476  
4477 return $quo;
4478 }
4479  
4480 Math_MPFR__Scalar: {
4481  
4482 my $quo = Math::MPFR::Rmpfr_init2($PREC);
4483 Math::MPFR::Rmpfr_div_ui($quo, $x, $y, $ROUND);
4484 Math::MPFR::Rmpfr_floor($quo, $quo);
4485 Math::MPFR::Rmpfr_mul_ui($quo, $quo, $y, $ROUND);
4486 Math::MPFR::Rmpfr_sub($quo, $x, $quo, $ROUND);
4487  
4488 return $quo;
4489 }
4490  
4491 Math_MPFR__Math_GMPq: {
4492  
4493 my $quo = Math::MPFR::Rmpfr_init2($PREC);
4494 Math::MPFR::Rmpfr_div_q($quo, $x, $y, $ROUND);
4495 Math::MPFR::Rmpfr_floor($quo, $quo);
4496 Math::MPFR::Rmpfr_mul_q($quo, $quo, $y, $ROUND);
4497 Math::MPFR::Rmpfr_sub($quo, $x, $quo, $ROUND);
4498  
4499 return $quo;
4500 }
4501  
4502 Math_MPFR__Math_GMPz: {
4503  
4504 my $quo = Math::MPFR::Rmpfr_init2($PREC);
4505 Math::MPFR::Rmpfr_div_z($quo, $x, $y, $ROUND);
4506 Math::MPFR::Rmpfr_floor($quo, $quo);
4507 Math::MPFR::Rmpfr_mul_z($quo, $quo, $y, $ROUND);
4508 Math::MPFR::Rmpfr_sub($quo, $x, $quo, $ROUND);
4509  
4510 return $quo;
4511 }
4512  
4513 Math_MPFR__Math_MPC: {
4514 $x = _mpfr2mpc($x);
4515 goto Math_MPC__Math_MPC;
4516 }
4517  
4518 #
4519 ## MPC
4520 #
4521 Math_MPC__Math_MPC: {
4522  
4523 my $quo = Math::MPC::Rmpc_init2($PREC);
4524 Math::MPC::Rmpc_div($quo, $x, $y, $ROUND);
4525  
4526 my $real = Math::MPFR::Rmpfr_init2($PREC);
4527 my $imag = Math::MPFR::Rmpfr_init2($PREC);
4528  
4529 Math::MPC::RMPC_RE($real, $quo);
4530 Math::MPC::RMPC_IM($imag, $quo);
4531  
4532 Math::MPFR::Rmpfr_floor($real, $real);
4533 Math::MPFR::Rmpfr_floor($imag, $imag);
4534  
4535 Math::MPC::Rmpc_set_fr_fr($quo, $real, $imag, $ROUND);
4536  
4537 Math::MPC::Rmpc_mul($quo, $quo, $y, $ROUND);
4538 Math::MPC::Rmpc_sub($quo, $x, $quo, $ROUND);
4539  
4540 return $quo;
4541 }
4542  
4543 Math_MPC__Scalar: {
4544  
4545 my $quo = Math::MPC::Rmpc_init2($PREC);
4546 Math::MPC::Rmpc_div_ui($quo, $x, $y, $ROUND);
4547  
4548 my $real = Math::MPFR::Rmpfr_init2($PREC);
4549 my $imag = Math::MPFR::Rmpfr_init2($PREC);
4550  
4551 Math::MPC::RMPC_RE($real, $quo);
4552 Math::MPC::RMPC_IM($imag, $quo);
4553  
4554 Math::MPFR::Rmpfr_floor($real, $real);
4555 Math::MPFR::Rmpfr_floor($imag, $imag);
4556  
4557 Math::MPC::Rmpc_set_fr_fr($quo, $real, $imag, $ROUND);
4558  
4559 Math::MPC::Rmpc_mul_ui($quo, $quo, $y, $ROUND);
4560 Math::MPC::Rmpc_sub($quo, $x, $quo, $ROUND);
4561  
4562 return $quo;
4563 }
4564  
4565 Math_MPC__Math_MPFR: {
4566 $y = _mpfr2mpc($y);
4567 goto Math_MPC__Math_MPC;
4568 }
4569  
4570 Math_MPC__Math_GMPz: {
4571 $y = _mpz2mpc($y);
4572 goto Math_MPC__Math_MPC;
4573 }
4574  
4575 Math_MPC__Math_GMPq: {
4576 $y = _mpq2mpc($y);
4577 goto Math_MPC__Math_MPC;
4578 }
4579 }
4580  
4581 sub mod ($$) {
4582 my ($x, $y) = @_;
4583  
4584 $x =
4585 ref($x) eq __PACKAGE__ ? $$x
4586 : ref($x) ? _star2obj($x)
4587 : _str2obj($x);
4588  
4589 if (ref($y) eq __PACKAGE__) {
4590 return bless \__mod__($x, $$y);
4591 }
4592  
4593 if (!ref($y)) {
4594  
4595 if ( ref($x) ne 'Math::GMPq'
4596 and CORE::int($y) eq $y
4597 and $y > 0
4598 and $y < ULONG_MAX) {
4599 return bless \__mod__($x, $y);
4600 }
4601  
4602 return bless \__mod__($x, _str2obj($y));
4603 }
4604  
4605 bless \__mod__($x, _star2obj($y));
4606 }
4607  
4608 #
4609 ## IMOD
4610 #
4611  
4612 sub imod ($$) {
4613 my ($x, $y) = @_;
4614  
4615 $x = _star2mpz($x) // goto &nan;
4616  
4617 if (!ref($y) and CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
4618  
4619 CORE::int($y) || goto &nan;
4620  
4621 my $neg_y = $y < 0;
4622 $y = -$y if $neg_y;
4623  
4624 my $r = Math::GMPz::Rmpz_init();
4625 Math::GMPz::Rmpz_mod_ui($r, $x, $y);
4626  
4627 if (!Math::GMPz::Rmpz_sgn($r)) {
4628 ## OK
4629 }
4630 elsif ($neg_y) {
4631 Math::GMPz::Rmpz_sub_ui($r, $r, $y);
4632 }
4633  
4634 return bless \$r;
4635 }
4636  
4637 $y = _star2mpz($y) // goto &nan;
4638  
4639 my $sign_y = Math::GMPz::Rmpz_sgn($y) || goto &nan;
4640  
4641 my $r = Math::GMPz::Rmpz_init();
4642 Math::GMPz::Rmpz_mod($r, $x, $y);
4643  
4644 if (!Math::GMPz::Rmpz_sgn($r)) {
4645 ## OK
4646 }
4647 elsif ($sign_y < 0) {
4648 Math::GMPz::Rmpz_add($r, $r, $y);
4649 }
4650  
4651 bless \$r;
4652 }
4653  
4654 #
4655 ## POLYMOD
4656 #
4657  
4658 sub polymod {
4659 my @list = map { ref($_) eq __PACKAGE__ ? $$_ : _star2obj($_) } @_;
4660  
4661 my @r;
4662 my $x = shift(@list);
4663  
4664 foreach my $m (@list) {
4665 my $mod = __mod__($x, $m);
4666  
4667 $x = __sub__($x, $mod);
4668 $x = __div__($x, $m);
4669  
4670 push @r, $mod;
4671 }
4672  
4673 push @r, $x;
4674 map { bless \$_ } @r;
4675 }
4676  
4677 # Modular operations
4678  
4679 sub addmod ($$$) {
4680 my ($x, $y, $m) = @_;
4681  
4682 $x = _star2mpz($x) // goto &nan;
4683 $y = _star2mpz($y) // goto &nan;
4684 $m = _star2mpz($m) // goto &nan;
4685  
4686 my $r = Math::GMPz::Rmpz_init();
4687 Math::GMPz::Rmpz_add($r, $x, $y);
4688 Math::GMPz::Rmpz_mod($r, $r, $m);
4689 bless \$r;
4690 }
4691  
4692 sub submod ($$$) {
4693 my ($x, $y, $m) = @_;
4694  
4695 $x = _star2mpz($x) // goto &nan;
4696 $y = _star2mpz($y) // goto &nan;
4697 $m = _star2mpz($m) // goto &nan;
4698  
4699 my $r = Math::GMPz::Rmpz_init();
4700 Math::GMPz::Rmpz_sub($r, $x, $y);
4701 Math::GMPz::Rmpz_mod($r, $r, $m);
4702 bless \$r;
4703 }
4704  
4705 sub mulmod ($$$) {
4706 my ($x, $y, $m) = @_;
4707  
4708 $x = _star2mpz($x) // goto &nan;
4709 $y = _star2mpz($y) // goto &nan;
4710 $m = _star2mpz($m) // goto &nan;
4711  
4712 my $r = Math::GMPz::Rmpz_init();
4713 Math::GMPz::Rmpz_mul($r, $x, $y);
4714 Math::GMPz::Rmpz_mod($r, $r, $m);
4715 bless \$r;
4716 }
4717  
4718 #
4719 ## DIVMOD
4720 #
4721  
4722 sub divmod ($$;$) {
4723 my ($x, $y, $m) = @_;
4724  
4725 if (defined($m)) { # modular division
4726  
4727 $x = _star2mpz($x) // goto &nan;
4728 $y = _star2mpz($y) // goto &nan;
4729 $m = _star2mpz($m) // goto &nan;
4730  
4731 my $r = Math::GMPz::Rmpz_init();
4732  
4733 if (Math::GMPz::Rmpz_divisible_p($x, $y) and Math::GMPz::Rmpz_sgn($y)) {
4734 Math::GMPz::Rmpz_divexact($r, $x, $y);
4735 Math::GMPz::Rmpz_mod($r, $r, $m);
4736 }
4737 elsif (Math::GMPz::Rmpz_invert($r, $y, $m)) {
4738 Math::GMPz::Rmpz_mul($r, $r, $x);
4739 Math::GMPz::Rmpz_mod($r, $r, $m);
4740 }
4741 else {
4742 goto &nan;
4743 }
4744  
4745 return bless \$r;
4746 }
4747  
4748 $x = _star2mpz($x) // return (nan(), nan());
4749 $y = _star2mpz($y) // return (nan(), nan());
4750  
4751 Math::GMPz::Rmpz_sgn($y)
4752 || return (nan(), nan());
4753  
4754 my $r = Math::GMPz::Rmpz_init();
4755 my $s = Math::GMPz::Rmpz_init();
4756  
4757 Math::GMPz::Rmpz_divmod($r, $s, $x, $y);
4758  
4759 ((bless \$r), (bless \$s));
4760 }
4761  
4762 #
4763 ## is_div
4764 #
4765  
4766 sub is_div ($$) {
4767 my ($n, $k) = @_;
4768  
4769 if (ref($n) eq __PACKAGE__ and ref($$n) eq 'Math::GMPz') {
4770 if (ref($k)) {
4771 if (ref($k) eq __PACKAGE__ and ref($$k) eq 'Math::GMPz') {
4772 return (Math::GMPz::Rmpz_sgn($$k) && Math::GMPz::Rmpz_divisible_p($$n, $$k));
4773 }
4774 }
4775 elsif (CORE::int($k) eq $k and $k and $k < ULONG_MAX and $k > LONG_MIN) {
4776 return Math::GMPz::Rmpz_divisible_ui_p($$n, $k < 0 ? -$k : $k);
4777 }
4778 }
4779  
4780 @_ = (${mod($n, $k)}, 0);
4781 goto &__eq__;
4782 }
4783  
4784 #
4785 ## is_congruent
4786 #
4787  
4788 sub is_congruent ($$$) {
4789 my ($n, $k, $m) = @_;
4790  
4791 $n = $$n if (ref($n) eq __PACKAGE__);
4792 $k = $$k if (ref($k) eq __PACKAGE__);
4793 $m = $$m if (ref($m) eq __PACKAGE__);
4794  
4795 if (ref($n) eq 'Math::GMPz') {
4796  
4797 if ( !ref($k)
4798 and !ref($m)
4799 and CORE::int($k) eq $k
4800 and $k >= 0
4801 and $k < ULONG_MAX
4802 and CORE::int($m) eq $m
4803 and $m > 0
4804 and $m < ULONG_MAX) {
4805 return Math::GMPz::Rmpz_congruent_ui_p($n, $k, $m);
4806 }
4807  
4808 if (ref($k) eq 'Math::GMPz' and ref($m) eq 'Math::GMPz') {
4809 return (Math::GMPz::Rmpz_sgn($m) && Math::GMPz::Rmpz_congruent_p($n, $k, $m));
4810 }
4811 }
4812  
4813 $n = _star2obj($n) if !ref($n);
4814 $k = _star2obj($k) if !ref($k);
4815 $m = _star2obj($m) if !ref($m);
4816  
4817 if (ref($n) eq 'Math::GMPz' and ref($k) eq 'Math::GMPz' and ref($m) eq 'Math::GMPz') {
4818 return (Math::GMPz::Rmpz_sgn($m) && Math::GMPz::Rmpz_congruent_p($n, $k, $m));
4819 }
4820  
4821 @_ = (__mod__($n, $m), __mod__($k, $m));
4822 goto &__eq__;
4823 }
4824  
4825 #
4826 ## SPECIAL
4827 #
4828  
4829 #
4830 ## LOG
4831 #
4832  
4833 sub __log__ {
4834 my ($x) = @_;
4835 goto(ref($x) =~ tr/:/_/rs);
4836  
4837 Math_MPFR: {
4838  
4839 # Complex for x < 0
4840 if (Math::MPFR::Rmpfr_sgn($x) < 0) {
4841 my $r = _mpfr2mpc($x);
4842 Math::MPC::Rmpc_log($r, $r, $ROUND);
4843 return $r;
4844 }
4845  
4846 my $r = Math::MPFR::Rmpfr_init2($PREC);
4847 Math::MPFR::Rmpfr_log($r, $x, $ROUND);
4848 return $r;
4849 }
4850  
4851 Math_MPC: {
4852 my $r = Math::MPC::Rmpc_init2($PREC);
4853 Math::MPC::Rmpc_log($r, $x, $ROUND);
4854 return $r;
4855 }
4856  
4857 }
4858  
4859 #
4860 ## LOG_2
4861 #
4862  
4863 sub __log2__ {
4864 my ($x) = @_;
4865 goto(ref($x) =~ tr/:/_/rs);
4866  
4867 Math_MPFR: {
4868  
4869 # Complex for x < 0
4870 if (Math::MPFR::Rmpfr_sgn($x) < 0) {
4871 $x = _mpfr2mpc($x);
4872 goto Math_MPC;
4873 }
4874  
4875 my $r = Math::MPFR::Rmpfr_init2($PREC);
4876 Math::MPFR::Rmpfr_log2($r, $x, $ROUND);
4877 return $r;
4878 }
4879  
4880 Math_MPC: {
4881 my $r = Math::MPC::Rmpc_init2($PREC);
4882 my $ln2 = Math::MPFR::Rmpfr_init2($PREC);
4883 Math::MPFR::Rmpfr_const_log2($ln2, $ROUND);
4884 Math::MPC::Rmpc_log($r, $x, $ROUND);
4885 Math::MPC::Rmpc_div_fr($r, $r, $ln2, $ROUND);
4886 return $r;
4887 }
4888 }
4889  
4890 #
4891 ## LOG_10
4892 #
4893  
4894 sub __log10__ {
4895 my ($x) = @_;
4896 goto(ref($x) =~ tr/:/_/rs);
4897  
4898 Math_MPFR: {
4899  
4900 # Complex for x < 0
4901 if (Math::MPFR::Rmpfr_sgn($x) < 0) {
4902 $x = _mpfr2mpc($x);
4903 goto Math_MPC;
4904 }
4905  
4906 my $r = Math::MPFR::Rmpfr_init2($PREC);
4907 Math::MPFR::Rmpfr_log10($r, $x, $ROUND);
4908 return $r;
4909 }
4910  
4911 Math_MPC: {
4912 state $MPC_VERSION = Math::MPC::MPC_VERSION();
4913  
4914 my $r = Math::MPC::Rmpc_init2($PREC);
4915  
4916 if ($MPC_VERSION >= 65536) { # available only in mpc>=1.0.0
4917 Math::MPC::Rmpc_log10($r, $x, $ROUND);
4918 }
4919 else {
4920 my $ln10 = Math::MPFR::Rmpfr_init2($PREC);
4921 Math::MPFR::Rmpfr_set_ui($ln10, 10, $ROUND);
4922 Math::MPFR::Rmpfr_log($ln10, $ln10, $ROUND);
4923 Math::MPC::Rmpc_log($r, $x, $ROUND);
4924 Math::MPC::Rmpc_div_fr($r, $r, $ln10, $ROUND);
4925 }
4926  
4927 return $r;
4928 }
4929 }
4930  
4931 sub ln { # used in overloading
4932 bless \__log__(_star2mpfr_mpc($_[0]));
4933 }
4934  
4935 sub log2 ($) {
4936 bless \__log2__(_star2mpfr_mpc($_[0]));
4937 }
4938  
4939 sub log10 ($) {
4940 bless \__log10__(_star2mpfr_mpc($_[0]));
4941 }
4942  
4943 sub log (_;$) {
4944 my ($x, $y) = @_;
4945  
4946 if (!defined($y)) {
4947 return bless \__log__(_star2mpfr_mpc($x));
4948 }
4949  
4950 bless \__div__(__log__(_star2mpfr_mpc($x)), __log__(_star2mpfr_mpc($y)));
4951 }
4952  
4953 #
4954 ## Integer logarithm to a given base
4955 #
4956  
4957 sub __ilog__ {
4958 my ($x, $y) = @_;
4959  
4960 if (ref($y) eq 'Math::GMPz' and Math::GMPz::Rmpz_fits_ulong_p($y)) {
4961 $y = Math::GMPz::Rmpz_get_ui($y);
4962 }
4963  
4964 # ilog(x, y <= 1) = NaN
4965 $y <= 1 and return;
4966  
4967 # ilog(x <= 0, y) = NaN
4968 Math::GMPz::Rmpz_sgn($x) <= 0 and return;
4969  
4970 # ilog(x,y) = 0, when y > x
4971 (ref($y) ? Math::GMPz::Rmpz_cmp($x, $y) : Math::GMPz::Rmpz_cmp_ui($x, $y)) >= 0
4972 or return 0;
4973  
4974 # Return faster for y <= 62
4975 if ($y <= 62) {
4976  
4977 $y = Math::GMPz::Rmpz_get_ui($y) if ref($y);
4978  
4979 my $e = (Math::GMPz::Rmpz_sizeinbase($x, $y) || return) - 1;
4980  
4981 if ($e > 0) {
4982 state $t = Math::GMPz::Rmpz_init_nobless();
4983 Math::GMPz::Rmpz_ui_pow_ui($t, $y, $e);
4984 Math::GMPz::Rmpz_cmp($t, $x) > 0 and --$e;
4985 }
4986  
4987 return $e;
4988 }
4989  
4990 # Make sure `y` is a Math::GMPz object
4991 $y = Math::GMPz::Rmpz_init_set_ui($y) if !ref($y);
4992  
4993 my $e = 0;
4994  
4995 state $t = Math::GMPz::Rmpz_init_nobless();
4996 state $round_z = Math::MPFR::MPFR_RNDZ();
4997 state $logx = Math::MPFR::Rmpfr_init2_nobless(64);
4998 state $logy = Math::MPFR::Rmpfr_init2_nobless(64);
4999  
5000 Math::MPFR::Rmpfr_set_z($logx, $x, $round_z);
5001 Math::MPFR::Rmpfr_set_z($logy, $y, $round_z);
5002  
5003 Math::MPFR::Rmpfr_log($logx, $logx, $round_z);
5004 Math::MPFR::Rmpfr_log($logy, $logy, $round_z);
5005  
5006 Math::MPFR::Rmpfr_div($logx, $logx, $logy, $round_z);
5007  
5008 if (Math::MPFR::Rmpfr_fits_ulong_p($logx, $round_z)) {
5009 $e = Math::MPFR::Rmpfr_get_ui($logx, $round_z) - 1;
5010 Math::GMPz::Rmpz_pow_ui($t, $y, $e + 1);
5011 }
5012 else {
5013 Math::GMPz::Rmpz_set($t, $y);
5014 }
5015  
5016 for (; Math::GMPz::Rmpz_cmp($t, $x) <= 0 ; Math::GMPz::Rmpz_mul($t, $t, $y)) {
5017 ++$e;
5018 }
5019  
5020 return $e;
5021 }
5022  
5023 sub ilog2 ($) {
5024 my ($x) = @_;
5025 $x = _star2mpz($x) // goto &nan;
5026 bless \Math::GMPz::Rmpz_init_set_ui(__ilog__($x, 2) // goto &nan);
5027 }
5028  
5029 sub ilog10 ($) {
5030 my ($x) = @_;
5031 $x = _star2mpz($x) // goto &nan;
5032 bless \Math::GMPz::Rmpz_init_set_ui(__ilog__($x, 10) // goto &nan);
5033 }
5034  
5035 sub ilog ($;$) {
5036 my ($x, $y) = @_;
5037  
5038 if (!defined($y)) {
5039 return bless \(_any2mpz(__log__(_star2mpfr_mpc($x))) // goto &nan);
5040 }
5041  
5042 $x = _star2mpz($x) // goto &nan;
5043  
5044 if (!ref($y) and CORE::int($y) eq $y and $y > 1 and $y < ULONG_MAX) {
5045 ## y is a native integer -- OK
5046 }
5047 else {
5048 $y = _star2mpz($y) // goto &nan;
5049 }
5050  
5051 bless \Math::GMPz::Rmpz_init_set_ui(__ilog__($x, $y) // goto &nan);
5052 }
5053  
5054 sub length ($;$) {
5055 my ($x, $y) = @_;
5056  
5057 $x = _star2mpz($x) // return undef;
5058  
5059 my $neg = ((Math::GMPz::Rmpz_sgn($x) || return 1) < 0) ? 1 : 0;
5060  
5061 if (defined($y)) {
5062 if (!ref($y) and CORE::int($y) eq $y and $y > 1 and $y < ULONG_MAX) {
5063 ## y is a native integer -- OK
5064 }
5065 else {
5066 $y = _star2mpz($y) // return undef;
5067 }
5068 }
5069 else {
5070 $y = 10;
5071 }
5072  
5073 if ($neg) {
5074 $x = Math::GMPz::Rmpz_init_set($x);
5075 Math::GMPz::Rmpz_abs($x, $x);
5076 }
5077  
5078 1 + (__ilog__($x, $y) // return 0);
5079 }
5080  
5081 #
5082 ## Square root
5083 #
5084  
5085 sub __sqrt__ {
5086 my ($x) = @_;
5087 goto(ref($x) =~ tr/:/_/rs);
5088  
5089 Math_MPFR: {
5090  
5091 # Complex for x < 0
5092 if (Math::MPFR::Rmpfr_sgn($x) < 0) {
5093 my $r = _mpfr2mpc($x);
5094 Math::MPC::Rmpc_sqrt($r, $r, $ROUND);
5095 return $r;
5096 }
5097  
5098 my $r = Math::MPFR::Rmpfr_init2($PREC);
5099 Math::MPFR::Rmpfr_sqrt($r, $x, $ROUND);
5100 return $r;
5101 }
5102  
5103 Math_MPC: {
5104 my $r = Math::MPC::Rmpc_init2($PREC);
5105 Math::MPC::Rmpc_sqrt($r, $x, $ROUND);
5106 return $r;
5107 }
5108 }
5109  
5110 sub sqrt { # used in overloading
5111 bless \__sqrt__(_star2mpfr_mpc($_[0]));
5112 }
5113  
5114 #
5115 ## Cube root
5116 #
5117  
5118 sub __cbrt__ {
5119 my ($x) = @_;
5120 goto(ref($x) =~ tr/:/_/rs);
5121  
5122 Math_MPFR: {
5123  
5124 # Complex for x < 0
5125 if (Math::MPFR::Rmpfr_sgn($x) < 0) {
5126 $x = _mpfr2mpc($x);
5127 goto Math_MPC;
5128 }
5129  
5130 my $r = Math::MPFR::Rmpfr_init2($PREC);
5131 Math::MPFR::Rmpfr_cbrt($r, $x, $ROUND);
5132 return $r;
5133 }
5134  
5135 Math_MPC: {
5136  
5137 state $three_inv = do {
5138 my $r = Math::MPC::Rmpc_init2_nobless($PREC);
5139 Math::MPC::Rmpc_set_ui($r, 3, $ROUND);
5140 Math::MPC::Rmpc_ui_div($r, 1, $r, $ROUND);
5141 $r;
5142 };
5143  
5144 my $r = Math::MPC::Rmpc_init2($PREC);
5145 Math::MPC::Rmpc_pow($r, $x, $three_inv, $ROUND);
5146 return $r;
5147 }
5148 }
5149  
5150 sub cbrt ($) {
5151 bless \__cbrt__(_star2mpfr_mpc($_[0]));
5152 }
5153  
5154 #
5155 ## Square (x^2)
5156 #
5157  
5158 sub sqr ($) {
5159 my ($x) = @_;
5160  
5161 $x =
5162 ref($x) eq __PACKAGE__
5163 ? $$x
5164 : _star2obj($x);
5165  
5166 bless \__mul__($x, $x);
5167 }
5168  
5169 #
5170 ## Normalized value: norm(a + b*i) = a^2 + b^2
5171 #
5172  
5173 sub __norm__ {
5174 my ($x) = @_;
5175 goto(ref($x) =~ tr/:/_/rs);
5176  
5177 Math_MPC: {
5178 my $r = Math::MPFR::Rmpfr_init2($PREC);
5179 Math::MPC::Rmpc_norm($r, $x, $ROUND);
5180 return $r;
5181 }
5182  
5183 Math_MPFR: {
5184 my $r = Math::MPFR::Rmpfr_init2($PREC);
5185 Math::MPFR::Rmpfr_sqr($r, $x, $ROUND);
5186 return $r;
5187 }
5188  
5189 Math_GMPz: {
5190 my $r = Math::GMPz::Rmpz_init();
5191 Math::GMPz::Rmpz_mul($r, $x, $x);
5192 return $r;
5193 }
5194  
5195 Math_GMPq: {
5196 my $r = Math::GMPq::Rmpq_init();
5197 Math::GMPq::Rmpq_mul($r, $x, $x);
5198 return $r;
5199 }
5200 }
5201  
5202 sub norm ($) {
5203 my ($x) = @_;
5204  
5205 $x =
5206 ref($x) eq __PACKAGE__
5207 ? $$x
5208 : _star2obj($x);
5209  
5210 bless \__norm__($x);
5211 }
5212  
5213 #
5214 ## Natural exponentiation function (e^x)
5215 #
5216  
5217 sub __exp__ {
5218 my ($x) = @_;
5219 goto(ref($x) =~ tr/:/_/rs);
5220  
5221 Math_MPFR: {
5222 my $r = Math::MPFR::Rmpfr_init2($PREC);
5223 Math::MPFR::Rmpfr_exp($r, $x, $ROUND);
5224 return $r;
5225 }
5226  
5227 Math_MPC: {
5228 my $r = Math::MPC::Rmpc_init2($PREC);
5229 Math::MPC::Rmpc_exp($r, $x, $ROUND);
5230 return $r;
5231 }
5232 }
5233  
5234 sub exp { # used in overloading
5235 bless \__exp__(_star2mpfr_mpc($_[0]));
5236 }
5237  
5238 sub exp2 ($) {
5239 my ($x) = @_;
5240  
5241 state $base = Math::GMPz::Rmpz_init_set_ui(2);
5242  
5243 if (ref($x) eq __PACKAGE__) {
5244 bless \__pow__($base, $$x);
5245 }
5246 elsif (!ref($x) and CORE::int($x) eq $x and $x > LONG_MIN and $x < ULONG_MAX) {
5247 bless \__pow__($base, $x);
5248 }
5249 else {
5250 bless \__pow__($base, _star2obj($x));
5251 }
5252 }
5253  
5254 sub exp10 ($) {
5255 my ($x) = @_;
5256  
5257 state $base = Math::GMPz::Rmpz_init_set_ui(10);
5258  
5259 if (ref($x) eq __PACKAGE__) {
5260 bless \__pow__($base, $$x);
5261 }
5262 elsif (!ref($x) and CORE::int($x) eq $x and $x > LONG_MIN and $x < ULONG_MAX) {
5263 bless \__pow__($base, $x);
5264 }
5265 else {
5266 bless \__pow__($base, _star2obj($x));
5267 }
5268 }
5269  
5270 #
5271 ## floor(x) function -- round towards -Infinity
5272 #
5273  
5274 sub __floor__ {
5275 my ($x) = @_;
5276 goto(ref($x) =~ tr/:/_/rs);
5277  
5278 Math_MPFR: {
5279 my $r = Math::MPFR::Rmpfr_init2($PREC);
5280 Math::MPFR::Rmpfr_floor($r, $x);
5281 return $r;
5282 }
5283  
5284 Math_GMPq: {
5285 my $z = Math::GMPz::Rmpz_init();
5286 Math::GMPz::Rmpz_set_q($z, $x);
5287 Math::GMPq::Rmpq_integer_p($x) && return $z;
5288 Math::GMPz::Rmpz_sub_ui($z, $z, 1) if Math::GMPq::Rmpq_sgn($x) < 0;
5289 return $z;
5290 }
5291  
5292 Math_MPC: {
5293  
5294 my $real = Math::MPFR::Rmpfr_init2($PREC);
5295 my $imag = Math::MPFR::Rmpfr_init2($PREC);
5296  
5297 Math::MPC::RMPC_RE($real, $x);
5298 Math::MPC::RMPC_IM($imag, $x);
5299  
5300 Math::MPFR::Rmpfr_floor($real, $real);
5301 Math::MPFR::Rmpfr_floor($imag, $imag);
5302  
5303 if (Math::MPFR::Rmpfr_zero_p($imag)) {
5304 return $real;
5305 }
5306  
5307 my $r = Math::MPC::Rmpc_init2($PREC);
5308 Math::MPC::Rmpc_set_fr_fr($r, $real, $imag, $ROUND);
5309 return $r;
5310 }
5311 }
5312  
5313 sub floor ($) {
5314 my ($x) = @_;
5315  
5316 if (ref($x) ne __PACKAGE__) {
5317 $x = __PACKAGE__->new($x);
5318 }
5319  
5320 ref($$x) eq 'Math::GMPz' and return $x; # already an integer
5321 bless \__floor__($$x);
5322 }
5323  
5324 #
5325 ## ceil(x) function -- round towards +Infinity
5326 #
5327  
5328 sub __ceil__ {
5329 my ($x) = @_;
5330 goto(ref($x) =~ tr/:/_/rs);
5331  
5332 Math_MPFR: {
5333 my $r = Math::MPFR::Rmpfr_init2($PREC);
5334 Math::MPFR::Rmpfr_ceil($r, $x);
5335 return $r;
5336 }
5337  
5338 Math_GMPq: {
5339 my $z = Math::GMPz::Rmpz_init();
5340 Math::GMPz::Rmpz_set_q($z, $x);
5341 Math::GMPq::Rmpq_integer_p($x) && return $z;
5342 Math::GMPz::Rmpz_add_ui($z, $z, 1) if Math::GMPq::Rmpq_sgn($x) > 0;
5343 return $z;
5344 }
5345  
5346 Math_MPC: {
5347  
5348 my $real = Math::MPFR::Rmpfr_init2($PREC);
5349 my $imag = Math::MPFR::Rmpfr_init2($PREC);
5350  
5351 Math::MPC::RMPC_RE($real, $x);
5352 Math::MPC::RMPC_IM($imag, $x);
5353  
5354 Math::MPFR::Rmpfr_ceil($real, $real);
5355 Math::MPFR::Rmpfr_ceil($imag, $imag);
5356  
5357 if (Math::MPFR::Rmpfr_zero_p($imag)) {
5358 return $real;
5359 }
5360  
5361 my $r = Math::MPC::Rmpc_init2($PREC);
5362 Math::MPC::Rmpc_set_fr_fr($r, $real, $imag, $ROUND);
5363 return $r;
5364 }
5365 }
5366  
5367 sub ceil ($) {
5368 my ($x) = @_;
5369  
5370 if (ref($x) ne __PACKAGE__) {
5371 $x = __PACKAGE__->new($x);
5372 }
5373  
5374 ref($$x) eq 'Math::GMPz' and return $x; # already an integer
5375 bless \__ceil__($$x);
5376 }
5377  
5378 #
5379 ## sin / sinh / asin / asinh
5380 #
5381  
5382 sub __sin__ {
5383 my ($x) = @_;
5384 goto(ref($x) =~ tr/:/_/rs);
5385  
5386 Math_MPFR: {
5387 my $r = Math::MPFR::Rmpfr_init2($PREC);
5388 Math::MPFR::Rmpfr_sin($r, $x, $ROUND);
5389 return $r;
5390 }
5391  
5392 Math_MPC: {
5393 my $r = Math::MPC::Rmpc_init2($PREC);
5394 Math::MPC::Rmpc_sin($r, $x, $ROUND);
5395 return $r;
5396 }
5397 }
5398  
5399 sub sin { # used in overloading
5400 bless \__sin__(_star2mpfr_mpc($_[0]));
5401 }
5402  
5403 sub __sinh__ {
5404 my ($x) = @_;
5405 goto(ref($x) =~ tr/:/_/rs);
5406  
5407 Math_MPFR: {
5408 my $r = Math::MPFR::Rmpfr_init2($PREC);
5409 Math::MPFR::Rmpfr_sinh($r, $x, $ROUND);
5410 return $r;
5411 }
5412  
5413 Math_MPC: {
5414 my $r = Math::MPC::Rmpc_init2($PREC);
5415 Math::MPC::Rmpc_sinh($r, $x, $ROUND);
5416 return $r;
5417 }
5418 }
5419  
5420 sub sinh ($) {
5421 bless \__sinh__(_star2mpfr_mpc($_[0]));
5422 }
5423  
5424 sub __asin__ {
5425 my ($x) = @_;
5426 goto(ref($x) =~ tr/:/_/rs);
5427  
5428 Math_MPFR: {
5429  
5430 # Return a complex number for x < -1 or x > 1
5431 if ( Math::MPFR::Rmpfr_cmp_ui($x, 1) > 0
5432 or Math::MPFR::Rmpfr_cmp_si($x, -1) < 0) {
5433 my $r = _mpfr2mpc($x);
5434 Math::MPC::Rmpc_asin($r, $r, $ROUND);
5435 return $r;
5436 }
5437  
5438 my $r = Math::MPFR::Rmpfr_init2($PREC);
5439 Math::MPFR::Rmpfr_asin($r, $x, $ROUND);
5440 return $r;
5441 }
5442  
5443 Math_MPC: {
5444 my $r = Math::MPC::Rmpc_init2($PREC);
5445 Math::MPC::Rmpc_asin($r, $x, $ROUND);
5446 return $r;
5447 }
5448 }
5449  
5450 sub asin ($) {
5451 bless \__asin__(_star2mpfr_mpc($_[0]));
5452 }
5453  
5454 sub __asinh__ {
5455 my ($x) = @_;
5456 goto(ref($x) =~ tr/:/_/rs);
5457  
5458 Math_MPFR: {
5459 my $r = Math::MPFR::Rmpfr_init2($PREC);
5460 Math::MPFR::Rmpfr_asinh($r, $x, $ROUND);
5461 return $r;
5462 }
5463  
5464 Math_MPC: {
5465 my $r = Math::MPC::Rmpc_init2($PREC);
5466 Math::MPC::Rmpc_asinh($r, $x, $ROUND);
5467 return $r;
5468 }
5469 }
5470  
5471 sub asinh ($) {
5472 bless \__asinh__(_star2mpfr_mpc($_[0]));
5473 }
5474  
5475 #
5476 ## cos / cosh / acos / acosh
5477 #
5478  
5479 sub __cos__ {
5480 my ($x) = @_;
5481 goto(ref($x) =~ tr/:/_/rs);
5482  
5483 Math_MPFR: {
5484 my $r = Math::MPFR::Rmpfr_init2($PREC);
5485 Math::MPFR::Rmpfr_cos($r, $x, $ROUND);
5486 return $r;
5487 }
5488  
5489 Math_MPC: {
5490 my $r = Math::MPC::Rmpc_init2($PREC);
5491 Math::MPC::Rmpc_cos($r, $x, $ROUND);
5492 return $r;
5493 }
5494 }
5495  
5496 sub cos { # used in overloading
5497 bless \__cos__(_star2mpfr_mpc($_[0]));
5498 }
5499  
5500 sub __cosh__ {
5501 my ($x) = @_;
5502 goto(ref($x) =~ tr/:/_/rs);
5503  
5504 Math_MPFR: {
5505 my $r = Math::MPFR::Rmpfr_init2($PREC);
5506 Math::MPFR::Rmpfr_cosh($r, $x, $ROUND);
5507 return $r;
5508 }
5509  
5510 Math_MPC: {
5511 my $r = Math::MPC::Rmpc_init2($PREC);
5512 Math::MPC::Rmpc_cosh($r, $x, $ROUND);
5513 return $r;
5514 }
5515 }
5516  
5517 sub cosh ($) {
5518 bless \__cosh__(_star2mpfr_mpc($_[0]));
5519 }
5520  
5521 sub __acos__ {
5522 my ($x) = @_;
5523 goto(ref($x) =~ tr/:/_/rs);
5524  
5525 Math_MPFR: {
5526  
5527 # Return a complex number for x < -1 or x > 1
5528 if ( Math::MPFR::Rmpfr_cmp_ui($x, 1) > 0
5529 or Math::MPFR::Rmpfr_cmp_si($x, -1) < 0) {
5530 my $r = _mpfr2mpc($x);
5531 Math::MPC::Rmpc_acos($r, $r, $ROUND);
5532 return $r;
5533 }
5534  
5535 my $r = Math::MPFR::Rmpfr_init2($PREC);
5536 Math::MPFR::Rmpfr_acos($r, $x, $ROUND);
5537 return $r;
5538 }
5539  
5540 Math_MPC: {
5541 my $r = Math::MPC::Rmpc_init2($PREC);
5542 Math::MPC::Rmpc_acos($r, $x, $ROUND);
5543 return $r;
5544 }
5545 }
5546  
5547 sub acos ($) {
5548 bless \__acos__(_star2mpfr_mpc($_[0]));
5549 }
5550  
5551 sub __acosh__ {
5552 my ($x) = @_;
5553 goto(ref($x) =~ tr/:/_/rs);
5554  
5555 Math_MPFR: {
5556  
5557 # Return a complex number for x < 1
5558 if (Math::MPFR::Rmpfr_cmp_ui($x, 1) < 0) {
5559 my $r = _mpfr2mpc($x);
5560 Math::MPC::Rmpc_acosh($r, $r, $ROUND);
5561 return $r;
5562 }
5563  
5564 my $r = Math::MPFR::Rmpfr_init2($PREC);
5565 Math::MPFR::Rmpfr_acosh($r, $x, $ROUND);
5566 return $r;
5567 }
5568  
5569 Math_MPC: {
5570 my $r = Math::MPC::Rmpc_init2($PREC);
5571 Math::MPC::Rmpc_acosh($r, $x, $ROUND);
5572 return $r;
5573 }
5574 }
5575  
5576 sub acosh ($) {
5577 bless \__acosh__(_star2mpfr_mpc($_[0]));
5578 }
5579  
5580 #
5581 ## tan / tanh / atan / atanh
5582 #
5583  
5584 sub __tan__ {
5585 my ($x) = @_;
5586 goto(ref($x) =~ tr/:/_/rs);
5587  
5588 Math_MPFR: {
5589 my $r = Math::MPFR::Rmpfr_init2($PREC);
5590 Math::MPFR::Rmpfr_tan($r, $x, $ROUND);
5591 return $r;
5592 }
5593  
5594 Math_MPC: {
5595 my $r = Math::MPC::Rmpc_init2($PREC);
5596 Math::MPC::Rmpc_tan($r, $x, $ROUND);
5597 return $r;
5598 }
5599 }
5600  
5601 sub tan ($) {
5602 bless \__tan__(_star2mpfr_mpc($_[0]));
5603 }
5604  
5605 sub __tanh__ {
5606 my ($x) = @_;
5607 goto(ref($x) =~ tr/:/_/rs);
5608  
5609 Math_MPFR: {
5610 my $r = Math::MPFR::Rmpfr_init2($PREC);
5611 Math::MPFR::Rmpfr_tanh($r, $x, $ROUND);
5612 return $r;
5613 }
5614  
5615 Math_MPC: {
5616 my $r = Math::MPC::Rmpc_init2($PREC);
5617 Math::MPC::Rmpc_tanh($r, $x, $ROUND);
5618 return $r;
5619 }
5620 }
5621  
5622 sub tanh ($) {
5623 bless \__tanh__(_star2mpfr_mpc($_[0]));
5624 }
5625  
5626 sub __atan__ {
5627 my ($x) = @_;
5628 goto(ref($x) =~ tr/:/_/rs);
5629  
5630 Math_MPFR: {
5631 my $r = Math::MPFR::Rmpfr_init2($PREC);
5632 Math::MPFR::Rmpfr_atan($r, $x, $ROUND);
5633 return $r;
5634 }
5635  
5636 Math_MPC: {
5637 my $r = Math::MPC::Rmpc_init2($PREC);
5638 Math::MPC::Rmpc_atan($r, $x, $ROUND);
5639 return $r;
5640 }
5641 }
5642  
5643 sub atan ($) {
5644 bless \__atan__(_star2mpfr_mpc($_[0]));
5645 }
5646  
5647 sub __atanh__ {
5648 my ($x) = @_;
5649 goto(ref($x) =~ tr/:/_/rs);
5650  
5651 Math_MPFR: {
5652  
5653 # Return a complex number for x < -1 or x > 1
5654 if ( Math::MPFR::Rmpfr_cmp_ui($x, +1) > 0
5655 or Math::MPFR::Rmpfr_cmp_si($x, -1) < 0) {
5656 my $r = _mpfr2mpc($x);
5657 Math::MPC::Rmpc_atanh($r, $r, $ROUND);
5658 return $r;
5659 }
5660  
5661 my $r = Math::MPFR::Rmpfr_init2($PREC);
5662 Math::MPFR::Rmpfr_atanh($r, $x, $ROUND);
5663 return $r;
5664 }
5665  
5666 Math_MPC: {
5667 my $r = Math::MPC::Rmpc_init2($PREC);
5668 Math::MPC::Rmpc_atanh($r, $x, $ROUND);
5669 return $r;
5670 }
5671 }
5672  
5673 sub atanh ($) {
5674 bless \__atanh__(_star2mpfr_mpc($_[0]));
5675 }
5676  
5677 sub __atan2__ {
5678 my ($x, $y) = @_;
5679 goto(join('__', ref($x), ref($y)) =~ tr/:/_/rs);
5680  
5681 Math_MPFR__Math_MPFR: {
5682 my $r = Math::MPFR::Rmpfr_init2($PREC);
5683 Math::MPFR::Rmpfr_atan2($r, $x, $y, $ROUND);
5684 return $r;
5685 }
5686  
5687 Math_MPFR__Math_MPC: {
5688 $x = _mpfr2mpc($x);
5689 goto Math_MPC__Math_MPC;
5690 }
5691  
5692 Math_MPC__Math_MPFR: {
5693 $y = _mpfr2mpc($y);
5694 goto Math_MPC__Math_MPC;
5695 }
5696  
5697 #
5698 ## atan2(x, y) = -i * log((y + x*i) / sqrt(x^2 + y^2))
5699 #
5700 Math_MPC__Math_MPC: {
5701 my $r = Math::MPC::Rmpc_init2($PREC);
5702  
5703 Math::MPC::Rmpc_mul_i($r, $x, 1, $ROUND);
5704 Math::MPC::Rmpc_add($r, $r, $y, $ROUND);
5705  
5706 my $t1 = Math::MPC::Rmpc_init2($PREC);
5707 my $t2 = Math::MPC::Rmpc_init2($PREC);
5708  
5709 Math::MPC::Rmpc_sqr($t1, $x, $ROUND);
5710 Math::MPC::Rmpc_sqr($t2, $y, $ROUND);
5711 Math::MPC::Rmpc_add($t1, $t1, $t2, $ROUND);
5712 Math::MPC::Rmpc_sqrt($t1, $t1, $ROUND);
5713  
5714 Math::MPC::Rmpc_div($r, $r, $t1, $ROUND);
5715 Math::MPC::Rmpc_log($r, $r, $ROUND);
5716 Math::MPC::Rmpc_mul_i($r, $r, -1, $ROUND);
5717  
5718 return $r;
5719 }
5720 }
5721  
5722 sub atan2 ($$) {
5723 bless \__atan2__(_star2mpfr_mpc($_[0]), _star2mpfr_mpc($_[1]));
5724 }
5725  
5726 #
5727 ## sec / sech / asec / asech
5728 #
5729  
5730 sub __sec__ {
5731 my ($x) = @_;
5732 goto(ref($x) =~ tr/:/_/rs);
5733  
5734 Math_MPFR: {
5735 my $r = Math::MPFR::Rmpfr_init2($PREC);
5736 Math::MPFR::Rmpfr_sec($r, $x, $ROUND);
5737 return $r;
5738 }
5739  
5740 # sec(x) = 1/cos(x)
5741 Math_MPC: {
5742 my $r = Math::MPC::Rmpc_init2($PREC);
5743 Math::MPC::Rmpc_cos($r, $x, $ROUND);
5744 Math::MPC::Rmpc_ui_div($r, 1, $r, $ROUND);
5745 return $r;
5746 }
5747 }
5748  
5749 sub sec ($) {
5750 bless \__sec__(_star2mpfr_mpc($_[0]));
5751 }
5752  
5753 sub __sech__ {
5754 my ($x) = @_;
5755 goto(ref($x) =~ tr/:/_/rs);
5756  
5757 Math_MPFR: {
5758 my $r = Math::MPFR::Rmpfr_init2($PREC);
5759 Math::MPFR::Rmpfr_sech($r, $x, $ROUND);
5760 return $r;
5761 }
5762  
5763 # sech(x) = 1/cosh(x)
5764 Math_MPC: {
5765 my $r = Math::MPC::Rmpc_init2($PREC);
5766 Math::MPC::Rmpc_cosh($r, $x, $ROUND);
5767 Math::MPC::Rmpc_ui_div($r, 1, $r, $ROUND);
5768 return $r;
5769 }
5770 }
5771  
5772 sub sech ($) {
5773 bless \__sech__(_star2mpfr_mpc($_[0]));
5774 }
5775  
5776 sub __asec__ {
5777 my ($x) = @_;
5778 goto(ref($x) =~ tr/:/_/rs);
5779  
5780 # asec(x) = acos(1/x)
5781 Math_MPFR: {
5782  
5783 # Return a complex number for x > -1 and x < 1
5784 if ( Math::MPFR::Rmpfr_cmp_ui($x, 1) < 0
5785 and Math::MPFR::Rmpfr_cmp_si($x, -1) > 0) {
5786 $x = _mpfr2mpc($x);
5787 goto Math_MPC;
5788 }
5789  
5790 my $r = Math::MPFR::Rmpfr_init2($PREC);
5791 Math::MPFR::Rmpfr_ui_div($r, 1, $x, $ROUND);
5792 Math::MPFR::Rmpfr_acos($r, $r, $ROUND);
5793 return $r;
5794 }
5795  
5796 # asec(x) = acos(1/x)
5797 Math_MPC: {
5798 my $r = Math::MPC::Rmpc_init2($PREC);
5799 Math::MPC::Rmpc_ui_div($r, 1, $x, $ROUND);
5800 Math::MPC::Rmpc_acos($r, $r, $ROUND);
5801 return $r;
5802 }
5803 }
5804  
5805 sub asec ($) {
5806 bless \__asec__(_star2mpfr_mpc($_[0]));
5807 }
5808  
5809 sub __asech__ {
5810 my ($x) = @_;
5811 goto(ref($x) =~ tr/:/_/rs);
5812  
5813 # asech(x) = acosh(1/x)
5814 Math_MPFR: {
5815  
5816 # Return a complex number for x < 0 or x > 1
5817 if ( Math::MPFR::Rmpfr_cmp_ui($x, 1) > 0
5818 or Math::MPFR::Rmpfr_cmp_ui($x, 0) < 0) {
5819 $x = _mpfr2mpc($x);
5820 goto Math_MPC;
5821 }
5822  
5823 my $r = Math::MPFR::Rmpfr_init2($PREC);
5824 Math::MPFR::Rmpfr_ui_div($r, 1, $x, $ROUND);
5825 Math::MPFR::Rmpfr_acosh($r, $r, $ROUND);
5826 return $r;
5827 }
5828  
5829 # asech(x) = acosh(1/x)
5830 Math_MPC: {
5831 my $r = Math::MPC::Rmpc_init2($PREC);
5832 Math::MPC::Rmpc_ui_div($r, 1, $x, $ROUND);
5833 Math::MPC::Rmpc_acosh($r, $r, $ROUND);
5834 return $r;
5835 }
5836 }
5837  
5838 sub asech ($) {
5839 bless \__asech__(_star2mpfr_mpc($_[0]));
5840 }
5841  
5842 #
5843 ## csc / csch / acsc / acsch
5844 #
5845  
5846 sub __csc__ {
5847 my ($x) = @_;
5848 goto(ref($x) =~ tr/:/_/rs);
5849  
5850 Math_MPFR: {
5851 my $r = Math::MPFR::Rmpfr_init2($PREC);
5852 Math::MPFR::Rmpfr_csc($r, $x, $ROUND);
5853 return $r;
5854 }
5855  
5856 # csc(x) = 1/sin(x)
5857 Math_MPC: {
5858 my $r = Math::MPC::Rmpc_init2($PREC);
5859 Math::MPC::Rmpc_sin($r, $x, $ROUND);
5860 Math::MPC::Rmpc_ui_div($r, 1, $r, $ROUND);
5861 return $r;
5862 }
5863 }
5864  
5865 sub csc ($) {
5866 bless \__csc__(_star2mpfr_mpc($_[0]));
5867 }
5868  
5869 sub __csch__ {
5870 my ($x) = @_;
5871 goto(ref($x) =~ tr/:/_/rs);
5872  
5873 Math_MPFR: {
5874 my $r = Math::MPFR::Rmpfr_init2($PREC);
5875 Math::MPFR::Rmpfr_csch($r, $x, $ROUND);
5876 return $r;
5877 }
5878  
5879 # csch(x) = 1/sinh(x)
5880 Math_MPC: {
5881 my $r = Math::MPC::Rmpc_init2($PREC);
5882 Math::MPC::Rmpc_sinh($r, $x, $ROUND);
5883 Math::MPC::Rmpc_ui_div($r, 1, $r, $ROUND);
5884 return $r;
5885 }
5886 }
5887  
5888 sub csch ($) {
5889 bless \__csch__(_star2mpfr_mpc($_[0]));
5890 }
5891  
5892 sub __acsc__ {
5893 my ($x) = @_;
5894 goto(ref($x) =~ tr/:/_/rs);
5895  
5896 # acsc(x) = asin(1/x)
5897 Math_MPFR: {
5898  
5899 # Return a complex number for x > -1 and x < 1
5900 if ( Math::MPFR::Rmpfr_cmp_ui($x, 1) < 0
5901 and Math::MPFR::Rmpfr_cmp_si($x, -1) > 0) {
5902 $x = _mpfr2mpc($x);
5903 goto Math_MPC;
5904 }
5905  
5906 my $r = Math::MPFR::Rmpfr_init2($PREC);
5907 Math::MPFR::Rmpfr_ui_div($r, 1, $x, $ROUND);
5908 Math::MPFR::Rmpfr_asin($r, $r, $ROUND);
5909 return $r;
5910 }
5911  
5912 # acsc(x) = asin(1/x)
5913 Math_MPC: {
5914 my $r = Math::MPC::Rmpc_init2($PREC);
5915 Math::MPC::Rmpc_ui_div($r, 1, $x, $ROUND);
5916 Math::MPC::Rmpc_asin($r, $r, $ROUND);
5917 return $r;
5918 }
5919 }
5920  
5921 sub acsc ($) {
5922 bless \__acsc__(_star2mpfr_mpc($_[0]));
5923 }
5924  
5925 sub __acsch__ {
5926 my ($x) = @_;
5927 goto(ref($x) =~ tr/:/_/rs);
5928  
5929 # acsch(x) = asinh(1/x)
5930 Math_MPFR: {
5931 my $r = Math::MPFR::Rmpfr_init2($PREC);
5932 Math::MPFR::Rmpfr_ui_div($r, 1, $x, $ROUND);
5933 Math::MPFR::Rmpfr_asinh($r, $r, $ROUND);
5934 return $r;
5935 }
5936  
5937 # acsch(x) = asinh(1/x)
5938 Math_MPC: {
5939 my $r = Math::MPC::Rmpc_init2($PREC);
5940 Math::MPC::Rmpc_ui_div($r, 1, $x, $ROUND);
5941 Math::MPC::Rmpc_asinh($r, $r, $ROUND);
5942 return $r;
5943 }
5944 }
5945  
5946 sub acsch ($) {
5947 bless \__acsch__(_star2mpfr_mpc($_[0]));
5948 }
5949  
5950 #
5951 ## cot / coth / acot / acoth
5952 #
5953  
5954 sub __cot__ {
5955 my ($x) = @_;
5956 goto(ref($x) =~ tr/:/_/rs);
5957  
5958 Math_MPFR: {
5959 my $r = Math::MPFR::Rmpfr_init2($PREC);
5960 Math::MPFR::Rmpfr_cot($r, $x, $ROUND);
5961 return $r;
5962 }
5963  
5964 # cot(x) = 1/tan(x)
5965 Math_MPC: {
5966 my $r = Math::MPC::Rmpc_init2($PREC);
5967 Math::MPC::Rmpc_tan($r, $x, $ROUND);
5968 Math::MPC::Rmpc_ui_div($r, 1, $r, $ROUND);
5969 return $r;
5970 }
5971 }
5972  
5973 sub cot ($) {
5974 bless \__cot__(_star2mpfr_mpc($_[0]));
5975 }
5976  
5977 sub __coth__ {
5978 my ($x) = @_;
5979 goto(ref($x) =~ tr/:/_/rs);
5980  
5981 Math_MPFR: {
5982 my $r = Math::MPFR::Rmpfr_init2($PREC);
5983 Math::MPFR::Rmpfr_coth($r, $x, $ROUND);
5984 return $r;
5985 }
5986  
5987 # coth(x) = 1/tanh(x)
5988 Math_MPC: {
5989 my $r = Math::MPC::Rmpc_init2($PREC);
5990 Math::MPC::Rmpc_tanh($r, $x, $ROUND);
5991 Math::MPC::Rmpc_ui_div($r, 1, $r, $ROUND);
5992 return $r;
5993 }
5994 }
5995  
5996 sub coth ($) {
5997 bless \__coth__(_star2mpfr_mpc($_[0]));
5998 }
5999  
6000 sub __acot__ {
6001 my ($x) = @_;
6002 goto(ref($x) =~ tr/:/_/rs);
6003  
6004 # acot(x) = atan(1/x)
6005 Math_MPFR: {
6006 my $r = Math::MPFR::Rmpfr_init2($PREC);
6007 Math::MPFR::Rmpfr_ui_div($r, 1, $x, $ROUND);
6008 Math::MPFR::Rmpfr_atan($r, $r, $ROUND);
6009 return $r;
6010 }
6011  
6012 # acot(x) = atan(1/x)
6013 Math_MPC: {
6014 my $r = Math::MPC::Rmpc_init2($PREC);
6015 Math::MPC::Rmpc_ui_div($r, 1, $x, $ROUND);
6016 Math::MPC::Rmpc_atan($r, $r, $ROUND);
6017 return $r;
6018 }
6019 }
6020  
6021 sub acot ($) {
6022 bless \__acot__(_star2mpfr_mpc($_[0]));
6023 }
6024  
6025 sub __acoth__ {
6026 my ($x) = @_;
6027 goto(ref($x) =~ tr/:/_/rs);
6028  
6029 # acoth(x) = atanh(1/x)
6030 Math_MPFR: {
6031  
6032 # Return a complex number for x > -1 and x < 1
6033 if ( Math::MPFR::Rmpfr_cmp_ui($x, 1) < 0
6034 and Math::MPFR::Rmpfr_cmp_si($x, -1) > 0) {
6035 $x = _mpfr2mpc($x);
6036 goto Math_MPC;
6037 }
6038  
6039 my $r = Math::MPFR::Rmpfr_init2($PREC);
6040 Math::MPFR::Rmpfr_ui_div($r, 1, $x, $ROUND);
6041 Math::MPFR::Rmpfr_atanh($r, $r, $ROUND);
6042 return $r;
6043 }
6044  
6045 # acoth(x) = atanh(1/x)
6046 Math_MPC: {
6047 my $r = Math::MPC::Rmpc_init2($PREC);
6048 Math::MPC::Rmpc_ui_div($r, 1, $x, $ROUND);
6049 Math::MPC::Rmpc_atanh($r, $r, $ROUND);
6050 return $r;
6051 }
6052 }
6053  
6054 sub acoth ($) {
6055 bless \__acoth__(_star2mpfr_mpc($_[0]));
6056 }
6057  
6058 sub deg2rad ($) {
6059 my ($x) = @_;
6060 my $f = Math::MPFR::Rmpfr_init2($PREC);
6061 Math::MPFR::Rmpfr_const_pi($f, $ROUND);
6062 Math::MPFR::Rmpfr_div_ui($f, $f, 180, $ROUND);
6063 bless \__mul__(_star2obj($x), $f);
6064 }
6065  
6066 sub rad2deg ($) {
6067 my ($x) = @_;
6068 my $f = Math::MPFR::Rmpfr_init2($PREC);
6069 Math::MPFR::Rmpfr_const_pi($f, $ROUND);
6070 Math::MPFR::Rmpfr_ui_div($f, 180, $f, $ROUND);
6071 bless \__mul__(_star2obj($x), $f);
6072 }
6073  
6074 #
6075 ## gamma
6076 #
6077  
6078 sub gamma ($) {
6079 my $r = Math::MPFR::Rmpfr_init2($PREC);
6080 Math::MPFR::Rmpfr_gamma($r, _star2mpfr($_[0]), $ROUND);
6081 bless \$r;
6082 }
6083  
6084 #
6085 ## lgamma
6086 #
6087  
6088 sub lgamma ($) {
6089 my $r = Math::MPFR::Rmpfr_init2($PREC);
6090 Math::MPFR::Rmpfr_lgamma($r, _star2mpfr($_[0]), $ROUND);
6091 bless \$r;
6092 }
6093  
6094 #
6095 ## lngamma
6096 #
6097  
6098 sub lngamma ($) {
6099 my $r = Math::MPFR::Rmpfr_init2($PREC);
6100 Math::MPFR::Rmpfr_lngamma($r, _star2mpfr($_[0]), $ROUND);
6101 bless \$r;
6102 }
6103  
6104 #
6105 ## digamma
6106 #
6107  
6108 sub digamma ($) {
6109 my $r = Math::MPFR::Rmpfr_init2($PREC);
6110 Math::MPFR::Rmpfr_digamma($r, _star2mpfr($_[0]), $ROUND);
6111 bless \$r;
6112 }
6113  
6114 #
6115 ## zeta
6116 #
6117  
6118 sub zeta ($) {
6119 my ($x) = @_;
6120  
6121 if (!ref($x) and CORE::int($x) eq $x and $x >= 0 and $x < ULONG_MAX) {
6122 ## $x is an unsigned integer
6123 }
6124 else {
6125 $x = _star2mpfr($x);
6126  
6127 # If $x fits inside an unsigned integer, then unpack it.
6128 if ( Math::MPFR::Rmpfr_integer_p($x)
6129 and Math::MPFR::Rmpfr_fits_ulong_p($x, $ROUND)) {
6130 $x = Math::MPFR::Rmpfr_get_ui($x, $ROUND);
6131 }
6132 }
6133  
6134 my $r = Math::MPFR::Rmpfr_init2($PREC);
6135  
6136 ref($x)
6137 ? Math::MPFR::Rmpfr_zeta($r, $x, $ROUND)
6138 : Math::MPFR::Rmpfr_zeta_ui($r, $x, $ROUND);
6139  
6140 bless \$r;
6141 }
6142  
6143 #
6144 ## Dirichlet eta function
6145 #
6146  
6147 # Implemented as:
6148 # eta(1) = ln(2)
6149 # eta(x) = (1 - 2**(1-x)) * zeta(x)
6150  
6151 sub eta ($) {
6152 my $x = _star2mpfr($_[0]);
6153  
6154 my $r = Math::MPFR::Rmpfr_init2($PREC);
6155 my $x_is_int = Math::MPFR::Rmpfr_integer_p($x);
6156  
6157 # Special case for eta(1) = log(2)
6158 if ($x_is_int and Math::MPFR::Rmpfr_cmp_ui($x, 1) == 0) {
6159 Math::MPFR::Rmpfr_const_log2($r, $ROUND);
6160 return bless \$r;
6161 }
6162  
6163 my $t = Math::MPFR::Rmpfr_init2($PREC);
6164  
6165 Math::MPFR::Rmpfr_ui_sub($r, 1, $x, $ROUND);
6166 Math::MPFR::Rmpfr_ui_pow($r, 2, $r, $ROUND);
6167 Math::MPFR::Rmpfr_ui_sub($r, 1, $r, $ROUND);
6168  
6169 if ($x_is_int and Math::MPFR::Rmpfr_fits_ulong_p($x, $ROUND)) {
6170 Math::MPFR::Rmpfr_zeta_ui($t, Math::MPFR::Rmpfr_get_ui($x, $ROUND), $ROUND);
6171 }
6172 else {
6173 Math::MPFR::Rmpfr_zeta($t, $x, $ROUND);
6174 }
6175  
6176 Math::MPFR::Rmpfr_mul($r, $r, $t, $ROUND);
6177 bless \$r;
6178 }
6179  
6180 #
6181 ## Beta(x,y) function
6182 #
6183  
6184 # Implemented as:
6185 # beta(x,y) = gamma(x)*gamma(y) / gamma(x+y)
6186  
6187 sub beta ($$) {
6188 my $x = _star2mpfr($_[0]);
6189 my $y = _star2mpfr($_[1]);
6190  
6191 state $has_beta = (Math::MPFR::MPFR_VERSION_MAJOR() >= 4);
6192  
6193 if ($has_beta) { # available since mpfr-4.0.0
6194 my $r = Math::MPFR::Rmpfr_init2($PREC);
6195 Math::MPFR::Rmpfr_beta($r, $x, $y, $ROUND);
6196 return bless \$r;
6197 }
6198  
6199 my $t1 = Math::MPFR::Rmpfr_init2($PREC); # gamma(x+y)
6200 my $t2 = Math::MPFR::Rmpfr_init2($PREC); # gamma(y)
6201  
6202 my $r = Math::MPFR::Rmpfr_init2($PREC);
6203  
6204 Math::MPFR::Rmpfr_add($t1, $x, $y, $ROUND);
6205 Math::MPFR::Rmpfr_gamma($t1, $t1, $ROUND);
6206 Math::MPFR::Rmpfr_gamma($r, $x, $ROUND);
6207 Math::MPFR::Rmpfr_gamma($t2, $y, $ROUND);
6208 Math::MPFR::Rmpfr_mul($r, $r, $t2, $ROUND);
6209 Math::MPFR::Rmpfr_div($r, $r, $t1, $ROUND);
6210  
6211 bless \$r;
6212 }
6213  
6214 #
6215 ## Airy function (Ai)
6216 #
6217  
6218 sub Ai ($) {
6219 my $r = Math::MPFR::Rmpfr_init2($PREC);
6220 Math::MPFR::Rmpfr_ai($r, _star2mpfr($_[0]), $ROUND);
6221 bless \$r;
6222 }
6223  
6224 #
6225 ## Exponential integral (Ei)
6226 #
6227  
6228 sub Ei ($) {
6229 my $r = Math::MPFR::Rmpfr_init2($PREC);
6230 Math::MPFR::Rmpfr_eint($r, _star2mpfr($_[0]), $ROUND);
6231 bless \$r;
6232 }
6233  
6234 #
6235 ## Logarithmic integral (Li)
6236 #
6237 sub Li ($) {
6238 my $r = Math::MPFR::Rmpfr_init2($PREC);
6239 Math::MPFR::Rmpfr_log($r, _star2mpfr($_[0]), $ROUND);
6240 Math::MPFR::Rmpfr_eint($r, $r, $ROUND);
6241 bless \$r;
6242 }
6243  
6244 #
6245 ## Dilogarithm function (Li_2)
6246 #
6247 sub Li2 ($) {
6248 my $r = Math::MPFR::Rmpfr_init2($PREC);
6249 Math::MPFR::Rmpfr_li2($r, _star2mpfr($_[0]), $ROUND);
6250 bless \$r;
6251 }
6252  
6253 #
6254 ## Error function
6255 #
6256 sub erf ($) {
6257 my $r = Math::MPFR::Rmpfr_init2($PREC);
6258 Math::MPFR::Rmpfr_erf($r, _star2mpfr($_[0]), $ROUND);
6259 bless \$r;
6260 }
6261  
6262 #
6263 ## Complementary error function
6264 #
6265 sub erfc ($) {
6266 my $r = Math::MPFR::Rmpfr_init2($PREC);
6267 Math::MPFR::Rmpfr_erfc($r, _star2mpfr($_[0]), $ROUND);
6268 bless \$r;
6269 }
6270  
6271 #
6272 ## Lambert W function
6273 #
6274  
6275 sub __LambertW__ {
6276 my ($x) = @_;
6277  
6278 my $p = Math::MPFR::Rmpfr_init2($PREC);
6279 Math::MPFR::Rmpfr_set_str($p, '1e-' . CORE::int($PREC >> 2), 10, $ROUND);
6280  
6281 goto(ref($x) =~ tr/:/_/rs);
6282  
6283 Math_MPFR: {
6284  
6285 # Return a complex number for x < -1/e
6286 if (Math::MPFR::Rmpfr_cmp_d($x, -1 / CORE::exp(1)) < 0) {
6287 $x = _mpfr2mpc($x);
6288 goto Math_MPC;
6289 }
6290  
6291 Math::MPFR::Rmpfr_set_ui((my $r = Math::MPFR::Rmpfr_init2($PREC)), 1, $ROUND);
6292 Math::MPFR::Rmpfr_set_ui((my $y = Math::MPFR::Rmpfr_init2($PREC)), 0, $ROUND);
6293  
6294 my $count = 0;
6295 my $tmp = Math::MPFR::Rmpfr_init2($PREC);
6296  
6297 while (1) {
6298 Math::MPFR::Rmpfr_sub($tmp, $r, $y, $ROUND);
6299 Math::MPFR::Rmpfr_cmpabs($tmp, $p) <= 0 and last;
6300  
6301 Math::MPFR::Rmpfr_set($y, $r, $ROUND);
6302  
6303 Math::MPFR::Rmpfr_log($tmp, $r, $ROUND);
6304 Math::MPFR::Rmpfr_add_ui($tmp, $tmp, 1, $ROUND);
6305  
6306 Math::MPFR::Rmpfr_add($r, $r, $x, $ROUND);
6307 Math::MPFR::Rmpfr_div($r, $r, $tmp, $ROUND);
6308 last if ++$count > $PREC;
6309 }
6310  
6311 Math::MPFR::Rmpfr_log($r, $r, $ROUND);
6312 return $r;
6313 }
6314  
6315 Math_MPC: {
6316 my $r = Math::MPC::Rmpc_init2($PREC);
6317 Math::MPC::Rmpc_sqrt($r, $x, $ROUND);
6318 Math::MPC::Rmpc_add_ui($r, $r, 1, $ROUND);
6319  
6320 my $y = Math::MPC::Rmpc_init2($PREC);
6321 Math::MPC::Rmpc_set_ui($y, 0, $ROUND);
6322  
6323 my $tmp = Math::MPC::Rmpc_init2($PREC);
6324 my $abs = Math::MPFR::Rmpfr_init2($PREC);
6325  
6326 my $count = 0;
6327 while (1) {
6328 Math::MPC::Rmpc_sub($tmp, $r, $y, $ROUND);
6329  
6330 Math::MPC::Rmpc_abs($abs, $tmp, $ROUND);
6331 Math::MPFR::Rmpfr_cmp($abs, $p) <= 0 and last;
6332  
6333 Math::MPC::Rmpc_set($y, $r, $ROUND);
6334  
6335 Math::MPC::Rmpc_log($tmp, $r, $ROUND);
6336 Math::MPC::Rmpc_add_ui($tmp, $tmp, 1, $ROUND);
6337  
6338 Math::MPC::Rmpc_add($r, $r, $x, $ROUND);
6339 Math::MPC::Rmpc_div($r, $r, $tmp, $ROUND);
6340 last if ++$count > $PREC;
6341 }
6342  
6343 Math::MPC::Rmpc_log($r, $r, $ROUND);
6344 return $r;
6345 }
6346 }
6347  
6348 sub LambertW ($) {
6349 bless \__LambertW__(_star2mpfr_mpc($_[0]));
6350 }
6351  
6352 #
6353 ## lgrt -- logarithmic root
6354 #
6355  
6356 sub __lgrt__ {
6357 my ($c) = @_;
6358  
6359 my $p = Math::MPFR::Rmpfr_init2($PREC);
6360 Math::MPFR::Rmpfr_set_str($p, '1e-' . CORE::int($PREC >> 2), 10, $ROUND);
6361  
6362 goto(ref($c) =~ tr/:/_/rs);
6363  
6364 Math_MPFR: {
6365  
6366 # Return a complex number for x < e^(-1/e)
6367 if (Math::MPFR::Rmpfr_cmp_d($c, CORE::exp(-1 / CORE::exp(1))) < 0) {
6368 $c = _mpfr2mpc($c);
6369 goto Math_MPC;
6370 }
6371  
6372 my $r = Math::MPFR::Rmpfr_init2($PREC);
6373 Math::MPFR::Rmpfr_log($r, $c, $ROUND);
6374  
6375 Math::MPFR::Rmpfr_set_ui((my $x = Math::MPFR::Rmpfr_init2($PREC)), 1, $ROUND);
6376 Math::MPFR::Rmpfr_set_ui((my $y = Math::MPFR::Rmpfr_init2($PREC)), 0, $ROUND);
6377  
6378 my $count = 0;
6379 my $tmp = Math::MPFR::Rmpfr_init2($PREC);
6380  
6381 while (1) {
6382 Math::MPFR::Rmpfr_sub($tmp, $x, $y, $ROUND);
6383 Math::MPFR::Rmpfr_cmpabs($tmp, $p) <= 0 and last;
6384  
6385 Math::MPFR::Rmpfr_set($y, $x, $ROUND);
6386  
6387 Math::MPFR::Rmpfr_log($tmp, $x, $ROUND);
6388 Math::MPFR::Rmpfr_add_ui($tmp, $tmp, 1, $ROUND);
6389  
6390 Math::MPFR::Rmpfr_add($x, $x, $r, $ROUND);
6391 Math::MPFR::Rmpfr_div($x, $x, $tmp, $ROUND);
6392 last if ++$count > $PREC;
6393 }
6394  
6395 return $x;
6396 }
6397  
6398 Math_MPC: {
6399 my $d = Math::MPC::Rmpc_init2($PREC);
6400 Math::MPC::Rmpc_log($d, $c, $ROUND);
6401  
6402 my $x = Math::MPC::Rmpc_init2($PREC);
6403 Math::MPC::Rmpc_sqrt($x, $c, $ROUND);
6404 Math::MPC::Rmpc_add_ui($x, $x, 1, $ROUND);
6405 Math::MPC::Rmpc_log($x, $x, $ROUND);
6406  
6407 my $y = Math::MPC::Rmpc_init2($PREC);
6408 Math::MPC::Rmpc_set_ui($y, 0, $ROUND);
6409  
6410 my $tmp = Math::MPC::Rmpc_init2($PREC);
6411 my $abs = Math::MPFR::Rmpfr_init2($PREC);
6412  
6413 my $count = 0;
6414 while (1) {
6415 Math::MPC::Rmpc_sub($tmp, $x, $y, $ROUND);
6416  
6417 Math::MPC::Rmpc_abs($abs, $tmp, $ROUND);
6418 Math::MPFR::Rmpfr_cmp($abs, $p) <= 0 and last;
6419  
6420 Math::MPC::Rmpc_set($y, $x, $ROUND);
6421  
6422 Math::MPC::Rmpc_log($tmp, $x, $ROUND);
6423 Math::MPC::Rmpc_add_ui($tmp, $tmp, 1, $ROUND);
6424  
6425 Math::MPC::Rmpc_add($x, $x, $d, $ROUND);
6426 Math::MPC::Rmpc_div($x, $x, $tmp, $ROUND);
6427 last if ++$count > $PREC;
6428 }
6429  
6430 return $x;
6431 }
6432 }
6433  
6434 sub lgrt ($) {
6435 bless \__lgrt__(_star2mpfr_mpc($_[0]));
6436 }
6437  
6438 #
6439 ## Arithmetic-geometric mean
6440 #
6441  
6442 sub __agm__ {
6443 my ($x, $y) = @_;
6444 goto(join('__', ref($x), ref($y)) =~ tr/:/_/rs);
6445  
6446 Math_MPFR__Math_MPFR: {
6447  
6448 if ( Math::MPFR::Rmpfr_sgn($x) < 0
6449 or Math::MPFR::Rmpfr_sgn($y) < 0) {
6450  
6451 $x = _mpfr2mpc($x);
6452 $y = _mpfr2mpc($y);
6453  
6454 goto Math_MPC__Math_MPC;
6455 }
6456  
6457 my $r = Math::MPFR::Rmpfr_init2($PREC);
6458 Math::MPFR::Rmpfr_agm($r, $x, $y, $ROUND);
6459 return $r;
6460 }
6461  
6462 Math_MPC__Math_MPC: {
6463  
6464 # agm(0, x) = 0
6465 Math::MPC::Rmpc_cmp_si_si($x, 0, 0) || return $x;
6466  
6467 # agm(x, 0) = 0
6468 Math::MPC::Rmpc_cmp_si_si($y, 0, 0) || return $y;
6469  
6470 my $a0 = Math::MPC::Rmpc_init2($PREC);
6471 my $g0 = Math::MPC::Rmpc_init2($PREC);
6472  
6473 my $a1 = Math::MPC::Rmpc_init2($PREC);
6474 my $g1 = Math::MPC::Rmpc_init2($PREC);
6475  
6476 my $t = Math::MPC::Rmpc_init2($PREC);
6477  
6478 Math::MPC::Rmpc_set($a0, $x, $ROUND);
6479 Math::MPC::Rmpc_set($g0, $y, $ROUND);
6480  
6481 my $count = 0;
6482 {
6483 Math::MPC::Rmpc_add($a1, $a0, $g0, $ROUND);
6484 Math::MPC::Rmpc_div_2ui($a1, $a1, 1, $ROUND);
6485  
6486 Math::MPC::Rmpc_mul($g1, $a0, $g0, $ROUND);
6487 Math::MPC::Rmpc_add($t, $a0, $g0, $ROUND);
6488 Math::MPC::Rmpc_sqr($t, $t, $ROUND);
6489 Math::MPC::Rmpc_cmp_si_si($t, 0, 0) || return $t;
6490 Math::MPC::Rmpc_div($g1, $g1, $t, $ROUND);
6491 Math::MPC::Rmpc_sqrt($g1, $g1, $ROUND);
6492 Math::MPC::Rmpc_add($t, $a0, $g0, $ROUND);
6493 Math::MPC::Rmpc_mul($g1, $g1, $t, $ROUND);
6494  
6495 if (Math::MPC::Rmpc_cmp($a0, $a1) and ++$count < $PREC) {
6496 Math::MPC::Rmpc_set($a0, $a1, $ROUND);
6497 Math::MPC::Rmpc_set($g0, $g1, $ROUND);
6498 redo;
6499 }
6500 }
6501  
6502 return $g0;
6503 }
6504  
6505 Math_MPFR__Math_MPC: {
6506 $x = _mpfr2mpc($x);
6507 goto Math_MPC__Math_MPC;
6508 }
6509  
6510 Math_MPC__Math_MPFR: {
6511 $y = _mpfr2mpc($y);
6512 goto Math_MPC__Math_MPC;
6513 }
6514 }
6515  
6516 sub agm ($$) {
6517 bless \__agm__(_star2mpfr_mpc($_[0]), _star2mpfr_mpc($_[1]));
6518 }
6519  
6520 #
6521 ## hypot
6522 #
6523  
6524 # hypot(x, y) = sqrt(x^2 + y^2)
6525  
6526 sub __hypot__ {
6527 my ($x, $y) = @_;
6528 goto(join('__', ref($x), ref($y)) =~ tr/:/_/rs);
6529  
6530 Math_MPFR__Math_MPFR: {
6531 my $r = Math::MPFR::Rmpfr_init2($PREC);
6532 Math::MPFR::Rmpfr_hypot($r, $x, $y, $ROUND);
6533 return $r;
6534 }
6535  
6536 Math_MPFR__Math_MPC: {
6537 my $r = Math::MPFR::Rmpfr_init2($PREC);
6538 Math::MPC::Rmpc_abs($r, $y, $ROUND);
6539 Math::MPFR::Rmpfr_hypot($r, $r, $x, $ROUND);
6540 return $r;
6541 }
6542  
6543 Math_MPC__Math_MPFR: {
6544 my $r = Math::MPFR::Rmpfr_init2($PREC);
6545 Math::MPC::Rmpc_abs($r, $x, $ROUND);
6546 Math::MPFR::Rmpfr_hypot($r, $r, $y, $ROUND);
6547 return $r;
6548 }
6549  
6550 Math_MPC__Math_MPC: {
6551 my $r = Math::MPFR::Rmpfr_init2($PREC);
6552 Math::MPC::Rmpc_abs($r, $x, $ROUND);
6553 my $t = Math::MPFR::Rmpfr_init2($PREC);
6554 Math::MPC::Rmpc_abs($t, $y, $ROUND);
6555 Math::MPFR::Rmpfr_hypot($r, $r, $t, $ROUND);
6556 return $r;
6557 }
6558 }
6559  
6560 sub hypot ($$) {
6561 bless \__hypot__(_star2mpfr_mpc($_[0]), _star2mpfr_mpc($_[1]));
6562 }
6563  
6564 #
6565 ## BesselJ
6566 #
6567  
6568 sub __BesselJ__ {
6569 my ($x, $n) = @_;
6570 goto(join('__', ref($x), ref($n) || 'Scalar') =~ tr/:/_/rs);
6571  
6572 Math_MPFR__Scalar: {
6573 my $r = Math::MPFR::Rmpfr_init2($PREC);
6574  
6575 if ($n == 0) {
6576 Math::MPFR::Rmpfr_j0($r, $x, $ROUND);
6577 }
6578 elsif ($n == 1) {
6579 Math::MPFR::Rmpfr_j1($r, $x, $ROUND);
6580 }
6581 else {
6582 Math::MPFR::Rmpfr_jn($r, $n, $x, $ROUND);
6583 }
6584  
6585 return $r;
6586 }
6587  
6588 Math_MPFR__Math_GMPz: {
6589  
6590 $n = Math::GMPz::Rmpz_get_d($n);
6591  
6592 # Limit goes to zero when n goes to +/-Infinity
6593 if (($n < LONG_MIN or $n > ULONG_MAX)
6594 and Math::MPFR::Rmpfr_number_p($x)) {
6595 my $r = Math::MPFR::Rmpfr_init2($PREC);
6596 Math::MPFR::Rmpfr_set_ui($r, 0, $ROUND);
6597 return $r;
6598 }
6599  
6600 goto Math_MPFR__Scalar;
6601 }
6602 }
6603  
6604 sub BesselJ ($$) {
6605 my ($x, $y) = @_;
6606  
6607 $x = ref($x) eq __PACKAGE__ ? _any2mpfr($$x) : _star2mpfr($x);
6608  
6609 if (!ref($y) and CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
6610 return bless \__BesselJ__($x, $y);
6611 }
6612  
6613 bless \__BesselJ__($x, _star2mpz($y) // (goto &nan));
6614 }
6615  
6616 #
6617 ## BesselY
6618 #
6619  
6620 sub __BesselY__ {
6621 my ($x, $n) = @_;
6622 goto(join('__', ref($x), ref($n) || 'Scalar') =~ tr/:/_/rs);
6623  
6624 Math_MPFR__Scalar: {
6625 my $r = Math::MPFR::Rmpfr_init2($PREC);
6626  
6627 if ($n == 0) {
6628 Math::MPFR::Rmpfr_y0($r, $x, $ROUND);
6629 }
6630 elsif ($n == 1) {
6631 Math::MPFR::Rmpfr_y1($r, $x, $ROUND);
6632 }
6633 else {
6634 Math::MPFR::Rmpfr_yn($r, $n, $x, $ROUND);
6635 }
6636  
6637 return $r;
6638 }
6639  
6640 Math_MPFR__Math_GMPz: {
6641  
6642 $n = Math::GMPz::Rmpz_get_d($n);
6643  
6644 if ($n < LONG_MIN or $n > ULONG_MAX) {
6645  
6646 my $r = Math::MPFR::Rmpfr_init2($PREC);
6647  
6648 if (Math::MPFR::Rmpfr_sgn($x) < 0 or !Math::MPFR::Rmpfr_number_p($x)) {
6649 Math::MPFR::Rmpfr_set_nan($r);
6650 return $r;
6651 }
6652  
6653 if ($n < 0) {
6654 Math::MPFR::Rmpfr_set_inf($r, 1);
6655 }
6656 else {
6657 Math::MPFR::Rmpfr_set_inf($r, -1);
6658 }
6659  
6660 return $r;
6661 }
6662  
6663 goto Math_MPFR__Scalar;
6664 }
6665 }
6666  
6667 sub BesselY ($$) {
6668 my ($x, $y) = @_;
6669  
6670 $x = ref($x) eq __PACKAGE__ ? _any2mpfr($$x) : _star2mpfr($x);
6671  
6672 if (!ref($y) and CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
6673 return bless \__BesselY__($x, $y);
6674 }
6675  
6676 bless \__BesselY__($x, _star2mpz($y) // (goto &nan));
6677 }
6678  
6679 #
6680 ## ROUND
6681 #
6682  
6683 sub __round__ {
6684 my ($x, $prec) = @_;
6685 goto(ref($x) =~ tr/:/_/rs);
6686  
6687 Math_MPFR: {
6688  
6689 my $nth = -CORE::int($prec);
6690  
6691 my $p = Math::MPFR::Rmpfr_init2($PREC);
6692 Math::MPFR::Rmpfr_set_str($p, '1e' . CORE::abs($nth), 10, $ROUND);
6693  
6694 my $r = Math::MPFR::Rmpfr_init2($PREC);
6695  
6696 if ($nth < 0) {
6697 Math::MPFR::Rmpfr_div($r, $x, $p, $ROUND);
6698 }
6699 else {
6700 Math::MPFR::Rmpfr_mul($r, $x, $p, $ROUND);
6701 }
6702  
6703 Math::MPFR::Rmpfr_round($r, $r);
6704  
6705 if ($nth < 0) {
6706 Math::MPFR::Rmpfr_mul($r, $r, $p, $ROUND);
6707 }
6708 else {
6709 Math::MPFR::Rmpfr_div($r, $r, $p, $ROUND);
6710 }
6711  
6712 return $r;
6713 }
6714  
6715 Math_MPC: {
6716  
6717 my $real = Math::MPFR::Rmpfr_init2($PREC);
6718 my $imag = Math::MPFR::Rmpfr_init2($PREC);
6719  
6720 Math::MPC::RMPC_RE($real, $x);
6721 Math::MPC::RMPC_IM($imag, $x);
6722  
6723 $real = __round__($real, $prec);
6724 $imag = __round__($imag, $prec);
6725  
6726 if (Math::MPFR::Rmpfr_zero_p($imag)) {
6727 return $real;
6728 }
6729  
6730 my $r = Math::MPC::Rmpc_init2($PREC);
6731 Math::MPC::Rmpc_set_fr_fr($r, $real, $imag, $ROUND);
6732 return $r;
6733 }
6734  
6735 Math_GMPq: {
6736  
6737 my $nth = -CORE::int($prec);
6738  
6739 my $n = Math::GMPq::Rmpq_init();
6740 Math::GMPq::Rmpq_set($n, $x);
6741  
6742 my $sgn = Math::GMPq::Rmpq_sgn($n);
6743  
6744 if ($sgn < 0) {
6745 Math::GMPq::Rmpq_neg($n, $n);
6746 }
6747  
6748 my $p = Math::GMPz::Rmpz_init_set_str('1' . ('0' x CORE::abs($nth)), 10);
6749  
6750 if ($nth < 0) {
6751 Math::GMPq::Rmpq_div_z($n, $n, $p);
6752 }
6753 else {
6754 Math::GMPq::Rmpq_mul_z($n, $n, $p);
6755 }
6756  
6757 state $half = do {
6758 my $q = Math::GMPq::Rmpq_init_nobless();
6759 Math::GMPq::Rmpq_set_ui($q, 1, 2);
6760 $q;
6761 };
6762  
6763 Math::GMPq::Rmpq_add($n, $n, $half);
6764  
6765 my $z = Math::GMPz::Rmpz_init();
6766 Math::GMPz::Rmpz_set_q($z, $n);
6767  
6768 if (Math::GMPz::Rmpz_odd_p($z) and Math::GMPq::Rmpq_integer_p($n)) {
6769 Math::GMPz::Rmpz_sub_ui($z, $z, 1);
6770 }
6771  
6772 Math::GMPq::Rmpq_set_z($n, $z);
6773  
6774 if ($nth < 0) {
6775 Math::GMPq::Rmpq_mul_z($n, $n, $p);
6776 }
6777 else {
6778 Math::GMPq::Rmpq_div_z($n, $n, $p);
6779 }
6780  
6781 if ($sgn < 0) {
6782 Math::GMPq::Rmpq_neg($n, $n);
6783 }
6784  
6785 if (Math::GMPq::Rmpq_integer_p($n)) {
6786 Math::GMPz::Rmpz_set_q($z, $n);
6787 return $z;
6788 }
6789  
6790 return $n;
6791 }
6792  
6793 Math_GMPz: {
6794 $x = _mpz2mpq($x);
6795 goto Math_GMPq;
6796 }
6797 }
6798  
6799 sub round ($;$) {
6800 my ($x, $y) = @_;
6801  
6802 $x = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
6803  
6804 if (!defined($y)) {
6805 return bless \__round__($x, 0);
6806 }
6807  
6808 $y = _star2si($y) // goto &nan;
6809  
6810 bless \__round__($x, $y);
6811 }
6812  
6813 #
6814 ## RAND / IRAND
6815 #
6816  
6817 sub __irand__ {
6818 my ($x, $y, $state) = @_;
6819  
6820 if (defined($y)) {
6821 my $cmp = Math::GMPz::Rmpz_cmp($y, $x);
6822  
6823 if ($cmp == 0) {
6824 return $x;
6825 }
6826 elsif ($cmp < 0) {
6827 ($x, $y) = ($y, $x);
6828 }
6829  
6830 my $r = Math::GMPz::Rmpz_init();
6831 Math::GMPz::Rmpz_sub($r, $y, $x);
6832 Math::GMPz::Rmpz_add_ui($r, $r, 1);
6833 Math::GMPz::Rmpz_urandomm($r, $state, $r, 1);
6834 Math::GMPz::Rmpz_add($r, $r, $x);
6835 return $r;
6836 }
6837  
6838 my $sgn = Math::GMPz::Rmpz_sgn($x) || return $x;
6839  
6840 my $r = Math::GMPz::Rmpz_init_set($x);
6841  
6842 if ($sgn < 0) {
6843 Math::GMPz::Rmpz_sub_ui($r, $r, 1);
6844 }
6845 else {
6846 Math::GMPz::Rmpz_add_ui($r, $r, 1);
6847 }
6848  
6849 Math::GMPz::Rmpz_urandomm($r, $state, $r, 1);
6850 Math::GMPz::Rmpz_neg($r, $r) if $sgn < 0;
6851 return $r;
6852 }
6853  
6854 {
6855 my $srand = srand();
6856  
6857 {
6858 state $state = Math::MPFR::Rmpfr_randinit_mt_nobless();
6859 Math::MPFR::Rmpfr_randseed_ui($state, $srand);
6860  
6861 sub rand (;$;$) {
6862 my ($x, $y) = @_;
6863  
6864 if (@_ == 0) {
6865 $x = one();
6866 }
6867  
6868 $x = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
6869  
6870 if (!defined($y)) {
6871 my $rand = Math::MPFR::Rmpfr_init2($PREC);
6872 Math::MPFR::Rmpfr_urandom($rand, $state, $ROUND);
6873 return bless \__mul__($rand, $x);
6874 }
6875  
6876 $y = ref($y) eq __PACKAGE__ ? $$y : _star2obj($y);
6877  
6878 my $rand = Math::MPFR::Rmpfr_init2($PREC);
6879 Math::MPFR::Rmpfr_urandom($rand, $state, $ROUND);
6880 $rand = __mul__($rand, __sub__($y, $x));
6881 bless \__add__($rand, $x);
6882 }
6883  
6884 sub seed ($) {
6885 my ($x) = @_;
6886  
6887 $x = $$x if (ref($x) eq __PACKAGE__);
6888  
6889 if (ref($x) ne 'Math::GMPz') {
6890 $x = _star2mpz($x) // do {
6891 require Carp;
6892 Carp::croak("seed(): invalid seed value <<$_[0]>> (expected an integer)");
6893 };
6894 }
6895 Math::MPFR::Rmpfr_randseed($state, $x);
6896 bless \$x;
6897 }
6898 }
6899  
6900 {
6901 state $state = Math::GMPz::zgmp_randinit_mt_nobless();
6902 Math::GMPz::zgmp_randseed_ui($state, $srand);
6903  
6904 sub irand ($;$) {
6905 my ($x, $y) = @_;
6906  
6907 $x = _star2mpz($x) // goto &nan;
6908  
6909 if (!defined($y)) {
6910 return bless \__irand__($x, undef, $state);
6911 }
6912  
6913 $y = _star2mpz($y) // goto &nan;
6914  
6915 bless \__irand__($x, $y, $state);
6916 }
6917  
6918 sub iseed ($) {
6919 my ($x) = @_;
6920  
6921 $x = _star2mpz($x) // do {
6922 require Carp;
6923 Carp::croak("iseed(): invalid seed value <<$_[0]>> (expected an integer)");
6924 };
6925  
6926 Math::GMPz::zgmp_randseed($state, $x);
6927 bless \$x;
6928 }
6929 }
6930 }
6931  
6932 #
6933 ## n-th Fibonacci number of k-th order
6934 #
6935  
6936 sub fibonacci ($;$) {
6937 my ($n, $k) = @_;
6938  
6939 $n = _star2ui($n) // goto &nan;
6940  
6941 # N-th Fibonacci number of k-th order
6942 if (defined($k)) {
6943  
6944 $k = _star2ui($k) // goto &nan;
6945  
6946 if ($k == 2) {
6947 my $z = Math::GMPz::Rmpz_init();
6948 Math::GMPz::Rmpz_fib_ui($z, $n);
6949 return bless \$z;
6950 }
6951  
6952 if ($n < $k - 1) {
6953 return zero();
6954 }
6955  
6956 # Algorithm after M. F. Hasler
6957 # See: https://oeis.org/A302990
6958  
6959 my @f = map {
6960 $_ < $k
6961 ? do {
6962 my $z = Math::GMPz::Rmpz_init();
6963 Math::GMPz::Rmpz_setbit($z, $_);
6964 $z;
6965 }
6966 : Math::GMPz::Rmpz_init_set_ui(1)
6967 } 1 .. ($k + 1);
6968  
6969 my $t = Math::GMPz::Rmpz_init();
6970  
6971 foreach my $i (2 * ++$k - 2 .. $n) {
6972 Math::GMPz::Rmpz_mul_2exp($t, $f[($i - 1) % $k], 1);
6973 Math::GMPz::Rmpz_sub($f[$i % $k], $t, $f[$i % $k]);
6974 }
6975  
6976 my $r = $f[$n % $k];
6977 return bless \$r;
6978 }
6979  
6980 my $r = Math::GMPz::Rmpz_init();
6981 Math::GMPz::Rmpz_fib_ui($r, $n);
6982 bless \$r;
6983 }
6984  
6985 #
6986 ## n-th Lucas number
6987 #
6988  
6989 sub lucas ($) {
6990 my ($n) = @_;
6991  
6992 $n = _star2ui($n) // goto &nan;
6993  
6994 my $r = Math::GMPz::Rmpz_init();
6995 Math::GMPz::Rmpz_lucnum_ui($r, $n);
6996 bless \$r;
6997 }
6998  
6999 sub __lucasV__ {
7000 my ($P, $Q, $n) = @_;
7001  
7002 my ($V1, $V2) = (Math::GMPz::Rmpz_init_set_ui(2), Math::GMPz::Rmpz_init_set($P));
7003 my ($Q1, $Q2) = (Math::GMPz::Rmpz_init_set_ui(1), Math::GMPz::Rmpz_init_set_ui(1));
7004  
7005 foreach my $bit (split(//, Math::GMPz::Rmpz_get_str($n, 2))) {
7006  
7007 Math::GMPz::Rmpz_mul($Q1, $Q1, $Q2);
7008  
7009 if ($bit) {
7010 Math::GMPz::Rmpz_mul($Q2, $Q1, $Q);
7011 Math::GMPz::Rmpz_mul($V1, $V1, $V2);
7012 Math::GMPz::Rmpz_mul($V2, $V2, $V2);
7013 Math::GMPz::Rmpz_submul($V1, $P, $Q1);
7014 Math::GMPz::Rmpz_submul_ui($V2, $Q2, 2);
7015 }
7016 else {
7017 Math::GMPz::Rmpz_set($Q2, $Q1);
7018 Math::GMPz::Rmpz_mul($V2, $V2, $V1);
7019 Math::GMPz::Rmpz_mul($V1, $V1, $V1);
7020 Math::GMPz::Rmpz_submul($V2, $P, $Q1);
7021 Math::GMPz::Rmpz_submul_ui($V1, $Q2, 2);
7022 }
7023 }
7024  
7025 return ($V1, $V2);
7026 }
7027  
7028 sub __lucasVmod__ {
7029 my ($P, $Q, $n, $m) = @_;
7030  
7031 my ($V1, $V2) = (Math::GMPz::Rmpz_init_set_ui(2), Math::GMPz::Rmpz_init_set($P));
7032 my ($Q1, $Q2) = (Math::GMPz::Rmpz_init_set_ui(1), Math::GMPz::Rmpz_init_set_ui(1));
7033  
7034 foreach my $bit (split(//, Math::GMPz::Rmpz_get_str($n, 2))) {
7035  
7036 Math::GMPz::Rmpz_mul($Q1, $Q1, $Q2);
7037 Math::GMPz::Rmpz_mod($Q1, $Q1, $m);
7038  
7039 if ($bit) {
7040 Math::GMPz::Rmpz_mul($Q2, $Q1, $Q);
7041 Math::GMPz::Rmpz_mul($V1, $V1, $V2);
7042 Math::GMPz::Rmpz_powm_ui($V2, $V2, 2, $m);
7043 Math::GMPz::Rmpz_submul($V1, $P, $Q1);
7044 Math::GMPz::Rmpz_submul_ui($V2, $Q2, 2);
7045 Math::GMPz::Rmpz_mod($V1, $V1, $m);
7046 }
7047 else {
7048 Math::GMPz::Rmpz_set($Q2, $Q1);
7049 Math::GMPz::Rmpz_mul($V2, $V2, $V1);
7050 Math::GMPz::Rmpz_powm_ui($V1, $V1, 2, $m);
7051 Math::GMPz::Rmpz_submul($V2, $P, $Q1);
7052 Math::GMPz::Rmpz_submul_ui($V1, $Q2, 2);
7053 Math::GMPz::Rmpz_mod($V2, $V2, $m);
7054 }
7055 }
7056  
7057 Math::GMPz::Rmpz_mod($V1, $V1, $m);
7058  
7059 return ($V1, $V2);
7060 }
7061  
7062 sub __lucasUV__ {
7063 my ($P, $Q, $n) = @_;
7064  
7065 my $U1 = Math::GMPz::Rmpz_init_set_ui(1);
7066  
7067 my ($V1, $V2) = (Math::GMPz::Rmpz_init_set_ui(2), Math::GMPz::Rmpz_init_set($P));
7068 my ($Q1, $Q2) = (Math::GMPz::Rmpz_init_set_ui(1), Math::GMPz::Rmpz_init_set_ui(1));
7069  
7070 my $t = Math::GMPz::Rmpz_init();
7071 my $s = Math::GMPz::Rmpz_scan1($n, 0);
7072  
7073 Math::GMPz::Rmpz_div_2exp($t, $n, $s + 1);
7074  
7075 foreach my $bit (split(//, Math::GMPz::Rmpz_get_str($t, 2))) {
7076  
7077 Math::GMPz::Rmpz_mul($Q1, $Q1, $Q2);
7078  
7079 if ($bit) {
7080 Math::GMPz::Rmpz_mul($Q2, $Q1, $Q);
7081 Math::GMPz::Rmpz_mul($U1, $U1, $V2);
7082 Math::GMPz::Rmpz_mul($V1, $V1, $V2);
7083  
7084 Math::GMPz::Rmpz_mul($V2, $V2, $V2);
7085 Math::GMPz::Rmpz_submul($V1, $Q1, $P);
7086 Math::GMPz::Rmpz_submul_ui($V2, $Q2, 2);
7087 }
7088 else {
7089 Math::GMPz::Rmpz_set($Q2, $Q1);
7090 Math::GMPz::Rmpz_mul($U1, $U1, $V1);
7091 Math::GMPz::Rmpz_mul($V2, $V2, $V1);
7092 Math::GMPz::Rmpz_sub($U1, $U1, $Q1);
7093 Math::GMPz::Rmpz_mul($V1, $V1, $V1);
7094 Math::GMPz::Rmpz_submul($V2, $Q1, $P);
7095 Math::GMPz::Rmpz_submul_ui($V1, $Q2, 2);
7096 }
7097 }
7098  
7099 Math::GMPz::Rmpz_mul($Q1, $Q1, $Q2);
7100 Math::GMPz::Rmpz_mul($Q2, $Q1, $Q);
7101 Math::GMPz::Rmpz_mul($U1, $U1, $V1);
7102 Math::GMPz::Rmpz_mul($V1, $V1, $V2);
7103 Math::GMPz::Rmpz_sub($U1, $U1, $Q1);
7104 Math::GMPz::Rmpz_submul($V1, $Q1, $P);
7105 Math::GMPz::Rmpz_mul($Q1, $Q1, $Q2);
7106  
7107 for (1 .. $s) {
7108 Math::GMPz::Rmpz_mul($U1, $U1, $V1);
7109 Math::GMPz::Rmpz_mul($V1, $V1, $V1);
7110 Math::GMPz::Rmpz_submul_ui($V1, $Q1, 2);
7111 Math::GMPz::Rmpz_mul($Q1, $Q1, $Q1);
7112 }
7113  
7114 return ($U1, $V1);
7115 }
7116  
7117 sub __lucasUVmod__ {
7118 my ($P, $Q, $n, $m) = @_;
7119  
7120 my $U1 = Math::GMPz::Rmpz_init_set_ui(1);
7121  
7122 my ($V1, $V2) = (Math::GMPz::Rmpz_init_set_ui(2), Math::GMPz::Rmpz_init_set($P));
7123 my ($Q1, $Q2) = (Math::GMPz::Rmpz_init_set_ui(1), Math::GMPz::Rmpz_init_set_ui(1));
7124  
7125 my $t = Math::GMPz::Rmpz_init();
7126 my $s = Math::GMPz::Rmpz_scan1($n, 0);
7127  
7128 Math::GMPz::Rmpz_div_2exp($t, $n, $s + 1);
7129  
7130 foreach my $bit (split(//, Math::GMPz::Rmpz_get_str($t, 2))) {
7131  
7132 Math::GMPz::Rmpz_mul($Q1, $Q1, $Q2);
7133 Math::GMPz::Rmpz_mod($Q1, $Q1, $m);
7134  
7135 if ($bit) {
7136 Math::GMPz::Rmpz_mul($Q2, $Q1, $Q);
7137 Math::GMPz::Rmpz_mul($U1, $U1, $V2);
7138 Math::GMPz::Rmpz_mul($V1, $V1, $V2);
7139  
7140 Math::GMPz::Rmpz_powm_ui($V2, $V2, 2, $m);
7141 Math::GMPz::Rmpz_submul($V1, $Q1, $P);
7142 Math::GMPz::Rmpz_submul_ui($V2, $Q2, 2);
7143  
7144 Math::GMPz::Rmpz_mod($V1, $V1, $m);
7145 Math::GMPz::Rmpz_mod($U1, $U1, $m);
7146 }
7147 else {
7148 Math::GMPz::Rmpz_set($Q2, $Q1);
7149 Math::GMPz::Rmpz_mul($U1, $U1, $V1);
7150 Math::GMPz::Rmpz_mul($V2, $V2, $V1);
7151 Math::GMPz::Rmpz_sub($U1, $U1, $Q1);
7152  
7153 Math::GMPz::Rmpz_powm_ui($V1, $V1, 2, $m);
7154 Math::GMPz::Rmpz_submul($V2, $Q1, $P);
7155 Math::GMPz::Rmpz_submul_ui($V1, $Q2, 2);
7156  
7157 Math::GMPz::Rmpz_mod($V2, $V2, $m);
7158 Math::GMPz::Rmpz_mod($U1, $U1, $m);
7159 }
7160 }
7161  
7162 Math::GMPz::Rmpz_mul($Q1, $Q1, $Q2);
7163 Math::GMPz::Rmpz_mul($Q2, $Q1, $Q);
7164 Math::GMPz::Rmpz_mul($U1, $U1, $V1);
7165 Math::GMPz::Rmpz_mul($V1, $V1, $V2);
7166 Math::GMPz::Rmpz_sub($U1, $U1, $Q1);
7167 Math::GMPz::Rmpz_submul($V1, $Q1, $P);
7168 Math::GMPz::Rmpz_mul($Q1, $Q1, $Q2);
7169  
7170 for (1 .. $s) {
7171 Math::GMPz::Rmpz_mul($U1, $U1, $V1);
7172 Math::GMPz::Rmpz_mod($U1, $U1, $m);
7173 Math::GMPz::Rmpz_powm_ui($V1, $V1, 2, $m);
7174 Math::GMPz::Rmpz_submul_ui($V1, $Q1, 2);
7175 Math::GMPz::Rmpz_powm_ui($Q1, $Q1, 2, $m);
7176 }
7177  
7178 Math::GMPz::Rmpz_mod($U1, $U1, $m);
7179 Math::GMPz::Rmpz_mod($V1, $V1, $m);
7180  
7181 return ($U1, $V1);
7182 }
7183  
7184 sub lucasU ($$$) {
7185 my ($P, $Q, $n) = @_;
7186  
7187 $P = _star2mpz($P) // goto &nan;
7188 $Q = _star2mpz($Q) // goto &nan;
7189 $n = _star2mpz($n) // goto &nan;
7190  
7191 # U_0(P, Q) = 0
7192 Math::GMPz::Rmpz_sgn($n) || goto &zero;
7193  
7194 # undefined for n < 0
7195 Math::GMPz::Rmpz_sgn($n) < 0 && goto &nan;
7196  
7197 my $D = Math::GMPz::Rmpz_init();
7198  
7199 Math::GMPz::Rmpz_mul($D, $P, $P);
7200 Math::GMPz::Rmpz_submul_ui($D, $Q, 4);
7201  
7202 # When `P*P - 4*Q != 0`, we can use a faster algorithm
7203 if (Math::GMPz::Rmpz_sgn($D)) {
7204  
7205 my ($V1, $V2) = __lucasV__($P, $Q, $n);
7206  
7207 Math::GMPz::Rmpz_mul_2exp($V2, $V2, 1);
7208 Math::GMPz::Rmpz_submul($V2, $V1, $P);
7209 Math::GMPz::Rmpz_divexact($V2, $V2, $D);
7210  
7211 return bless \$V2;
7212 }
7213  
7214 my ($U) = __lucasUV__($P, $Q, $n);
7215  
7216 bless \$U;
7217 }
7218  
7219 sub lucasUmod ($$$$) {
7220 my ($P, $Q, $n, $m) = @_;
7221  
7222 $P = _star2mpz($P) // goto &nan;
7223 $Q = _star2mpz($Q) // goto &nan;
7224 $n = _star2mpz($n) // goto &nan;
7225 $m = _star2mpz($m) // goto &nan;
7226  
7227 # undefined for m=0
7228 Math::GMPz::Rmpz_sgn($m) || goto &nan;
7229  
7230 # U_0(P, Q) = 0
7231 Math::GMPz::Rmpz_sgn($n) || goto &zero;
7232  
7233 # undefined for n < 0
7234 Math::GMPz::Rmpz_sgn($n) < 0 && goto &nan;
7235  
7236 my $D = Math::GMPz::Rmpz_init();
7237  
7238 Math::GMPz::Rmpz_mul($D, $P, $P);
7239 Math::GMPz::Rmpz_submul_ui($D, $Q, 4);
7240  
7241 # When `gcd(P*P - 4*Q, m) = 1`, we can use a faster algorithm
7242 if (Math::GMPz::Rmpz_invert($D, $D, $m)) {
7243  
7244 my ($V1, $V2) = __lucasVmod__($P, $Q, $n, $m);
7245  
7246 Math::GMPz::Rmpz_mul_2exp($V2, $V2, 1);
7247 Math::GMPz::Rmpz_submul($V2, $V1, $P);
7248 Math::GMPz::Rmpz_mul($V2, $V2, $D);
7249 Math::GMPz::Rmpz_mod($V2, $V2, $m);
7250  
7251 return bless \$V2;
7252 }
7253  
7254 my ($U) = __lucasUVmod__($P, $Q, $n, $m);
7255  
7256 bless \$U;
7257 }
7258  
7259 sub lucasV ($$$) {
7260 my ($P, $Q, $n) = @_;
7261  
7262 $P = _star2mpz($P) // goto &nan;
7263 $Q = _star2mpz($Q) // goto &nan;
7264 $n = _star2mpz($n) // goto &nan;
7265  
7266 # undefined for n < 0
7267 Math::GMPz::Rmpz_sgn($n) < 0 && goto &nan;
7268  
7269 my ($V) = __lucasV__($P, $Q, $n);
7270  
7271 bless \$V;
7272 }
7273  
7274 sub lucasVmod ($$$$) {
7275 my ($P, $Q, $n, $m) = @_;
7276  
7277 $P = _star2mpz($P) // goto &nan;
7278 $Q = _star2mpz($Q) // goto &nan;
7279 $n = _star2mpz($n) // goto &nan;
7280 $m = _star2mpz($m) // goto &nan;
7281  
7282 # undefined for m=0
7283 Math::GMPz::Rmpz_sgn($m) || goto &nan;
7284  
7285 # undefined for n < 0
7286 Math::GMPz::Rmpz_sgn($n) < 0 && goto &nan;
7287  
7288 my ($V) = __lucasVmod__($P, $Q, $n, $m);
7289  
7290 bless \$V;
7291 }
7292  
7293 #
7294 ## fibonacci(n) mod m
7295 #
7296  
7297 sub fibmod ($$) {
7298 my ($n, $m) = @_;
7299  
7300 $n = _star2mpz($n) // goto &nan;
7301 $m = _star2mpz($m) // goto &nan;
7302  
7303 Math::GMPz::Rmpz_sgn($m) == 0 and goto &nan;
7304  
7305 my $sgn = Math::GMPz::Rmpz_sgn($n);
7306  
7307 $sgn < 0 and goto &nan;
7308 $sgn == 0 and goto &zero;
7309  
7310 #<<<
7311 my ($f, $g, $w) = (
7312 Math::GMPz::Rmpz_init_set_ui(0),
7313 Math::GMPz::Rmpz_init_set_ui(1),
7314 );
7315 #>>>
7316  
7317 my $t = Math::GMPz::Rmpz_init();
7318  
7319 foreach my $bit (split(//, substr(Math::GMPz::Rmpz_get_str($n, 2), 1))) {
7320  
7321 Math::GMPz::Rmpz_powm_ui($g, $g, 2, $m);
7322 Math::GMPz::Rmpz_powm_ui($f, $f, 2, $m);
7323  
7324 Math::GMPz::Rmpz_mul_2exp($t, $g, 2);
7325 Math::GMPz::Rmpz_sub($t, $t, $f);
7326  
7327 $w
7328 ? Math::GMPz::Rmpz_add_ui($t, $t, 2)
7329 : Math::GMPz::Rmpz_sub_ui($t, $t, 2);
7330  
7331 Math::GMPz::Rmpz_add($f, $f, $g);
7332  
7333 if ($bit) {
7334 Math::GMPz::Rmpz_sub($f, $t, $f);
7335 Math::GMPz::Rmpz_set($g, $t);
7336 $w = 0;
7337 }
7338 else {
7339 Math::GMPz::Rmpz_sub($g, $t, $f);
7340 $w = 1;
7341 }
7342 }
7343  
7344 Math::GMPz::Rmpz_mod($g, $g, $m);
7345  
7346 return bless \$g;
7347 }
7348  
7349 #
7350 ## lucas(n) mod m
7351 #
7352  
7353 sub lucasmod ($$) {
7354 my ($n, $m) = @_;
7355  
7356 $n = _star2mpz($n) // goto &nan;
7357 $m = _star2mpz($m) // goto &nan;
7358  
7359 Math::GMPz::Rmpz_sgn($m) == 0 and goto &nan;
7360  
7361 my $sgn = Math::GMPz::Rmpz_sgn($n);
7362  
7363 $sgn < 0 and goto &nan;
7364 $sgn == 0 and return bless \Math::GMPz::Rmpz_init_set_ui(2);
7365  
7366 #<<<
7367 my ($f, $g, $w) = (
7368 Math::GMPz::Rmpz_init_set_ui(3),
7369 Math::GMPz::Rmpz_init_set_ui(1),
7370 );
7371 #>>>
7372  
7373 foreach my $bit (split(//, substr(Math::GMPz::Rmpz_get_str($n, 2), 1))) {
7374  
7375 Math::GMPz::Rmpz_powm_ui($g, $g, 2, $m);
7376 Math::GMPz::Rmpz_powm_ui($f, $f, 2, $m);
7377  
7378 if ($w) {
7379 Math::GMPz::Rmpz_sub_ui($g, $g, 2);
7380 Math::GMPz::Rmpz_add_ui($f, $f, 2);
7381 }
7382 else {
7383 Math::GMPz::Rmpz_add_ui($g, $g, 2);
7384 Math::GMPz::Rmpz_sub_ui($f, $f, 2);
7385 }
7386  
7387 if ($bit) {
7388 Math::GMPz::Rmpz_sub($g, $f, $g);
7389 $w = 0;
7390 }
7391 else {
7392 Math::GMPz::Rmpz_sub($f, $f, $g);
7393 $w = 1;
7394 }
7395 }
7396  
7397 Math::GMPz::Rmpz_mod($g, $g, $m);
7398  
7399 bless \$g;
7400 }
7401  
7402 #
7403 ## Chebyshev polynomials: T_n(x)
7404 #
7405  
7406 sub _quadratic_mul {
7407 my ($xa, $xb, $ya, $yb, $w) = @_;
7408 (__add__(__mul__($xa, $ya), __mul__(__mul__($xb, $yb), $w)), __add__(__mul__($xa, $yb), __mul__($xb, $ya)));
7409 }
7410  
7411 sub _quadratic_invmod {
7412 my ($xa, $xb, $w, $m) = @_;
7413  
7414 $xa = __mod__($xa, $m);
7415 $xb = __mod__($xb, $m);
7416  
7417 my $t = invmod(__sub__(__mul__($xa, $xa), __mul__(__mul__($xb, $xb), $w)), $m);
7418 (__mod__(__mul__($xa, $$t), $m), __mod__(__neg__(__mul__($xb, $$t)), $m));
7419 }
7420  
7421 sub _quadratic_pow {
7422 my ($x, $y, $w, $n) = @_;
7423  
7424 my ($c1, $c2) = (Math::GMPz::Rmpz_init_set_ui(1), Math::GMPz::Rmpz_init_set_ui(0));
7425  
7426 for (; $n > 0 ; $n >>= 1) {
7427  
7428 if ($n & 1) {
7429 ($c1, $c2) = _quadratic_mul($c1, $c2, $x, $y, $w);
7430 }
7431  
7432 ($x, $y) = _quadratic_mul($x, $y, $x, $y, $w);
7433 }
7434  
7435 ($c1, $c2);
7436 }
7437  
7438 sub _mpz_quadratic_powmod {
7439 my ($x, $y, $a, $b, $w, $n, $m) = @_;
7440  
7441 state $t = Math::GMPz::Rmpz_init_nobless();
7442  
7443 for my $i (0 .. Math::GMPz::Rmpz_sizeinbase($n, 2) - 1) {
7444  
7445 if (Math::GMPz::Rmpz_tstbit($n, $i)) {
7446  
7447 # (x, y) = ((a*x + b*y*w) % m, (a*y + b*x) % m)
7448 Math::GMPz::Rmpz_mul($t, $b, $w);
7449 Math::GMPz::Rmpz_mul($t, $t, $y);
7450 Math::GMPz::Rmpz_addmul($t, $a, $x);
7451 Math::GMPz::Rmpz_mul($y, $y, $a);
7452 Math::GMPz::Rmpz_addmul($y, $x, $b);
7453 Math::GMPz::Rmpz_mod($x, $t, $m);
7454 Math::GMPz::Rmpz_mod($y, $y, $m);
7455 }
7456  
7457 # (a, b) = ((a*a + b*b*w) % m, (2*a*b) % m)
7458 Math::GMPz::Rmpz_mul($t, $a, $b);
7459 Math::GMPz::Rmpz_mul_2exp($t, $t, 1);
7460 Math::GMPz::Rmpz_powm_ui($a, $a, 2, $m);
7461 Math::GMPz::Rmpz_powm_ui($b, $b, 2, $m);
7462 Math::GMPz::Rmpz_addmul($a, $b, $w);
7463 Math::GMPz::Rmpz_mod($b, $t, $m);
7464 }
7465 }
7466  
7467 sub _quadratic_powmod {
7468 my ($x, $y, $w, $n, $m) = @_;
7469  
7470 my $negative_power = 0;
7471  
7472 if (Math::GMPz::Rmpz_sgn($n) < 0) {
7473 $n = Math::GMPz::Rmpz_init_set($n); # copy
7474 Math::GMPz::Rmpz_abs($n, $n);
7475 $negative_power = 1;
7476 }
7477  
7478 my ($c1, $c2) = (Math::GMPz::Rmpz_init_set_ui(1), Math::GMPz::Rmpz_init_set_ui(0));
7479  
7480 if (ref($x) eq 'Math::GMPz' and ref($y) eq 'Math::GMPz' and ref($w) eq 'Math::GMPz' and ref($m) eq 'Math::GMPz') {
7481 _mpz_quadratic_powmod($c1, $c2, $x, $y, $w, $n, $m);
7482 }
7483 else {
7484 for my $i (0 .. Math::GMPz::Rmpz_sizeinbase($n, 2) - 1) {
7485  
7486 if (Math::GMPz::Rmpz_tstbit($n, $i)) {
7487 ($c1, $c2) = map { __mod__($_, $m) } _quadratic_mul($c1, $c2, $x, $y, $w);
7488 }
7489  
7490 ($x, $y) = map { __mod__($_, $m) } _quadratic_mul($x, $y, $x, $y, $w);
7491 }
7492 }
7493  
7494 if ($negative_power) {
7495 ($c1, $c2) = _quadratic_invmod($c1, $c2, $w, $m);
7496 }
7497  
7498 ($c1, $c2);
7499 }
7500  
7501 sub quadratic_powmod ($$$$$) {
7502 my ($x, $y, $w, $n, $m) = @_;
7503  
7504 $x = _star2obj($x);
7505 $y = _star2obj($y);
7506 $w = _star2obj($w);
7507 $n = _star2mpz($n) // return (nan(), nan());
7508 $m = _star2obj($m);
7509  
7510 my ($r1, $r2) = _quadratic_powmod($x, $y, $w, $n, $m);
7511  
7512 ((bless \$r1), (bless \$r2));
7513 }
7514  
7515 sub chebyshevT ($$) {
7516 my ($n, $x) = @_;
7517  
7518 $n = _star2si($n) // goto &nan;
7519 $x = _star2obj($x);
7520  
7521 $n = -$n if $n < 0;
7522 $n == 0 and goto &one;
7523 $n == 1 and return bless \$x;
7524  
7525 if (ref($x) eq 'Math::GMPz' or (ref($x) eq 'Math::GMPq' and __is_int__($x))) {
7526 return lucasV(2 * $x, 1, $n)->idiv(2);
7527 }
7528  
7529 # T_n(x) = 1/2 * ((x - sqrt(x^2 - 1))^n + (x + sqrt(x^2 - 1))^n)
7530  
7531 my ($r1, $r2) = _quadratic_pow($x, Math::GMPz::Rmpz_init_set_si(-1), __dec__(__mul__($x, $x)), $n);
7532 bless \$r1;
7533 }
7534  
7535 #
7536 ## Modular Chebyshev polynomials: T_n(x) mod m
7537 #
7538  
7539 sub chebyshevTmod ($$$) {
7540 my ($n, $x, $m) = @_;
7541  
7542 $n = _star2mpz($n) // goto &nan;
7543 $x = _star2obj($x);
7544 $m = _star2mpz($m) // goto &nan;
7545  
7546 if (Math::GMPz::Rmpz_sgn($n) < 0) {
7547 $n = Math::GMPz::Rmpz_init_set($n); # copy
7548 Math::GMPz::Rmpz_abs($n, $n);
7549 }
7550  
7551 if (Math::GMPz::Rmpz_odd_p($m) and (ref($x) eq 'Math::GMPz' or (ref($x) eq 'Math::GMPq' and __is_int__($x)))) {
7552 return lucasVmod(2 * $x, 1, $n, $m)->divmod(2, $m);
7553 }
7554  
7555 # T_n(x) = 1/2 * ((x - sqrt(x^2 - 1))^n + (x + sqrt(x^2 - 1))^n)
7556  
7557 my ($r1, $r2) = _quadratic_powmod($x, Math::GMPz::Rmpz_init_set_si(-1), __dec__(__mul__($x, $x)), $n, $m);
7558 my $r = bless \$r1;
7559 $r = $r->mod($m);
7560 $r;
7561 }
7562  
7563 #
7564 ## Chebyshev polynomials: U_n(x)
7565 #
7566  
7567 sub chebyshevU ($$) {
7568 my ($n, $x) = @_;
7569  
7570 $n = _star2si($n) // goto &nan;
7571 $n == 0 and goto &one;
7572  
7573 my $negative = 0;
7574  
7575 if ($n < 0) {
7576  
7577 $n == -1 and goto &zero;
7578 $n == -2 and goto &mone;
7579  
7580 $n = -$n - 2;
7581 $negative = 1;
7582 }
7583  
7584 $x = _star2obj($x);
7585  
7586 if (ref($x) eq 'Math::GMPz' or (ref($x) eq 'Math::GMPz' and __is_int__($x))) {
7587 my $r = lucasU(2 * $x, 1, $n + 1);
7588 $r = $r->neg if $negative;
7589 return $r;
7590 }
7591  
7592 # U_n(x) = ((x + sqrt(x^2 - 1))^(n+1) - (x - sqrt(x^2 - 1))^(n+1)) / (2 * sqrt(x^2 - 1))
7593  
7594 my ($r1, $r2) = _quadratic_pow($x, Math::GMPz::Rmpz_init_set_ui(1), __dec__(__mul__($x, $x)), $n + 1);
7595  
7596 my $r = bless \$r2;
7597 $r = $r->neg if $negative;
7598 $r;
7599 }
7600  
7601 #
7602 ## Modular Chebyshev polynomials: U_n(x) mod m
7603 #
7604  
7605 sub chebyshevUmod {
7606 my ($n, $x, $m) = @_;
7607  
7608 $n = _star2mpz($n) // goto &nan;
7609 $x = _star2obj($x);
7610 $m = _star2mpz($m) // goto &nan;
7611  
7612 my $negative = 0;
7613  
7614 if (Math::GMPz::Rmpz_sgn($n) < 0) {
7615  
7616 if (Math::GMPz::Rmpz_cmp_si($n, -1) == 0) {
7617 return (zero()->mod(bless \$m));
7618 }
7619  
7620 if (Math::GMPz::Rmpz_cmp_si($n, -2) == 0) {
7621 return (mone()->mod(bless \$m));
7622 }
7623  
7624 $n = -$n - 2;
7625 $negative = 1;
7626 }
7627  
7628 if (ref($x) eq 'Math::GMPz' or (ref($x) eq 'Math::GMPq' and __is_int__($x))) {
7629 my $r = lucasUmod(2 * $x, 1, $n + 1, $m);
7630 $r = $r->neg->mod($m) if $negative;
7631 return $r;
7632 }
7633  
7634 # U_n(x) = ((x + sqrt(x^2 - 1))^(n+1) - (x - sqrt(x^2 - 1))^(n+1)) / (2 * sqrt(x^2 - 1))
7635  
7636 my ($r1, $r2) = _quadratic_pow($x, Math::GMPz::Rmpz_init_set_ui(1), __dec__(__mul__($x, $x)), $n + 1, $m);
7637  
7638 my $r = bless \$r2;
7639 $r = $r->neg if $negative;
7640 $r = $r->mod($m);
7641 $r;
7642 }
7643  
7644 #
7645 ## Laguerre polynomials: L_n(x)
7646 #
7647  
7648 sub laguerreL ($$) {
7649 my ($n, $x) = @_;
7650  
7651 $n = _star2ui($n) // goto &nan;
7652 $x = _star2obj($x);
7653  
7654 $n == 0 and goto &one; # L_0(x) = 1
7655 $n == 1 and return bless \__sub__(1, $x); # L_1(x) = 1-x
7656  
7657 my $t = Math::GMPz::Rmpz_init();
7658 my $u = Math::GMPz::Rmpz_init_set_ui(1);
7659  
7660 my @terms;
7661 foreach my $k (0 .. $n) {
7662 Math::GMPz::Rmpz_bin_uiui($t, $n, $k);
7663 Math::GMPz::Rmpz_neg($t, $t) if ($k & 1);
7664 push @terms, __div__(__mul__(__pow__($x, $k), $t), $u);
7665 Math::GMPz::Rmpz_mul_ui($u, $u, $k + 1);
7666 }
7667  
7668 bless \_binsplit(\@terms, \&__add__);
7669 }
7670  
7671 #
7672 ## Legendre polynomials: P_n(x)
7673 #
7674  
7675 sub legendreP ($$) {
7676 my ($n, $x) = @_;
7677  
7678 $n = _star2ui($n) // goto &nan;
7679 $x = _star2obj($x);
7680  
7681 $n == 0 and goto &one;
7682 $n == 1 and return bless \$x;
7683  
7684 my $x1 = __dec__($x);
7685 my $x2 = __inc__($x);
7686  
7687 my $t = Math::GMPz::Rmpz_init();
7688  
7689 my @terms;
7690 foreach my $k (0 .. $n) {
7691 Math::GMPz::Rmpz_bin_uiui($t, $n, $k);
7692 Math::GMPz::Rmpz_mul($t, $t, $t);
7693 push @terms, __mul__(__mul__(__pow__($x1, $n - $k), __pow__($x2, $k)), $t);
7694 }
7695  
7696 my $sum = _binsplit(\@terms, \&__add__);
7697  
7698 Math::GMPz::Rmpz_set_ui($t, 0);
7699 Math::GMPz::Rmpz_setbit($t, $n);
7700  
7701 bless \__div__($sum, $t);
7702 }
7703  
7704 #
7705 ## The physicists' Hermite polynomials H_n(x)
7706 #
7707  
7708 sub hermiteH ($$) {
7709 my ($n, $x) = @_;
7710  
7711 $n = _star2ui($n) // goto &nan;
7712 $x = _star2obj($x);
7713  
7714 $x = __add__($x, $x);
7715  
7716 $n == 0 and goto &one;
7717 $n == 1 and return bless \$x;
7718  
7719 my $t = Math::GMPz::Rmpz_init();
7720 my $u = Math::GMPz::Rmpz_init_set_ui(1);
7721  
7722 my $v = Math::GMPz::Rmpz_init();
7723 Math::GMPz::Rmpz_fac_ui($v, $n);
7724  
7725 my @terms;
7726 foreach my $m (0 .. $n >> 1) {
7727 Math::GMPz::Rmpz_mul($t, $v, $u);
7728 Math::GMPz::Rmpz_neg($t, $t) if ($m & 1);
7729  
7730 push @terms, __div__(__pow__($x, $n - ($m << 1)), $t);
7731  
7732 my $d = ($n - ($m << 1)) * ($n - ($m << 1) - 1);
7733 Math::GMPz::Rmpz_divexact_ui($v, $v, $d) if $d;
7734 Math::GMPz::Rmpz_mul_ui($u, $u, $m + 1);
7735 }
7736  
7737 my $sum = _binsplit(\@terms, \&__add__);
7738 Math::GMPz::Rmpz_fac_ui($v, $n);
7739 bless \__mul__($sum, $v);
7740 }
7741  
7742 #
7743 ## The probabilists' Hermite polynomials He_n(x)
7744 #
7745  
7746 sub hermiteHe ($$) {
7747 my ($n, $x) = @_;
7748  
7749 $n = _star2ui($n) // goto &nan;
7750 $x = _star2obj($x);
7751  
7752 $n == 0 and goto &one;
7753 $n == 1 and return bless \$x;
7754  
7755 my $t = Math::GMPz::Rmpz_init();
7756 my $u = Math::GMPz::Rmpz_init_set_ui(1);
7757  
7758 my $v = Math::GMPz::Rmpz_init();
7759 Math::GMPz::Rmpz_fac_ui($v, $n);
7760  
7761 my @terms;
7762 foreach my $m (0 .. $n >> 1) {
7763 Math::GMPz::Rmpz_mul($t, $v, $u);
7764 Math::GMPz::Rmpz_mul_2exp($t, $t, $m);
7765 Math::GMPz::Rmpz_neg($t, $t) if ($m & 1);
7766  
7767 push @terms, __div__(__pow__($x, $n - ($m << 1)), $t);
7768  
7769 my $d = ($n - ($m << 1)) * ($n - ($m << 1) - 1);
7770 Math::GMPz::Rmpz_divexact_ui($v, $v, $d) if $d;
7771 Math::GMPz::Rmpz_mul_ui($u, $u, $m + 1);
7772 }
7773  
7774 my $sum = _binsplit(\@terms, \&__add__);
7775 Math::GMPz::Rmpz_fac_ui($v, $n);
7776 bless \__mul__($sum, $v);
7777 }
7778  
7779 #
7780 ## Primorial
7781 #
7782 sub primorial ($) {
7783 my ($n) = @_;
7784  
7785 $n = _star2ui($n) // goto &nan;
7786  
7787 my $r = Math::GMPz::Rmpz_init();
7788 Math::GMPz::Rmpz_primorial_ui($r, $n);
7789 bless \$r;
7790 }
7791  
7792 sub min {
7793 my @terms = map { ref($_) eq __PACKAGE__ ? $$_ : _star2obj($_) } @_;
7794  
7795 @terms || return undef;
7796  
7797 my $min = shift(@terms);
7798 foreach my $curr (@terms) {
7799 if ((__cmp__($curr, $min) // return undef) < 0) {
7800 $min = $curr;
7801 }
7802 }
7803  
7804 bless \$min;
7805 }
7806  
7807 sub max {
7808 my @terms = map { ref($_) eq __PACKAGE__ ? $$_ : _star2obj($_) } @_;
7809  
7810 @terms || return undef;
7811  
7812 my $max = shift(@terms);
7813 foreach my $curr (@terms) {
7814 if ((__cmp__($curr, $max) // return undef) > 0) {
7815 $max = $curr;
7816 }
7817 }
7818  
7819 bless \$max;
7820 }
7821  
7822 sub sum {
7823 my @terms = map { ref($_) eq __PACKAGE__ ? $$_ : _star2obj($_) } @_;
7824  
7825 @terms || goto &zero;
7826  
7827 my @non_mpz;
7828 my $sum = Math::GMPz::Rmpz_init_set_ui(0);
7829  
7830 foreach my $n (@terms) {
7831 if (ref($n) eq 'Math::GMPz') {
7832 Math::GMPz::Rmpz_add($sum, $sum, $n);
7833 }
7834 else {
7835 push @non_mpz, $n;
7836 }
7837 }
7838  
7839 if (@non_mpz) {
7840 $sum = __add__($sum, _binsplit(\@non_mpz, \&__add__));
7841 }
7842  
7843 bless \$sum;
7844 }
7845  
7846 sub prod {
7847 my @terms = map { ref($_) eq __PACKAGE__ ? $$_ : _star2obj($_) } @_;
7848 @terms || goto &one;
7849 bless \_binsplit(\@terms, \&__mul__);
7850 }
7851  
7852 sub _secant_numbers {
7853 my ($n) = @_;
7854  
7855 state @cache;
7856  
7857 if ($n <= $#cache) {
7858 return @cache;
7859 }
7860  
7861 $n <<= 1 if ($n <= 512);
7862  
7863 my @S = (Math::GMPz::Rmpz_init_set_ui(1));
7864  
7865 foreach my $k (1 .. $n) {
7866 Math::GMPz::Rmpz_mul_ui($S[$k] = Math::GMPz::Rmpz_init(), $S[$k - 1], $k);
7867 }
7868  
7869 foreach my $k (1 .. $n) {
7870 foreach my $j ($k + 1 .. $n) {
7871 Math::GMPz::Rmpz_addmul_ui($S[$j], $S[$j - 1], ($j - $k) * ($j - $k + 2));
7872 }
7873 }
7874  
7875 push @cache, @S[@cache .. (@S <= 1024 ? $#S : 1024)];
7876  
7877 return @S;
7878 }
7879  
7880 sub _tangent_numbers {
7881 my ($n) = @_;
7882  
7883 state @cache;
7884  
7885 if ($n <= $#cache) {
7886 return @cache;
7887 }
7888  
7889 $n <<= 1 if ($n <= 512);
7890  
7891 my @T = (Math::GMPz::Rmpz_init_set_ui(1));
7892  
7893 foreach my $k (1 .. $n) {
7894 Math::GMPz::Rmpz_mul_ui($T[$k] = Math::GMPz::Rmpz_init(), $T[$k - 1], $k);
7895 }
7896  
7897 foreach my $k (1 .. $n) {
7898 foreach my $j ($k .. $n) {
7899 Math::GMPz::Rmpz_mul_ui($T[$j], $T[$j], $j - $k + 2);
7900 Math::GMPz::Rmpz_addmul_ui($T[$j], $T[$j - 1], $j - $k);
7901 }
7902 }
7903  
7904 push @cache, @T[@cache .. (@T <= 1024 ? $#T : 1024)];
7905  
7906 return @T;
7907 }
7908  
7909 sub _bernoulli_numbers {
7910 my ($n) = @_;
7911  
7912 $n = ($n >> 1) + 1;
7913  
7914 state @cache;
7915  
7916 if ($n <= $#cache) {
7917 return @cache;
7918 }
7919  
7920 my @B;
7921 my @T = _tangent_numbers($n);
7922  
7923 my $t = Math::GMPz::Rmpz_init();
7924  
7925 foreach my $k (scalar(@cache) .. 2 * @T) {
7926  
7927 $k % 2 == 0 or $k == 1 or next;
7928  
7929 my $q = Math::GMPq::Rmpq_init();
7930  
7931 if ($k == 0) {
7932 Math::GMPq::Rmpq_set_ui($q, 1, 1);
7933 $B[$k] = $q;
7934 next;
7935 }
7936  
7937 if ($k == 1) {
7938 Math::GMPq::Rmpq_set_si($q, -1, 2);
7939 $B[$k] = $q;
7940 next;
7941 }
7942  
7943 # T_k
7944 Math::GMPz::Rmpz_mul_ui($t, $T[($k >> 1) - 1], $k);
7945 Math::GMPz::Rmpz_neg($t, $t) if ((($k >> 1) - 1) & 1);
7946 Math::GMPq::Rmpq_set_z($q, $t);
7947  
7948 # (2^k - 1) * 2^k
7949 Math::GMPz::Rmpz_set_ui($t, 0);
7950 Math::GMPz::Rmpz_setbit($t, $k);
7951 Math::GMPz::Rmpz_sub_ui($t, $t, 1);
7952 Math::GMPz::Rmpz_mul_2exp($t, $t, $k);
7953  
7954 # B_k = q
7955 Math::GMPq::Rmpq_div_z($q, $q, $t);
7956  
7957 $B[($k >> 1) + 1] = $q;
7958 }
7959  
7960 push @cache, @B[@cache .. (@B <= 1024 ? $#B : 1024)];
7961  
7962 return (@cache, (@B > @cache ? @B[@cache .. $#B] : ()));
7963 }
7964  
7965 sub bernoulli_polynomial ($$) {
7966 my ($n, $x) = @_;
7967  
7968 #
7969 ## B_n(x) = Sum_{k=0..n} binomial(n, k) * bernoulli(n-k) * x^k
7970 #
7971  
7972 $n = _star2ui($n) // goto &nan;
7973 $x = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
7974  
7975 my @B = _bernoulli_numbers($n);
7976  
7977 my $u = $n + 1;
7978 my $z = Math::GMPz::Rmpz_init();
7979 my $q = Math::GMPq::Rmpq_init();
7980  
7981 my @terms;
7982  
7983 foreach my $k (0 .. $n) {
7984  
7985 --$u & 1 and $u > 1 and next; # B_n = 0 for odd n > 1
7986  
7987 Math::GMPz::Rmpz_bin_uiui($z, $n, $k);
7988 Math::GMPq::Rmpq_mul_z($q, $u <= 1 ? $B[$u] : $B[($u >> 1) + 1], $z);
7989  
7990 push @terms, __mul__(__pow__($x, $k), $q);
7991 }
7992  
7993 bless \_binsplit([reverse @terms], \&__add__);
7994 }
7995  
7996 #
7997 ## Bernoulli number
7998 #
7999  
8000 # Algorithm due to Kevin J. McGown (December 8, 2005).
8001 # Described in his paper: "Computing Bernoulli Numbers Quickly".
8002  
8003 sub __bernfrac__ {
8004 my ($n) = @_; # $n is an unsigned integer
8005  
8006 #<<<
8007 # B(n) = (-1)^(n/2 + 1) * zeta(n)*2*n! / (2*pi)^n
8008  
8009 if ($n == 0) {
8010 goto &_one;
8011 }
8012  
8013 if ($n == 1) {
8014 my $r = Math::GMPq::Rmpq_init();
8015 Math::GMPq::Rmpq_set_ui($r, 1, 2);
8016 return $r;
8017 }
8018  
8019 if (($n & 1) and ($n > 1)) { # Bn = 0 for odd n>1
8020 goto &_zero;
8021 }
8022  
8023 if ($n < 512) {
8024 return ((_bernoulli_numbers($n))[($n>>1)+1]);
8025 }
8026  
8027 state $round = Math::MPFR::MPFR_RNDN();
8028 state $tau = 6.28318530717958647692528676655900576839433879875;
8029  
8030 my $log2B = (CORE::log(4 * $tau * $n) / 2 + $n * (CORE::log($n / $tau) - 1)) / CORE::log(2);
8031  
8032 my $prec = CORE::int($n + $log2B) + ($n <= 90 ? 24 : 0);
8033 state $d = Math::GMPz::Rmpz_init_nobless();
8034 Math::GMPz::Rmpz_fac_ui($d, $n); # d = n!
8035  
8036 my $K = Math::MPFR::Rmpfr_init2($prec);
8037 Math::MPFR::Rmpfr_const_pi($K, $round); # K = pi
8038 Math::MPFR::Rmpfr_pow_si($K, $K, -$n, $round); # K = K^(-n)
8039 Math::MPFR::Rmpfr_mul_z($K, $K, $d, $round); # K = K*d
8040 Math::MPFR::Rmpfr_div_2ui($K, $K, $n - 1, $round); # K = K / 2^(n-1)
8041  
8042 # `d` is the denominator of bernoulli(n)
8043 Math::GMPz::Rmpz_set_ui($d, 2); # d = 2
8044  
8045 my @primes = (2);
8046  
8047 { # Sieve the primes <= n+1
8048 # Sieve of Eratosthenes + Dana Jacobsen's optimizations
8049 my $N = $n + 1;
8050  
8051 my @composite;
8052 my $bound = CORE::int(CORE::sqrt($N));
8053  
8054 for (my $i = 3 ; $i <= $bound ; $i += 2) {
8055 if (!exists($composite[$i])) {
8056 for (my $j = $i * $i ; $j <= $N ; $j += 2 * $i) {
8057 undef $composite[$j];
8058 }
8059 }
8060 }
8061  
8062 foreach my $k (1 .. ($N - 1) >> 1) {
8063 if (!exists($composite[2 * $k + 1])) {
8064  
8065 push(@primes, 2 * $k + 1);
8066  
8067 if ($n % (2 * $k) == 0) { # d = d*p iff (p-1)|n
8068 Math::GMPz::Rmpz_mul_ui($d, $d, 2 * $k + 1);
8069 }
8070 }
8071 }
8072 }
8073  
8074 state $N = Math::MPFR::Rmpfr_init2_nobless(64);
8075  
8076 Math::MPFR::Rmpfr_mul_z($K, $K, $d, $round); # K = K*d
8077 Math::MPFR::Rmpfr_set_ui($N, $n - 1, $round); # N = n-1
8078 Math::MPFR::Rmpfr_ui_div($N, 1, $N, $round); # N = 1/N
8079 Math::MPFR::Rmpfr_pow($N, $K, $N, $round); # N = K^N
8080  
8081 Math::MPFR::Rmpfr_ceil($N, $N); # N = ceil(N)
8082  
8083 my $bound = Math::MPFR::Rmpfr_get_ui($N, $round); # bound = int(N)
8084  
8085 my $z = Math::MPFR::Rmpfr_init2($prec); # zeta(n)
8086 my $u = Math::GMPz::Rmpz_init(); # p^n
8087  
8088 Math::MPFR::Rmpfr_set_ui($z, 1, $round); # z = 1
8089  
8090 # `Math::GMPf` would perform slightly faster here.
8091 for (my $i = 0 ; $primes[$i] <= $bound ; ++$i) { # primes <= bound
8092 Math::GMPz::Rmpz_ui_pow_ui($u, $primes[$i], $n); # u = p^n
8093 Math::MPFR::Rmpfr_mul_z($z, $z, $u, $round); # z = z*u
8094 Math::GMPz::Rmpz_sub_ui($u, $u, 1); # u = u-1
8095 Math::MPFR::Rmpfr_div_z($z, $z, $u, $round); # z = z/u
8096 }
8097  
8098 Math::MPFR::Rmpfr_mul($z, $z, $K, $round); # z = z * K
8099 Math::MPFR::Rmpfr_ceil($z, $z); # z = ceil(z)
8100  
8101 my $q = Math::GMPq::Rmpq_init();
8102  
8103 Math::GMPq::Rmpq_set_den($q, $d); # denominator
8104 Math::MPFR::Rmpfr_get_z($d, $z, $round);
8105 Math::GMPz::Rmpz_neg($d, $d) if $n % 4 == 0; # d = -d, iff 4|n
8106 Math::GMPq::Rmpq_set_num($q, $d); # numerator
8107  
8108 #>>>
8109 return $q; # Bn
8110 }
8111  
8112 sub bernfrac ($;$) {
8113 my ($n, $x) = @_;
8114  
8115 @_ == 2 && goto &bernoulli_polynomial;
8116 $n = _star2ui($n) // goto &nan;
8117  
8118 bless \__bernfrac__($n);
8119 }
8120  
8121 *bernoulli = \&bernfrac;
8122  
8123 sub faulhaber_polynomial {
8124 my ($n, $x) = @_;
8125  
8126 $n = _star2ui($n) // goto &nan;
8127 $x = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
8128  
8129 $n += 1;
8130 $x = __inc__($x);
8131  
8132 bernoulli_polynomial($n, $x)->sub(bernfrac($n))->div($n);
8133 }
8134  
8135 *faulhaber = \&faulhaber_polynomial;
8136  
8137 sub euler_polynomial ($$) {
8138 my ($n, $x) = @_;
8139  
8140 #
8141 ## E_n(x) = Sum_{k=0..n} binomial(n, n-k) * euler_number(n-k) / 2^(n-k) * (x - 1/2)^k
8142 #
8143  
8144 $n = _star2ui($n) // goto &nan;
8145 $x = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
8146  
8147 my @S = _secant_numbers($n >> 1);
8148  
8149 my $u = $n + 1;
8150 my $z = Math::GMPz::Rmpz_init();
8151  
8152 $x = __dec__(__add__($x, $x)); # x = 2*x - 1
8153  
8154 my @terms;
8155  
8156 foreach my $k (0 .. $n) {
8157 --$u & 1 and next; # E_n = 0 for all odd n
8158  
8159 Math::GMPz::Rmpz_bin_uiui($z, $n, $u);
8160 Math::GMPz::Rmpz_mul($z, $z, $S[$u >> 1]);
8161 Math::GMPz::Rmpz_neg($z, $z) if (($u >> 1) & 1);
8162  
8163 push @terms, __mul__(__pow__($x, $k), $z);
8164 }
8165  
8166 Math::GMPz::Rmpz_set_ui($z, 0);
8167 Math::GMPz::Rmpz_setbit($z, $n);
8168  
8169 bless \__div__(_binsplit(\@terms, \&__add__), $z);
8170 }
8171  
8172 sub euler ($;$) {
8173 my ($n, $x) = @_;
8174  
8175 @_ == 2 && goto &euler_polynomial;
8176  
8177 $n = _star2ui($n) // goto &nan;
8178 $n & 1 and goto &zero; # E_n = 0 for all odd indices
8179  
8180 my $e = Math::GMPz::Rmpz_init_set((_secant_numbers($n >> 1))[$n >> 1]);
8181 Math::GMPz::Rmpz_neg($e, $e) if (($n >> 1) & 1);
8182 bless \$e;
8183 }
8184  
8185 sub secant_number ($) {
8186 my ($n) = @_;
8187  
8188 $n = _star2ui($n) // goto &nan;
8189  
8190 my @E = _secant_numbers($n);
8191 bless \Math::GMPz::Rmpz_init_set($E[$n]);
8192 }
8193  
8194 sub tangent_number ($) {
8195 my ($n) = @_;
8196  
8197 $n = _star2ui($n) // goto &nan;
8198  
8199 $n || goto &zero;
8200 my @T = _tangent_numbers($n);
8201 bless \Math::GMPz::Rmpz_init_set($T[$n - 1]);
8202 }
8203  
8204 #
8205 ## The n-th Harmonic number: 1 + 1/2 + ... + 1/n
8206 #
8207  
8208 sub harmfrac ($) {
8209 my ($n) = @_;
8210  
8211 $n = _star2ui($n) // goto &nan;
8212  
8213 $n || goto &zero;
8214 $n < 0 && goto &nan;
8215  
8216 # Use binary splitting for large values of n. (by Fredrik Johansson)
8217 # https://fredrik-j.blogspot.com/2009/02/how-not-to-compute-harmonic-numbers.html
8218 if ($n > 7000) {
8219  
8220 my $num = Math::GMPz::Rmpz_init_set_ui(1);
8221 my $den = Math::GMPz::Rmpz_init_set_ui($n + 1);
8222  
8223 my $temp = Math::GMPz::Rmpz_init();
8224  
8225 # Translation of Dana Jacobsen's code from Math::Prime::Util::{PP,GMP}.
8226 # https://metacpan.org/pod/Math::Prime::Util::PP
8227 # https://metacpan.org/pod/Math::Prime::Util::GMP
8228 sub {
8229 my ($num, $den) = @_;
8230 Math::GMPz::Rmpz_sub($temp, $den, $num);
8231  
8232 if (Math::GMPz::Rmpz_cmp_ui($temp, 1) == 0) {
8233 Math::GMPz::Rmpz_set($den, $num);
8234 Math::GMPz::Rmpz_set_ui($num, 1);
8235 }
8236 elsif (Math::GMPz::Rmpz_cmp_ui($temp, 2) == 0) {
8237 Math::GMPz::Rmpz_set($den, $num);
8238 Math::GMPz::Rmpz_mul_2exp($num, $num, 1);
8239 Math::GMPz::Rmpz_add_ui($num, $num, 1);
8240 Math::GMPz::Rmpz_addmul($den, $den, $den);
8241 }
8242 else {
8243 Math::GMPz::Rmpz_add($temp, $num, $den);
8244 Math::GMPz::Rmpz_div_2exp($temp, $temp, 1);
8245 my $q = Math::GMPz::Rmpz_init_set($temp);
8246 my $r = Math::GMPz::Rmpz_init_set($temp);
8247 __SUB__->($num, $q);
8248 __SUB__->($r, $den);
8249 Math::GMPz::Rmpz_mul($num, $num, $den);
8250 Math::GMPz::Rmpz_mul($temp, $q, $r);
8251 Math::GMPz::Rmpz_add($num, $num, $temp);
8252 Math::GMPz::Rmpz_mul($den, $den, $q);
8253 }
8254 }
8255 ->($num, $den);
8256  
8257 my $q = Math::GMPq::Rmpq_init();
8258 Math::GMPq::Rmpq_set_num($q, $num);
8259 Math::GMPq::Rmpq_set_den($q, $den);
8260 Math::GMPq::Rmpq_canonicalize($q);
8261 return bless \$q;
8262 }
8263  
8264 my $num = Math::GMPz::Rmpz_init_set_ui(1);
8265 my $den = Math::GMPz::Rmpz_init_set_ui(1);
8266  
8267 for (my $k = 2 ; $k <= $n ; ++$k) {
8268 Math::GMPz::Rmpz_mul_ui($num, $num, $k); # num = num * k
8269 Math::GMPz::Rmpz_add($num, $num, $den); # num = num + den
8270 Math::GMPz::Rmpz_mul_ui($den, $den, $k); # den = den * k
8271 }
8272  
8273 my $r = Math::GMPq::Rmpq_init();
8274 Math::GMPq::Rmpq_set_num($r, $num);
8275 Math::GMPq::Rmpq_set_den($r, $den);
8276 Math::GMPq::Rmpq_canonicalize($r);
8277 bless \$r;
8278 }
8279  
8280 *harmonic = \&harmfrac;
8281  
8282 #
8283 ## Bernoulli number as a floating-point value
8284 #
8285  
8286 sub bernreal ($) {
8287 my ($n) = @_;
8288  
8289 $n = _star2ui($n) // goto &nan;
8290  
8291 if ($n == 0) {
8292 my $r = Math::MPFR::Rmpfr_init2($PREC);
8293 Math::MPFR::Rmpfr_set_ui($r, 1, $ROUND);
8294 return bless \$r;
8295 }
8296  
8297 if ($n == 1) {
8298 my $r = Math::MPFR::Rmpfr_init2($PREC);
8299 Math::MPFR::Rmpfr_set_d($r, 0.5, $ROUND);
8300 return bless \$r;
8301 }
8302  
8303 if ($n & 1) { # Bn = 0 for odd n>1
8304 my $r = Math::MPFR::Rmpfr_init2($PREC);
8305 Math::MPFR::Rmpfr_set_ui($r, 0, $ROUND);
8306 return bless \$r;
8307 }
8308  
8309 my $f = Math::MPFR::Rmpfr_init2($PREC);
8310 my $p = Math::MPFR::Rmpfr_init2($PREC);
8311  
8312 Math::MPFR::Rmpfr_zeta_ui($f, $n, $ROUND); # f = zeta(n)
8313 Math::MPFR::Rmpfr_set_ui($p, $n + 1, $ROUND); # p = n+1
8314 Math::MPFR::Rmpfr_gamma($p, $p, $ROUND); # p = gamma(p)
8315  
8316 Math::MPFR::Rmpfr_mul($f, $f, $p, $ROUND); # f = f * p
8317  
8318 Math::MPFR::Rmpfr_const_pi($p, $ROUND); # p = PI
8319 Math::MPFR::Rmpfr_pow_ui($p, $p, $n, $ROUND); # p = p^n
8320  
8321 Math::MPFR::Rmpfr_div_2ui($f, $f, $n - 1, $ROUND); # f = f / 2^(n-1)
8322  
8323 Math::MPFR::Rmpfr_div($f, $f, $p, $ROUND); # f = f/p
8324 Math::MPFR::Rmpfr_neg($f, $f, $ROUND) if $n % 4 == 0;
8325  
8326 bless \$f;
8327 }
8328  
8329 # Natural logarithm of the n-th Bernoulli number
8330  
8331 sub lnbern ($) {
8332 my ($n) = @_;
8333  
8334 $n = _star2mpz($n) // goto &nan;
8335  
8336 # log(|B(n)|) = (1 - n)*log(2) - n*log(Ï€) + log(zeta(n)) + log(n!)
8337  
8338 (Math::GMPz::Rmpz_sgn($n) || goto &zero) < 0 and goto &nan;
8339  
8340 my $L = Math::MPFR::Rmpfr_init2($PREC);
8341 Math::MPFR::Rmpfr_const_log2($L, $ROUND);
8342  
8343 if (Math::GMPz::Rmpz_cmp_ui($n, 1) == 0) {
8344 Math::MPFR::Rmpfr_neg($L, $L, $ROUND);
8345 return bless \$L;
8346 }
8347  
8348 Math::GMPz::Rmpz_odd_p($n) && goto &ninf; # log(Bn) = -Inf for odd n>1
8349  
8350 my $pi = Math::MPFR::Rmpfr_init2($PREC);
8351 Math::MPFR::Rmpfr_const_pi($pi, $ROUND); # pi = π
8352  
8353 my $t = Math::MPFR::Rmpfr_init2($PREC);
8354 Math::MPFR::Rmpfr_log($t, $pi, $ROUND); # t = log(Ï€)
8355 Math::MPFR::Rmpfr_mul_z($t, $t, $n, $ROUND); # t = n*log(Ï€)
8356  
8357 my $s = Math::GMPz::Rmpz_init();
8358 Math::GMPz::Rmpz_ui_sub($s, 1, $n); # s = 1-n
8359  
8360 Math::MPFR::Rmpfr_mul_z($L, $L, $s, $ROUND); # L = (1 - n)*log(2)
8361 Math::MPFR::Rmpfr_sub($L, $L, $t, $ROUND); # L -= n*log(Ï€)
8362  
8363 if (Math::GMPz::Rmpz_fits_ulong_p($n)) { # n is a native unsigned integer
8364 Math::MPFR::Rmpfr_zeta_ui($t, Math::GMPz::Rmpz_get_ui($n), $ROUND);
8365 }
8366 else {
8367 Math::MPFR::Rmpfr_set_z($t, $n, $ROUND); # t = n
8368 Math::MPFR::Rmpfr_zeta($t, $t, $ROUND); # t = zeta(n)
8369 }
8370  
8371 Math::MPFR::Rmpfr_log($t, $t, $ROUND); # t = log(zeta(n))
8372 Math::MPFR::Rmpfr_add($L, $L, $t, $ROUND); # L += log(zeta(n))
8373  
8374 Math::GMPz::Rmpz_add_ui($s, $n, 1); # s = n+1
8375 Math::MPFR::Rmpfr_set_z($t, $s, $ROUND); # t = n+1
8376 Math::MPFR::Rmpfr_lngamma($t, $t, $ROUND); # t = log(gamma(n+1)) = log(n!)
8377  
8378 Math::MPFR::Rmpfr_add($L, $L, $t, $ROUND); # L += log(n!)
8379  
8380 # If 4|n, then B_n is negative; log(-Re(x)) = log(Re(x)) + π*i, for x>0
8381 if (Math::GMPz::Rmpz_divisible_2exp_p($n, 2)) {
8382 my $c = Math::MPC::Rmpc_init2($PREC);
8383 Math::MPC::Rmpc_set_fr_fr($c, $L, $pi, $ROUND);
8384 return bless \$c;
8385 }
8386  
8387 bless \$L;
8388 }
8389  
8390 #
8391 ## The n-th Harmonic number as a floating-point value
8392 #
8393  
8394 # harmreal(x) = digamma(x+1) + EulerGamma
8395  
8396 sub __harmreal__ {
8397 my ($x) = @_; # $x is a Math::MPFR object
8398  
8399 my $r = Math::MPFR::Rmpfr_init2($PREC);
8400 Math::MPFR::Rmpfr_add_ui($r, $x, 1, $ROUND);
8401 Math::MPFR::Rmpfr_digamma($r, $r, $ROUND);
8402  
8403 my $t = Math::MPFR::Rmpfr_init2($PREC);
8404 Math::MPFR::Rmpfr_const_euler($t, $ROUND);
8405 Math::MPFR::Rmpfr_add($r, $r, $t, $ROUND);
8406  
8407 $r;
8408 }
8409  
8410 sub harmreal ($) {
8411 bless \__harmreal__(_star2mpfr($_[0]) // (goto &nan));
8412 }
8413  
8414 #
8415 ## Subfactorial
8416 #
8417  
8418 sub subfactorial ($;$) {
8419 my ($m, $k) = @_;
8420  
8421 $m = _star2ui($m) // goto &nan;
8422  
8423 if (defined($k)) {
8424 $k = _star2si($k) // goto &nan;
8425 }
8426 else {
8427 $k = 0;
8428 }
8429  
8430 my $n = $m - $k;
8431  
8432 goto &zero if ($k < 0);
8433 goto &one if ($n == 0);
8434 goto &zero if ($n < 0);
8435  
8436 my $tau = 6.28318530717958647692528676655900576839433879875;
8437 my $prec = 4 + CORE::int(($n * CORE::log($n) + CORE::log($tau * $n) / 2 - $n) / CORE::log(2));
8438  
8439 my $z = Math::GMPz::Rmpz_init();
8440 Math::GMPz::Rmpz_fac_ui($z, $n);
8441  
8442 state $round_z = Math::MPFR::MPFR_RNDZ();
8443  
8444 my $f = Math::MPFR::Rmpfr_init2($prec);
8445 Math::MPFR::Rmpfr_set_ui($f, 1, $round_z);
8446 Math::MPFR::Rmpfr_exp($f, $f, $round_z);
8447 Math::MPFR::Rmpfr_z_div($f, $z, $f, $round_z);
8448 Math::MPFR::Rmpfr_add_d($f, $f, 0.5, $round_z);
8449 Math::MPFR::Rmpfr_floor($f, $f);
8450 Math::MPFR::Rmpfr_get_z($z, $f, $round_z);
8451  
8452 if ($k != 0) {
8453 my $t = Math::GMPz::Rmpz_init();
8454 Math::GMPz::Rmpz_bin_uiui($t, $m, $k);
8455 Math::GMPz::Rmpz_mul($z, $z, $t);
8456 }
8457  
8458 bless \$z;
8459 }
8460  
8461 sub superfactorial ($) {
8462 my ($n) = @_;
8463  
8464 $n = _star2ui($n) // goto &nan;
8465  
8466 my @list;
8467 foreach my $k (2 .. $n) {
8468 my $z = Math::GMPz::Rmpz_init();
8469 Math::GMPz::Rmpz_ui_pow_ui($z, $k, $n - $k + 1);
8470 push @list, $z;
8471 }
8472  
8473 @list || goto &one;
8474 bless \_binsplit(\@list, \&__mul__);
8475 }
8476  
8477 sub lnsuperfactorial ($) {
8478 my ($n) = @_;
8479  
8480 $n = _star2ui($n) // goto &nan;
8481  
8482 my $r = Math::MPFR::Rmpfr_init2($PREC);
8483 my $t = Math::MPFR::Rmpfr_init2($PREC);
8484  
8485 Math::MPFR::Rmpfr_set_ui($r, 0, $ROUND);
8486  
8487 foreach my $k (2 .. $n) {
8488 Math::MPFR::Rmpfr_set_ui($t, $k, $ROUND);
8489 Math::MPFR::Rmpfr_log($t, $t, $ROUND);
8490 Math::MPFR::Rmpfr_mul_ui($t, $t, $n - $k + 1, $ROUND);
8491 Math::MPFR::Rmpfr_add($r, $r, $t, $ROUND);
8492 }
8493  
8494 bless \$r;
8495 }
8496  
8497 sub hyperfactorial ($) {
8498 my ($n) = @_;
8499  
8500 $n = _star2ui($n) // goto &nan;
8501  
8502 my @list;
8503 foreach my $k (2 .. $n) {
8504 my $z = Math::GMPz::Rmpz_init();
8505 Math::GMPz::Rmpz_ui_pow_ui($z, $k, $k);
8506 push @list, $z;
8507 }
8508  
8509 @list || goto &one;
8510 bless \_binsplit(\@list, \&__mul__);
8511 }
8512  
8513 sub lnhyperfactorial ($) {
8514 my ($n) = @_;
8515  
8516 $n = _star2ui($n) // goto &nan;
8517  
8518 my $r = Math::MPFR::Rmpfr_init2($PREC);
8519 my $t = Math::MPFR::Rmpfr_init2($PREC);
8520  
8521 Math::MPFR::Rmpfr_set_ui($r, 0, $ROUND);
8522  
8523 foreach my $k (2 .. $n) {
8524 Math::MPFR::Rmpfr_set_ui($t, $k, $ROUND);
8525 Math::MPFR::Rmpfr_log($t, $t, $ROUND);
8526 Math::MPFR::Rmpfr_mul_ui($t, $t, $k, $ROUND);
8527 Math::MPFR::Rmpfr_add($r, $r, $t, $ROUND);
8528 }
8529  
8530 bless \$r;
8531 }
8532  
8533 #
8534 ## Factorial
8535 #
8536  
8537 sub factorial ($) {
8538 my ($n) = @_;
8539  
8540 $n = _star2ui($n) // goto &nan;
8541  
8542 my $r = Math::GMPz::Rmpz_init();
8543 Math::GMPz::Rmpz_fac_ui($r, $n);
8544 bless \$r;
8545 }
8546  
8547 #
8548 ## Double-factorial
8549 #
8550  
8551 sub dfactorial ($) {
8552 my ($n) = @_;
8553  
8554 $n = _star2ui($n) // goto &nan;
8555  
8556 my $r = Math::GMPz::Rmpz_init();
8557 Math::GMPz::Rmpz_2fac_ui($r, $n);
8558 bless \$r;
8559 }
8560  
8561 #
8562 ## M-factorial
8563 #
8564  
8565 sub mfactorial ($$) {
8566 my ($n, $k) = @_;
8567  
8568 $n = _star2ui($n) // goto &nan;
8569 $k = _star2ui($k) // goto &nan;
8570  
8571 my $r = Math::GMPz::Rmpz_init();
8572 Math::GMPz::Rmpz_mfac_uiui($r, $n, $k);
8573 bless \$r;
8574 }
8575  
8576 #
8577 ## falling_factorial(x, +y) = binomial(x, y) * y!
8578 ## falling_factorial(x, -y) = 1/falling_factorial(x + y, y)
8579 #
8580 sub falling_factorial ($$) {
8581 my ($x, $y) = @_;
8582  
8583 $x = _star2mpz($x) // goto &nan;
8584 $y = _star2si($y) // goto &nan;
8585  
8586 my $r = Math::GMPz::Rmpz_init_set($x);
8587  
8588 if ($y < 0) {
8589 Math::GMPz::Rmpz_add_ui($r, $r, -$y);
8590 }
8591  
8592 Math::GMPz::Rmpz_fits_ulong_p($r)
8593 ? Math::GMPz::Rmpz_bin_uiui($r, Math::GMPz::Rmpz_get_ui($r), $y < 0 ? -$y : $y)
8594 : Math::GMPz::Rmpz_bin_ui($r, $r, $y < 0 ? -$y : $y);
8595  
8596 Math::GMPz::Rmpz_sgn($r) || do {
8597 $y < 0
8598 ? (goto &nan)
8599 : (goto &zero);
8600 };
8601  
8602 state $t = Math::GMPz::Rmpz_init_nobless();
8603 Math::GMPz::Rmpz_fac_ui($t, $y < 0 ? -$y : $y);
8604 Math::GMPz::Rmpz_mul($r, $r, $t);
8605  
8606 if ($y < 0) {
8607 my $q = Math::GMPq::Rmpq_init();
8608 Math::GMPq::Rmpq_set_z($q, $r);
8609 Math::GMPq::Rmpq_inv($q, $q);
8610 return bless \$q;
8611 }
8612  
8613 bless \$r;
8614 }
8615  
8616 #
8617 ## rising_factorial(x, +y) = binomial(x + y - 1, y) * y!
8618 ## rising_factorial(x, -y) = 1/rising_factorial(x - y, y)
8619 #
8620 sub rising_factorial ($$) {
8621 my ($x, $y) = @_;
8622  
8623 $x = _star2mpz($x) // goto &nan;
8624 $y = _star2si($y) // goto &nan;
8625  
8626 my $r = Math::GMPz::Rmpz_init_set($x);
8627 Math::GMPz::Rmpz_add_ui($r, $r, $y < 0 ? -$y : $y);
8628 Math::GMPz::Rmpz_sub_ui($r, $r, 1);
8629  
8630 if ($y < 0) {
8631 Math::GMPz::Rmpz_sub_ui($r, $r, $y < 0 ? -$y : $y);
8632 }
8633  
8634 Math::GMPz::Rmpz_fits_ulong_p($r)
8635 ? Math::GMPz::Rmpz_bin_uiui($r, Math::GMPz::Rmpz_get_ui($r), $y < 0 ? -$y : $y)
8636 : Math::GMPz::Rmpz_bin_ui($r, $r, $y < 0 ? -$y : $y);
8637  
8638 Math::GMPz::Rmpz_sgn($r) || do {
8639 $y < 0
8640 ? (goto &nan)
8641 : (goto &zero);
8642 };
8643  
8644 state $t = Math::GMPz::Rmpz_init_nobless();
8645 Math::GMPz::Rmpz_fac_ui($t, $y < 0 ? -$y : $y);
8646 Math::GMPz::Rmpz_mul($r, $r, $t);
8647  
8648 if ($y < 0) {
8649 my $q = Math::GMPq::Rmpq_init();
8650 Math::GMPq::Rmpq_set_z($q, $r);
8651 Math::GMPq::Rmpq_inv($q, $q);
8652 return bless \$q;
8653 }
8654  
8655 bless \$r;
8656 }
8657  
8658 #
8659 ## Greatest common multiple
8660 #
8661  
8662 sub gcd {
8663 my ($x, $y) = @_;
8664  
8665 @_ or goto &zero; # By convention, gcd of an empty set is 0.
8666 @_ == 1 and return $x;
8667  
8668 my $r = Math::GMPz::Rmpz_init();
8669  
8670 if (@_ > 2) {
8671 my @terms = map { _star2mpz($_) // goto &nan } @_;
8672  
8673 Math::GMPz::Rmpz_set($r, shift(@terms));
8674  
8675 foreach my $z (@terms) {
8676 Math::GMPz::Rmpz_gcd($r, $r, $z);
8677 Math::GMPz::Rmpz_cmp_ui($r, 1) || last;
8678 }
8679  
8680 return bless \$r;
8681 }
8682  
8683 if (ref($y) and !ref($x)) {
8684 ($x, $y) = ($y, $x);
8685 }
8686  
8687 $x = _star2mpz($x) // goto &nan;
8688  
8689 if (!ref($y) and CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
8690 Math::GMPz::Rmpz_gcd_ui($r, $x, $y < 0 ? -$y : $y);
8691 }
8692 else {
8693 $y = _star2mpz($y) // goto &nan;
8694 Math::GMPz::Rmpz_gcd($r, $x, $y);
8695 }
8696  
8697 bless \$r;
8698 }
8699  
8700 sub gcdext ($$) {
8701 my ($n, $k) = @_;
8702  
8703 $n = _star2mpz($n) // return (nan(), nan());
8704 $k = _star2mpz($k) // return (nan(), nan());
8705  
8706 my $g = Math::GMPz::Rmpz_init();
8707 my $u = Math::GMPz::Rmpz_init();
8708 my $v = Math::GMPz::Rmpz_init();
8709  
8710 Math::GMPz::Rmpz_gcdext($g, $u, $v, $n, $k);
8711  
8712 ((bless \$u), (bless \$v), (bless \$g));
8713 }
8714  
8715 #
8716 ## Least common multiple
8717 #
8718  
8719 sub __lcm__ {
8720 my ($n, $k) = @_;
8721 my $r = Math::GMPz::Rmpz_init();
8722 Math::GMPz::Rmpz_lcm($r, $n, $k);
8723 $r;
8724 }
8725  
8726 sub lcm {
8727 my ($x, $y) = @_;
8728  
8729 @_ or goto &one; # By convention, lcm of an empty set is 1.
8730 @_ == 1 and return $x;
8731  
8732 if (@_ > 2) {
8733 my @terms = map { _star2mpz($_) // goto &nan } @_;
8734 return bless \_binsplit(\@terms, \&__lcm__);
8735 }
8736  
8737 if (ref($y) and !ref($x)) {
8738 ($x, $y) = ($y, $x);
8739 }
8740  
8741 $x = _star2mpz($x) // goto &nan;
8742  
8743 my $r = Math::GMPz::Rmpz_init();
8744  
8745 if (!ref($y) and CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
8746 Math::GMPz::Rmpz_lcm_ui($r, $x, $y < 0 ? -$y : $y);
8747 }
8748 else {
8749 $y = _star2mpz($y) // goto &nan;
8750 Math::GMPz::Rmpz_lcm($r, $x, $y);
8751 }
8752  
8753 bless \$r;
8754 }
8755  
8756 #
8757 ## Next prime after `n`.
8758 #
8759  
8760 sub next_prime ($) {
8761 my ($n) = @_;
8762 $n = _star2mpz($n) // goto &nan;
8763 my $r = Math::GMPz::Rmpz_init();
8764 Math::GMPz::Rmpz_nextprime($r, $n);
8765 bless \$r;
8766 }
8767  
8768 #
8769 ## True if `n` is an integer
8770 #
8771  
8772 sub __is_int__ {
8773 my ($x) = @_;
8774  
8775 ref($x) eq 'Math::GMPz' && return 1;
8776 ref($x) eq 'Math::GMPq' && return Math::GMPq::Rmpq_integer_p($x);
8777 ref($x) eq 'Math::MPFR' && return Math::MPFR::Rmpfr_integer_p($x);
8778  
8779 (@_) = _any2mpfr($x);
8780 goto __SUB__;
8781 }
8782  
8783 sub is_int ($) {
8784 my ($n) = @_;
8785 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
8786 ref($n) eq 'Math::GMPz' ? 1 : __is_int__($n);
8787 }
8788  
8789 #
8790 ## True if `n` is a rational number
8791 #
8792  
8793 sub is_rat ($) {
8794 my ($n) = @_;
8795 my $ref = ref(ref($n) eq __PACKAGE__ ? $$n : _star2obj($n));
8796 $ref eq 'Math::GMPz' or $ref eq 'Math::GMPq';
8797 }
8798  
8799 #
8800 ## True if `n` is probably a prime number
8801 #
8802  
8803 sub is_prime ($;$) {
8804 my ($n, $r) = @_;
8805  
8806 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
8807  
8808 if (ref($n) ne 'Math::GMPz') {
8809 __is_int__($n) || return 0;
8810 $n = _any2mpz($n) // return 0;
8811 }
8812  
8813 Math::GMPz::Rmpz_sgn($n) > 0 or return 0;
8814 $r = defined($r) ? (CORE::abs(CORE::int($r)) || 23) : 23;
8815 Math::GMPz::Rmpz_probab_prime_p($n, $r);
8816 }
8817  
8818 #
8819 ## True if `n` is coprime to `k`
8820 #
8821  
8822 sub is_coprime ($$) {
8823 my ($n, $k) = @_;
8824  
8825 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
8826  
8827 if (ref($n) ne 'Math::GMPz') {
8828 __is_int__($n) || return 0;
8829 $n = _any2mpz($n) // return 0;
8830 }
8831  
8832 if (!ref($k) and CORE::int($k) eq $k and $k >= 0 and $k < ULONG_MAX) {
8833 ## `k` is a native integer
8834 }
8835 else {
8836 $k = ref($k) eq __PACKAGE__ ? $$k : _star2obj($k);
8837  
8838 if (ref($k) ne 'Math::GMPz') {
8839 __is_int__($k) || return 0;
8840 $k = _any2mpz($k) // return 0;
8841 }
8842 }
8843  
8844 if (ref($k)) {
8845 state $t = Math::GMPz::Rmpz_init_nobless();
8846 Math::GMPz::Rmpz_gcd($t, $n, $k);
8847 return (Math::GMPz::Rmpz_cmp_ui($t, 1) == 0);
8848 }
8849  
8850 Math::GMPz::Rmpz_gcd_ui($Math::GMPz::NULL, $n, $k) == 1;
8851 }
8852  
8853 #
8854 ## True if all the prime factors of `n` are <= k.
8855 #
8856  
8857 sub is_smooth ($$) {
8858 my ($n, $k) = @_;
8859  
8860 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
8861  
8862 if (ref($n) ne 'Math::GMPz') {
8863 __is_int__($n) || return 0;
8864 $n = _any2mpz($n) // return 0;
8865 }
8866  
8867 return 0 if Math::GMPz::Rmpz_sgn($n) <= 0;
8868  
8869 $k = _star2ui($k) // return 0;
8870  
8871 return 1 if Math::GMPz::Rmpz_cmp_ui($n, 1) == 0;
8872 return 0 if $k <= 1;
8873  
8874 my $B = _cached_primorial($k);
8875  
8876 state $g = Math::GMPz::Rmpz_init_nobless();
8877 Math::GMPz::Rmpz_gcd($g, $n, $B);
8878  
8879 if (Math::GMPz::Rmpz_cmp_ui($g, 1) == 0) {
8880 return 0;
8881 }
8882  
8883 my $t = Math::GMPz::Rmpz_init_set($n);
8884  
8885 while (Math::GMPz::Rmpz_cmp_ui($g, 1) > 0) {
8886 Math::GMPz::Rmpz_remove($t, $t, $g);
8887 return 1 if Math::GMPz::Rmpz_cmp_ui($t, 1) == 0;
8888 Math::GMPz::Rmpz_gcd($g, $t, $g);
8889 }
8890  
8891 return 0;
8892 }
8893  
8894 #
8895 ## True if all the prime factors of `n` are >= k.
8896 #
8897  
8898 sub is_rough ($$) {
8899 my ($n, $k) = @_;
8900  
8901 $n = _star2mpz($n) // return 0;
8902  
8903 return 0 if Math::GMPz::Rmpz_sgn($n) <= 0;
8904 return 1 if Math::GMPz::Rmpz_cmp_ui($n, 1) == 0;
8905  
8906 $k = (_star2ui($k) // return 0) - 1;
8907  
8908 return 1 if $k <= 1;
8909  
8910 my $B = _cached_primorial($k);
8911  
8912 state $g = Math::GMPz::Rmpz_init_nobless();
8913 Math::GMPz::Rmpz_gcd($g, $n, $B);
8914 (Math::GMPz::Rmpz_cmp_ui($g, 1) > 0) ? 0 : 1;
8915 }
8916  
8917 #
8918 ## Smooth part of n, containing all prime factors p|n such that p <= k.
8919 #
8920 sub smooth_part {
8921 my ($n, $k) = @_;
8922  
8923 $n = _star2mpz($n) // goto &nan;
8924  
8925 goto &zero if Math::GMPz::Rmpz_sgn($n) <= 0;
8926  
8927 $k = _star2ui($k) // goto &nan;
8928  
8929 goto &one if $k <= 1;
8930 goto &one if Math::GMPz::Rmpz_cmp_ui($n, 1) == 0;
8931  
8932 my $B = _cached_primorial($k);
8933  
8934 state $g = Math::GMPz::Rmpz_init_nobless();
8935 Math::GMPz::Rmpz_gcd($g, $n, $B);
8936  
8937 if (Math::GMPz::Rmpz_cmp_ui($g, 1) == 0) {
8938 goto &one;
8939 }
8940  
8941 my $t = Math::GMPz::Rmpz_init_set($n);
8942  
8943 while (Math::GMPz::Rmpz_cmp_ui($g, 1) > 0) {
8944 Math::GMPz::Rmpz_remove($t, $t, $g);
8945 return (bless \$n) if Math::GMPz::Rmpz_cmp_ui($t, 1) == 0;
8946 Math::GMPz::Rmpz_gcd($g, $t, $g);
8947 }
8948  
8949 Math::GMPz::Rmpz_divexact($t, $n, $t);
8950 bless \$t;
8951 }
8952  
8953 #
8954 ## Rough part of n, containing all prime factors p|n such that p >= k.
8955 #
8956 sub rough_part {
8957 my ($n, $k) = @_;
8958  
8959 $n = _star2mpz($n) // goto &nan;
8960  
8961 goto &zero if Math::GMPz::Rmpz_sgn($n) <= 0;
8962  
8963 $k = (_star2ui($k) // goto &nan) - 1;
8964  
8965 return (bless \$n) if $k <= 1;
8966 goto &one if Math::GMPz::Rmpz_cmp_ui($n, 1) == 0;
8967  
8968 my $B = _cached_primorial($k);
8969  
8970 state $g = Math::GMPz::Rmpz_init_nobless();
8971 Math::GMPz::Rmpz_gcd($g, $n, $B);
8972  
8973 if (Math::GMPz::Rmpz_cmp_ui($g, 1) == 0) {
8974 return bless \$n;
8975 }
8976  
8977 my $t = Math::GMPz::Rmpz_init_set($n);
8978  
8979 while (Math::GMPz::Rmpz_cmp_ui($g, 1) > 0) {
8980 Math::GMPz::Rmpz_remove($t, $t, $g);
8981 goto &one if Math::GMPz::Rmpz_cmp_ui($t, 1) == 0;
8982 Math::GMPz::Rmpz_gcd($g, $t, $g);
8983 }
8984  
8985 bless \$t;
8986 }
8987  
8988 sub is_smooth_over_prod ($$) {
8989 my ($n, $k) = @_;
8990  
8991 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
8992  
8993 if (ref($n) ne 'Math::GMPz') {
8994 __is_int__($n) || return 0;
8995 $n = _any2mpz($n) // return 0;
8996 }
8997  
8998 return 0 if Math::GMPz::Rmpz_sgn($n) <= 0;
8999  
9000 $k = ref($k) eq __PACKAGE__ ? $$k : _star2obj($k);
9001  
9002 if (ref($k) ne 'Math::GMPz') {
9003 __is_int__($k) || return 0;
9004 $k = _any2mpz($k) // return 0;
9005 }
9006  
9007 return 0 if Math::GMPz::Rmpz_sgn($k) <= 0;
9008 return 1 if Math::GMPz::Rmpz_cmp_ui($n, 1) == 0;
9009  
9010 state $g = Math::GMPz::Rmpz_init_nobless();
9011  
9012 Math::GMPz::Rmpz_gcd($g, $n, $k);
9013  
9014 if (Math::GMPz::Rmpz_cmp_ui($g, 1) == 0) {
9015 return 0;
9016 }
9017  
9018 my $t = Math::GMPz::Rmpz_init_set($n);
9019  
9020 while (Math::GMPz::Rmpz_cmp_ui($g, 1) > 0) {
9021 Math::GMPz::Rmpz_remove($t, $t, $g);
9022 return 1 if Math::GMPz::Rmpz_cmp_ui($t, 1) == 0;
9023 Math::GMPz::Rmpz_gcd($g, $t, $g);
9024 }
9025  
9026 return 0;
9027 }
9028  
9029 #
9030 ## Numerator of a number
9031 #
9032  
9033 sub numerator ($) {
9034 my ($x) = @_;
9035  
9036 my $r = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
9037  
9038 while (1) {
9039  
9040 if (ref($r) eq 'Math::GMPq') {
9041 my $z = Math::GMPz::Rmpz_init();
9042 Math::GMPq::Rmpq_get_num($z, $r);
9043 return bless \$z;
9044 }
9045  
9046 ref($r) eq 'Math::GMPz' and return $x; # is an integer
9047  
9048 $r = _any2mpq($r) // goto &nan;
9049 }
9050 }
9051  
9052 #
9053 ## Denominator of a number
9054 #
9055  
9056 sub denominator ($) {
9057 my ($x) = @_;
9058  
9059 my $r = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
9060  
9061 while (1) {
9062  
9063 if (ref($r) eq 'Math::GMPq') {
9064 my $z = Math::GMPz::Rmpz_init();
9065 Math::GMPq::Rmpq_get_den($z, $r);
9066 return bless \$z;
9067 }
9068  
9069 ref($r) eq 'Math::GMPz' and goto &one; # is an integer
9070  
9071 $r = _any2mpq($r) // goto &nan;
9072 }
9073 }
9074  
9075 #
9076 ## (numerator, denominator)
9077 #
9078  
9079 sub nude ($) {
9080 my ($x) = @_;
9081  
9082 if (ref($x) ne __PACKAGE__) {
9083 $x = __PACKAGE__->new($x);
9084 }
9085  
9086 ($x->numerator, $x->denominator);
9087 }
9088  
9089 #
9090 ## Sign of a number
9091 #
9092  
9093 sub __sgn__ {
9094 my ($x) = @_;
9095 goto(ref($x) =~ tr/:/_/rs);
9096  
9097 Math_MPFR: {
9098 return Math::MPFR::Rmpfr_sgn($x);
9099 }
9100  
9101 Math_GMPq: {
9102 return Math::GMPq::Rmpq_sgn($x);
9103 }
9104  
9105 Math_GMPz: {
9106 return Math::GMPz::Rmpz_sgn($x);
9107 }
9108  
9109 # sgn(x) = x / abs(x)
9110 Math_MPC: {
9111 my $abs = Math::MPFR::Rmpfr_init2($PREC);
9112 Math::MPC::Rmpc_abs($abs, $x, $ROUND);
9113  
9114 if (Math::MPFR::Rmpfr_zero_p($abs)) { # it's zero
9115 return 0;
9116 }
9117  
9118 my $r = Math::MPC::Rmpc_init2($PREC);
9119 Math::MPC::Rmpc_div_fr($r, $x, $abs, $ROUND);
9120 return $r;
9121 }
9122 }
9123  
9124 sub sgn ($) {
9125 my ($x) = @_;
9126 my $r = __sgn__(ref($x) eq __PACKAGE__ ? $$x : _star2obj($x));
9127 ref($r) ? (bless \$r) : $r;
9128 }
9129  
9130 #
9131 ## True if `x` is a real number
9132 #
9133  
9134 sub is_real ($) {
9135 my ($x) = @_;
9136  
9137 my $r = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
9138  
9139 while (1) {
9140 my $ref = ref($r);
9141  
9142 $ref eq 'Math::GMPz' && return 1;
9143 $ref eq 'Math::GMPq' && return 1;
9144 $ref eq 'Math::MPFR' && return Math::MPFR::Rmpfr_number_p($r);
9145  
9146 $r = _any2mpfr($r);
9147 }
9148 }
9149  
9150 #
9151 ## True if `x` is an imaginary number
9152 #
9153  
9154 sub is_imag ($) {
9155 my ($x) = @_;
9156  
9157 my $r = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
9158 ref($r) eq 'Math::MPC' or return 0;
9159  
9160 my $f = Math::MPFR::Rmpfr_init2($PREC);
9161 Math::MPC::RMPC_RE($f, $r);
9162 Math::MPFR::Rmpfr_zero_p($f) || return 0; # is complex
9163 Math::MPC::RMPC_IM($f, $r);
9164 !Math::MPFR::Rmpfr_zero_p($f);
9165 }
9166  
9167 #
9168 ## True if `x` is a complex number
9169 #
9170  
9171 sub is_complex ($) {
9172 my ($x) = @_;
9173  
9174 my $r = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
9175 ref($r) eq 'Math::MPC' or return 0;
9176  
9177 my $f = Math::MPFR::Rmpfr_init2($PREC);
9178 Math::MPC::RMPC_IM($f, $r);
9179 Math::MPFR::Rmpfr_zero_p($f) && return 0; # is real
9180 Math::MPC::RMPC_RE($f, $r);
9181 !Math::MPFR::Rmpfr_zero_p($f);
9182 }
9183  
9184 #
9185 ## True if `x == +Inf`
9186 #
9187  
9188 sub is_inf ($) {
9189 my ($x) = @_;
9190  
9191 my $r = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
9192  
9193 while (1) {
9194 my $ref = ref($r);
9195  
9196 $ref eq 'Math::GMPz' && return 0;
9197 $ref eq 'Math::GMPq' && return 0;
9198 $ref eq 'Math::MPFR' && return (Math::MPFR::Rmpfr_inf_p($r) and Math::MPFR::Rmpfr_sgn($r) > 0);
9199  
9200 $r = _any2mpfr($r);
9201 }
9202 }
9203  
9204 #
9205 ## True if `x == -Inf`
9206 #
9207  
9208 sub is_ninf ($) {
9209 my ($x) = @_;
9210  
9211 my $r = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
9212  
9213 while (1) {
9214 my $ref = ref($r);
9215  
9216 $ref eq 'Math::GMPz' && return 0;
9217 $ref eq 'Math::GMPq' && return 0;
9218 $ref eq 'Math::MPFR' && return (Math::MPFR::Rmpfr_inf_p($r) and Math::MPFR::Rmpfr_sgn($r) < 0);
9219  
9220 $r = _any2mpfr($r);
9221 }
9222 }
9223  
9224 #
9225 ## True if `x` is Not-a-Number (NaN)
9226 #
9227  
9228 sub is_nan ($) {
9229 my ($x) = @_;
9230  
9231 $x = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
9232  
9233 ref($x) eq 'Math::GMPz' && return 0;
9234 ref($x) eq 'Math::GMPq' && return 0;
9235 ref($x) eq 'Math::MPFR' && return Math::MPFR::Rmpfr_nan_p($x);
9236  
9237 my $t = Math::MPFR::Rmpfr_init2($PREC);
9238  
9239 Math::MPC::RMPC_RE($t, $x);
9240 Math::MPFR::Rmpfr_nan_p($t) && return 1;
9241  
9242 Math::MPC::RMPC_IM($t, $x);
9243 Math::MPFR::Rmpfr_nan_p($t) && return 1;
9244  
9245 return 0;
9246 }
9247  
9248 #
9249 ## True if `n` is an even integer
9250 #
9251  
9252 sub is_even ($) {
9253 my ($n) = @_;
9254  
9255 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
9256  
9257 if (ref($n) ne 'Math::GMPz') {
9258 __is_int__($n) || return 0;
9259 $n = _any2mpz($n) // return 0;
9260 }
9261  
9262 Math::GMPz::Rmpz_even_p($n);
9263 }
9264  
9265 #
9266 ## True if `n` is an odd integer
9267 #
9268  
9269 sub is_odd ($) {
9270 my ($n) = @_;
9271  
9272 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
9273  
9274 if (ref($n) ne 'Math::GMPz') {
9275 __is_int__($n) || return 0;
9276 $n = _any2mpz($n) // return 0;
9277 }
9278  
9279 Math::GMPz::Rmpz_odd_p($n);
9280 }
9281  
9282 #
9283 ## True if `n == 0`
9284 #
9285  
9286 sub is_zero ($) {
9287 my ($n) = @_;
9288 @_ = ((ref($n) eq __PACKAGE__ ? $$n : _star2obj($n)), 0);
9289 goto &__eq__;
9290 }
9291  
9292 #
9293 ## True if `n == 1`
9294 #
9295  
9296 sub is_one ($) {
9297 my ($n) = @_;
9298 @_ = ((ref($n) eq __PACKAGE__ ? $$n : _star2obj($n)), 1);
9299 goto &__eq__;
9300 }
9301  
9302 #
9303 ## True if `n == -1`
9304 #
9305  
9306 sub is_mone ($) {
9307 my ($n) = @_;
9308 @_ = ((ref($n) eq __PACKAGE__ ? $$n : _star2obj($n)), -1);
9309 goto &__eq__;
9310 }
9311  
9312 #
9313 ## True if `n` is positive
9314 #
9315  
9316 sub is_pos ($) {
9317 my ($n) = @_;
9318 (__cmp__((ref($n) eq __PACKAGE__ ? $$n : _star2obj($n)), 0) // return undef) > 0;
9319 }
9320  
9321 #
9322 ## True if `n` is negative
9323 #
9324  
9325 sub is_neg ($) {
9326 my ($n) = @_;
9327 (__cmp__((ref($n) eq __PACKAGE__ ? $$n : _star2obj($n)), 0) // return undef) < 0;
9328 }
9329  
9330 #
9331 ## True if `n` is a perfect square
9332 #
9333  
9334 sub is_square ($) {
9335 my ($n) = @_;
9336  
9337 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
9338  
9339 if (ref($n) ne 'Math::GMPz') {
9340 __is_int__($n) || return 0;
9341 $n = _any2mpz($n) // return 0;
9342 }
9343  
9344 Math::GMPz::Rmpz_perfect_square_p($n);
9345 }
9346  
9347 #
9348 ## True if `n` is a k-gonal number
9349 #
9350  
9351 sub __is_polygonal__ {
9352 my ($n, $k, $second) = @_;
9353  
9354 Math::GMPz::Rmpz_sgn($n) || return 1;
9355  
9356 # polygonal_root(n, k)
9357 # = ((k - 4) ± sqrt(8 * (k - 2) * n + (k - 4)^2)) / (2 * (k - 2))
9358  
9359 state $t = Math::GMPz::Rmpz_init_nobless();
9360 state $u = Math::GMPz::Rmpz_init_nobless();
9361  
9362 Math::GMPz::Rmpz_sub_ui($u, $k, 2); # u = k-2
9363 Math::GMPz::Rmpz_mul($t, $n, $u); # t = n*u
9364 Math::GMPz::Rmpz_mul_2exp($t, $t, 3); # t = t*8
9365  
9366 Math::GMPz::Rmpz_sub_ui($u, $u, 2); # u = u-2
9367 Math::GMPz::Rmpz_mul($u, $u, $u); # u = u^2
9368  
9369 Math::GMPz::Rmpz_add($t, $t, $u); # t = t+u
9370 Math::GMPz::Rmpz_perfect_square_p($t) || return 0;
9371 Math::GMPz::Rmpz_sqrt($t, $t); # t = sqrt(t)
9372  
9373 Math::GMPz::Rmpz_sub_ui($u, $k, 4); # u = k-4
9374  
9375 $second
9376 ? Math::GMPz::Rmpz_sub($t, $u, $t) # t = u-t
9377 : Math::GMPz::Rmpz_add($t, $t, $u); # t = t+u
9378  
9379 Math::GMPz::Rmpz_add_ui($u, $u, 2); # u = u+2
9380 Math::GMPz::Rmpz_mul_2exp($u, $u, 1); # u = u*2
9381  
9382 Math::GMPz::Rmpz_divisible_p($t, $u); # true iff u|t
9383 }
9384  
9385 sub is_polygonal ($$) {
9386 my ($n, $k) = @_;
9387  
9388 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
9389  
9390 if (ref($n) ne 'Math::GMPz') {
9391 __is_int__($n) || return 0;
9392 $n = _any2mpz($n) // return 0;
9393 }
9394  
9395 $k = _star2mpz($k) // return 0;
9396 __is_polygonal__($n, $k);
9397 }
9398  
9399 #
9400 ## Is a second polygonal number?
9401 #
9402  
9403 sub is_polygonal2 ($$) {
9404 my ($n, $k) = @_;
9405  
9406 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
9407  
9408 if (ref($n) ne 'Math::GMPz') {
9409 __is_int__($n) || return 0;
9410 $n = _any2mpz($n) // return 0;
9411 }
9412  
9413 $k = _star2mpz($k) // return 0;
9414  
9415 __is_polygonal__($n, $k, 1);
9416 }
9417  
9418 #
9419 ## Integer polygonal root
9420 #
9421  
9422 sub __ipolygonal_root__ {
9423 my ($n, $k, $second) = @_;
9424  
9425 # polygonal_root(n, k)
9426 # = ((k - 4) ± sqrt(8 * (k - 2) * n + (k - 4)^2)) / (2 * (k - 2))
9427  
9428 state $t = Math::GMPz::Rmpz_init_nobless();
9429 state $u = Math::GMPz::Rmpz_init_nobless();
9430  
9431 Math::GMPz::Rmpz_sub_ui($u, $k, 2); # u = k-2
9432 Math::GMPz::Rmpz_mul($t, $n, $u); # t = n*u
9433 Math::GMPz::Rmpz_mul_2exp($t, $t, 3); # t = t*8
9434  
9435 Math::GMPz::Rmpz_sub_ui($u, $u, 2); # u = u-2
9436 Math::GMPz::Rmpz_mul($u, $u, $u); # u = u^2
9437 Math::GMPz::Rmpz_add($t, $t, $u); # t = t+u
9438  
9439 Math::GMPz::Rmpz_sgn($t) < 0 && goto &_nan; # `t` is negative
9440  
9441 Math::GMPz::Rmpz_sqrt($t, $t); # t = sqrt(t)
9442 Math::GMPz::Rmpz_sub_ui($u, $k, 4); # u = k-4
9443  
9444 $second
9445 ? Math::GMPz::Rmpz_sub($t, $u, $t) # t = u-t
9446 : Math::GMPz::Rmpz_add($t, $t, $u); # t = t+u
9447  
9448 Math::GMPz::Rmpz_add_ui($u, $u, 2); # u = u+2
9449 Math::GMPz::Rmpz_mul_2exp($u, $u, 1); # u = u*2
9450  
9451 Math::GMPz::Rmpz_sgn($u) || return $n; # `u` is zero
9452  
9453 my $r = Math::GMPz::Rmpz_init();
9454 Math::GMPz::Rmpz_div($r, $t, $u); # r = floor(t/u)
9455 return $r;
9456 }
9457  
9458 sub ipolygonal_root ($$) {
9459 my ($n, $k) = @_;
9460  
9461 $n = _star2mpz($n) // goto &nan;
9462 $k = _star2mpz($k) // goto &nan;
9463  
9464 bless \__ipolygonal_root__($n, $k);
9465 }
9466  
9467 #
9468 ## Second integer polygonal root
9469 #
9470  
9471 sub ipolygonal_root2 ($$) {
9472 my ($n, $k) = @_;
9473  
9474 $n = _star2mpz($n) // goto &nan;
9475 $k = _star2mpz($k) // goto &nan;
9476  
9477 bless \__ipolygonal_root__($n, $k, 1);
9478 }
9479  
9480 #
9481 ## n-th k-gonal number
9482 #
9483  
9484 sub polygonal ($$) {
9485 my ($n, $k) = @_;
9486  
9487 $n = _star2mpz($n) // goto &nan;
9488  
9489 if (!ref($k) and CORE::int($k) eq $k and $k >= 0 and $k < ULONG_MAX) {
9490 ## `k` is a native unsigned integer
9491 }
9492 else {
9493 $k = _star2mpz($k) // goto &nan;
9494 }
9495  
9496 #
9497 ## polygonal(n, k) = n * (k*n - k - 2*n + 4) / 2
9498 #
9499  
9500 my $r = Math::GMPz::Rmpz_init();
9501  
9502 if (!ref($k)) { # `k` is a native unsigned integer
9503 Math::GMPz::Rmpz_mul_ui($r, $n, $k); # r = n*k
9504 Math::GMPz::Rmpz_sub_ui($r, $r, $k); # r = r-k
9505 }
9506 else {
9507 Math::GMPz::Rmpz_mul($r, $n, $k); # r = n*k
9508 Math::GMPz::Rmpz_sub($r, $r, $k); # r = r-k
9509 }
9510  
9511 Math::GMPz::Rmpz_submul_ui($r, $n, 2); # r = r-2*n
9512 Math::GMPz::Rmpz_add_ui($r, $r, 4); # r = r+4
9513 Math::GMPz::Rmpz_mul($r, $r, $n); # r = r*n
9514 Math::GMPz::Rmpz_div_2exp($r, $r, 1); # r = r/2
9515  
9516 bless \$r;
9517 }
9518  
9519 #
9520 ## True if n = c^k for some integer c
9521 #
9522  
9523 sub __is_power__ {
9524 my ($n, $k) = @_;
9525  
9526 # Everything is a first power
9527 $k == 1 and return 1;
9528  
9529 Math::GMPz::Rmpz_cmp_ui($n, 1) == 0 and return 1;
9530  
9531 # Return a true value when $n=-1 and $k is odd
9532 $k % 2 and (Math::GMPz::Rmpz_cmp_si($n, -1) == 0) and return 1;
9533  
9534 # Don't accept a non-positive power
9535 # Also, when $n is negative and $k is even, return faster
9536 if ($k <= 0 or ($k % 2 == 0 and Math::GMPz::Rmpz_sgn($n) < 0)) {
9537 return 0;
9538 }
9539  
9540 # Optimization for perfect squares
9541 $k == 2 and return Math::GMPz::Rmpz_perfect_square_p($n);
9542  
9543 Math::GMPz::Rmpz_perfect_power_p($n) || return 0;
9544 state $t = Math::GMPz::Rmpz_init_nobless();
9545 Math::GMPz::Rmpz_root($t, $n, $k);
9546 }
9547  
9548 sub is_power ($;$) {
9549 my ($n, $k) = @_;
9550  
9551 $n = _star2mpz($n) // return 0;
9552 $k // return Math::GMPz::Rmpz_perfect_power_p($n);
9553 $k = _star2si($k) // return 0;
9554  
9555 __is_power__($n, $k);
9556 }
9557  
9558 sub is_power_of ($$) {
9559 my ($n, $k) = @_;
9560  
9561 $n = _star2obj($n);
9562 $k = _star2obj($k);
9563  
9564 if (ref($n) ne 'Math::GMPz') {
9565 __is_int__($n) || return 0;
9566 $n = _any2mpz($n) // return 0;
9567 }
9568  
9569 if (ref($k) ne 'Math::GMPz') {
9570 $k = _any2mpz($k) // 0;
9571 }
9572  
9573 if (Math::GMPz::Rmpz_cmp_ui($k, 2) == 0) {
9574 return (Math::GMPz::Rmpz_popcount($n) == 1);
9575 }
9576  
9577 my $e = __ilog__($n, $k) // return 0;
9578  
9579 state $t = Math::GMPz::Rmpz_init_nobless();
9580 Math::GMPz::Rmpz_pow_ui($t, $k, $e);
9581  
9582 (Math::GMPz::Rmpz_cmp($t, $n) == 0);
9583 }
9584  
9585 #
9586 ## kronecker
9587 #
9588  
9589 sub kronecker ($$) {
9590 my ($n, $k) = @_;
9591  
9592 if (!ref($n) and CORE::int($n) eq $n and $n < ULONG_MAX and $n > LONG_MIN) {
9593  
9594 if (!ref($k) and CORE::int($k) eq $k and $k < ULONG_MAX and $k > LONG_MIN) {
9595 $k =
9596 ($k < 0)
9597 ? Math::GMPz::Rmpz_init_set_si($k)
9598 : Math::GMPz::Rmpz_init_set_ui($k);
9599 }
9600 else {
9601 $k = _star2mpz($k) // goto &nan;
9602 }
9603  
9604 return (
9605 $n < 0
9606 ? Math::GMPz::Rmpz_si_kronecker($n, $k)
9607 : Math::GMPz::Rmpz_ui_kronecker($n, $k)
9608 );
9609 }
9610  
9611 $n = _star2mpz($n) // goto &nan;
9612  
9613 if (!ref($k) and CORE::int($k) eq $k and $k < ULONG_MAX and $k > LONG_MIN) {
9614 return (
9615 $k < 0
9616 ? Math::GMPz::Rmpz_kronecker_si($n, $k)
9617 : Math::GMPz::Rmpz_kronecker_ui($n, $k)
9618 );
9619 }
9620  
9621 $k = _star2mpz($k) // goto &nan;
9622  
9623 Math::GMPz::Rmpz_kronecker($n, $k);
9624 }
9625  
9626 #
9627 ## valuation
9628 #
9629  
9630 sub __valuation__ { # takes two Math::GMPz objects
9631 my ($x, $y) = @_;
9632 Math::GMPz::Rmpz_sgn($y) || return (0, $x);
9633 Math::GMPz::Rmpz_cmpabs_ui($y, 1) || return (0, $x);
9634 my $r = Math::GMPz::Rmpz_init();
9635 my $v = Math::GMPz::Rmpz_remove($r, $x, $y);
9636 ($v, $r);
9637 }
9638  
9639 sub valuation ($$) {
9640 my ($n, $k) = @_;
9641  
9642 $n = _star2mpz($n) // goto &nan;
9643 $k = _star2mpz($k) // goto &nan;
9644  
9645 (__valuation__($n, $k))[0];
9646 }
9647  
9648 #
9649 ## remdiv
9650 #
9651  
9652 sub remdiv ($$) {
9653 my ($n, $k) = @_;
9654  
9655 $n = _star2mpz($n) // goto &nan;
9656 $k = _star2mpz($k) // goto &nan;
9657  
9658 bless \((__valuation__($n, $k))[1]);
9659 }
9660  
9661 #
9662 ## Make n coprime to k, by removing from n common factors with k
9663 #
9664 sub make_coprime {
9665 my ($n, $k) = @_;
9666  
9667 $n = _star2mpz($n) // goto &nan;
9668 $k = _star2mpz($k) // goto &nan;
9669  
9670 if (Math::GMPz::Rmpz_sgn($n) == 0) {
9671 return bless \$n;
9672 }
9673  
9674 my $r = Math::GMPz::Rmpz_init_set($n);
9675 my $g = Math::GMPz::Rmpz_init();
9676  
9677 Math::GMPz::Rmpz_gcd($g, $r, $k);
9678  
9679 while (Math::GMPz::Rmpz_cmp_ui($g, 1) > 0) {
9680 Math::GMPz::Rmpz_remove($r, $r, $g);
9681 Math::GMPz::Rmpz_gcd($g, $r, $g);
9682 }
9683  
9684 bless \$r;
9685 }
9686  
9687 #
9688 ## Invmod
9689 #
9690  
9691 sub invmod ($$) {
9692 my ($n, $k) = @_;
9693  
9694 $n = _star2mpz($n) // goto &nan;
9695 $k = _star2mpz($k) // goto &nan;
9696  
9697 my $r = Math::GMPz::Rmpz_init();
9698 Math::GMPz::Rmpz_invert($r, $n, $k) || (goto &nan);
9699 bless \$r;
9700 }
9701  
9702 sub _modular_rational {
9703 my ($n, $m) = @_;
9704  
9705 if (ref($n) ne 'Math::GMPq') {
9706 $n = _any2mpq($n) // return;
9707 }
9708  
9709 state $z = Math::GMPz::Rmpz_init_nobless();
9710  
9711 my $t = Math::GMPz::Rmpz_init();
9712 Math::GMPq::Rmpq_get_den($z, $n);
9713 Math::GMPz::Rmpz_invert($t, $z, $m) or return;
9714 Math::GMPq::Rmpq_get_num($z, $n);
9715 Math::GMPz::Rmpz_mul($t, $t, $z);
9716  
9717 return $t;
9718 }
9719  
9720 #
9721 ## Ratmod
9722 #
9723  
9724 sub ratmod ($$) {
9725 my ($n, $m) = @_;
9726  
9727 $n = _star2obj($n) // goto &nan;
9728 $m = _star2mpz($m) // goto &nan;
9729  
9730 my $r = Math::GMPz::Rmpz_init();
9731  
9732 if (ref($n) eq 'Math::GMPz') {
9733 Math::GMPz::Rmpz_mod($r, $n, $m);
9734 }
9735 else {
9736 $r = _modular_rational($n, $m) // goto &nan;
9737 Math::GMPz::Rmpz_mod($r, $r, $m);
9738 }
9739  
9740 bless \$r;
9741 }
9742  
9743 #
9744 ## Powmod
9745 #
9746  
9747 sub powmod ($$$) {
9748 my ($n, $k, $m) = @_;
9749  
9750 $n = _star2obj($n) // goto &nan;
9751 $k = _star2mpz($k) // goto &nan;
9752 $m = _star2mpz($m) // goto &nan;
9753  
9754 Math::GMPz::Rmpz_sgn($m) || goto &nan;
9755  
9756 if (ref($n) ne 'Math::GMPz') {
9757 if (__is_int__($n)) {
9758 $n = _any2mpz($n) // goto &nan;
9759 }
9760 else {
9761 $n = _modular_rational($n, $m) // goto &nan;
9762 }
9763 }
9764  
9765 my $r = Math::GMPz::Rmpz_init();
9766  
9767 if (Math::GMPz::Rmpz_sgn($k) < 0) {
9768 Math::GMPz::Rmpz_invert($r, $n, $m) or goto &nan;
9769 }
9770  
9771 Math::GMPz::Rmpz_fits_ulong_p($k)
9772 ? Math::GMPz::Rmpz_powm_ui($r, $n, Math::GMPz::Rmpz_get_ui($k), $m)
9773 : Math::GMPz::Rmpz_powm($r, $n, $k, $m);
9774  
9775 bless \$r;
9776 }
9777  
9778 #
9779 ## Geometric summation formula
9780 ## https://en.wikipedia.org/wiki/Geometric_series
9781 #
9782  
9783 sub geometric_sum ($$) {
9784 my ($n, $r) = @_;
9785  
9786 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
9787 $r = ref($r) eq __PACKAGE__ ? $$r : _star2obj($r);
9788  
9789 bless \__div__(__sub__(__pow__($r, __add__($n, 1)), 1), __sub__($r, 1));
9790 }
9791  
9792 #
9793 ## Faulhaber's summation formula
9794 ## https://en.wikipedia.org/wiki/Faulhaber%27s_formula
9795 #
9796  
9797 sub faulhaber_sum ($$) {
9798 my ($n, $p) = @_;
9799  
9800 $n = _star2mpz($n) // goto &nan;
9801 $p = _star2ui($p) // goto &nan;
9802  
9803 if ($p == 0) {
9804 return bless \$n;
9805 }
9806  
9807 if ($p == 1 or $p == 3) {
9808 my $r = Math::GMPz::Rmpz_init();
9809 Math::GMPz::Rmpz_add_ui($r, $n, 1);
9810 Math::GMPz::Rmpz_mul($r, $r, $n);
9811 Math::GMPz::Rmpz_div_2exp($r, $r, 1);
9812 Math::GMPz::Rmpz_mul($r, $r, $r) if ($p == 3);
9813 return bless \$r;
9814 }
9815  
9816 state $z = Math::GMPz::Rmpz_init_nobless();
9817  
9818 if ($p == 2) { # n*(n+1)*(2*n+1)/6
9819 my $r = Math::GMPz::Rmpz_init();
9820 Math::GMPz::Rmpz_add_ui($z, $n, 1);
9821 Math::GMPz::Rmpz_mul($r, $z, $n);
9822 Math::GMPz::Rmpz_mul_2exp($z, $z, 1);
9823 Math::GMPz::Rmpz_sub_ui($z, $z, 1);
9824 Math::GMPz::Rmpz_mul($r, $r, $z);
9825 Math::GMPz::Rmpz_divexact_ui($r, $r, 6);
9826 return bless \$r;
9827 }
9828  
9829 # When p >= n, sum the powers directly.
9830 if (Math::GMPz::Rmpz_cmp_ui($n, $p) <= 0) {
9831 my $r = Math::GMPz::Rmpz_init_set_ui(0);
9832 foreach my $k (1 .. Math::GMPz::Rmpz_get_ui($n)) {
9833 Math::GMPz::Rmpz_ui_pow_ui($z, $k, $p);
9834 Math::GMPz::Rmpz_add($r, $r, $z);
9835 }
9836 return bless \$r;
9837 }
9838  
9839 my @B = _bernoulli_numbers($p);
9840  
9841 my $q = Math::GMPq::Rmpq_init();
9842 my $u = Math::GMPz::Rmpz_init_set_ui(1);
9843  
9844 my $sum = Math::GMPq::Rmpq_init();
9845 Math::GMPq::Rmpq_set_ui($sum, 0, 1);
9846  
9847 # Sum_{k=1..n} k^p = 1/(p+1) * Sum_{j=0..p} binomial(p+1, j) * n^(p-j+1) * bernoulli(j)
9848 # = 1/(p+1) * Sum_{j=0..p} binomial(p+1, p-j) * n^(j+1) * bernoulli(p-j)
9849  
9850 foreach my $j (0 .. $p - 2) {
9851  
9852 Math::GMPz::Rmpz_mul($u, $u, $n);
9853  
9854 # Skip when bernoulli(p-j) == 0
9855 ($p - $j) % 2 == 0 or next;
9856  
9857 Math::GMPz::Rmpz_bin_uiui($z, $p + 1, $p - $j);
9858 Math::GMPz::Rmpz_mul($z, $z, $u);
9859 Math::GMPq::Rmpq_mul_z($q, $B[(($p - $j) >> 1) + 1], $z);
9860 Math::GMPq::Rmpq_add($sum, $sum, $q);
9861 }
9862  
9863 # sum += (1/2) * n^p * (2*n + p + 1)
9864 Math::GMPz::Rmpz_mul($u, $u, $n);
9865 Math::GMPz::Rmpz_mul_2exp($z, $n, 1);
9866 Math::GMPz::Rmpz_add_ui($z, $z, $p + 1);
9867 Math::GMPz::Rmpz_mul($u, $u, $z);
9868 Math::GMPq::Rmpq_set_ui($q, 1, 2);
9869 Math::GMPq::Rmpq_mul_z($q, $q, $u);
9870 Math::GMPq::Rmpq_add($sum, $sum, $q);
9871  
9872 # z = sum/(p+1)
9873 Math::GMPq::Rmpq_get_num($u, $sum);
9874 Math::GMPz::Rmpz_divexact_ui($u, $u, $p + 1);
9875 bless \$u;
9876 }
9877  
9878 #
9879 ## Dirichlet hyperbola method
9880 ## https://en.wikipedia.org/wiki/Dirichlet_hyperbola_method
9881 #
9882  
9883 sub dirichlet_sum ($$$$$) {
9884 my ($n, $f, $g, $F, $G) = @_;
9885  
9886 $n = _star2mpz($n) // goto &nan;
9887 Math::GMPz::Rmpz_sgn($n) > 0 or goto &zero;
9888  
9889 $f //= sub { 1 };
9890 $g //= sub { 1 };
9891  
9892 $F //= sub { $_[0] };
9893 $G //= sub { $_[0] };
9894  
9895 my $s = Math::GMPz::Rmpz_init();
9896 Math::GMPz::Rmpz_sqrt($s, $n);
9897  
9898 my $sum = Math::GMPz::Rmpz_init_set_ui(0);
9899  
9900 my $t = bless \Math::GMPz::Rmpz_init_set_ui(0);
9901 my $u = bless \Math::GMPz::Rmpz_init_set_ui(0);
9902  
9903 Math::GMPz::Rmpz_fits_slong_p($s) || goto &nan;
9904  
9905 foreach my $k (1 .. Math::GMPz::Rmpz_get_ui($s)) {
9906  
9907 Math::GMPz::Rmpz_set_ui($$t, $k);
9908 Math::GMPz::Rmpz_div_ui($$u, $n, $k);
9909  
9910 my $f_r = $f->($t);
9911 my $g_r = $g->($t);
9912 my $F_r = $F->($u);
9913 my $G_r = $G->($u);
9914  
9915 $f_r = _star2mpz($f_r) // goto &nan;
9916 $g_r = _star2mpz($g_r) // goto &nan;
9917 $F_r = _star2mpz($F_r) // goto &nan;
9918 $G_r = _star2mpz($G_r) // goto &nan;
9919  
9920 Math::GMPz::Rmpz_addmul($sum, $f_r, $G_r);
9921 Math::GMPz::Rmpz_addmul($sum, $g_r, $F_r);
9922 }
9923  
9924 $sum = __sub__($sum, __mul__(_star2mpz($F->(bless \$s)), _star2mpz($G->(bless \$s))));
9925 bless \$sum;
9926 }
9927  
9928 #
9929 ## Catalan numbers
9930 #
9931  
9932 sub catalan ($;$) {
9933 my ($n, $k) = @_;
9934  
9935 # Catalan's triangle
9936 # catalan(n, k) = binomial(n+k, k) - binomial(n+k, k-1)
9937 if (scalar(@_) == 2) {
9938  
9939 $n = _star2mpz($n) // goto &nan;
9940 $k = _star2ui($k) // goto &nan;
9941  
9942 my $t = Math::GMPz::Rmpz_init();
9943 my $u = Math::GMPz::Rmpz_init();
9944  
9945 Math::GMPz::Rmpz_add_ui($t, $n, $k);
9946 Math::GMPz::Rmpz_bin_ui($u, $t, $k);
9947 ($k > 0)
9948 ? Math::GMPz::Rmpz_bin_ui($t, $t, $k - 1)
9949 : Math::GMPz::Rmpz_bin_si($t, $t, $k - 1);
9950 Math::GMPz::Rmpz_sub($u, $u, $t);
9951  
9952 return bless \$u;
9953 }
9954  
9955 $n = _star2ui($n) // goto &nan;
9956  
9957 my $r = Math::GMPz::Rmpz_init();
9958 Math::GMPz::Rmpz_bin_uiui($r, $n << 1, $n);
9959 Math::GMPz::Rmpz_divexact_ui($r, $r, $n + 1);
9960 bless \$r;
9961 }
9962  
9963 #
9964 ## Bell numbers
9965 #
9966  
9967 sub bell ($) {
9968 my ($n) = @_;
9969  
9970 $n = _star2ui($n) // goto &nan;
9971  
9972 my @acc;
9973  
9974 my $t = Math::GMPz::Rmpz_init();
9975 my $bell = Math::GMPz::Rmpz_init_set_ui(1);
9976  
9977 foreach my $k (1 .. $n) {
9978  
9979 Math::GMPz::Rmpz_set($t, $bell);
9980  
9981 foreach my $item (@acc) {
9982 Math::GMPz::Rmpz_add($t, $t, $item);
9983 Math::GMPz::Rmpz_set($item, $t);
9984 }
9985  
9986 unshift @acc, $bell;
9987 $bell = Math::GMPz::Rmpz_init_set($acc[-1]);
9988 }
9989  
9990 bless \$bell;
9991 }
9992  
9993 #
9994 ## Binomial coefficient
9995 #
9996  
9997 sub binomial ($$) {
9998 my ($n, $k) = @_;
9999  
10000 # `n` and `k` are native unsigned integers
10001 if ( !ref($n)
10002 and !ref($k)
10003 and CORE::int($n) eq $n
10004 and CORE::int($k) eq $k
10005 and $n >= 0
10006 and $k >= 0
10007 and $n < 1e6
10008 and $k < ULONG_MAX) {
10009 my $r = Math::GMPz::Rmpz_init();
10010 Math::GMPz::Rmpz_bin_uiui($r, $n, $k);
10011 return bless \$r;
10012 }
10013  
10014 $n = _star2mpz($n) // goto &nan;
10015 $k = _star2si($k) // goto &nan;
10016  
10017 my $r = Math::GMPz::Rmpz_init();
10018  
10019 if ($k >= 0 and Math::GMPz::Rmpz_fits_ulong_p($n) and Math::GMPz::Rmpz_cmp_ui($n, 1e6) <= 0) {
10020 Math::GMPz::Rmpz_bin_uiui($r, Math::GMPz::Rmpz_get_ui($n), $k);
10021 }
10022 else {
10023 $k < 0
10024 ? Math::GMPz::Rmpz_bin_si($r, $n, $k)
10025 : Math::GMPz::Rmpz_bin_ui($r, $n, $k);
10026 }
10027  
10028 bless \$r;
10029 }
10030  
10031 #
10032 ## Multinomial coefficient
10033 #
10034  
10035 sub multinomial {
10036 my ($n, @mset) = @_;
10037  
10038 $n = _star2mpz($n) // goto &nan;
10039  
10040 my $bin = Math::GMPz::Rmpz_init();
10041 my $sum = Math::GMPz::Rmpz_init_set($n);
10042 my $prod = Math::GMPz::Rmpz_init_set_ui(1);
10043  
10044 foreach my $k (@mset) {
10045  
10046 $k = _star2si($k) // goto &nan;
10047  
10048 $k < 0
10049 ? Math::GMPz::Rmpz_sub_ui($sum, $sum, -$k)
10050 : Math::GMPz::Rmpz_add_ui($sum, $sum, $k);
10051  
10052 if ($k >= 0 and Math::GMPz::Rmpz_fits_ulong_p($sum)) {
10053 Math::GMPz::Rmpz_bin_uiui($bin, Math::GMPz::Rmpz_get_ui($sum), $k);
10054 }
10055 else {
10056 $k < 0
10057 ? Math::GMPz::Rmpz_bin_si($bin, $sum, $k)
10058 : Math::GMPz::Rmpz_bin_ui($bin, $sum, $k);
10059 }
10060  
10061 Math::GMPz::Rmpz_mul($prod, $prod, $bin);
10062 }
10063  
10064 bless \$prod;
10065 }
10066  
10067 #
10068 ## AND
10069 #
10070  
10071 sub and { # used in overloading
10072 my ($n, $k) = @_;
10073  
10074 $n = _star2mpz($n) // goto &nan;
10075 $k = _star2mpz($k) // goto &nan;
10076  
10077 my $r = Math::GMPz::Rmpz_init();
10078 Math::GMPz::Rmpz_and($r, $n, $k);
10079 bless \$r;
10080 }
10081  
10082 #
10083 ## OR
10084 #
10085  
10086 sub or { # used in overloading
10087 my ($n, $k) = @_;
10088  
10089 $n = _star2mpz($n) // goto &nan;
10090 $k = _star2mpz($k) // goto &nan;
10091  
10092 my $r = Math::GMPz::Rmpz_init();
10093 Math::GMPz::Rmpz_ior($r, $n, $k);
10094 bless \$r;
10095 }
10096  
10097 #
10098 ## XOR
10099 #
10100  
10101 sub xor { # used in overloading
10102 my ($n, $k) = @_;
10103  
10104 $n = _star2mpz($n) // goto &nan;
10105 $k = _star2mpz($k) // goto &nan;
10106  
10107 my $r = Math::GMPz::Rmpz_init();
10108 Math::GMPz::Rmpz_xor($r, $n, $k);
10109 bless \$r;
10110 }
10111  
10112 #
10113 ## NOT
10114 #
10115  
10116 sub not { # used in overloading
10117 my ($n) = @_;
10118  
10119 $n = _star2mpz($n) // goto &nan;
10120  
10121 my $r = Math::GMPz::Rmpz_init();
10122 Math::GMPz::Rmpz_com($r, $n);
10123 bless \$r;
10124 }
10125  
10126 #
10127 ## Get k-th bit of integer `n`
10128 #
10129  
10130 sub getbit ($$) {
10131 my ($n, $k) = @_;
10132  
10133 $n = _star2mpz($n) // return undef;
10134 $k = _star2ui($k) // return undef;
10135  
10136 Math::GMPz::Rmpz_tstbit($n, $k);
10137 }
10138  
10139 #
10140 ## Set k-th bit of integer `n` to 1
10141 #
10142  
10143 sub setbit ($$) {
10144 my ($n, $k) = @_;
10145  
10146 $n = _star2mpz($n) // return undef;
10147 $k = _star2ui($k) // return undef;
10148  
10149 my $r = Math::GMPz::Rmpz_init_set($n);
10150 Math::GMPz::Rmpz_setbit($r, $k);
10151 bless \$r;
10152 }
10153  
10154 #
10155 ## FLIP-BIT (XOR)
10156 #
10157  
10158 sub flipbit ($$) {
10159 my ($n, $k) = @_;
10160  
10161 $n = _star2mpz($n) // goto &nan;
10162 $k = _star2ui($k) // goto &nan;
10163  
10164 my $r = Math::GMPz::Rmpz_init_set($n);
10165  
10166 Math::GMPz::Rmpz_tstbit($r, $k)
10167 ? Math::GMPz::Rmpz_clrbit($r, $k)
10168 : Math::GMPz::Rmpz_setbit($r, $k);
10169  
10170 bless \$r;
10171 }
10172  
10173 #
10174 ## CLEAR BIT (set bit $y to 0)
10175 #
10176  
10177 sub clearbit ($$) {
10178 my ($n, $k) = @_;
10179  
10180 $n = _star2mpz($n) // goto &nan;
10181 $k = _star2ui($k) // goto &nan;
10182  
10183 my $r = Math::GMPz::Rmpz_init_set($n);
10184 Math::GMPz::Rmpz_clrbit($r, $k);
10185 bless \$r;
10186 }
10187  
10188 #
10189 ## Scan n starting from bit index k, towards more
10190 ## significant bits, until the first 0 is found.
10191 #
10192  
10193 sub bit_scan0 ($;$) {
10194 my ($n, $k) = @_;
10195  
10196 $n = _star2mpz($n) // return undef;
10197  
10198 if (defined($k)) {
10199 $k = _star2ui($k) // return undef;
10200 }
10201 else {
10202 $k = 0;
10203 }
10204  
10205 Math::GMPz::Rmpz_scan0($n, $k);
10206 }
10207  
10208 #
10209 ## Scan n starting from bit index k, towards more
10210 ## significant bits, until the first 1 is found.
10211 #
10212  
10213 sub bit_scan1 ($;$) {
10214 my ($n, $k) = @_;
10215  
10216 $n = _star2mpz($n) // return undef;
10217  
10218 if (defined($k)) {
10219 $k = _star2ui($k) // return undef;
10220 }
10221 else {
10222 $k = 0;
10223 }
10224  
10225 Math::GMPz::Rmpz_scan1($n, $k);
10226 }
10227  
10228 #
10229 ## LEFT SHIFT
10230 #
10231  
10232 sub lsft { # used in overloading
10233 my ($n, $k) = @_;
10234  
10235 $n = _star2mpz($n) // goto &nan;
10236 $k = _star2si($k) // goto &nan;
10237  
10238 my $r = Math::GMPz::Rmpz_init();
10239  
10240 $k < 0
10241 ? Math::GMPz::Rmpz_div_2exp($r, $n, -$k)
10242 : Math::GMPz::Rmpz_mul_2exp($r, $n, CORE::int($k));
10243  
10244 bless \$r;
10245 }
10246  
10247 #
10248 ## RIGHT SHIFT
10249 #
10250  
10251 sub rsft { # used in overloading
10252 my ($n, $k) = @_;
10253  
10254 $n = _star2mpz($n) // goto &nan;
10255 $k = _star2si($k) // goto &nan;
10256  
10257 my $r = Math::GMPz::Rmpz_init();
10258  
10259 $k < 0
10260 ? Math::GMPz::Rmpz_mul_2exp($r, $n, -$k)
10261 : Math::GMPz::Rmpz_div_2exp($r, $n, CORE::int($k));
10262  
10263 bless \$r;
10264 }
10265  
10266 #
10267 ## Population count: number of 1's in the binary representation of `n`
10268 #
10269  
10270 sub popcount ($) {
10271 my ($n) = @_;
10272  
10273 $n = _star2mpz($n) // return undef;
10274  
10275 if (Math::GMPz::Rmpz_sgn($n) < 0) {
10276 $n = Math::GMPz::Rmpz_init_set($n);
10277 Math::GMPz::Rmpz_neg($n, $n);
10278 }
10279  
10280 Math::GMPz::Rmpz_popcount($n);
10281 }
10282  
10283 #
10284 ## Hamming distance
10285 #
10286  
10287 sub hamdist ($$) {
10288 my ($n, $k) = @_;
10289  
10290 $n = _star2mpz($n) // return undef;
10291 $k = _star2mpz($k) // return undef;
10292  
10293 Math::GMPz::Rmpz_hamdist($n, $k);
10294 }
10295  
10296 #
10297 ## Conversions
10298 #
10299  
10300 sub as_bin ($) {
10301 my ($n) = @_;
10302 $n = _star2mpz($n) // return undef;
10303 Math::GMPz::Rmpz_get_str($n, 2);
10304 }
10305  
10306 sub as_oct ($) {
10307 my ($n) = @_;
10308 $n = _star2mpz($n) // return undef;
10309 Math::GMPz::Rmpz_get_str($n, 8);
10310 }
10311  
10312 sub as_hex ($) {
10313 my ($n) = @_;
10314 $n = _star2mpz($n) // return undef;
10315 Math::GMPz::Rmpz_get_str($n, 16);
10316 }
10317  
10318 sub as_int ($;$) {
10319 my ($n, $k) = @_;
10320  
10321 my $base = 10;
10322 if (defined($k)) {
10323  
10324 $base = _star2ui($k) // 0;
10325  
10326 if ($base < 2 or $base > 62) {
10327 require Carp;
10328 Carp::croak("base must be between 2 and 62, got $k");
10329 }
10330 }
10331  
10332 $n = _star2mpz($n) // return undef;
10333 Math::GMPz::Rmpz_get_str($n, $base);
10334 }
10335  
10336 sub __as_rat__ {
10337 my ($n, $k) = @_;
10338  
10339 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
10340  
10341 my $base = 10;
10342 if (defined($k)) {
10343  
10344 $base = _star2ui($k) // 0;
10345  
10346 if ($base < 2 or $base > 62) {
10347 require Carp;
10348 Carp::croak("base must be between 2 and 62, got $k");
10349 }
10350 }
10351  
10352 my $ref = ref($n);
10353 if ($ref eq 'Math::GMPq' or $ref eq 'Math::GMPz') {
10354 return (
10355 $ref eq 'Math::GMPq'
10356 ? Math::GMPq::Rmpq_get_str($n, $base)
10357 : Math::GMPz::Rmpz_get_str($n, $base)
10358 );
10359 }
10360  
10361 $n = _any2mpq($n) // return undef;
10362 Math::GMPq::Rmpq_get_str($n, $base);
10363 }
10364  
10365 sub as_rat ($;$) {
10366 my ($n, $k) = @_;
10367 __as_rat__($n, $k) // return undef;
10368 }
10369  
10370 sub as_frac ($;$) {
10371 my ($n, $k) = @_;
10372 my $rat = __as_rat__($n, $k) // return undef;
10373 $rat .= '/1' if (index($rat, '/') == -1);
10374 $rat;
10375 }
10376  
10377 sub as_dec ($;$) {
10378 my ($n, $d) = @_;
10379  
10380 my $prec = $PREC;
10381 if (defined($d)) {
10382  
10383 $prec = _star2ui($d) // 0;
10384 $prec <<= 2;
10385  
10386 state $min_prec = Math::MPFR::RMPFR_PREC_MIN();
10387 state $max_prec = Math::MPFR::RMPFR_PREC_MAX();
10388  
10389 if ($prec < $min_prec or $prec > $max_prec) {
10390 require Carp;
10391 Carp::croak("precision must be between $min_prec and $max_prec, got ", $prec);
10392 }
10393 }
10394  
10395 local $PREC = $prec;
10396 __stringify__(_star2mpfr_mpc($n));
10397 }
10398  
10399 sub __base__ {
10400 my ($x, $base) = @_;
10401 goto(ref($x) =~ tr/:/_/rs);
10402  
10403 Math_GMPz: {
10404 return Math::GMPz::Rmpz_get_str($x, $base);
10405 }
10406  
10407 Math_GMPq: {
10408 return Math::GMPq::Rmpq_get_str($x, $base);
10409 }
10410  
10411 Math_MPFR: {
10412 return Math::MPFR::Rmpfr_get_str($x, $base, 0, $ROUND);
10413 }
10414  
10415 Math_MPC: {
10416  
10417 # return Math::MPC::Rmpc_get_str($base, 0, $x, $ROUND); # not OK
10418  
10419 my $fr = Math::MPFR::Rmpfr_init2(CORE::int($PREC));
10420 Math::MPC::RMPC_RE($fr, $x);
10421 my $real = __base__($fr, $base);
10422 Math::MPC::RMPC_IM($fr, $x);
10423 return $real if Math::MPFR::Rmpfr_zero_p($fr);
10424 my $imag = __base__($fr, $base);
10425 return "($real $imag)";
10426 }
10427 }
10428  
10429 sub base {
10430 my ($n, $k) = @_;
10431  
10432 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
10433  
10434 my $base = 10;
10435 if (defined($k)) {
10436  
10437 $base = _star2ui($k) // 0;
10438  
10439 if ($base < 2 or $base > 62) {
10440 require Carp;
10441 Carp::croak("base must be between 2 and 62, got $k");
10442 }
10443 }
10444  
10445 __base__($n, $base);
10446 }
10447  
10448 sub rat_approx ($) {
10449 my ($x) = @_;
10450  
10451 $x = _star2mpfr($x);
10452  
10453 Math::MPFR::Rmpfr_number_p($x) || goto &nan;
10454  
10455 my $n1 = Math::GMPz::Rmpz_init_set_ui(0);
10456 my $n2 = Math::GMPz::Rmpz_init_set_ui(1);
10457  
10458 my $d1 = Math::GMPz::Rmpz_init_set_ui(1);
10459 my $d2 = Math::GMPz::Rmpz_init_set_ui(0);
10460  
10461 my $q = Math::GMPq::Rmpq_init();
10462 my $z = Math::GMPz::Rmpz_init();
10463  
10464 my $s = __stringify__($x);
10465  
10466 my $f1 = Math::MPFR::Rmpfr_init2($PREC);
10467 my $f2 = Math::MPFR::Rmpfr_init2($PREC);
10468 my $f3 = Math::MPFR::Rmpfr_init2($PREC);
10469  
10470 Math::MPFR::Rmpfr_set($f1, $x, $ROUND);
10471  
10472 while (1) {
10473 Math::MPFR::Rmpfr_floor($f2, $f1);
10474 Math::MPFR::Rmpfr_get_z($z, $f2, $ROUND);
10475  
10476 Math::GMPz::Rmpz_addmul($n1, $n2, $z); # n1 += n2 * z
10477 Math::GMPz::Rmpz_addmul($d1, $d2, $z); # d1 += d2 * z
10478  
10479 ($n1, $n2) = ($n2, $n1);
10480 ($d1, $d2) = ($d2, $d1);
10481  
10482 # q = n2 / d2
10483 Math::GMPq::Rmpq_set_num($q, $n2);
10484 Math::GMPq::Rmpq_set_den($q, $d2);
10485 Math::GMPq::Rmpq_canonicalize($q);
10486  
10487 Math::MPFR::Rmpfr_set_q($f3, $q, $ROUND);
10488 CORE::index(__stringify__($f3), $s) == 0 and last;
10489  
10490 # f1 = 1 / (f1 - f2)
10491 Math::MPFR::Rmpfr_sub($f1, $f1, $f2, $ROUND);
10492 Math::MPFR::Rmpfr_zero_p($f1) && last;
10493 Math::MPFR::Rmpfr_ui_div($f1, 1, $f1, $ROUND);
10494 }
10495  
10496 bless \$q;
10497 }
10498  
10499 my %DIGITS_36;
10500 @DIGITS_36{0 .. 9, 'a' .. 'z'} = (0 .. 35);
10501  
10502 my %DIGITS_62;
10503 @DIGITS_62{0 .. 9, 'A' .. 'Z', 'a' .. 'z'} = (0 .. 61);
10504  
10505 sub digits ($;$) {
10506 my ($n, $k) = @_;
10507  
10508 $n = _star2mpz($n) // return;
10509  
10510 $k //= 10;
10511  
10512 my $sgn = Math::GMPz::Rmpz_sgn($n);
10513  
10514 if ($sgn == 0) { # n = 0
10515 return (0);
10516 }
10517 elsif ($sgn < 0) { # n < 0; make it absolute
10518 $n = Math::GMPz::Rmpz_init_set($n);
10519 Math::GMPz::Rmpz_abs($n, $n);
10520 }
10521  
10522 if (!ref($k) and CORE::int($k) eq $k and $k > 1 and $k < ULONG_MAX) {
10523 return if ($k <= 1); # not defined for k <= 1
10524 }
10525 else {
10526 $k = _star2mpz($k) // return;
10527 return if (Math::GMPz::Rmpz_cmp_ui($k, 1) <= 0); # not defined for k <= 1
10528 }
10529  
10530 # Return faster for k=2..62
10531 if (ref($k) ? (Math::GMPz::Rmpz_cmp_ui($k, 62) <= 0) : ($k <= 62)) {
10532 $k = Math::GMPz::Rmpz_get_ui($k) if ref($k);
10533 return map { $k <= 36 ? $DIGITS_36{$_} : $DIGITS_62{$_} }
10534 split(//, scalar reverse scalar(Math::GMPz::Rmpz_get_str($n, $k)));
10535 }
10536  
10537 # Subquadratic algorithm from "Modern Computer Arithmetic" by Richard P. Brent and Paul Zimmermann
10538 if (!ref($k) || Math::GMPz::Rmpz_fits_ulong_p($k)) {
10539  
10540 my $A = $n;
10541 my $B = ref($k) ? Math::GMPz::Rmpz_get_ui($k) : $k;
10542  
10543 # Find r such that B^(2r - 2) <= A < B^(2r)
10544 my $r = (__ilog__($A, $B) >> 1) + 1;
10545  
10546 state $Q = Math::GMPz::Rmpz_init_nobless();
10547 state $R = Math::GMPz::Rmpz_init_nobless();
10548  
10549 return sub {
10550 my ($A, $r) = @_;
10551  
10552 # Cut the recursion early
10553 if (Math::GMPz::Rmpz_fits_ulong_p($A)) {
10554 my $v = Math::GMPz::Rmpz_init_set($A);
10555 my $m = Math::GMPz::Rmpz_init();
10556  
10557 my @digits;
10558 while (Math::GMPz::Rmpz_sgn($v)) {
10559 push @digits, Math::GMPz::Rmpz_divmod_ui($v, $m, $v, $B);
10560 }
10561 return @digits;
10562 }
10563  
10564 #~ if (Math::GMPz::Rmpz_cmp_ui($A, $B) < 0) {
10565 #~ return Math::GMPz::Rmpz_get_ui($A);
10566 #~ }
10567  
10568 my $t = Math::GMPz::Rmpz_init();
10569 Math::GMPz::Rmpz_ui_pow_ui($t, $B, 2 * ($r - 1)); # can this be optimized away?
10570  
10571 if (Math::GMPz::Rmpz_cmp($t, $A) > 0) {
10572 --$r;
10573 }
10574  
10575 Math::GMPz::Rmpz_ui_pow_ui($t, $B, $r);
10576 Math::GMPz::Rmpz_divmod($Q, $R, $A, $t);
10577  
10578 my $w = ($r + 1) >> 1;
10579 Math::GMPz::Rmpz_set($t, $Q);
10580  
10581 my @right = __SUB__->($R, $w);
10582 my @left = __SUB__->($t, $w);
10583  
10584 (@right, (0) x ($r - scalar(@right)), @left);
10585 }
10586 ->($A, $r);
10587 }
10588  
10589 # This algorithm will be used only when base > ULONG_MAX
10590 my @digits;
10591  
10592 $n = Math::GMPz::Rmpz_init_set($n); # copy
10593  
10594 while (Math::GMPz::Rmpz_sgn($n) > 0) {
10595 my $m = Math::GMPz::Rmpz_init();
10596 Math::GMPz::Rmpz_divmod($n, $m, $n, $k);
10597 push @digits, bless \$m;
10598 }
10599  
10600 return @digits;
10601 }
10602  
10603 sub sumdigits ($;$) {
10604 my ($n, $k) = @_;
10605  
10606 $n = _star2mpz($n) // goto &nan;
10607  
10608 $k //= 10;
10609  
10610 my $sgn = Math::GMPz::Rmpz_sgn($n);
10611  
10612 if ($sgn == 0) { # n = 0
10613 goto &zero;
10614 }
10615 elsif ($sgn < 0) { # n < 0; make it absolute
10616 $n = Math::GMPz::Rmpz_init_set($n);
10617 Math::GMPz::Rmpz_abs($n, $n);
10618 }
10619  
10620 if (!ref($k) and CORE::int($k) eq $k and $k > 1 and $k < ULONG_MAX) {
10621 goto &nan if ($k <= 1); # not defined for k <= 1
10622 }
10623 else {
10624 $k = _star2mpz($k) // goto &nan;
10625 goto &nan if (Math::GMPz::Rmpz_cmp_ui($k, 1) <= 0); # not defined for k <= 1
10626 }
10627  
10628 # Return faster for k=2..62
10629 #<<<
10630 if (ref($k) ? (Math::GMPz::Rmpz_cmp_ui($k, 62) <= 0) : ($k <= 62)) {
10631 $k = Math::GMPz::Rmpz_get_ui($k) if ref($k);
10632  
10633 return bless \Math::GMPz::Rmpz_init_set_ui(Math::GMPz::Rmpz_popcount($n)) if $k == 2;
10634  
10635 if (Math::GMPz::Rmpz_sizeinbase($n, $k) <= 1e6) {
10636 return bless \Math::GMPz::Rmpz_init_set_ui(List::Util::sum(map { $k <= 36 ? $DIGITS_36{$_} : $DIGITS_62{$_} } split(//, Math::GMPz::Rmpz_get_str($n, $k))));
10637 }
10638 }
10639 #>>>
10640  
10641 # Subquadratic algorithm from "Modern Computer Arithmetic" by Richard P. Brent and Paul Zimmermann
10642 if (!ref($k) || Math::GMPz::Rmpz_fits_ulong_p($k)) {
10643  
10644 my $A = $n;
10645 my $B = ref($k) ? Math::GMPz::Rmpz_get_ui($k) : $k;
10646  
10647 # Find r such that B^(2r - 2) <= A < B^(2r)
10648 my $r = (__ilog__($A, $B) >> 1) + 1;
10649  
10650 state $Q = Math::GMPz::Rmpz_init_nobless();
10651 state $R = Math::GMPz::Rmpz_init_nobless();
10652  
10653 my $total = sub {
10654 my ($A, $r) = @_;
10655  
10656 # Cut the recursion early
10657 if (Math::GMPz::Rmpz_fits_ulong_p($A)) {
10658 my $v = Math::GMPz::Rmpz_init_set($A);
10659 my $m = Math::GMPz::Rmpz_init();
10660  
10661 my $sum = 0;
10662 while (Math::GMPz::Rmpz_sgn($v)) {
10663 $sum += Math::GMPz::Rmpz_divmod_ui($v, $m, $v, $B);
10664 }
10665 return $sum;
10666 }
10667  
10668 #~ if (Math::GMPz::Rmpz_cmp_ui($A, $B) < 0) {
10669 #~ return Math::GMPz::Rmpz_get_ui($A);
10670 #~ }
10671  
10672 my $w = ($r + 1) >> 1;
10673 my $t = Math::GMPz::Rmpz_init();
10674  
10675 Math::GMPz::Rmpz_ui_pow_ui($t, $B, $r);
10676 Math::GMPz::Rmpz_divmod($Q, $R, $A, $t);
10677 Math::GMPz::Rmpz_set($t, $Q);
10678  
10679 __SUB__->($R, $w) + __SUB__->($t, $w);
10680 }
10681 ->($A, $r);
10682  
10683 ($total < ULONG_MAX)
10684 && return bless \Math::GMPz::Rmpz_init_set_ui($total);
10685 }
10686  
10687 # This algorithm will be used only for very large bases,
10688 # base > ULONG_MAX, or when the sum of digits exceeds ULONG_MAX.
10689 $k = Math::GMPz::Rmpz_init_set_ui($k) if !ref($k);
10690 $n = Math::GMPz::Rmpz_init_set($n); # copy
10691  
10692 my $m = Math::GMPz::Rmpz_init();
10693 my $sum = Math::GMPz::Rmpz_init_set_ui(0);
10694  
10695 while (Math::GMPz::Rmpz_sgn($n) > 0) {
10696 Math::GMPz::Rmpz_divmod($n, $m, $n, $k);
10697 Math::GMPz::Rmpz_add($sum, $sum, $m);
10698 }
10699  
10700 bless \$sum;
10701 }
10702  
10703 my %FROM_DIGITS_36;
10704 @FROM_DIGITS_36{0 .. 35} = (0 .. 9, 'a' .. 'z');
10705  
10706 my %FROM_DIGITS_62;
10707 @FROM_DIGITS_62{0 .. 61} = (0 .. 9, 'A' .. 'Z', 'a' .. 'z');
10708  
10709 sub digits2num {
10710 my ($digits, $base) = @_;
10711  
10712 ref($digits) eq 'ARRAY' or goto &nan;
10713  
10714 $base //= 10;
10715  
10716 if ($base <= 1) {
10717 goto &nan;
10718 }
10719  
10720 @$digits || goto &zero;
10721  
10722 #<<<
10723 if ($base <= 62) {
10724 $base = ref($base) ? Math::GMPz::Rmpz_get_ui(_star2mpz($base) // goto &nan) : CORE::int($base);
10725 return bless \Math::GMPz::Rmpz_init_set_str(scalar reverse(join('', map { ($base <= 36 ? $FROM_DIGITS_36{$_} : $FROM_DIGITS_62{$_}) // goto &nan } @$digits)), $base);
10726 }
10727 #>>>
10728  
10729 if (!ref($base) and CORE::int($base) eq $base and $base > 1 and $base < ULONG_MAX) {
10730 $base = Math::GMPz::Rmpz_init_set_ui($base);
10731 }
10732 else {
10733 $base = Math::GMPz::Rmpz_init_set(_star2mpz($base) // goto &nan);
10734 }
10735  
10736 my @digits = map {
10737 (!ref($_) and CORE::int($_) eq $_ and $_ >= 0 and $_ < ULONG_MAX)
10738 ? Math::GMPz::Rmpz_init_set_ui($_)
10739 : Math::GMPz::Rmpz_init_set(_star2mpz($_) // goto &nan);
10740 } @$digits;
10741  
10742 my $L = \@digits;
10743  
10744 # Algorithm from "Modern Computer Arithmetic"
10745 # by Richard P. Brent and Paul Zimmermann
10746  
10747 for (my $k = scalar(@digits) ; $k > 1 ; $k = ($k >> 1) + ($k & 1)) {
10748  
10749 my @T;
10750 for (0 .. ($k >> 1) - 1) {
10751 Math::GMPz::Rmpz_addmul($L->[$_ << 1], $L->[($_ << 1) + 1], $base);
10752 push @T, $L->[$_ << 1];
10753 }
10754  
10755 push(@T, $L->[-1]) if ($k & 1);
10756 $L = \@T;
10757 Math::GMPz::Rmpz_mul($base, $base, $base);
10758 }
10759  
10760 bless \($L->[0]);
10761 }
10762  
10763 sub bsearch ($$;$) {
10764 my ($left, $right, $block) = @_;
10765  
10766 if (@_ == 3) {
10767 $left = Math::GMPz::Rmpz_init_set(_star2mpz($left) // return undef);
10768 $right = Math::GMPz::Rmpz_init_set(_star2mpz($right) // return undef);
10769 }
10770 else {
10771 $block = $right;
10772 $right = Math::GMPz::Rmpz_init_set(_star2mpz($left) // return undef);
10773 $left = Math::GMPz::Rmpz_init_set_ui(0);
10774 }
10775  
10776 my $middle = Math::GMPz::Rmpz_init();
10777  
10778 while (Math::GMPz::Rmpz_cmp($left, $right) <= 0) {
10779  
10780 Math::GMPz::Rmpz_add($middle, $left, $right);
10781 Math::GMPz::Rmpz_div_2exp($middle, $middle, 1);
10782  
10783 my $cmp = do {
10784 local $_ = bless \Math::GMPz::Rmpz_init_set($middle);
10785 $block->($_) || return $_;
10786 };
10787  
10788 if ($cmp > 0) {
10789 Math::GMPz::Rmpz_sub_ui($right, $middle, 1);
10790 }
10791 else {
10792 Math::GMPz::Rmpz_add_ui($left, $middle, 1);
10793 }
10794 }
10795  
10796 return undef;
10797 }
10798  
10799 sub bsearch_ge ($$;$) {
10800 my ($left, $right, $block) = @_;
10801  
10802 if (@_ == 3) {
10803 $left = Math::GMPz::Rmpz_init_set(_star2mpz($left) // return undef);
10804 $right = Math::GMPz::Rmpz_init_set(_star2mpz($right) // return undef);
10805 }
10806 else {
10807 $block = $right;
10808 $right = Math::GMPz::Rmpz_init_set(_star2mpz($left) // return undef);
10809 $left = Math::GMPz::Rmpz_init_set_ui(0);
10810 }
10811  
10812 my $middle = Math::GMPz::Rmpz_init();
10813  
10814 while (1) {
10815  
10816 Math::GMPz::Rmpz_add($middle, $left, $right);
10817 Math::GMPz::Rmpz_div_2exp($middle, $middle, 1);
10818  
10819 my $cmp = do {
10820 local $_ = bless \Math::GMPz::Rmpz_init_set($middle);
10821 $block->($_) || return $_;
10822 };
10823  
10824 if ($cmp < 0) {
10825 Math::GMPz::Rmpz_add_ui($left, $middle, 1);
10826  
10827 if (Math::GMPz::Rmpz_cmp($left, $right) > 0) {
10828 Math::GMPz::Rmpz_add_ui($middle, $middle, 1);
10829 last;
10830 }
10831 }
10832 else {
10833 Math::GMPz::Rmpz_sub_ui($right, $middle, 1);
10834 Math::GMPz::Rmpz_cmp($left, $right) > 0 and last;
10835 }
10836 }
10837  
10838 bless \$middle;
10839 }
10840  
10841 sub bsearch_le ($$;$) {
10842 my ($left, $right, $block) = @_;
10843  
10844 if (@_ == 3) {
10845 $left = Math::GMPz::Rmpz_init_set(_star2mpz($left) // return undef);
10846 $right = Math::GMPz::Rmpz_init_set(_star2mpz($right) // return undef);
10847 }
10848 else {
10849 $block = $right;
10850 $right = Math::GMPz::Rmpz_init_set(_star2mpz($left) // return undef);
10851 $left = Math::GMPz::Rmpz_init_set_ui(0);
10852 }
10853  
10854 my $middle = Math::GMPz::Rmpz_init();
10855  
10856 while (1) {
10857  
10858 Math::GMPz::Rmpz_add($middle, $left, $right);
10859 Math::GMPz::Rmpz_div_2exp($middle, $middle, 1);
10860  
10861 my $cmp = do {
10862 local $_ = bless \Math::GMPz::Rmpz_init_set($middle);
10863 $block->($_) || return $_;
10864 };
10865  
10866 if ($cmp < 0) {
10867 Math::GMPz::Rmpz_add_ui($left, $middle, 1);
10868 Math::GMPz::Rmpz_cmp($left, $right) > 0 and last;
10869 }
10870 else {
10871 Math::GMPz::Rmpz_sub_ui($right, $middle, 1);
10872 if (Math::GMPz::Rmpz_cmp($left, $right) > 0) {
10873 Math::GMPz::Rmpz_sub_ui($middle, $middle, 1);
10874 last;
10875 }
10876 }
10877 }
10878  
10879 bless \$middle;
10880 }
10881  
10882 1; # End of Math::AnyNum
« Build ↑ Summary