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
↑ 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 0 ▶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.42';
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($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($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 my $e = Math::MPFR::Rmpfr_init2($PREC);
1467 Math::MPFR::Rmpfr_set_ui($e, 1, $ROUND);
1468 Math::MPFR::Rmpfr_exp($e, $e, $ROUND);
1469 bless \$e;
1470 }
1471  
1472 sub phi {
1473 my $phi = Math::MPFR::Rmpfr_init2($PREC);
1474 Math::MPFR::Rmpfr_set_ui($phi, 5, $ROUND);
1475 Math::MPFR::Rmpfr_div_2ui($phi, $phi, 2, $ROUND); # phi=5/4
1476 Math::MPFR::Rmpfr_sqrt($phi, $phi, $ROUND);
1477 Math::MPFR::Rmpfr_add_d($phi, $phi, 0.5, $ROUND);
1478 bless \$phi;
1479 }
1480  
1481 #
1482 ## Stringification
1483 #
1484  
1485 sub __stringify__ {
1486 my ($x) = @_;
1487 goto(ref($x) =~ tr/:/_/rs);
1488  
1489 Math_GMPz: {
1490 return Math::GMPz::Rmpz_get_str($x, 10);
1491 }
1492  
1493 Math_GMPq: {
1494 return Math::GMPq::Rmpq_get_str($x, 10);
1495 }
1496  
1497 Math_MPFR: {
1498  
1499 Math::MPFR::Rmpfr_number_p($x)
1500 || return (
1501 Math::MPFR::Rmpfr_nan_p($x) ? 'NaN'
1502 : Math::MPFR::Rmpfr_sgn($x) < 0 ? '-Inf'
1503 : 'Inf'
1504 );
1505  
1506 # log(10)/log(2) =~ 3.3219280948873623
1507 my $digits = $PREC >> 2;
1508 my ($mantissa, $exponent) = Math::MPFR::Rmpfr_deref2($x, 10, $digits, $ROUND);
1509  
1510 my $sgn = '';
1511 if (substr($mantissa, 0, 1) eq '-') {
1512 $sgn = substr($mantissa, 0, 1, '');
1513 }
1514  
1515 $mantissa == 0 and return '0';
1516  
1517 if (CORE::abs($exponent) < CORE::length($mantissa)) {
1518  
1519 if ($exponent > 0) {
1520 substr($mantissa, $exponent, 0, '.');
1521 }
1522 else {
1523 substr($mantissa, 0, 0, '0.' . ('0' x CORE::abs($exponent)));
1524 }
1525  
1526 $mantissa = reverse($mantissa);
1527 $mantissa =~ s/^0+//;
1528 $mantissa =~ s/^\.//;
1529 $mantissa = reverse($mantissa);
1530  
1531 return ($sgn . $mantissa);
1532 }
1533  
1534 substr($mantissa, 1, 0, '.');
1535 return ($sgn . $mantissa . 'e' . ($exponent - 1));
1536 }
1537  
1538 Math_MPC: {
1539 my $fr = Math::MPFR::Rmpfr_init2($PREC);
1540  
1541 Math::MPC::RMPC_RE($fr, $x);
1542 my $re = __stringify__($fr);
1543  
1544 Math::MPC::RMPC_IM($fr, $x);
1545 my $im = __stringify__($fr);
1546  
1547 if ($im eq '0' or $im eq '-0') {
1548 return $re;
1549 }
1550  
1551 my $sign = '+';
1552  
1553 if (substr($im, 0, 1) eq '-') {
1554 $sign = '-';
1555 substr($im, 0, 1, '');
1556 }
1557  
1558 $im = '' if $im eq '1';
1559 return ($re eq '0' ? $sign eq '+' ? "${im}i" : "$sign${im}i" : "$re$sign${im}i");
1560 }
1561 }
1562  
1563 sub stringify { # used in overloading
1564 @_ = (${$_[0]});
1565 goto &__stringify__;
1566 }
1567  
1568 #
1569 ## Numification (object to a native integer or a double)
1570 #
1571  
1572 sub numify { # used in overloading
1573 my $x = ${$_[0]};
1574  
1575 goto(ref($x) =~ tr/:/_/rs);
1576  
1577 Math_MPFR: {
1578 if (Math::MPFR::Rmpfr_integer_p($x)) {
1579  
1580 if (Math::MPFR::Rmpfr_fits_ulong_p($x, $ROUND)) {
1581 return Math::MPFR::Rmpfr_get_ui($x, $ROUND);
1582 }
1583  
1584 if (Math::MPFR::Rmpfr_fits_slong_p($x, $ROUND)) {
1585 return Math::MPFR::Rmpfr_get_si($x, $ROUND);
1586 }
1587 }
1588  
1589 return Math::MPFR::Rmpfr_get_d($x, $ROUND);
1590 }
1591  
1592 Math_GMPq: {
1593  
1594 if (Math::GMPq::Rmpq_integer_p($x)) {
1595 $x = _mpq2mpz($x);
1596 goto Math_GMPz;
1597 }
1598  
1599 return Math::GMPq::Rmpq_get_d($x);
1600 }
1601  
1602 Math_GMPz: {
1603  
1604 if (Math::GMPz::Rmpz_fits_ulong_p($x)) {
1605 return Math::GMPz::Rmpz_get_ui($x);
1606 }
1607  
1608 if (Math::GMPz::Rmpz_fits_slong_p($x)) {
1609 return Math::GMPz::Rmpz_get_si($x);
1610 }
1611  
1612 return Math::GMPz::Rmpz_get_str($x, 10);
1613 }
1614  
1615 Math_MPC: {
1616 my $r = Math::MPFR::Rmpfr_init2($PREC);
1617 Math::MPC::RMPC_RE($r, $x);
1618 $x = $r;
1619 goto Math_MPFR;
1620 }
1621 }
1622  
1623 sub boolify { # used in overloading
1624 my $x = ${$_[0]};
1625  
1626 goto(ref($x) =~ tr/:/_/rs);
1627  
1628 Math_MPFR: {
1629 return !!Math::MPFR::Rmpfr_sgn($x);
1630 }
1631  
1632 Math_GMPq: {
1633 return !!Math::GMPq::Rmpq_sgn($x);
1634 }
1635  
1636 Math_GMPz: {
1637 return !!Math::GMPz::Rmpz_sgn($x);
1638 }
1639  
1640 Math_MPC: {
1641 my $r = Math::MPFR::Rmpfr_init2($PREC);
1642 Math::MPC::RMPC_RE($r, $x);
1643 Math::MPFR::Rmpfr_sgn($r) && return 1;
1644 Math::MPFR::Rmpfr_nan_p($r) && return 0;
1645 Math::MPC::RMPC_IM($r, $x);
1646 return !!Math::MPFR::Rmpfr_sgn($r);
1647 }
1648 }
1649  
1650 #
1651 ## EQUALITY
1652 #
1653  
1654 sub __eq__ {
1655 my ($x, $y) = @_;
1656 goto(join('__', ref($x), ref($y) || 'Scalar') =~ tr/:/_/rs);
1657  
1658 #
1659 ## MPFR
1660 #
1661 Math_MPFR__Math_MPFR: {
1662 return Math::MPFR::Rmpfr_equal_p($x, $y);
1663 }
1664  
1665 Math_MPFR__Math_GMPz: {
1666 return (Math::MPFR::Rmpfr_integer_p($x) and Math::MPFR::Rmpfr_cmp_z($x, $y) == 0);
1667 }
1668  
1669 Math_MPFR__Math_GMPq: {
1670 return (Math::MPFR::Rmpfr_number_p($x) and Math::MPFR::Rmpfr_cmp_q($x, $y) == 0);
1671 }
1672  
1673 Math_MPFR__Math_MPC: {
1674 $x = _mpfr2mpc($x);
1675 goto Math_MPC__Math_MPC;
1676 }
1677  
1678 Math_MPFR__Scalar: {
1679 return (
1680 Math::MPFR::Rmpfr_integer_p($x)
1681 and (
1682 ($y || return !Math::MPFR::Rmpfr_sgn($x)) < 0
1683 ? Math::MPFR::Rmpfr_cmp_si($x, $y)
1684 : Math::MPFR::Rmpfr_cmp_ui($x, $y)
1685 ) == 0
1686 );
1687 }
1688  
1689 #
1690 ## GMPq
1691 #
1692 Math_GMPq__Math_GMPq: {
1693 return Math::GMPq::Rmpq_equal($x, $y);
1694 }
1695  
1696 Math_GMPq__Math_GMPz: {
1697 return (Math::GMPq::Rmpq_integer_p($x) and Math::GMPq::Rmpq_cmp_z($x, $y) == 0);
1698 }
1699  
1700 Math_GMPq__Math_MPFR: {
1701 return (Math::MPFR::Rmpfr_number_p($y) and Math::MPFR::Rmpfr_cmp_q($y, $x) == 0);
1702 }
1703  
1704 Math_GMPq__Math_MPC: {
1705 $x = _mpq2mpc($x);
1706 goto Math_MPC__Math_MPC;
1707 }
1708  
1709 Math_GMPq__Scalar: {
1710 return (
1711 Math::GMPq::Rmpq_integer_p($x)
1712 and (
1713 ($y || return !Math::GMPq::Rmpq_sgn($x)) < 0
1714 ? Math::GMPq::Rmpq_cmp_si($x, $y, 1)
1715 : Math::GMPq::Rmpq_cmp_ui($x, $y, 1)
1716 ) == 0
1717 );
1718 }
1719  
1720 #
1721 ## GMPz
1722 #
1723 Math_GMPz__Math_GMPz: {
1724 return (Math::GMPz::Rmpz_cmp($x, $y) == 0);
1725 }
1726  
1727 Math_GMPz__Math_GMPq: {
1728 return (Math::GMPq::Rmpq_integer_p($y) and Math::GMPq::Rmpq_cmp_z($y, $x) == 0);
1729 }
1730  
1731 Math_GMPz__Math_MPFR: {
1732 return (Math::MPFR::Rmpfr_integer_p($y) and Math::MPFR::Rmpfr_cmp_z($y, $x) == 0);
1733 }
1734  
1735 Math_GMPz__Math_MPC: {
1736 $x = _mpz2mpc($x);
1737 goto Math_MPC__Math_MPC;
1738 }
1739  
1740 Math_GMPz__Scalar: {
1741 return (
1742 (
1743 ($y || return !Math::GMPz::Rmpz_sgn($x)) < 0
1744 ? Math::GMPz::Rmpz_cmp_si($x, $y)
1745 : Math::GMPz::Rmpz_cmp_ui($x, $y)
1746 ) == 0
1747 );
1748 }
1749  
1750 #
1751 ## MPC
1752 #
1753 Math_MPC__Math_MPC: {
1754  
1755 my $f1 = Math::MPFR::Rmpfr_init2($PREC);
1756 my $f2 = Math::MPFR::Rmpfr_init2($PREC);
1757  
1758 Math::MPC::RMPC_RE($f1, $x);
1759 Math::MPC::RMPC_RE($f2, $y);
1760  
1761 Math::MPFR::Rmpfr_equal_p($f1, $f2) || return 0;
1762  
1763 Math::MPC::RMPC_IM($f1, $x);
1764 Math::MPC::RMPC_IM($f2, $y);
1765  
1766 return Math::MPFR::Rmpfr_equal_p($f1, $f2);
1767 }
1768  
1769 Math_MPC__Math_GMPz: {
1770 $y = _mpz2mpc($y);
1771 goto Math_MPC__Math_MPC;
1772 }
1773  
1774 Math_MPC__Math_GMPq: {
1775 $y = _mpq2mpc($y);
1776 goto Math_MPC__Math_MPC;
1777 }
1778  
1779 Math_MPC__Math_MPFR: {
1780 $y = _mpfr2mpc($y);
1781 goto Math_MPC__Math_MPC;
1782 }
1783  
1784 Math_MPC__Scalar: {
1785 my $f = Math::MPFR::Rmpfr_init2($PREC);
1786 Math::MPC::RMPC_IM($f, $x);
1787 Math::MPFR::Rmpfr_zero_p($f) || return 0;
1788 Math::MPC::RMPC_RE($f, $x);
1789 $x = $f;
1790 goto Math_MPFR__Scalar;
1791 }
1792 }
1793  
1794 sub eq { # used in overloading
1795 my ($x, $y) = @_;
1796  
1797 if (ref($y) eq __PACKAGE__) {
1798 @_ = ($$x, $$y);
1799 goto &__eq__;
1800 }
1801  
1802 if (!ref($y)) {
1803 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
1804 @_ = ($$x, $y);
1805 }
1806 else {
1807 @_ = ($$x, _str2obj($y));
1808 }
1809 goto &__eq__;
1810 }
1811  
1812 @_ = ($$x, _star2obj($y));
1813 goto &__eq__;
1814 }
1815  
1816 #
1817 ## INEQUALITY
1818 #
1819  
1820 sub __ne__ {
1821 my ($x, $y) = @_;
1822 goto(join('__', ref($x), ref($y) || 'Scalar') =~ tr/:/_/rs);
1823  
1824 #
1825 ## MPFR
1826 #
1827 Math_MPFR__Math_MPFR: {
1828 return !Math::MPFR::Rmpfr_equal_p($x, $y);
1829 }
1830  
1831 Math_MPFR__Math_GMPz: {
1832 return (!Math::MPFR::Rmpfr_integer_p($x) or Math::MPFR::Rmpfr_cmp_z($x, $y) != 0);
1833 }
1834  
1835 Math_MPFR__Math_GMPq: {
1836 return (!Math::MPFR::Rmpfr_number_p($x) or Math::MPFR::Rmpfr_cmp_q($x, $y) != 0);
1837 }
1838  
1839 Math_MPFR__Math_MPC: {
1840 $x = _mpfr2mpc($x);
1841 goto Math_MPC__Math_MPC;
1842 }
1843  
1844 Math_MPFR__Scalar: {
1845 return (
1846 !Math::MPFR::Rmpfr_integer_p($x)
1847 or (
1848 ($y || return !!Math::MPFR::Rmpfr_sgn($x)) < 0
1849 ? Math::MPFR::Rmpfr_cmp_si($x, $y)
1850 : Math::MPFR::Rmpfr_cmp_ui($x, $y)
1851 ) != 0
1852 );
1853 }
1854  
1855 #
1856 ## GMPq
1857 #
1858 Math_GMPq__Math_GMPq: {
1859 return !Math::GMPq::Rmpq_equal($x, $y);
1860 }
1861  
1862 Math_GMPq__Math_GMPz: {
1863 return (!Math::GMPq::Rmpq_integer_p($x) or Math::GMPq::Rmpq_cmp_z($x, $y) != 0);
1864 }
1865  
1866 Math_GMPq__Math_MPFR: {
1867 return (!Math::MPFR::Rmpfr_number_p($y) or Math::MPFR::Rmpfr_cmp_q($y, $x) != 0);
1868 }
1869  
1870 Math_GMPq__Math_MPC: {
1871 $x = _mpq2mpc($x);
1872 goto Math_MPC__Math_MPC;
1873 }
1874  
1875 Math_GMPq__Scalar: {
1876 return (
1877 !Math::GMPq::Rmpq_integer_p($x)
1878 or (
1879 ($y || return !!Math::GMPq::Rmpq_sgn($x)) < 0
1880 ? Math::GMPq::Rmpq_cmp_si($x, $y, 1)
1881 : Math::GMPq::Rmpq_cmp_ui($x, $y, 1)
1882 ) != 0
1883 );
1884 }
1885  
1886 #
1887 ## GMPz
1888 #
1889 Math_GMPz__Math_GMPz: {
1890 return (Math::GMPz::Rmpz_cmp($x, $y) != 0);
1891 }
1892  
1893 Math_GMPz__Math_GMPq: {
1894 return (!Math::GMPq::Rmpq_integer_p($y) or Math::GMPq::Rmpq_cmp_z($y, $x) != 0);
1895 }
1896  
1897 Math_GMPz__Math_MPFR: {
1898 return (!Math::MPFR::Rmpfr_integer_p($y) or Math::MPFR::Rmpfr_cmp_z($y, $x) != 0);
1899 }
1900  
1901 Math_GMPz__Math_MPC: {
1902 $x = _mpz2mpc($x);
1903 goto Math_MPC__Math_MPC;
1904 }
1905  
1906 Math_GMPz__Scalar: {
1907 return (
1908 (
1909 ($y || return !!Math::GMPz::Rmpz_sgn($x)) < 0
1910 ? Math::GMPz::Rmpz_cmp_si($x, $y)
1911 : Math::GMPz::Rmpz_cmp_ui($x, $y)
1912 ) != 0
1913 );
1914 }
1915  
1916 #
1917 ## MPC
1918 #
1919 Math_MPC__Math_MPC: {
1920  
1921 my $f1 = Math::MPFR::Rmpfr_init2($PREC);
1922 my $f2 = Math::MPFR::Rmpfr_init2($PREC);
1923  
1924 Math::MPC::RMPC_RE($f1, $x);
1925 Math::MPC::RMPC_RE($f2, $y);
1926  
1927 Math::MPFR::Rmpfr_equal_p($f1, $f2) || return 1;
1928  
1929 Math::MPC::RMPC_IM($f1, $x);
1930 Math::MPC::RMPC_IM($f2, $y);
1931  
1932 return !Math::MPFR::Rmpfr_equal_p($f1, $f2);
1933 }
1934  
1935 Math_MPC__Math_GMPz: {
1936 $y = _mpz2mpc($y);
1937 goto Math_MPC__Math_MPC;
1938 }
1939  
1940 Math_MPC__Math_GMPq: {
1941 $y = _mpq2mpc($y);
1942 goto Math_MPC__Math_MPC;
1943 }
1944  
1945 Math_MPC__Math_MPFR: {
1946 $y = _mpfr2mpc($y);
1947 goto Math_MPC__Math_MPC;
1948 }
1949  
1950 Math_MPC__Scalar: {
1951 my $f = Math::MPFR::Rmpfr_init2($PREC);
1952 Math::MPC::RMPC_IM($f, $x);
1953 Math::MPFR::Rmpfr_zero_p($f) || return 1;
1954 Math::MPC::RMPC_RE($f, $x);
1955 $x = $f;
1956 goto Math_MPFR__Scalar;
1957 }
1958 }
1959  
1960 sub ne { # used in overloading
1961 my ($x, $y) = @_;
1962  
1963 if (ref($y) eq __PACKAGE__) {
1964 @_ = ($$x, $$y);
1965 goto &__ne__;
1966 }
1967  
1968 if (!ref($y)) {
1969 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
1970 @_ = ($$x, $y);
1971 }
1972 else {
1973 @_ = ($$x, _str2obj($y));
1974 }
1975 goto &__ne__;
1976 }
1977  
1978 @_ = ($$x, _star2obj($y));
1979 goto &__ne__;
1980 }
1981  
1982 #
1983 ## COMPARISON
1984 #
1985  
1986 sub __cmp__ {
1987 my ($x, $y) = @_;
1988 goto(join('__', ref($x), ref($y) || 'Scalar') =~ tr/:/_/rs);
1989  
1990 #
1991 ## MPFR
1992 #
1993 Math_MPFR__Math_MPFR: {
1994  
1995 if ( Math::MPFR::Rmpfr_nan_p($x)
1996 or Math::MPFR::Rmpfr_nan_p($y)) {
1997 return undef;
1998 }
1999  
2000 return Math::MPFR::Rmpfr_cmp($x, $y);
2001 }
2002  
2003 Math_MPFR__Math_GMPz: {
2004 return (
2005 Math::MPFR::Rmpfr_nan_p($x)
2006 ? undef
2007 : Math::MPFR::Rmpfr_cmp_z($x, $y)
2008 );
2009 }
2010  
2011 Math_MPFR__Math_GMPq: {
2012 return (
2013 Math::MPFR::Rmpfr_nan_p($x)
2014 ? undef
2015 : Math::MPFR::Rmpfr_cmp_q($x, $y)
2016 );
2017 }
2018  
2019 Math_MPFR__Math_MPC: {
2020 $x = _mpfr2mpc($x);
2021 goto Math_MPC__Math_MPC;
2022 }
2023  
2024 Math_MPFR__Scalar: {
2025 return (
2026 Math::MPFR::Rmpfr_nan_p($x) ? undef
2027 : ($y || return Math::MPFR::Rmpfr_sgn($x)) < 0 ? Math::MPFR::Rmpfr_cmp_si($x, $y)
2028 : Math::MPFR::Rmpfr_cmp_ui($x, $y)
2029 );
2030 }
2031  
2032 #
2033 ## GMPq
2034 #
2035 Math_GMPq__Math_GMPq: {
2036 return Math::GMPq::Rmpq_cmp($x, $y);
2037 }
2038  
2039 Math_GMPq__Math_GMPz: {
2040 return Math::GMPq::Rmpq_cmp_z($x, $y);
2041 }
2042  
2043 Math_GMPq__Math_MPFR: {
2044 return (
2045 Math::MPFR::Rmpfr_nan_p($y)
2046 ? undef
2047 : -Math::MPFR::Rmpfr_cmp_q($y, $x)
2048 );
2049 }
2050  
2051 Math_GMPq__Math_MPC: {
2052 $x = _mpq2mpc($x);
2053 goto Math_MPC__Math_MPC;
2054 }
2055  
2056 Math_GMPq__Scalar: {
2057 return (
2058 ($y || return Math::GMPq::Rmpq_sgn($x)) < 0
2059 ? Math::GMPq::Rmpq_cmp_si($x, $y, 1)
2060 : Math::GMPq::Rmpq_cmp_ui($x, $y, 1)
2061 );
2062 }
2063  
2064 #
2065 ## GMPz
2066 #
2067 Math_GMPz__Math_GMPz: {
2068 return Math::GMPz::Rmpz_cmp($x, $y);
2069 }
2070  
2071 Math_GMPz__Math_GMPq: {
2072 return -Math::GMPq::Rmpq_cmp_z($y, $x);
2073 }
2074  
2075 Math_GMPz__Math_MPFR: {
2076 return (
2077 Math::MPFR::Rmpfr_nan_p($y)
2078 ? undef
2079 : -Math::MPFR::Rmpfr_cmp_z($y, $x)
2080 );
2081 }
2082  
2083 Math_GMPz__Math_MPC: {
2084 $x = _mpz2mpc($x);
2085 goto Math_MPC__Math_MPC;
2086 }
2087  
2088 Math_GMPz__Scalar: {
2089 return (
2090 ($y || return Math::GMPz::Rmpz_sgn($x)) < 0
2091 ? Math::GMPz::Rmpz_cmp_si($x, $y)
2092 : Math::GMPz::Rmpz_cmp_ui($x, $y)
2093 );
2094 }
2095  
2096 #
2097 ## MPC
2098 #
2099 Math_MPC__Math_MPC: {
2100 my $f = Math::MPFR::Rmpfr_init2($PREC);
2101  
2102 Math::MPC::RMPC_RE($f, $x);
2103 Math::MPFR::Rmpfr_nan_p($f) && return undef;
2104  
2105 Math::MPC::RMPC_RE($f, $y);
2106 Math::MPFR::Rmpfr_nan_p($f) && return undef;
2107  
2108 Math::MPC::RMPC_IM($f, $x);
2109 Math::MPFR::Rmpfr_nan_p($f) && return undef;
2110  
2111 Math::MPC::RMPC_IM($f, $y);
2112 Math::MPFR::Rmpfr_nan_p($f) && return undef;
2113  
2114 my $si = Math::MPC::Rmpc_cmp($x, $y);
2115 my $re_cmp = Math::MPC::RMPC_INEX_RE($si);
2116  
2117 return (
2118 ($re_cmp == 0)
2119 ? Math::MPC::RMPC_INEX_IM($si)
2120 : $re_cmp
2121 );
2122 }
2123  
2124 Math_MPC__Math_GMPz: {
2125 $y = _mpz2mpc($y);
2126 goto Math_MPC__Math_MPC;
2127 }
2128  
2129 Math_MPC__Math_GMPq: {
2130 $y = _mpq2mpc($y);
2131 goto Math_MPC__Math_MPC;
2132 }
2133  
2134 Math_MPC__Math_MPFR: {
2135 $y = _mpfr2mpc($y);
2136 goto Math_MPC__Math_MPC;
2137 }
2138  
2139 Math_MPC__Scalar: {
2140 $y = _any2mpc(_str2obj($y));
2141 goto Math_MPC__Math_MPC;
2142 }
2143 }
2144  
2145 sub cmp ($$) {
2146 my ($x, $y) = @_;
2147  
2148 if (ref($y) eq __PACKAGE__) {
2149 @_ = ($$x, $$y);
2150 goto &__cmp__;
2151 }
2152  
2153 if (!ref($y)) {
2154 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
2155 @_ = ($$x, $y);
2156 }
2157 else {
2158 @_ = ($$x, _str2obj($y));
2159 }
2160 goto &__cmp__;
2161 }
2162  
2163 @_ = ($$x, _star2obj($y));
2164 goto &__cmp__;
2165 }
2166  
2167 # Absolute comparison
2168  
2169 sub acmp ($$) {
2170 my ($x, $y) = @_;
2171  
2172 if (!ref($y) and CORE::int($y) eq $y and $y >= 0 and $y < ULONG_MAX) {
2173 ## `y` is a native unsigned integer
2174 }
2175 else {
2176 $y = __abs__(ref($y) eq __PACKAGE__ ? $$y : _star2obj($y));
2177 }
2178  
2179 __cmp__(__abs__(ref($x) eq __PACKAGE__ ? $$x : _star2obj($x)), $y);
2180 }
2181  
2182 # Approximate comparison
2183  
2184 sub approx_cmp ($$;$) {
2185 my ($x, $y, $places) = @_;
2186  
2187 if (defined($places)) {
2188 if (!ref($places) and CORE::int($places) eq $places and $places > LONG_MIN and $places < ULONG_MAX) {
2189 ## places is a native integer
2190 }
2191 else {
2192 $places = _any2si(_star2obj($places)) // return undef;
2193 }
2194 }
2195 else {
2196 $places = -(($PREC >> 2) - 1);
2197 }
2198  
2199 $x = _star2obj($x);
2200 $y = _star2obj($y);
2201  
2202 if ( ref($x) eq 'Math::MPFR'
2203 or ref($y) eq 'Math::MPFR'
2204 or ref($x) eq 'Math::MPC'
2205 or ref($y) eq 'Math::MPC') {
2206 $x = _star2mpfr_mpc($x);
2207 $y = _star2mpfr_mpc($y);
2208 }
2209  
2210 $x = __round__($x, $places);
2211 $y = __round__($y, $places);
2212  
2213 __cmp__($x, $y);
2214 }
2215  
2216 #
2217 ## GREATER THAN
2218 #
2219  
2220 sub gt { # used in overloading
2221 my ($x, $y) = @_;
2222  
2223 if (ref($y) eq __PACKAGE__) {
2224 return ((__cmp__($$x, $$y) // return undef) > 0);
2225 }
2226  
2227 if (!ref($y)) {
2228 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
2229 return ((__cmp__($$x, $y) // return undef) > 0);
2230 }
2231 return ((__cmp__($$x, _str2obj($y)) // return undef) > 0);
2232 }
2233  
2234 (__cmp__($$x, _star2obj($y)) // return undef) > 0;
2235 }
2236  
2237 #
2238 ## EQUAL OR GREATER THAN
2239 #
2240  
2241 sub ge { # used in overloading
2242 my ($x, $y) = @_;
2243  
2244 if (ref($y) eq __PACKAGE__) {
2245 return ((__cmp__($$x, $$y) // return undef) >= 0);
2246 }
2247  
2248 if (!ref($y)) {
2249 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
2250 return ((__cmp__($$x, $y) // return undef) >= 0);
2251 }
2252 return ((__cmp__($$x, _str2obj($y)) // return undef) >= 0);
2253 }
2254  
2255 (__cmp__($$x, _star2obj($y)) // return undef) >= 0;
2256 }
2257  
2258 #
2259 ## LESS THAN
2260 #
2261  
2262 sub lt { # used in overloading
2263 my ($x, $y) = @_;
2264  
2265 if (ref($y) eq __PACKAGE__) {
2266 return ((__cmp__($$x, $$y) // return undef) < 0);
2267 }
2268  
2269 if (!ref($y)) {
2270 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
2271 return ((__cmp__($$x, $y) // return undef) < 0);
2272 }
2273 return ((__cmp__($$x, _str2obj($y)) // return undef) < 0);
2274 }
2275  
2276 (__cmp__($$x, _star2obj($y)) // return undef) < 0;
2277 }
2278  
2279 #
2280 ## EQUAL OR LESS THAN
2281 #
2282  
2283 sub le { # used in overloading
2284 my ($x, $y) = @_;
2285  
2286 if (ref($y) eq __PACKAGE__) {
2287 return ((__cmp__($$x, $$y) // return undef) <= 0);
2288 }
2289  
2290 if (!ref($y)) {
2291 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
2292 return ((__cmp__($$x, $y) // return undef) <= 0);
2293 }
2294 return ((__cmp__($$x, _str2obj($y)) // return undef) <= 0);
2295 }
2296  
2297 (__cmp__($$x, _star2obj($y)) // return undef) <= 0;
2298 }
2299  
2300 #
2301 ## COPY
2302 #
2303  
2304 sub _copy {
2305 my ($x) = @_;
2306 my $ref = ref($x);
2307  
2308 if ($ref eq 'Math::GMPz') {
2309 Math::GMPz::Rmpz_init_set($x);
2310 }
2311 elsif ($ref eq 'Math::MPFR') {
2312 my $r = Math::MPFR::Rmpfr_init2($PREC);
2313 Math::MPFR::Rmpfr_set($r, $x, $ROUND);
2314 $r;
2315 }
2316 elsif ($ref eq 'Math::GMPq') {
2317 my $r = Math::GMPq::Rmpq_init();
2318 Math::GMPq::Rmpq_set($r, $x);
2319 $r;
2320 }
2321 elsif ($ref eq 'Math::MPC') {
2322 my $r = Math::MPC::Rmpc_init2($PREC);
2323 Math::MPC::Rmpc_set($r, $x, $ROUND);
2324 $r;
2325 }
2326 else {
2327 _str2obj("$x"); # this should not happen
2328 }
2329 }
2330  
2331 sub copy ($) {
2332 my ($x) = @_;
2333 bless \_copy($$x);
2334 }
2335  
2336 #
2337 ## CONVERSION TO INTEGER
2338 #
2339  
2340 sub int { # used in overloading
2341 my ($x) = @_;
2342  
2343 bless \(
2344 (
2345 ref($x) eq __PACKAGE__
2346 ? ref($$x) eq 'Math::GMPz'
2347 ? (return $x)
2348 : _any2mpz($$x)
2349 : _star2mpz($x)
2350 ) // goto &nan
2351 );
2352 }
2353  
2354 #
2355 ## CONVERSION TO RATIONAL
2356 #
2357  
2358 sub rat ($) {
2359 my ($x) = @_;
2360  
2361 if (ref($x) eq __PACKAGE__) {
2362 ref($$x) eq 'Math::GMPq' && return $x;
2363 return bless \(_any2mpq($$x) // (goto &nan));
2364 }
2365  
2366 # Parse a decimal number as an exact fraction
2367 if ("$x" =~ /^([+-]?+(?=\.?[0-9])[0-9_]*+(?:\.[0-9_]++)?(?:[Ee](?:[+-]?+[0-9_]+))?)\z/) {
2368 my $frac = _str2frac(lc($1) =~ tr/_//dr);
2369 my $q = Math::GMPq::Rmpq_init();
2370 Math::GMPq::Rmpq_set_str($q, $frac, 10);
2371 Math::GMPq::Rmpq_canonicalize($q) if (index($frac, '/') != -1);
2372 return bless \$q;
2373 }
2374  
2375 my $r = _star2obj($x);
2376 ref($r) eq 'Math::GMPq' && return bless \$r;
2377 bless \(_any2mpq($r) // (goto &nan));
2378 }
2379  
2380 #
2381 ## CONVERSION TO FLOATING-POINT
2382 #
2383  
2384 sub float ($) {
2385 my ($x) = @_;
2386  
2387 ref($x) eq __PACKAGE__
2388 && (ref($$x) eq 'Math::MPFR' || ref($$x) eq 'Math::MPC')
2389 && return $x;
2390  
2391 bless \_star2mpfr_mpc($x);
2392 }
2393  
2394 #
2395 ## CONVERSION TO COMPLEX
2396 #
2397  
2398 sub complex ($;$) {
2399 my ($x, $y) = @_;
2400  
2401 if (defined $y) {
2402 return bless \_reals2mpc($x, $y);
2403 }
2404  
2405 bless \(
2406 ref($x) eq __PACKAGE__
2407 ? ref($$x) eq 'Math::MPC'
2408 ? (return $x)
2409 : _any2mpc($$x)
2410 : _any2mpc(_star2obj($x))
2411 );
2412 }
2413  
2414 #
2415 ## ABSOLUTE VALUE
2416 #
2417  
2418 sub __abs__ {
2419 my ($x) = @_;
2420 goto(ref($x) =~ tr/:/_/rs);
2421  
2422 Math_MPFR: {
2423 Math::MPFR::Rmpfr_sgn($x) >= 0 and return $x;
2424 my $r = Math::MPFR::Rmpfr_init2($PREC);
2425 Math::MPFR::Rmpfr_abs($r, $x, $ROUND);
2426 return $r;
2427 }
2428  
2429 Math_GMPq: {
2430 Math::GMPq::Rmpq_sgn($x) >= 0 and return $x;
2431 my $r = Math::GMPq::Rmpq_init();
2432 Math::GMPq::Rmpq_abs($r, $x);
2433 return $r;
2434 }
2435  
2436 Math_GMPz: {
2437 Math::GMPz::Rmpz_sgn($x) >= 0 and return $x;
2438 my $r = Math::GMPz::Rmpz_init();
2439 Math::GMPz::Rmpz_abs($r, $x);
2440 return $r;
2441 }
2442  
2443 Math_MPC: {
2444 my $r = Math::MPFR::Rmpfr_init2($PREC);
2445 Math::MPC::Rmpc_abs($r, $x, $ROUND);
2446 return $r;
2447 }
2448 }
2449  
2450 sub abs { # used in overloading
2451 my ($x) = @_;
2452 bless \__abs__(ref($x) eq __PACKAGE__ ? $$x : _star2obj($x));
2453 }
2454  
2455 #
2456 ## ADDITIVE INVERSE (-x)
2457 #
2458  
2459 sub __neg__ {
2460 my ($x) = @_;
2461 goto(ref($x) =~ tr/:/_/rs);
2462  
2463 Math_MPFR: {
2464 my $r = Math::MPFR::Rmpfr_init2($PREC);
2465 Math::MPFR::Rmpfr_neg($r, $x, $ROUND);
2466 return $r;
2467 }
2468  
2469 Math_GMPq: {
2470 my $r = Math::GMPq::Rmpq_init();
2471 Math::GMPq::Rmpq_neg($r, $x);
2472 return $r;
2473 }
2474  
2475 Math_GMPz: {
2476 my $r = Math::GMPz::Rmpz_init();
2477 Math::GMPz::Rmpz_neg($r, $x);
2478 return $r;
2479 }
2480  
2481 Math_MPC: {
2482 my $r = Math::MPC::Rmpc_init2($PREC);
2483 Math::MPC::Rmpc_neg($r, $x, $ROUND);
2484 return $r;
2485 }
2486 }
2487  
2488 sub neg { # used in overloading
2489 my ($x) = @_;
2490 bless \__neg__(ref($x) eq __PACKAGE__ ? $$x : _star2obj($x));
2491 }
2492  
2493 #
2494 ## MULTIPLICATIVE INVERSE (1/x)
2495 #
2496  
2497 sub __inv__ {
2498 my ($x) = @_;
2499 goto(ref($x) =~ tr/:/_/rs);
2500  
2501 Math_MPFR: {
2502 my $r = Math::MPFR::Rmpfr_init2($PREC);
2503 Math::MPFR::Rmpfr_ui_div($r, 1, $x, $ROUND);
2504 return $r;
2505 }
2506  
2507 Math_GMPq: {
2508  
2509 # Check for division by zero
2510 Math::GMPq::Rmpq_sgn($x) || do {
2511 $x = _mpq2mpfr($x);
2512 goto Math_MPFR;
2513 };
2514  
2515 my $r = Math::GMPq::Rmpq_init();
2516 Math::GMPq::Rmpq_inv($r, $x);
2517 return $r;
2518 }
2519  
2520 Math_GMPz: {
2521  
2522 # Check for division by zero
2523 Math::GMPz::Rmpz_sgn($x) || do {
2524 $x = _mpz2mpfr($x);
2525 goto Math_MPFR;
2526 };
2527  
2528 my $r = Math::GMPq::Rmpq_init();
2529 Math::GMPq::Rmpq_set_z($r, $x);
2530 Math::GMPq::Rmpq_inv($r, $r);
2531 return $r;
2532 }
2533  
2534 Math_MPC: {
2535 my $r = Math::MPC::Rmpc_init2($PREC);
2536 Math::MPC::Rmpc_set_ui($r, 1, $ROUND);
2537 Math::MPC::Rmpc_div($r, $r, $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 Math::MPC::Rmpc_set_ui($r, (($y < 0) ? (-$y) : $y), $ROUND);
3518 Math::MPC::Rmpc_neg($r, $r, $ROUND) if ($y < 0);
3519 Math::MPC::Rmpc_div($r, $x, $r, $ROUND);
3520 return $r;
3521 }
3522  
3523 Scalar__Math_MPC: {
3524  
3525 # XXX: don't use Rmpc_ui_div(), as it is broken in mpc=1.4.0
3526 my $r = Math::MPC::Rmpc_init2($PREC);
3527 Math::MPC::Rmpc_set_ui($r, (($x < 0) ? (-$x) : $x), $ROUND);
3528 Math::MPC::Rmpc_neg($r, $r, $ROUND) if ($x < 0);
3529 Math::MPC::Rmpc_div($r, $r, $y, $ROUND);
3530 return $r;
3531 }
3532  
3533 Math_MPC__Math_MPFR: {
3534 my $r = Math::MPC::Rmpc_init2($PREC);
3535 Math::MPC::Rmpc_div_fr($r, $x, $y, $ROUND);
3536 return $r;
3537 }
3538  
3539 Math_MPC__Math_GMPz: {
3540 my $r = Math::MPC::Rmpc_init2($PREC);
3541 Math::MPC::Rmpc_set_z($r, $y, $ROUND);
3542 Math::MPC::Rmpc_div($r, $x, $r, $ROUND);
3543 return $r;
3544 }
3545  
3546 Math_MPC__Math_GMPq: {
3547 my $r = Math::MPC::Rmpc_init2($PREC);
3548 Math::MPC::Rmpc_set_q($r, $y, $ROUND);
3549 Math::MPC::Rmpc_div($r, $x, $r, $ROUND);
3550 return $r;
3551 }
3552 }
3553  
3554 sub div { # used in overloading
3555 my ($x, $y) = @_;
3556  
3557 if (!ref($x) and CORE::int($x) eq $x and $x < ULONG_MAX and $x > LONG_MIN) {
3558  
3559 if (ref($y) eq __PACKAGE__) {
3560 return bless \__div__($x, $$y);
3561 }
3562  
3563 return bless \__div__($x, ref($y) ? _star2obj($y) : _str2obj($y));
3564 }
3565  
3566 $x =
3567 ref($x) eq __PACKAGE__ ? $$x
3568 : ref($x) ? _star2obj($x)
3569 : _str2obj($x);
3570  
3571 if (ref($y) eq __PACKAGE__) {
3572 return bless \__div__($x, $$y);
3573 }
3574  
3575 if (!ref($y)) {
3576 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN and CORE::int($y)) {
3577 return bless \__div__($x, $y);
3578 }
3579  
3580 return bless \__div__($x, _str2obj($y));
3581 }
3582  
3583 bless \__div__($x, _star2obj($y));
3584 }
3585  
3586 #
3587 ## INTEGER ADDITION
3588 #
3589  
3590 sub iadd ($$) {
3591 my ($x, $y) = @_;
3592  
3593 if (!ref($x) and ref($y)) {
3594 ($x, $y) = ($y, $x);
3595 }
3596  
3597 $x = _star2mpz($x) // goto &nan;
3598  
3599 if (!ref($y) and CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
3600 my $r = Math::GMPz::Rmpz_init();
3601 $y < 0
3602 ? Math::GMPz::Rmpz_sub_ui($r, $x, -$y)
3603 : Math::GMPz::Rmpz_add_ui($r, $x, $y);
3604 return bless \$r;
3605 }
3606  
3607 $y = _star2mpz($y) // goto &nan;
3608  
3609 my $r = Math::GMPz::Rmpz_init();
3610 Math::GMPz::Rmpz_add($r, $x, $y);
3611 bless \$r;
3612 }
3613  
3614 #
3615 ## INTEGER SUBTRACTION
3616 #
3617  
3618 sub isub ($$) {
3619 my ($x, $y) = @_;
3620  
3621 $x = _star2mpz($x) // goto &nan;
3622  
3623 if (!ref($y) and CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
3624 my $r = Math::GMPz::Rmpz_init();
3625 $y < 0
3626 ? Math::GMPz::Rmpz_add_ui($r, $x, -$y)
3627 : Math::GMPz::Rmpz_sub_ui($r, $x, $y);
3628 return bless \$r;
3629 }
3630  
3631 $y = _star2mpz($y) // goto &nan;
3632  
3633 my $r = Math::GMPz::Rmpz_init();
3634 Math::GMPz::Rmpz_sub($r, $x, $y);
3635 bless \$r;
3636 }
3637  
3638 #
3639 ## INTEGER MULTIPLICATION
3640 #
3641  
3642 sub imul ($$) {
3643 my ($x, $y) = @_;
3644  
3645 if (!ref($x) and ref($y)) {
3646 ($x, $y) = ($y, $x);
3647 }
3648  
3649 $x = _star2mpz($x) // goto &nan;
3650  
3651 if (!ref($y) and CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
3652 my $r = Math::GMPz::Rmpz_init();
3653 Math::GMPz::Rmpz_mul_ui($r, $x, $y < 0 ? -$y : $y);
3654 Math::GMPz::Rmpz_neg($r, $r) if $y < 0;
3655 return bless \$r;
3656 }
3657  
3658 $y = _star2mpz($y) // goto &nan;
3659  
3660 my $r = Math::GMPz::Rmpz_init();
3661 Math::GMPz::Rmpz_mul($r, $x, $y);
3662 bless \$r;
3663 }
3664  
3665 #
3666 ## INTEGER DIVISION
3667 #
3668  
3669 sub idiv ($$) {
3670 my ($x, $y) = @_;
3671  
3672 $x = _star2mpz($x) // goto &nan;
3673  
3674 if (!ref($y) and CORE::int($y) eq $y and CORE::int($y) > 0 and $y < ULONG_MAX) {
3675 my $r = Math::GMPz::Rmpz_init();
3676 Math::GMPz::Rmpz_div_ui($r, $x, $y);
3677 return bless \$r;
3678 }
3679  
3680 $y = _star2mpz($y) // goto &nan;
3681  
3682 # Detect division by zero
3683 Math::GMPz::Rmpz_sgn($y) || do {
3684 my $sign = Math::GMPz::Rmpz_sgn($x);
3685  
3686 if ($sign == 0) { # 0/0
3687 goto &nan;
3688 }
3689 elsif ($sign > 0) { # x/0 where: x > 0
3690 goto &inf;
3691 }
3692 else { # x/0 where: x < 0
3693 goto &ninf;
3694 }
3695 };
3696  
3697 my $r = Math::GMPz::Rmpz_init();
3698 Math::GMPz::Rmpz_div($r, $x, $y);
3699 bless \$r;
3700 }
3701  
3702 sub idiv_ceil ($$) {
3703 my ($x, $y) = @_;
3704  
3705 $x = _star2mpz($x) // (goto &nan);
3706  
3707 if (!ref($y) and CORE::int($y) eq $y and CORE::int($y) > 0 and $y < ULONG_MAX) {
3708 my $r = Math::GMPz::Rmpz_init();
3709 Math::GMPz::Rmpz_cdiv_q_ui($r, $x, $y);
3710 return bless \$r;
3711 }
3712  
3713 $y = _star2mpz($y) // (goto &nan);
3714  
3715 # Detect division by zero
3716 Math::GMPz::Rmpz_sgn($y) || do {
3717 my $sign = Math::GMPz::Rmpz_sgn($x);
3718  
3719 if ($sign == 0) { # 0/0
3720 goto &nan;
3721 }
3722 elsif ($sign > 0) { # x/0 where: x > 0
3723 goto &inf;
3724 }
3725 else { # x/0 where: x < 0
3726 goto &ninf;
3727 }
3728 };
3729  
3730 my $r = Math::GMPz::Rmpz_init();
3731 Math::GMPz::Rmpz_cdiv_q($r, $x, $y);
3732 bless \$r;
3733 }
3734  
3735 sub idiv_trunc ($$) {
3736 my ($x, $y) = @_;
3737  
3738 $x = _star2mpz($x) // (goto &nan);
3739  
3740 if (!ref($y) and CORE::int($y) eq $y and CORE::int($y) > 0 and $y < ULONG_MAX) {
3741 my $r = Math::GMPz::Rmpz_init();
3742 Math::GMPz::Rmpz_tdiv_q_ui($r, $x, $y);
3743 return bless \$r;
3744 }
3745  
3746 $y = _star2mpz($y) // (goto &nan);
3747  
3748 # Detect division by zero
3749 Math::GMPz::Rmpz_sgn($y) || do {
3750 my $sign = Math::GMPz::Rmpz_sgn($x);
3751  
3752 if ($sign == 0) { # 0/0
3753 goto &nan;
3754 }
3755 elsif ($sign > 0) { # x/0 where: x > 0
3756 goto &inf;
3757 }
3758 else { # x/0 where: x < 0
3759 goto &ninf;
3760 }
3761 };
3762  
3763 my $r = Math::GMPz::Rmpz_init();
3764 Math::GMPz::Rmpz_tdiv_q($r, $x, $y);
3765 bless \$r;
3766 }
3767  
3768 sub idiv_round ($$) {
3769 my ($x, $y) = @_;
3770  
3771 $x = _star2mpz($x) // (goto &nan);
3772 $y = _star2mpz($y) // (goto &nan);
3773  
3774 # Detect division by zero
3775 Math::GMPz::Rmpz_sgn($y) || do {
3776 my $sign = Math::GMPz::Rmpz_sgn($x);
3777  
3778 if ($sign == 0) { # 0/0
3779 goto &nan;
3780 }
3781 elsif ($sign > 0) { # x/0 where: x > 0
3782 goto &inf;
3783 }
3784 else { # x/0 where: x < 0
3785 goto &ninf;
3786 }
3787 };
3788  
3789 my $r = Math::GMPz::Rmpz_init();
3790 Math::GMPz::Rmpz_set($r, $y);
3791 Math::GMPz::Rmpz_addmul_ui($r, $x, 2);
3792 Math::GMPz::Rmpz_div($r, $r, $y);
3793 Math::GMPz::Rmpz_div_2exp($r, $r, 1);
3794 bless \$r;
3795 }
3796  
3797 #
3798 ## POWER (x^y)
3799 #
3800  
3801 sub __pow__ {
3802 my ($x, $y) = @_;
3803 goto(join('__', ref($x), ref($y) || 'Scalar') =~ tr/:/_/rs);
3804  
3805 #
3806 ## GMPq
3807 #
3808 Math_GMPq__Scalar: {
3809  
3810 my $r = Math::GMPq::Rmpq_init();
3811 Math::GMPq::Rmpq_pow_ui($r, $x, CORE::abs($y));
3812  
3813 if ($y < 0) {
3814 Math::GMPq::Rmpq_sgn($r) || goto &_inf;
3815 Math::GMPq::Rmpq_inv($r, $r);
3816 }
3817  
3818 return $r;
3819 }
3820  
3821 Math_GMPq__Math_GMPq: {
3822  
3823 # Integer power
3824 if (Math::GMPq::Rmpq_integer_p($y)) {
3825 $y = Math::GMPq::Rmpq_get_d($y);
3826 goto Math_GMPq__Scalar;
3827 }
3828  
3829 # (-x)^(a/b) is a complex number
3830 if (Math::GMPq::Rmpq_sgn($x) < 0) {
3831 $x = _mpq2mpc($x);
3832 $y = _mpq2mpc($y);
3833 goto Math_MPC__Math_MPC;
3834 }
3835  
3836 $x = _mpq2mpfr($x);
3837 $y = _mpq2mpfr($y);
3838  
3839 goto Math_MPFR__Math_MPFR;
3840 }
3841  
3842 Math_GMPq__Math_GMPz: {
3843 $y = Math::GMPz::Rmpz_get_d($y);
3844 goto Math_GMPq__Scalar;
3845 }
3846  
3847 Math_GMPq__Math_MPFR: {
3848 $x = _mpq2mpfr($x);
3849 goto Math_MPFR__Math_MPFR;
3850 }
3851  
3852 Math_GMPq__Math_MPC: {
3853 $x = _mpq2mpc($x);
3854 goto Math_MPC__Math_MPC;
3855 }
3856  
3857 #
3858 ## GMPz
3859 #
3860  
3861 Math_GMPz__Scalar: {
3862  
3863 my $r = Math::GMPz::Rmpz_init();
3864 Math::GMPz::Rmpz_pow_ui($r, $x, CORE::abs($y));
3865  
3866 if ($y < 0) {
3867 Math::GMPz::Rmpz_sgn($r) || goto &_inf;
3868  
3869 my $q = Math::GMPq::Rmpq_init();
3870 Math::GMPq::Rmpq_set_z($q, $r);
3871 Math::GMPq::Rmpq_inv($q, $q);
3872 return $q;
3873 }
3874  
3875 return $r;
3876 }
3877  
3878 Math_GMPz__Math_GMPz: {
3879 $y = Math::GMPz::Rmpz_get_d($y);
3880 goto Math_GMPz__Scalar;
3881 }
3882  
3883 Math_GMPz__Math_GMPq: {
3884  
3885 if (Math::GMPq::Rmpq_integer_p($y)) {
3886 $y = Math::GMPq::Rmpq_get_d($y);
3887 goto Math_GMPz__Scalar;
3888 }
3889  
3890 $x = _mpz2mpfr($x);
3891 $y = _mpq2mpfr($y);
3892  
3893 goto Math_MPFR__Math_MPFR;
3894 }
3895  
3896 Math_GMPz__Math_MPFR: {
3897 $x = _mpz2mpfr($x);
3898 goto Math_MPFR__Math_MPFR;
3899 }
3900  
3901 Math_GMPz__Math_MPC: {
3902 $x = _mpz2mpc($x);
3903 goto Math_MPC__Math_MPC;
3904 }
3905  
3906 #
3907 ## MPFR
3908 #
3909 Math_MPFR__Math_MPFR: {
3910  
3911 if ( Math::MPFR::Rmpfr_sgn($x) < 0
3912 and !Math::MPFR::Rmpfr_integer_p($y)
3913 and Math::MPFR::Rmpfr_number_p($y)) {
3914 $x = _mpfr2mpc($x);
3915 goto Math_MPC__Math_MPFR;
3916 }
3917  
3918 my $r = Math::MPFR::Rmpfr_init2($PREC);
3919 Math::MPFR::Rmpfr_pow($r, $x, $y, $ROUND);
3920 return $r;
3921 }
3922  
3923 Math_MPFR__Scalar: {
3924 my $r = Math::MPFR::Rmpfr_init2($PREC);
3925 $y < 0
3926 ? Math::MPFR::Rmpfr_pow_si($r, $x, $y, $ROUND)
3927 : Math::MPFR::Rmpfr_pow_ui($r, $x, $y, $ROUND);
3928 return $r;
3929 }
3930  
3931 Math_MPFR__Math_GMPq: {
3932 $y = _mpq2mpfr($y);
3933 goto Math_MPFR__Math_MPFR;
3934 }
3935  
3936 Math_MPFR__Math_GMPz: {
3937 my $r = Math::MPFR::Rmpfr_init2($PREC);
3938 Math::MPFR::Rmpfr_pow_z($r, $x, $y, $ROUND);
3939 return $r;
3940 }
3941  
3942 Math_MPFR__Math_MPC: {
3943 $x = _mpfr2mpc($x);
3944 goto Math_MPC__Math_MPC;
3945 }
3946  
3947 #
3948 ## MPC
3949 #
3950 Math_MPC__Math_MPC: {
3951 my $r = Math::MPC::Rmpc_init2($PREC);
3952 Math::MPC::Rmpc_pow($r, $x, $y, $ROUND);
3953 return $r;
3954 }
3955  
3956 Math_MPC__Scalar: {
3957 my $r = Math::MPC::Rmpc_init2($PREC);
3958 $y < 0
3959 ? Math::MPC::Rmpc_pow_si($r, $x, $y, $ROUND)
3960 : Math::MPC::Rmpc_pow_ui($r, $x, $y, $ROUND);
3961 return $r;
3962 }
3963  
3964 Math_MPC__Math_MPFR: {
3965 my $r = Math::MPC::Rmpc_init2($PREC);
3966 Math::MPC::Rmpc_pow_fr($r, $x, $y, $ROUND);
3967 return $r;
3968 }
3969  
3970 Math_MPC__Math_GMPz: {
3971 my $r = Math::MPC::Rmpc_init2($PREC);
3972 Math::MPC::Rmpc_pow_z($r, $x, $y, $ROUND);
3973 return $r;
3974 }
3975  
3976 Math_MPC__Math_GMPq: {
3977 $y = _mpq2mpc($y);
3978 goto Math_MPC__Math_MPC;
3979 }
3980 }
3981  
3982 sub pow ($$) {
3983 my ($x, $y) = @_;
3984  
3985 $x =
3986 ref($x) eq __PACKAGE__ ? $$x
3987 : ref($x) ? _star2obj($x)
3988 : _str2obj($x);
3989  
3990 if (ref($y) eq __PACKAGE__) {
3991 return bless \__pow__($x, $$y);
3992 }
3993  
3994 if (!ref($y)) {
3995 if (CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
3996 return bless \__pow__($x, $y);
3997 }
3998  
3999 return bless \__pow__($x, _str2obj($y));
4000 }
4001  
4002 bless \__pow__($x, _star2obj($y));
4003 }
4004  
4005 #
4006 ## INTEGER POWER
4007 #
4008  
4009 sub ipow ($$) {
4010 my ($x, $y) = @_;
4011  
4012 $y = _star2si($y) // goto &nan;
4013  
4014 # Both `x` and `y` are unsigned native integers
4015 if ( !ref($x)
4016 and CORE::int($x) eq $x
4017 and $x >= 0
4018 and $x < ULONG_MAX
4019 and $y >= 0) {
4020 my $r = Math::GMPz::Rmpz_init();
4021 Math::GMPz::Rmpz_ui_pow_ui($r, $x, $y);
4022 return bless \$r;
4023 }
4024  
4025 $x = _star2mpz($x) // goto &nan;
4026  
4027 my $r = Math::GMPz::Rmpz_init();
4028 Math::GMPz::Rmpz_pow_ui($r, $x, $y < 0 ? -$y : $y);
4029  
4030 if ($y < 0) {
4031 Math::GMPz::Rmpz_sgn($r) || goto &inf; # 0^(-y) = Inf
4032 state $ONE_Z = Math::GMPz::Rmpz_init_set_ui_nobless(1);
4033 Math::GMPz::Rmpz_div($r, $ONE_Z, $r);
4034 }
4035  
4036 bless \$r;
4037 }
4038  
4039 #
4040 ## IPOW2
4041 #
4042  
4043 sub ipow2 ($) {
4044 my ($n) = @_;
4045  
4046 $n = _star2si($n) // goto &nan;
4047  
4048 goto &zero if $n < 0;
4049  
4050 my $r = Math::GMPz::Rmpz_init();
4051 Math::GMPz::Rmpz_setbit($r, $n);
4052 bless \$r;
4053 }
4054  
4055 #
4056 ## IPOW10
4057 #
4058  
4059 sub ipow10 ($) {
4060 my ($n) = @_;
4061  
4062 $n = _star2si($n) // goto &nan;
4063  
4064 goto &zero if $n < 0;
4065  
4066 my $r = Math::GMPz::Rmpz_init();
4067 Math::GMPz::Rmpz_ui_pow_ui($r, 10, $n);
4068 bless \$r;
4069 }
4070  
4071 #
4072 ## ROOT
4073 #
4074  
4075 sub root ($$) {
4076 my ($x, $y) = @_;
4077  
4078 $x =
4079 ref($x) eq __PACKAGE__ ? $$x
4080 : ref($x) ? _star2obj($x)
4081 : _str2obj($x);
4082  
4083 $y =
4084 ref($y) eq __PACKAGE__ ? $$y
4085 : ref($y) ? _star2obj($y)
4086 : _str2obj($y);
4087  
4088 bless \__pow__($x, __inv__($y));
4089 }
4090  
4091 #
4092 ## Polygonal root
4093 #
4094  
4095 sub __polygonal_root__ {
4096 my ($n, $k, $second) = @_;
4097 goto(join('__', ref($n), ref($k)) =~ tr/:/_/rs);
4098  
4099 # polygonal_root(n, k)
4100 # = ((k - 4) ± sqrt(8 * (k - 2) * n + (k - 4)^2)) / (2 * (k - 2))
4101  
4102 Math_MPFR__Math_MPFR: {
4103 my $t = Math::MPFR::Rmpfr_init2($PREC);
4104 my $u = Math::MPFR::Rmpfr_init2($PREC);
4105  
4106 Math::MPFR::Rmpfr_sub_ui($u, $k, 2, $ROUND); # u = k-2
4107 Math::MPFR::Rmpfr_mul($t, $n, $u, $ROUND); # t = n*u
4108 Math::MPFR::Rmpfr_mul_2ui($t, $t, 3, $ROUND); # t = t*8
4109  
4110 Math::MPFR::Rmpfr_sub_ui($u, $u, 2, $ROUND); # u = u-2
4111 Math::MPFR::Rmpfr_sqr($u, $u, $ROUND); # u = u^2
4112 Math::MPFR::Rmpfr_add($t, $t, $u, $ROUND); # t = t+u
4113  
4114 # Return a complex number for `t < 0`
4115 if (Math::MPFR::Rmpfr_sgn($t) < 0) {
4116 $n = _mpfr2mpc($n);
4117 $k = _mpfr2mpc($k);
4118 goto Math_MPC__Math_MPC;
4119 }
4120  
4121 Math::MPFR::Rmpfr_sqrt($t, $t, $ROUND); # t = sqrt(t)
4122 Math::MPFR::Rmpfr_sub_ui($u, $k, 4, $ROUND); # u = k-4
4123  
4124 $second
4125 ? Math::MPFR::Rmpfr_sub($t, $u, $t, $ROUND) # t = u-t
4126 : Math::MPFR::Rmpfr_add($t, $t, $u, $ROUND); # t = t+u
4127  
4128 Math::MPFR::Rmpfr_add_ui($u, $u, 2, $ROUND); # u = u+2
4129 Math::MPFR::Rmpfr_mul_2ui($u, $u, 1, $ROUND); # u = u*2
4130  
4131 Math::MPFR::Rmpfr_zero_p($u) && return $n; # `u` is zero
4132 Math::MPFR::Rmpfr_div($t, $t, $u, $ROUND); # t = t/u
4133 return $t;
4134 }
4135  
4136 Math_MPFR__Math_MPC: {
4137 $n = _mpfr2mpc($n);
4138 goto Math_MPC__Math_MPC;
4139 }
4140  
4141 Math_MPC__Math_MPFR: {
4142 $k = _mpfr2mpc($k);
4143 goto Math_MPC__Math_MPC;
4144 }
4145  
4146 Math_MPC__Math_MPC: {
4147 my $t = Math::MPC::Rmpc_init2($PREC);
4148 my $u = Math::MPC::Rmpc_init2($PREC);
4149  
4150 Math::MPC::Rmpc_sub_ui($u, $k, 2, $ROUND); # u = k-2
4151 Math::MPC::Rmpc_mul($t, $n, $u, $ROUND); # t = n*u
4152 Math::MPC::Rmpc_mul_2ui($t, $t, 3, $ROUND); # t = t*8
4153  
4154 Math::MPC::Rmpc_sub_ui($u, $u, 2, $ROUND); # u = u-2
4155 Math::MPC::Rmpc_sqr($u, $u, $ROUND); # u = u^2
4156 Math::MPC::Rmpc_add($t, $t, $u, $ROUND); # t = t+u
4157  
4158 Math::MPC::Rmpc_sqrt($t, $t, $ROUND); # t = sqrt(t)
4159 Math::MPC::Rmpc_sub_ui($u, $k, 4, $ROUND); # u = k-4
4160  
4161 $second
4162 ? Math::MPC::Rmpc_sub($t, $u, $t, $ROUND) # t = u-t
4163 : Math::MPC::Rmpc_add($t, $t, $u, $ROUND); # t = t+u
4164  
4165 Math::MPC::Rmpc_add_ui($u, $u, 2, $ROUND); # u = u+2
4166 Math::MPC::Rmpc_mul_2ui($u, $u, 1, $ROUND); # u = u*2
4167  
4168 if (Math::MPC::Rmpc_cmp_si($t, 0) == 0) { # `u` is zero
4169 return $n;
4170 }
4171  
4172 Math::MPC::Rmpc_div($t, $t, $u, $ROUND); # t = t/u
4173 return $t;
4174 }
4175 }
4176  
4177 sub polygonal_root ($$) {
4178 bless \__polygonal_root__(_star2mpfr_mpc($_[0]), _star2mpfr_mpc($_[1]));
4179 }
4180  
4181 #
4182 ## Second polygonal root
4183 #
4184  
4185 sub polygonal_root2 ($$) {
4186 bless \__polygonal_root__(_star2mpfr_mpc($_[0]), _star2mpfr_mpc($_[1]), 1);
4187 }
4188  
4189 #
4190 ## isqrt
4191 #
4192  
4193 sub isqrt ($) {
4194 my ($x) = @_;
4195  
4196 $x = _star2mpz($x) // goto &nan;
4197  
4198 Math::GMPz::Rmpz_sgn($x) < 0 and goto &nan;
4199 my $r = Math::GMPz::Rmpz_init();
4200 Math::GMPz::Rmpz_sqrt($r, $x);
4201 bless \$r;
4202 }
4203  
4204 #
4205 ## icbrt
4206 #
4207  
4208 sub icbrt ($) {
4209 my ($x) = @_;
4210  
4211 $x = _star2mpz($x) // goto &nan;
4212  
4213 my $r = Math::GMPz::Rmpz_init();
4214 Math::GMPz::Rmpz_root($r, $x, 3);
4215 bless \$r;
4216 }
4217  
4218 #
4219 ## IROOT
4220 #
4221  
4222 sub iroot ($$) {
4223 my ($x, $y) = @_;
4224  
4225 $x = _star2mpz($x) // goto &nan;
4226 $y = _star2si($y) // goto &nan;
4227  
4228 if ($y == 0) {
4229 Math::GMPz::Rmpz_sgn($x) || goto &zero; # 0^Inf = 0
4230  
4231 # 1^Inf = 1 ; (-1)^Inf = 1
4232 if (Math::GMPz::Rmpz_cmpabs_ui($x, 1) == 0) {
4233 goto &one;
4234 }
4235  
4236 goto &inf;
4237 }
4238  
4239 if ($y < 0) {
4240 my $sign = Math::GMPz::Rmpz_sgn($x)
4241 || goto &inf; # 1 / 0^k = Inf
4242  
4243 if ($sign < 0) {
4244 goto &nan;
4245 }
4246  
4247 if (Math::GMPz::Rmpz_cmp_ui($x, 1) == 0) { # 1 / 1^k = 1
4248 goto &one;
4249 }
4250  
4251 goto &zero;
4252 }
4253  
4254 if ($y % 2 == 0 and Math::GMPz::Rmpz_sgn($x) < 0) {
4255 goto &nan;
4256 }
4257  
4258 my $r = Math::GMPz::Rmpz_init();
4259 $y == 2
4260 ? Math::GMPz::Rmpz_sqrt($r, $x)
4261 : Math::GMPz::Rmpz_root($r, $x, $y);
4262 bless \$r;
4263 }
4264  
4265 #
4266 ## ISQRTREM
4267 #
4268  
4269 sub isqrtrem ($) {
4270 my ($x) = @_;
4271  
4272 $x = _star2mpz($x) // return (nan(), nan());
4273  
4274 Math::GMPz::Rmpz_sgn($x) < 0
4275 and return (nan(), nan());
4276  
4277 my $r = Math::GMPz::Rmpz_init();
4278 my $s = Math::GMPz::Rmpz_init();
4279  
4280 Math::GMPz::Rmpz_sqrtrem($r, $s, $x);
4281  
4282 ((bless \$r), (bless \$s));
4283 }
4284  
4285 #
4286 ## IROOTREM
4287 #
4288  
4289 sub irootrem ($$) {
4290 my ($x, $y) = @_;
4291  
4292 $x = _star2mpz($x) // return (nan(), nan());
4293 $y = _star2si($y) // return (nan(), nan());
4294  
4295 if ($y == 0) {
4296  
4297 # 0^Inf = 0
4298 if (Math::GMPz::Rmpz_sgn($x) == 0) {
4299 return (zero(), mone());
4300 }
4301  
4302 my $r = Math::GMPz::Rmpz_init();
4303 Math::GMPz::Rmpz_sub_ui($r, $x, 1);
4304  
4305 # 1^Inf = 1 ; (-1)^Inf = 1
4306 if (Math::GMPz::Rmpz_cmpabs_ui($x, 1) == 0) {
4307 return (one(), (bless \$r));
4308 }
4309  
4310 return (inf(), (bless \$r));
4311 }
4312  
4313 if ($y < 0) {
4314 my $sgn = Math::GMPz::Rmpz_sgn($x);
4315  
4316 # 1 / 0^k = Inf
4317 if ($sgn == 0) {
4318 return (inf(), zero());
4319 }
4320  
4321 # 1 / 1^k = 1
4322 if (Math::GMPz::Rmpz_cmp_ui($x, 1) == 0) {
4323 return (one(), zero());
4324 }
4325  
4326 # x is negative
4327 if ($sgn < 0) {
4328 return (nan(), nan());
4329 }
4330  
4331 return (zero(), ninf());
4332 }
4333  
4334 if ($y % 2 == 0 and Math::GMPz::Rmpz_sgn($x) < 0) {
4335 return (nan(), nan());
4336 }
4337  
4338 my $r = Math::GMPz::Rmpz_init();
4339 my $s = Math::GMPz::Rmpz_init();
4340  
4341 $y == 2
4342 ? Math::GMPz::Rmpz_sqrtrem($r, $s, $x)
4343 : Math::GMPz::Rmpz_rootrem($r, $s, $x, $y);
4344  
4345 ((bless \$r), (bless \$s));
4346 }
4347  
4348 #
4349 ## MOD
4350 #
4351  
4352 sub __mod__ {
4353 my ($x, $y) = @_;
4354 goto(join('__', ref($x), ref($y) || 'Scalar') =~ tr/:/_/rs);
4355  
4356 #
4357 ## GMPq
4358 #
4359 Math_GMPq__Math_GMPq: {
4360  
4361 if (Math::GMPq::Rmpq_integer_p($y)) {
4362 $y = _mpq2mpz($y);
4363 goto Math_GMPq__Math_GMPz;
4364 }
4365  
4366 Math::GMPq::Rmpq_sgn($y) || goto &_nan;
4367  
4368 my $quo = Math::GMPq::Rmpq_init();
4369 Math::GMPq::Rmpq_div($quo, $x, $y);
4370  
4371 # Floor
4372 Math::GMPq::Rmpq_integer_p($quo) || do {
4373 my $z = Math::GMPz::Rmpz_init();
4374 Math::GMPz::Rmpz_set_q($z, $quo);
4375 Math::GMPz::Rmpz_sub_ui($z, $z, 1) if Math::GMPq::Rmpq_sgn($quo) < 0;
4376 Math::GMPq::Rmpq_set_z($quo, $z);
4377 };
4378  
4379 Math::GMPq::Rmpq_mul($quo, $quo, $y);
4380 Math::GMPq::Rmpq_sub($quo, $x, $quo);
4381  
4382 return $quo;
4383 }
4384  
4385 Math_GMPq__Math_GMPz: {
4386 Math::GMPz::Rmpz_sgn($y) || goto &_nan;
4387 my $r = _modular_rational($x, $y) // do {
4388  
4389 my $quo = Math::GMPq::Rmpq_init();
4390 Math::GMPq::Rmpq_div_z($quo, $x, $y);
4391  
4392 # Floor
4393 Math::GMPq::Rmpq_integer_p($quo) || do {
4394 my $z = Math::GMPz::Rmpz_init();
4395 Math::GMPz::Rmpz_set_q($z, $quo);
4396 Math::GMPz::Rmpz_sub_ui($z, $z, 1) if Math::GMPq::Rmpq_sgn($quo) < 0;
4397 Math::GMPq::Rmpq_set_z($quo, $z);
4398 };
4399  
4400 Math::GMPq::Rmpq_mul_z($quo, $quo, $y);
4401 Math::GMPq::Rmpq_sub($quo, $x, $quo);
4402  
4403 return $quo;
4404 };
4405 Math::GMPz::Rmpz_mod($r, $r, $y);
4406 return $r;
4407 }
4408  
4409 Math_GMPq__Math_MPFR: {
4410 $x = _mpq2mpfr($x);
4411 goto Math_MPFR__Math_MPFR;
4412 }
4413  
4414 Math_GMPq__Math_MPC: {
4415 $x = _mpq2mpc($x);
4416 goto Math_MPC__Math_MPC;
4417 }
4418  
4419 #
4420 ## GMPz
4421 #
4422 Math_GMPz__Math_GMPz: {
4423  
4424 my $sgn_y = Math::GMPz::Rmpz_sgn($y) || goto &_nan;
4425  
4426 my $r = Math::GMPz::Rmpz_init();
4427 Math::GMPz::Rmpz_mod($r, $x, $y);
4428  
4429 if (!Math::GMPz::Rmpz_sgn($r)) {
4430 ## ok
4431 }
4432 elsif ($sgn_y < 0) {
4433 Math::GMPz::Rmpz_add($r, $r, $y);
4434 }
4435  
4436 return $r;
4437 }
4438  
4439 Math_GMPz__Scalar: {
4440 my $r = Math::GMPz::Rmpz_init();
4441 Math::GMPz::Rmpz_mod_ui($r, $x, $y);
4442 return $r;
4443 }
4444  
4445 Math_GMPz__Math_GMPq: {
4446 $x = _mpz2mpq($x);
4447 goto Math_GMPq__Math_GMPq;
4448 }
4449  
4450 Math_GMPz__Math_MPFR: {
4451 $x = _mpz2mpfr($x);
4452 goto Math_MPFR__Math_MPFR;
4453 }
4454  
4455 Math_GMPz__Math_MPC: {
4456 $x = _mpz2mpc($x);
4457 goto Math_MPC__Math_MPC;
4458 }
4459  
4460 #
4461 ## MPFR
4462 #
4463 Math_MPFR__Math_MPFR: {
4464  
4465 my $quo = Math::MPFR::Rmpfr_init2($PREC);
4466 Math::MPFR::Rmpfr_div($quo, $x, $y, $ROUND);
4467 Math::MPFR::Rmpfr_floor($quo, $quo);
4468 Math::MPFR::Rmpfr_mul($quo, $quo, $y, $ROUND);
4469 Math::MPFR::Rmpfr_sub($quo, $x, $quo, $ROUND);
4470  
4471 return $quo;
4472 }
4473  
4474 Math_MPFR__Scalar: {
4475  
4476 my $quo = Math::MPFR::Rmpfr_init2($PREC);
4477 Math::MPFR::Rmpfr_div_ui($quo, $x, $y, $ROUND);
4478 Math::MPFR::Rmpfr_floor($quo, $quo);
4479 Math::MPFR::Rmpfr_mul_ui($quo, $quo, $y, $ROUND);
4480 Math::MPFR::Rmpfr_sub($quo, $x, $quo, $ROUND);
4481  
4482 return $quo;
4483 }
4484  
4485 Math_MPFR__Math_GMPq: {
4486  
4487 my $quo = Math::MPFR::Rmpfr_init2($PREC);
4488 Math::MPFR::Rmpfr_div_q($quo, $x, $y, $ROUND);
4489 Math::MPFR::Rmpfr_floor($quo, $quo);
4490 Math::MPFR::Rmpfr_mul_q($quo, $quo, $y, $ROUND);
4491 Math::MPFR::Rmpfr_sub($quo, $x, $quo, $ROUND);
4492  
4493 return $quo;
4494 }
4495  
4496 Math_MPFR__Math_GMPz: {
4497  
4498 my $quo = Math::MPFR::Rmpfr_init2($PREC);
4499 Math::MPFR::Rmpfr_div_z($quo, $x, $y, $ROUND);
4500 Math::MPFR::Rmpfr_floor($quo, $quo);
4501 Math::MPFR::Rmpfr_mul_z($quo, $quo, $y, $ROUND);
4502 Math::MPFR::Rmpfr_sub($quo, $x, $quo, $ROUND);
4503  
4504 return $quo;
4505 }
4506  
4507 Math_MPFR__Math_MPC: {
4508 $x = _mpfr2mpc($x);
4509 goto Math_MPC__Math_MPC;
4510 }
4511  
4512 #
4513 ## MPC
4514 #
4515 Math_MPC__Math_MPC: {
4516  
4517 my $quo = Math::MPC::Rmpc_init2($PREC);
4518 Math::MPC::Rmpc_div($quo, $x, $y, $ROUND);
4519  
4520 my $real = Math::MPFR::Rmpfr_init2($PREC);
4521 my $imag = Math::MPFR::Rmpfr_init2($PREC);
4522  
4523 Math::MPC::RMPC_RE($real, $quo);
4524 Math::MPC::RMPC_IM($imag, $quo);
4525  
4526 Math::MPFR::Rmpfr_floor($real, $real);
4527 Math::MPFR::Rmpfr_floor($imag, $imag);
4528  
4529 Math::MPC::Rmpc_set_fr_fr($quo, $real, $imag, $ROUND);
4530  
4531 Math::MPC::Rmpc_mul($quo, $quo, $y, $ROUND);
4532 Math::MPC::Rmpc_sub($quo, $x, $quo, $ROUND);
4533  
4534 return $quo;
4535 }
4536  
4537 Math_MPC__Scalar: {
4538  
4539 my $quo = Math::MPC::Rmpc_init2($PREC);
4540 Math::MPC::Rmpc_div_ui($quo, $x, $y, $ROUND);
4541  
4542 my $real = Math::MPFR::Rmpfr_init2($PREC);
4543 my $imag = Math::MPFR::Rmpfr_init2($PREC);
4544  
4545 Math::MPC::RMPC_RE($real, $quo);
4546 Math::MPC::RMPC_IM($imag, $quo);
4547  
4548 Math::MPFR::Rmpfr_floor($real, $real);
4549 Math::MPFR::Rmpfr_floor($imag, $imag);
4550  
4551 Math::MPC::Rmpc_set_fr_fr($quo, $real, $imag, $ROUND);
4552  
4553 Math::MPC::Rmpc_mul_ui($quo, $quo, $y, $ROUND);
4554 Math::MPC::Rmpc_sub($quo, $x, $quo, $ROUND);
4555  
4556 return $quo;
4557 }
4558  
4559 Math_MPC__Math_MPFR: {
4560 $y = _mpfr2mpc($y);
4561 goto Math_MPC__Math_MPC;
4562 }
4563  
4564 Math_MPC__Math_GMPz: {
4565 $y = _mpz2mpc($y);
4566 goto Math_MPC__Math_MPC;
4567 }
4568  
4569 Math_MPC__Math_GMPq: {
4570 $y = _mpq2mpc($y);
4571 goto Math_MPC__Math_MPC;
4572 }
4573 }
4574  
4575 sub mod ($$) {
4576 my ($x, $y) = @_;
4577  
4578 $x =
4579 ref($x) eq __PACKAGE__ ? $$x
4580 : ref($x) ? _star2obj($x)
4581 : _str2obj($x);
4582  
4583 if (ref($y) eq __PACKAGE__) {
4584 return bless \__mod__($x, $$y);
4585 }
4586  
4587 if (!ref($y)) {
4588  
4589 if ( ref($x) ne 'Math::GMPq'
4590 and CORE::int($y) eq $y
4591 and $y > 0
4592 and $y < ULONG_MAX) {
4593 return bless \__mod__($x, $y);
4594 }
4595  
4596 return bless \__mod__($x, _str2obj($y));
4597 }
4598  
4599 bless \__mod__($x, _star2obj($y));
4600 }
4601  
4602 #
4603 ## IMOD
4604 #
4605  
4606 sub imod ($$) {
4607 my ($x, $y) = @_;
4608  
4609 $x = _star2mpz($x) // goto &nan;
4610  
4611 if (!ref($y) and CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
4612  
4613 CORE::int($y) || goto &nan;
4614  
4615 my $neg_y = $y < 0;
4616 $y = -$y if $neg_y;
4617  
4618 my $r = Math::GMPz::Rmpz_init();
4619 Math::GMPz::Rmpz_mod_ui($r, $x, $y);
4620  
4621 if (!Math::GMPz::Rmpz_sgn($r)) {
4622 ## OK
4623 }
4624 elsif ($neg_y) {
4625 Math::GMPz::Rmpz_sub_ui($r, $r, $y);
4626 }
4627  
4628 return bless \$r;
4629 }
4630  
4631 $y = _star2mpz($y) // goto &nan;
4632  
4633 my $sign_y = Math::GMPz::Rmpz_sgn($y) || goto &nan;
4634  
4635 my $r = Math::GMPz::Rmpz_init();
4636 Math::GMPz::Rmpz_mod($r, $x, $y);
4637  
4638 if (!Math::GMPz::Rmpz_sgn($r)) {
4639 ## OK
4640 }
4641 elsif ($sign_y < 0) {
4642 Math::GMPz::Rmpz_add($r, $r, $y);
4643 }
4644  
4645 bless \$r;
4646 }
4647  
4648 #
4649 ## POLYMOD
4650 #
4651  
4652 sub polymod {
4653 my @list = map { ref($_) eq __PACKAGE__ ? $$_ : _star2obj($_) } @_;
4654  
4655 my @r;
4656 my $x = shift(@list);
4657  
4658 foreach my $m (@list) {
4659 my $mod = __mod__($x, $m);
4660  
4661 $x = __sub__($x, $mod);
4662 $x = __div__($x, $m);
4663  
4664 push @r, $mod;
4665 }
4666  
4667 push @r, $x;
4668 map { bless \$_ } @r;
4669 }
4670  
4671 # Modular operations
4672  
4673 sub addmod ($$$) {
4674 my ($x, $y, $m) = @_;
4675  
4676 $x = _star2mpz($x) // goto &nan;
4677 $y = _star2mpz($y) // goto &nan;
4678 $m = _star2mpz($m) // goto &nan;
4679  
4680 my $r = Math::GMPz::Rmpz_init();
4681 Math::GMPz::Rmpz_add($r, $x, $y);
4682 Math::GMPz::Rmpz_mod($r, $r, $m);
4683 bless \$r;
4684 }
4685  
4686 sub submod ($$$) {
4687 my ($x, $y, $m) = @_;
4688  
4689 $x = _star2mpz($x) // goto &nan;
4690 $y = _star2mpz($y) // goto &nan;
4691 $m = _star2mpz($m) // goto &nan;
4692  
4693 my $r = Math::GMPz::Rmpz_init();
4694 Math::GMPz::Rmpz_sub($r, $x, $y);
4695 Math::GMPz::Rmpz_mod($r, $r, $m);
4696 bless \$r;
4697 }
4698  
4699 sub mulmod ($$$) {
4700 my ($x, $y, $m) = @_;
4701  
4702 $x = _star2mpz($x) // goto &nan;
4703 $y = _star2mpz($y) // goto &nan;
4704 $m = _star2mpz($m) // goto &nan;
4705  
4706 my $r = Math::GMPz::Rmpz_init();
4707 Math::GMPz::Rmpz_mul($r, $x, $y);
4708 Math::GMPz::Rmpz_mod($r, $r, $m);
4709 bless \$r;
4710 }
4711  
4712 #
4713 ## DIVMOD
4714 #
4715  
4716 sub divmod ($$;$) {
4717 my ($x, $y, $m) = @_;
4718  
4719 if (defined($m)) { # modular division
4720  
4721 $x = _star2mpz($x) // goto &nan;
4722 $y = _star2mpz($y) // goto &nan;
4723 $m = _star2mpz($m) // goto &nan;
4724  
4725 my $r = Math::GMPz::Rmpz_init();
4726  
4727 if (Math::GMPz::Rmpz_divisible_p($x, $y) and Math::GMPz::Rmpz_sgn($y)) {
4728 Math::GMPz::Rmpz_divexact($r, $x, $y);
4729 Math::GMPz::Rmpz_mod($r, $r, $m);
4730 }
4731 elsif (Math::GMPz::Rmpz_invert($r, $y, $m)) {
4732 Math::GMPz::Rmpz_mul($r, $r, $x);
4733 Math::GMPz::Rmpz_mod($r, $r, $m);
4734 }
4735 else {
4736 goto &nan;
4737 }
4738  
4739 return bless \$r;
4740 }
4741  
4742 $x = _star2mpz($x) // return (nan(), nan());
4743 $y = _star2mpz($y) // return (nan(), nan());
4744  
4745 Math::GMPz::Rmpz_sgn($y)
4746 || return (nan(), nan());
4747  
4748 my $r = Math::GMPz::Rmpz_init();
4749 my $s = Math::GMPz::Rmpz_init();
4750  
4751 Math::GMPz::Rmpz_divmod($r, $s, $x, $y);
4752  
4753 ((bless \$r), (bless \$s));
4754 }
4755  
4756 #
4757 ## is_div
4758 #
4759  
4760 sub is_div ($$) {
4761 my ($n, $k) = @_;
4762  
4763 if (ref($n) eq __PACKAGE__ and ref($$n) eq 'Math::GMPz') {
4764 if (ref($k)) {
4765 if (ref($k) eq __PACKAGE__ and ref($$k) eq 'Math::GMPz') {
4766 return (Math::GMPz::Rmpz_sgn($$k) && Math::GMPz::Rmpz_divisible_p($$n, $$k));
4767 }
4768 }
4769 elsif (CORE::int($k) eq $k and $k and $k < ULONG_MAX and $k > LONG_MIN) {
4770 return Math::GMPz::Rmpz_divisible_ui_p($$n, $k < 0 ? -$k : $k);
4771 }
4772 }
4773  
4774 @_ = (${mod($n, $k)}, 0);
4775 goto &__eq__;
4776 }
4777  
4778 #
4779 ## is_congruent
4780 #
4781  
4782 sub is_congruent ($$$) {
4783 my ($n, $k, $m) = @_;
4784  
4785 $n = $$n if (ref($n) eq __PACKAGE__);
4786 $k = $$k if (ref($k) eq __PACKAGE__);
4787 $m = $$m if (ref($m) eq __PACKAGE__);
4788  
4789 if (ref($n) eq 'Math::GMPz') {
4790  
4791 if ( !ref($k)
4792 and !ref($m)
4793 and CORE::int($k) eq $k
4794 and $k >= 0
4795 and $k < ULONG_MAX
4796 and CORE::int($m) eq $m
4797 and $m > 0
4798 and $m < ULONG_MAX) {
4799 return Math::GMPz::Rmpz_congruent_ui_p($n, $k, $m);
4800 }
4801  
4802 if (ref($k) eq 'Math::GMPz' and ref($m) eq 'Math::GMPz') {
4803 return (Math::GMPz::Rmpz_sgn($m) && Math::GMPz::Rmpz_congruent_p($n, $k, $m));
4804 }
4805 }
4806  
4807 $n = _star2obj($n) if !ref($n);
4808 $k = _star2obj($k) if !ref($k);
4809 $m = _star2obj($m) if !ref($m);
4810  
4811 if (ref($n) eq 'Math::GMPz' and ref($k) eq 'Math::GMPz' and ref($m) eq 'Math::GMPz') {
4812 return (Math::GMPz::Rmpz_sgn($m) && Math::GMPz::Rmpz_congruent_p($n, $k, $m));
4813 }
4814  
4815 @_ = (__mod__($n, $m), __mod__($k, $m));
4816 goto &__eq__;
4817 }
4818  
4819 #
4820 ## SPECIAL
4821 #
4822  
4823 #
4824 ## LOG
4825 #
4826  
4827 sub __log__ {
4828 my ($x) = @_;
4829 goto(ref($x) =~ tr/:/_/rs);
4830  
4831 Math_MPFR: {
4832  
4833 # Complex for x < 0
4834 if (Math::MPFR::Rmpfr_sgn($x) < 0) {
4835 my $r = _mpfr2mpc($x);
4836 Math::MPC::Rmpc_log($r, $r, $ROUND);
4837 return $r;
4838 }
4839  
4840 my $r = Math::MPFR::Rmpfr_init2($PREC);
4841 Math::MPFR::Rmpfr_log($r, $x, $ROUND);
4842 return $r;
4843 }
4844  
4845 Math_MPC: {
4846 my $r = Math::MPC::Rmpc_init2($PREC);
4847 Math::MPC::Rmpc_log($r, $x, $ROUND);
4848 return $r;
4849 }
4850  
4851 }
4852  
4853 #
4854 ## LOG_2
4855 #
4856  
4857 sub __log2__ {
4858 my ($x) = @_;
4859 goto(ref($x) =~ tr/:/_/rs);
4860  
4861 Math_MPFR: {
4862  
4863 # Complex for x < 0
4864 if (Math::MPFR::Rmpfr_sgn($x) < 0) {
4865 $x = _mpfr2mpc($x);
4866 goto Math_MPC;
4867 }
4868  
4869 my $r = Math::MPFR::Rmpfr_init2($PREC);
4870 Math::MPFR::Rmpfr_log2($r, $x, $ROUND);
4871 return $r;
4872 }
4873  
4874 Math_MPC: {
4875 state $MPC_VERSION = Math::MPC::MPC_VERSION();
4876  
4877 my $r = Math::MPC::Rmpc_init2($PREC);
4878  
4879 if ($MPC_VERSION >= 66560) { # available only in mpc>=1.4.0
4880 Math::MPC::Rmpc_log2($r, $x, $ROUND);
4881 }
4882 else {
4883 my $ln2 = Math::MPFR::Rmpfr_init2($PREC);
4884 Math::MPFR::Rmpfr_const_log2($ln2, $ROUND);
4885 Math::MPC::Rmpc_log($r, $x, $ROUND);
4886 Math::MPC::Rmpc_div_fr($r, $r, $ln2, $ROUND);
4887 }
4888 return $r;
4889 }
4890 }
4891  
4892 #
4893 ## LOG_10
4894 #
4895  
4896 sub __log10__ {
4897 my ($x) = @_;
4898 goto(ref($x) =~ tr/:/_/rs);
4899  
4900 Math_MPFR: {
4901  
4902 # Complex for x < 0
4903 if (Math::MPFR::Rmpfr_sgn($x) < 0) {
4904 $x = _mpfr2mpc($x);
4905 goto Math_MPC;
4906 }
4907  
4908 my $r = Math::MPFR::Rmpfr_init2($PREC);
4909 Math::MPFR::Rmpfr_log10($r, $x, $ROUND);
4910 return $r;
4911 }
4912  
4913 Math_MPC: {
4914 state $MPC_VERSION = Math::MPC::MPC_VERSION();
4915  
4916 my $r = Math::MPC::Rmpc_init2($PREC);
4917  
4918 if ($MPC_VERSION >= 65536) { # available only in mpc>=1.0.0
4919 Math::MPC::Rmpc_log10($r, $x, $ROUND);
4920 }
4921 else {
4922 my $ln10 = Math::MPFR::Rmpfr_init2($PREC);
4923 Math::MPFR::Rmpfr_set_ui($ln10, 10, $ROUND);
4924 Math::MPFR::Rmpfr_log($ln10, $ln10, $ROUND);
4925 Math::MPC::Rmpc_log($r, $x, $ROUND);
4926 Math::MPC::Rmpc_div_fr($r, $r, $ln10, $ROUND);
4927 }
4928  
4929 return $r;
4930 }
4931 }
4932  
4933 sub ln { # used in overloading
4934 bless \__log__(_star2mpfr_mpc($_[0]));
4935 }
4936  
4937 sub log2 ($) {
4938 bless \__log2__(_star2mpfr_mpc($_[0]));
4939 }
4940  
4941 sub log10 ($) {
4942 bless \__log10__(_star2mpfr_mpc($_[0]));
4943 }
4944  
4945 sub log (_;$) {
4946 my ($x, $y) = @_;
4947  
4948 if (!defined($y)) {
4949 return bless \__log__(_star2mpfr_mpc($x));
4950 }
4951  
4952 bless \__div__(__log__(_star2mpfr_mpc($x)), __log__(_star2mpfr_mpc($y)));
4953 }
4954  
4955 #
4956 ## Integer logarithm to a given base
4957 #
4958  
4959 sub __ilog__ {
4960 my ($x, $y) = @_;
4961  
4962 if (ref($y) eq 'Math::GMPz' and Math::GMPz::Rmpz_fits_ulong_p($y)) {
4963 $y = Math::GMPz::Rmpz_get_ui($y);
4964 }
4965  
4966 # ilog(x, y <= 1) = NaN
4967 $y <= 1 and return;
4968  
4969 # ilog(x <= 0, y) = NaN
4970 Math::GMPz::Rmpz_sgn($x) <= 0 and return;
4971  
4972 # ilog(x,y) = 0, when y > x
4973 (ref($y) ? Math::GMPz::Rmpz_cmp($x, $y) : Math::GMPz::Rmpz_cmp_ui($x, $y)) >= 0
4974 or return 0;
4975  
4976 # Return faster for y <= 62
4977 if ($y <= 62) {
4978  
4979 $y = Math::GMPz::Rmpz_get_ui($y) if ref($y);
4980  
4981 my $e = (Math::GMPz::Rmpz_sizeinbase($x, $y) || return) - 1;
4982  
4983 if ($e > 0) {
4984 state $t = Math::GMPz::Rmpz_init_nobless();
4985 Math::GMPz::Rmpz_ui_pow_ui($t, $y, $e);
4986 Math::GMPz::Rmpz_cmp($t, $x) > 0 and --$e;
4987 }
4988  
4989 return $e;
4990 }
4991  
4992 # Make sure `y` is a Math::GMPz object
4993 $y = Math::GMPz::Rmpz_init_set_ui($y) if !ref($y);
4994  
4995 my $e = 0;
4996  
4997 state $t = Math::GMPz::Rmpz_init_nobless();
4998 state $round_z = Math::MPFR::MPFR_RNDZ();
4999 state $logx = Math::MPFR::Rmpfr_init2_nobless(64);
5000 state $logy = Math::MPFR::Rmpfr_init2_nobless(64);
5001  
5002 Math::MPFR::Rmpfr_set_z($logx, $x, $round_z);
5003 Math::MPFR::Rmpfr_set_z($logy, $y, $round_z);
5004  
5005 Math::MPFR::Rmpfr_log($logx, $logx, $round_z);
5006 Math::MPFR::Rmpfr_log($logy, $logy, $round_z);
5007  
5008 Math::MPFR::Rmpfr_div($logx, $logx, $logy, $round_z);
5009  
5010 if (Math::MPFR::Rmpfr_fits_ulong_p($logx, $round_z)) {
5011 $e = Math::MPFR::Rmpfr_get_ui($logx, $round_z) - 1;
5012 Math::GMPz::Rmpz_pow_ui($t, $y, $e + 1);
5013 }
5014 else {
5015 Math::GMPz::Rmpz_set($t, $y);
5016 }
5017  
5018 for (; Math::GMPz::Rmpz_cmp($t, $x) <= 0 ; Math::GMPz::Rmpz_mul($t, $t, $y)) {
5019 ++$e;
5020 }
5021  
5022 return $e;
5023 }
5024  
5025 sub ilog2 ($) {
5026 my ($x) = @_;
5027 $x = _star2mpz($x) // goto &nan;
5028 bless \Math::GMPz::Rmpz_init_set_ui(__ilog__($x, 2) // goto &nan);
5029 }
5030  
5031 sub ilog10 ($) {
5032 my ($x) = @_;
5033 $x = _star2mpz($x) // goto &nan;
5034 bless \Math::GMPz::Rmpz_init_set_ui(__ilog__($x, 10) // goto &nan);
5035 }
5036  
5037 sub ilog ($;$) {
5038 my ($x, $y) = @_;
5039  
5040 if (!defined($y)) {
5041 return bless \(_any2mpz(__log__(_star2mpfr_mpc($x))) // goto &nan);
5042 }
5043  
5044 $x = _star2mpz($x) // goto &nan;
5045  
5046 if (!ref($y) and CORE::int($y) eq $y and $y > 1 and $y < ULONG_MAX) {
5047 ## y is a native integer -- OK
5048 }
5049 else {
5050 $y = _star2mpz($y) // goto &nan;
5051 }
5052  
5053 bless \Math::GMPz::Rmpz_init_set_ui(__ilog__($x, $y) // goto &nan);
5054 }
5055  
5056 sub length ($;$) {
5057 my ($x, $y) = @_;
5058  
5059 $x = _star2mpz($x) // return undef;
5060  
5061 my $neg = ((Math::GMPz::Rmpz_sgn($x) || return 1) < 0) ? 1 : 0;
5062  
5063 if (defined($y)) {
5064 if (!ref($y) and CORE::int($y) eq $y and $y > 1 and $y < ULONG_MAX) {
5065 ## y is a native integer -- OK
5066 }
5067 else {
5068 $y = _star2mpz($y) // return undef;
5069 }
5070 }
5071 else {
5072 $y = 10;
5073 }
5074  
5075 if ($neg) {
5076 $x = Math::GMPz::Rmpz_init_set($x);
5077 Math::GMPz::Rmpz_abs($x, $x);
5078 }
5079  
5080 1 + (__ilog__($x, $y) // return 0);
5081 }
5082  
5083 #
5084 ## Square root
5085 #
5086  
5087 sub __sqrt__ {
5088 my ($x) = @_;
5089 goto(ref($x) =~ tr/:/_/rs);
5090  
5091 Math_MPFR: {
5092  
5093 # Complex for x < 0
5094 if (Math::MPFR::Rmpfr_sgn($x) < 0) {
5095 my $r = _mpfr2mpc($x);
5096 Math::MPC::Rmpc_sqrt($r, $r, $ROUND);
5097 return $r;
5098 }
5099  
5100 my $r = Math::MPFR::Rmpfr_init2($PREC);
5101 Math::MPFR::Rmpfr_sqrt($r, $x, $ROUND);
5102 return $r;
5103 }
5104  
5105 Math_MPC: {
5106 my $r = Math::MPC::Rmpc_init2($PREC);
5107 Math::MPC::Rmpc_sqrt($r, $x, $ROUND);
5108 return $r;
5109 }
5110 }
5111  
5112 sub sqrt { # used in overloading
5113 bless \__sqrt__(_star2mpfr_mpc($_[0]));
5114 }
5115  
5116 #
5117 ## Cube root
5118 #
5119  
5120 sub __cbrt__ {
5121 my ($x) = @_;
5122 goto(ref($x) =~ tr/:/_/rs);
5123  
5124 Math_MPFR: {
5125  
5126 # Complex for x < 0
5127 if (Math::MPFR::Rmpfr_sgn($x) < 0) {
5128 $x = _mpfr2mpc($x);
5129 goto Math_MPC;
5130 }
5131  
5132 my $r = Math::MPFR::Rmpfr_init2($PREC);
5133 Math::MPFR::Rmpfr_cbrt($r, $x, $ROUND);
5134 return $r;
5135 }
5136  
5137 Math_MPC: {
5138 my $three_inv = Math::MPFR::Rmpfr_init2($PREC);
5139 Math::MPFR::Rmpfr_set_ui($three_inv, 3, $ROUND);
5140 Math::MPFR::Rmpfr_ui_div($three_inv, 1, $three_inv, $ROUND);
5141  
5142 my $r = Math::MPC::Rmpc_init2($PREC);
5143 Math::MPC::Rmpc_pow_fr($r, $x, $three_inv, $ROUND);
5144 return $r;
5145 }
5146 }
5147  
5148 sub cbrt ($) {
5149 bless \__cbrt__(_star2mpfr_mpc($_[0]));
5150 }
5151  
5152 #
5153 ## Square (x^2)
5154 #
5155  
5156 sub sqr ($) {
5157 my ($x) = @_;
5158  
5159 $x =
5160 ref($x) eq __PACKAGE__
5161 ? $$x
5162 : _star2obj($x);
5163  
5164 bless \__mul__($x, $x);
5165 }
5166  
5167 #
5168 ## Normalized value: norm(a + b*i) = a^2 + b^2
5169 #
5170  
5171 sub __norm__ {
5172 my ($x) = @_;
5173 goto(ref($x) =~ tr/:/_/rs);
5174  
5175 Math_MPC: {
5176 my $r = Math::MPFR::Rmpfr_init2($PREC);
5177 Math::MPC::Rmpc_norm($r, $x, $ROUND);
5178 return $r;
5179 }
5180  
5181 Math_MPFR: {
5182 my $r = Math::MPFR::Rmpfr_init2($PREC);
5183 Math::MPFR::Rmpfr_sqr($r, $x, $ROUND);
5184 return $r;
5185 }
5186  
5187 Math_GMPz: {
5188 my $r = Math::GMPz::Rmpz_init();
5189 Math::GMPz::Rmpz_mul($r, $x, $x);
5190 return $r;
5191 }
5192  
5193 Math_GMPq: {
5194 my $r = Math::GMPq::Rmpq_init();
5195 Math::GMPq::Rmpq_mul($r, $x, $x);
5196 return $r;
5197 }
5198 }
5199  
5200 sub norm ($) {
5201 my ($x) = @_;
5202  
5203 $x =
5204 ref($x) eq __PACKAGE__
5205 ? $$x
5206 : _star2obj($x);
5207  
5208 bless \__norm__($x);
5209 }
5210  
5211 #
5212 ## Natural exponentiation function (e^x)
5213 #
5214  
5215 sub __exp__ {
5216 my ($x) = @_;
5217 goto(ref($x) =~ tr/:/_/rs);
5218  
5219 Math_MPFR: {
5220 my $r = Math::MPFR::Rmpfr_init2($PREC);
5221 Math::MPFR::Rmpfr_exp($r, $x, $ROUND);
5222 return $r;
5223 }
5224  
5225 Math_MPC: {
5226 my $r = Math::MPC::Rmpc_init2($PREC);
5227 Math::MPC::Rmpc_exp($r, $x, $ROUND);
5228 return $r;
5229 }
5230 }
5231  
5232 sub exp { # used in overloading
5233 bless \__exp__(_star2mpfr_mpc($_[0]));
5234 }
5235  
5236 sub exp2 ($) {
5237 my ($x) = @_;
5238  
5239 state $base = Math::GMPz::Rmpz_init_set_ui(2);
5240  
5241 if (ref($x) eq __PACKAGE__) {
5242 bless \__pow__($base, $$x);
5243 }
5244 elsif (!ref($x) and CORE::int($x) eq $x and $x > LONG_MIN and $x < ULONG_MAX) {
5245 bless \__pow__($base, $x);
5246 }
5247 else {
5248 bless \__pow__($base, _star2obj($x));
5249 }
5250 }
5251  
5252 sub exp10 ($) {
5253 my ($x) = @_;
5254  
5255 state $base = Math::GMPz::Rmpz_init_set_ui(10);
5256  
5257 if (ref($x) eq __PACKAGE__) {
5258 bless \__pow__($base, $$x);
5259 }
5260 elsif (!ref($x) and CORE::int($x) eq $x and $x > LONG_MIN and $x < ULONG_MAX) {
5261 bless \__pow__($base, $x);
5262 }
5263 else {
5264 bless \__pow__($base, _star2obj($x));
5265 }
5266 }
5267  
5268 #
5269 ## floor(x) function -- round towards -Infinity
5270 #
5271  
5272 sub __floor__ {
5273 my ($x) = @_;
5274 goto(ref($x) =~ tr/:/_/rs);
5275  
5276 Math_MPFR: {
5277 my $r = Math::MPFR::Rmpfr_init2($PREC);
5278 Math::MPFR::Rmpfr_floor($r, $x);
5279 return $r;
5280 }
5281  
5282 Math_GMPq: {
5283 my $z = Math::GMPz::Rmpz_init();
5284 Math::GMPz::Rmpz_set_q($z, $x);
5285 Math::GMPq::Rmpq_integer_p($x) && return $z;
5286 Math::GMPz::Rmpz_sub_ui($z, $z, 1) if Math::GMPq::Rmpq_sgn($x) < 0;
5287 return $z;
5288 }
5289  
5290 Math_MPC: {
5291  
5292 my $real = Math::MPFR::Rmpfr_init2($PREC);
5293 my $imag = Math::MPFR::Rmpfr_init2($PREC);
5294  
5295 Math::MPC::RMPC_RE($real, $x);
5296 Math::MPC::RMPC_IM($imag, $x);
5297  
5298 Math::MPFR::Rmpfr_floor($real, $real);
5299 Math::MPFR::Rmpfr_floor($imag, $imag);
5300  
5301 if (Math::MPFR::Rmpfr_zero_p($imag)) {
5302 return $real;
5303 }
5304  
5305 my $r = Math::MPC::Rmpc_init2($PREC);
5306 Math::MPC::Rmpc_set_fr_fr($r, $real, $imag, $ROUND);
5307 return $r;
5308 }
5309 }
5310  
5311 sub floor ($) {
5312 my ($x) = @_;
5313  
5314 if (ref($x) ne __PACKAGE__) {
5315 $x = __PACKAGE__->new($x);
5316 }
5317  
5318 ref($$x) eq 'Math::GMPz' and return $x; # already an integer
5319 bless \__floor__($$x);
5320 }
5321  
5322 #
5323 ## ceil(x) function -- round towards +Infinity
5324 #
5325  
5326 sub __ceil__ {
5327 my ($x) = @_;
5328 goto(ref($x) =~ tr/:/_/rs);
5329  
5330 Math_MPFR: {
5331 my $r = Math::MPFR::Rmpfr_init2($PREC);
5332 Math::MPFR::Rmpfr_ceil($r, $x);
5333 return $r;
5334 }
5335  
5336 Math_GMPq: {
5337 my $z = Math::GMPz::Rmpz_init();
5338 Math::GMPz::Rmpz_set_q($z, $x);
5339 Math::GMPq::Rmpq_integer_p($x) && return $z;
5340 Math::GMPz::Rmpz_add_ui($z, $z, 1) if Math::GMPq::Rmpq_sgn($x) > 0;
5341 return $z;
5342 }
5343  
5344 Math_MPC: {
5345  
5346 my $real = Math::MPFR::Rmpfr_init2($PREC);
5347 my $imag = Math::MPFR::Rmpfr_init2($PREC);
5348  
5349 Math::MPC::RMPC_RE($real, $x);
5350 Math::MPC::RMPC_IM($imag, $x);
5351  
5352 Math::MPFR::Rmpfr_ceil($real, $real);
5353 Math::MPFR::Rmpfr_ceil($imag, $imag);
5354  
5355 if (Math::MPFR::Rmpfr_zero_p($imag)) {
5356 return $real;
5357 }
5358  
5359 my $r = Math::MPC::Rmpc_init2($PREC);
5360 Math::MPC::Rmpc_set_fr_fr($r, $real, $imag, $ROUND);
5361 return $r;
5362 }
5363 }
5364  
5365 sub ceil ($) {
5366 my ($x) = @_;
5367  
5368 if (ref($x) ne __PACKAGE__) {
5369 $x = __PACKAGE__->new($x);
5370 }
5371  
5372 ref($$x) eq 'Math::GMPz' and return $x; # already an integer
5373 bless \__ceil__($$x);
5374 }
5375  
5376 #
5377 ## sin / sinh / asin / asinh
5378 #
5379  
5380 sub __sin__ {
5381 my ($x) = @_;
5382 goto(ref($x) =~ tr/:/_/rs);
5383  
5384 Math_MPFR: {
5385 my $r = Math::MPFR::Rmpfr_init2($PREC);
5386 Math::MPFR::Rmpfr_sin($r, $x, $ROUND);
5387 return $r;
5388 }
5389  
5390 Math_MPC: {
5391 my $r = Math::MPC::Rmpc_init2($PREC);
5392 Math::MPC::Rmpc_sin($r, $x, $ROUND);
5393 return $r;
5394 }
5395 }
5396  
5397 sub sin { # used in overloading
5398 bless \__sin__(_star2mpfr_mpc($_[0]));
5399 }
5400  
5401 sub __sinh__ {
5402 my ($x) = @_;
5403 goto(ref($x) =~ tr/:/_/rs);
5404  
5405 Math_MPFR: {
5406 my $r = Math::MPFR::Rmpfr_init2($PREC);
5407 Math::MPFR::Rmpfr_sinh($r, $x, $ROUND);
5408 return $r;
5409 }
5410  
5411 Math_MPC: {
5412 my $r = Math::MPC::Rmpc_init2($PREC);
5413 Math::MPC::Rmpc_sinh($r, $x, $ROUND);
5414 return $r;
5415 }
5416 }
5417  
5418 sub sinh ($) {
5419 bless \__sinh__(_star2mpfr_mpc($_[0]));
5420 }
5421  
5422 sub __asin__ {
5423 my ($x) = @_;
5424 goto(ref($x) =~ tr/:/_/rs);
5425  
5426 Math_MPFR: {
5427  
5428 # Return a complex number for x < -1 or x > 1
5429 if ( Math::MPFR::Rmpfr_cmp_ui($x, 1) > 0
5430 or Math::MPFR::Rmpfr_cmp_si($x, -1) < 0) {
5431 my $r = _mpfr2mpc($x);
5432 Math::MPC::Rmpc_asin($r, $r, $ROUND);
5433 return $r;
5434 }
5435  
5436 my $r = Math::MPFR::Rmpfr_init2($PREC);
5437 Math::MPFR::Rmpfr_asin($r, $x, $ROUND);
5438 return $r;
5439 }
5440  
5441 Math_MPC: {
5442 my $r = Math::MPC::Rmpc_init2($PREC);
5443 Math::MPC::Rmpc_asin($r, $x, $ROUND);
5444 return $r;
5445 }
5446 }
5447  
5448 sub asin ($) {
5449 bless \__asin__(_star2mpfr_mpc($_[0]));
5450 }
5451  
5452 sub __asinh__ {
5453 my ($x) = @_;
5454 goto(ref($x) =~ tr/:/_/rs);
5455  
5456 Math_MPFR: {
5457 my $r = Math::MPFR::Rmpfr_init2($PREC);
5458 Math::MPFR::Rmpfr_asinh($r, $x, $ROUND);
5459 return $r;
5460 }
5461  
5462 Math_MPC: {
5463 my $r = Math::MPC::Rmpc_init2($PREC);
5464 Math::MPC::Rmpc_asinh($r, $x, $ROUND);
5465 return $r;
5466 }
5467 }
5468  
5469 sub asinh ($) {
5470 bless \__asinh__(_star2mpfr_mpc($_[0]));
5471 }
5472  
5473 #
5474 ## cos / cosh / acos / acosh
5475 #
5476  
5477 sub __cos__ {
5478 my ($x) = @_;
5479 goto(ref($x) =~ tr/:/_/rs);
5480  
5481 Math_MPFR: {
5482 my $r = Math::MPFR::Rmpfr_init2($PREC);
5483 Math::MPFR::Rmpfr_cos($r, $x, $ROUND);
5484 return $r;
5485 }
5486  
5487 Math_MPC: {
5488 my $r = Math::MPC::Rmpc_init2($PREC);
5489 Math::MPC::Rmpc_cos($r, $x, $ROUND);
5490 return $r;
5491 }
5492 }
5493  
5494 sub cos { # used in overloading
5495 bless \__cos__(_star2mpfr_mpc($_[0]));
5496 }
5497  
5498 sub __cosh__ {
5499 my ($x) = @_;
5500 goto(ref($x) =~ tr/:/_/rs);
5501  
5502 Math_MPFR: {
5503 my $r = Math::MPFR::Rmpfr_init2($PREC);
5504 Math::MPFR::Rmpfr_cosh($r, $x, $ROUND);
5505 return $r;
5506 }
5507  
5508 Math_MPC: {
5509 my $r = Math::MPC::Rmpc_init2($PREC);
5510 Math::MPC::Rmpc_cosh($r, $x, $ROUND);
5511 return $r;
5512 }
5513 }
5514  
5515 sub cosh ($) {
5516 bless \__cosh__(_star2mpfr_mpc($_[0]));
5517 }
5518  
5519 sub __acos__ {
5520 my ($x) = @_;
5521 goto(ref($x) =~ tr/:/_/rs);
5522  
5523 Math_MPFR: {
5524  
5525 # Return a complex number for x < -1 or x > 1
5526 if ( Math::MPFR::Rmpfr_cmp_ui($x, 1) > 0
5527 or Math::MPFR::Rmpfr_cmp_si($x, -1) < 0) {
5528 my $r = _mpfr2mpc($x);
5529 Math::MPC::Rmpc_acos($r, $r, $ROUND);
5530 return $r;
5531 }
5532  
5533 my $r = Math::MPFR::Rmpfr_init2($PREC);
5534 Math::MPFR::Rmpfr_acos($r, $x, $ROUND);
5535 return $r;
5536 }
5537  
5538 Math_MPC: {
5539 my $r = Math::MPC::Rmpc_init2($PREC);
5540 Math::MPC::Rmpc_acos($r, $x, $ROUND);
5541 return $r;
5542 }
5543 }
5544  
5545 sub acos ($) {
5546 bless \__acos__(_star2mpfr_mpc($_[0]));
5547 }
5548  
5549 sub __acosh__ {
5550 my ($x) = @_;
5551 goto(ref($x) =~ tr/:/_/rs);
5552  
5553 Math_MPFR: {
5554  
5555 # Return a complex number for x < 1
5556 if (Math::MPFR::Rmpfr_cmp_ui($x, 1) < 0) {
5557 my $r = _mpfr2mpc($x);
5558 Math::MPC::Rmpc_acosh($r, $r, $ROUND);
5559 return $r;
5560 }
5561  
5562 my $r = Math::MPFR::Rmpfr_init2($PREC);
5563 Math::MPFR::Rmpfr_acosh($r, $x, $ROUND);
5564 return $r;
5565 }
5566  
5567 Math_MPC: {
5568 my $r = Math::MPC::Rmpc_init2($PREC);
5569 Math::MPC::Rmpc_acosh($r, $x, $ROUND);
5570 return $r;
5571 }
5572 }
5573  
5574 sub acosh ($) {
5575 bless \__acosh__(_star2mpfr_mpc($_[0]));
5576 }
5577  
5578 #
5579 ## tan / tanh / atan / atanh
5580 #
5581  
5582 sub __tan__ {
5583 my ($x) = @_;
5584 goto(ref($x) =~ tr/:/_/rs);
5585  
5586 Math_MPFR: {
5587 my $r = Math::MPFR::Rmpfr_init2($PREC);
5588 Math::MPFR::Rmpfr_tan($r, $x, $ROUND);
5589 return $r;
5590 }
5591  
5592 Math_MPC: {
5593 my $r = Math::MPC::Rmpc_init2($PREC);
5594 Math::MPC::Rmpc_tan($r, $x, $ROUND);
5595 return $r;
5596 }
5597 }
5598  
5599 sub tan ($) {
5600 bless \__tan__(_star2mpfr_mpc($_[0]));
5601 }
5602  
5603 sub __tanh__ {
5604 my ($x) = @_;
5605 goto(ref($x) =~ tr/:/_/rs);
5606  
5607 Math_MPFR: {
5608 my $r = Math::MPFR::Rmpfr_init2($PREC);
5609 Math::MPFR::Rmpfr_tanh($r, $x, $ROUND);
5610 return $r;
5611 }
5612  
5613 Math_MPC: {
5614 my $r = Math::MPC::Rmpc_init2($PREC);
5615 Math::MPC::Rmpc_tanh($r, $x, $ROUND);
5616 return $r;
5617 }
5618 }
5619  
5620 sub tanh ($) {
5621 bless \__tanh__(_star2mpfr_mpc($_[0]));
5622 }
5623  
5624 sub __atan__ {
5625 my ($x) = @_;
5626 goto(ref($x) =~ tr/:/_/rs);
5627  
5628 Math_MPFR: {
5629 my $r = Math::MPFR::Rmpfr_init2($PREC);
5630 Math::MPFR::Rmpfr_atan($r, $x, $ROUND);
5631 return $r;
5632 }
5633  
5634 Math_MPC: {
5635 my $r = Math::MPC::Rmpc_init2($PREC);
5636 Math::MPC::Rmpc_atan($r, $x, $ROUND);
5637 return $r;
5638 }
5639 }
5640  
5641 sub atan ($) {
5642 bless \__atan__(_star2mpfr_mpc($_[0]));
5643 }
5644  
5645 sub __atanh__ {
5646 my ($x) = @_;
5647 goto(ref($x) =~ tr/:/_/rs);
5648  
5649 Math_MPFR: {
5650  
5651 # Return a complex number for x < -1 or x > 1
5652 if ( Math::MPFR::Rmpfr_cmp_ui($x, +1) > 0
5653 or Math::MPFR::Rmpfr_cmp_si($x, -1) < 0) {
5654 my $r = _mpfr2mpc($x);
5655 Math::MPC::Rmpc_atanh($r, $r, $ROUND);
5656 return $r;
5657 }
5658  
5659 my $r = Math::MPFR::Rmpfr_init2($PREC);
5660 Math::MPFR::Rmpfr_atanh($r, $x, $ROUND);
5661 return $r;
5662 }
5663  
5664 Math_MPC: {
5665 my $r = Math::MPC::Rmpc_init2($PREC);
5666 Math::MPC::Rmpc_atanh($r, $x, $ROUND);
5667 return $r;
5668 }
5669 }
5670  
5671 sub atanh ($) {
5672 bless \__atanh__(_star2mpfr_mpc($_[0]));
5673 }
5674  
5675 sub __atan2__ {
5676 my ($x, $y) = @_;
5677 goto(join('__', ref($x), ref($y)) =~ tr/:/_/rs);
5678  
5679 Math_MPFR__Math_MPFR: {
5680 my $r = Math::MPFR::Rmpfr_init2($PREC);
5681 Math::MPFR::Rmpfr_atan2($r, $x, $y, $ROUND);
5682 return $r;
5683 }
5684  
5685 Math_MPFR__Math_MPC: {
5686 $x = _mpfr2mpc($x);
5687 goto Math_MPC__Math_MPC;
5688 }
5689  
5690 Math_MPC__Math_MPFR: {
5691 $y = _mpfr2mpc($y);
5692 goto Math_MPC__Math_MPC;
5693 }
5694  
5695 #
5696 ## atan2(x, y) = -i * log((y + x*i) / sqrt(x^2 + y^2))
5697 #
5698 Math_MPC__Math_MPC: {
5699 my $r = Math::MPC::Rmpc_init2($PREC);
5700  
5701 Math::MPC::Rmpc_mul_i($r, $x, 1, $ROUND);
5702 Math::MPC::Rmpc_add($r, $r, $y, $ROUND);
5703  
5704 my $t1 = Math::MPC::Rmpc_init2($PREC);
5705 my $t2 = Math::MPC::Rmpc_init2($PREC);
5706  
5707 Math::MPC::Rmpc_sqr($t1, $x, $ROUND);
5708 Math::MPC::Rmpc_sqr($t2, $y, $ROUND);
5709 Math::MPC::Rmpc_add($t1, $t1, $t2, $ROUND);
5710 Math::MPC::Rmpc_sqrt($t1, $t1, $ROUND);
5711  
5712 Math::MPC::Rmpc_div($r, $r, $t1, $ROUND);
5713 Math::MPC::Rmpc_log($r, $r, $ROUND);
5714 Math::MPC::Rmpc_mul_i($r, $r, -1, $ROUND);
5715  
5716 return $r;
5717 }
5718 }
5719  
5720 sub atan2 ($$) {
5721 bless \__atan2__(_star2mpfr_mpc($_[0]), _star2mpfr_mpc($_[1]));
5722 }
5723  
5724 #
5725 ## sec / sech / asec / asech
5726 #
5727  
5728 sub __sec__ {
5729 my ($x) = @_;
5730 goto(ref($x) =~ tr/:/_/rs);
5731  
5732 Math_MPFR: {
5733 my $r = Math::MPFR::Rmpfr_init2($PREC);
5734 Math::MPFR::Rmpfr_sec($r, $x, $ROUND);
5735 return $r;
5736 }
5737  
5738 # sec(x) = 1/cos(x)
5739 Math_MPC: {
5740 my $r = Math::MPC::Rmpc_init2(CORE::int($PREC));
5741 my $t = Math::MPC::Rmpc_init2(CORE::int($PREC));
5742 Math::MPC::Rmpc_cos($r, $x, $ROUND);
5743 Math::MPC::Rmpc_set_ui($t, 1, $ROUND);
5744 Math::MPC::Rmpc_div($r, $t, $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(CORE::int($PREC));
5766 my $t = Math::MPC::Rmpc_init2(CORE::int($PREC));
5767 Math::MPC::Rmpc_cosh($r, $x, $ROUND);
5768 Math::MPC::Rmpc_set_ui($t, 1, $ROUND);
5769 Math::MPC::Rmpc_div($r, $t, $r, $ROUND);
5770 return $r;
5771 }
5772 }
5773  
5774 sub sech ($) {
5775 bless \__sech__(_star2mpfr_mpc($_[0]));
5776 }
5777  
5778 sub __asec__ {
5779 my ($x) = @_;
5780 goto(ref($x) =~ tr/:/_/rs);
5781  
5782 # asec(x) = acos(1/x)
5783 Math_MPFR: {
5784  
5785 # Return a complex number for x > -1 and x < 1
5786 if ( Math::MPFR::Rmpfr_cmp_ui($x, 1) < 0
5787 and Math::MPFR::Rmpfr_cmp_si($x, -1) > 0) {
5788 $x = _mpfr2mpc($x);
5789 goto Math_MPC;
5790 }
5791  
5792 my $r = Math::MPFR::Rmpfr_init2($PREC);
5793 Math::MPFR::Rmpfr_ui_div($r, 1, $x, $ROUND);
5794 Math::MPFR::Rmpfr_acos($r, $r, $ROUND);
5795 return $r;
5796 }
5797  
5798 # asec(x) = acos(1/x)
5799 Math_MPC: {
5800 my $r = Math::MPC::Rmpc_init2(CORE::int($PREC));
5801 Math::MPC::Rmpc_set_ui($r, 1, $ROUND);
5802 Math::MPC::Rmpc_div($r, $r, $x, $ROUND);
5803 Math::MPC::Rmpc_acos($r, $r, $ROUND);
5804 return $r;
5805 }
5806 }
5807  
5808 sub asec ($) {
5809 bless \__asec__(_star2mpfr_mpc($_[0]));
5810 }
5811  
5812 sub __asech__ {
5813 my ($x) = @_;
5814 goto(ref($x) =~ tr/:/_/rs);
5815  
5816 # asech(x) = acosh(1/x)
5817 Math_MPFR: {
5818  
5819 # Return a complex number for x < 0 or x > 1
5820 if ( Math::MPFR::Rmpfr_cmp_ui($x, 1) > 0
5821 or Math::MPFR::Rmpfr_cmp_ui($x, 0) < 0) {
5822 $x = _mpfr2mpc($x);
5823 goto Math_MPC;
5824 }
5825  
5826 my $r = Math::MPFR::Rmpfr_init2($PREC);
5827 Math::MPFR::Rmpfr_ui_div($r, 1, $x, $ROUND);
5828 Math::MPFR::Rmpfr_acosh($r, $r, $ROUND);
5829 return $r;
5830 }
5831  
5832 # asech(x) = acosh(1/x)
5833 Math_MPC: {
5834 my $r = Math::MPC::Rmpc_init2(CORE::int($PREC));
5835 Math::MPC::Rmpc_set_ui($r, 1, $ROUND);
5836 Math::MPC::Rmpc_div($r, $r, $x, $ROUND);
5837 Math::MPC::Rmpc_acosh($r, $r, $ROUND);
5838 return $r;
5839 }
5840 }
5841  
5842 sub asech ($) {
5843 bless \__asech__(_star2mpfr_mpc($_[0]));
5844 }
5845  
5846 #
5847 ## csc / csch / acsc / acsch
5848 #
5849  
5850 sub __csc__ {
5851 my ($x) = @_;
5852 goto(ref($x) =~ tr/:/_/rs);
5853  
5854 Math_MPFR: {
5855 my $r = Math::MPFR::Rmpfr_init2($PREC);
5856 Math::MPFR::Rmpfr_csc($r, $x, $ROUND);
5857 return $r;
5858 }
5859  
5860 # csc(x) = 1/sin(x)
5861 Math_MPC: {
5862 my $r = Math::MPC::Rmpc_init2(CORE::int($PREC));
5863 my $t = Math::MPC::Rmpc_init2(CORE::int($PREC));
5864 Math::MPC::Rmpc_sin($r, $x, $ROUND);
5865 Math::MPC::Rmpc_set_ui($t, 1, $ROUND);
5866 Math::MPC::Rmpc_div($r, $t, $r, $ROUND);
5867 return $r;
5868 }
5869 }
5870  
5871 sub csc ($) {
5872 bless \__csc__(_star2mpfr_mpc($_[0]));
5873 }
5874  
5875 sub __csch__ {
5876 my ($x) = @_;
5877 goto(ref($x) =~ tr/:/_/rs);
5878  
5879 Math_MPFR: {
5880 my $r = Math::MPFR::Rmpfr_init2($PREC);
5881 Math::MPFR::Rmpfr_csch($r, $x, $ROUND);
5882 return $r;
5883 }
5884  
5885 # csch(x) = 1/sinh(x)
5886 Math_MPC: {
5887 my $r = Math::MPC::Rmpc_init2(CORE::int($PREC));
5888 my $t = Math::MPC::Rmpc_init2(CORE::int($PREC));
5889 Math::MPC::Rmpc_sinh($r, $x, $ROUND);
5890 Math::MPC::Rmpc_set_ui($t, 1, $ROUND);
5891 Math::MPC::Rmpc_div($r, $t, $r, $ROUND);
5892 return $r;
5893 }
5894 }
5895  
5896 sub csch ($) {
5897 bless \__csch__(_star2mpfr_mpc($_[0]));
5898 }
5899  
5900 sub __acsc__ {
5901 my ($x) = @_;
5902 goto(ref($x) =~ tr/:/_/rs);
5903  
5904 # acsc(x) = asin(1/x)
5905 Math_MPFR: {
5906  
5907 # Return a complex number for x > -1 and x < 1
5908 if ( Math::MPFR::Rmpfr_cmp_ui($x, 1) < 0
5909 and Math::MPFR::Rmpfr_cmp_si($x, -1) > 0) {
5910 $x = _mpfr2mpc($x);
5911 goto Math_MPC;
5912 }
5913  
5914 my $r = Math::MPFR::Rmpfr_init2($PREC);
5915 Math::MPFR::Rmpfr_ui_div($r, 1, $x, $ROUND);
5916 Math::MPFR::Rmpfr_asin($r, $r, $ROUND);
5917 return $r;
5918 }
5919  
5920 # acsc(x) = asin(1/x)
5921 Math_MPC: {
5922 my $r = Math::MPC::Rmpc_init2(CORE::int($PREC));
5923 Math::MPC::Rmpc_set_ui($r, 1, $ROUND);
5924 Math::MPC::Rmpc_div($r, $r, $x, $ROUND);
5925 Math::MPC::Rmpc_asin($r, $r, $ROUND);
5926 return $r;
5927 }
5928 }
5929  
5930 sub acsc ($) {
5931 bless \__acsc__(_star2mpfr_mpc($_[0]));
5932 }
5933  
5934 sub __acsch__ {
5935 my ($x) = @_;
5936 goto(ref($x) =~ tr/:/_/rs);
5937  
5938 # acsch(x) = asinh(1/x)
5939 Math_MPFR: {
5940 my $r = Math::MPFR::Rmpfr_init2($PREC);
5941 Math::MPFR::Rmpfr_ui_div($r, 1, $x, $ROUND);
5942 Math::MPFR::Rmpfr_asinh($r, $r, $ROUND);
5943 return $r;
5944 }
5945  
5946 # acsch(x) = asinh(1/x)
5947 Math_MPC: {
5948 my $r = Math::MPC::Rmpc_init2(CORE::int($PREC));
5949 Math::MPC::Rmpc_set_ui($r, 1, $ROUND);
5950 Math::MPC::Rmpc_div($r, $r, $x, $ROUND);
5951 Math::MPC::Rmpc_asinh($r, $r, $ROUND);
5952 return $r;
5953 }
5954 }
5955  
5956 sub acsch ($) {
5957 bless \__acsch__(_star2mpfr_mpc($_[0]));
5958 }
5959  
5960 #
5961 ## cot / coth / acot / acoth
5962 #
5963  
5964 sub __cot__ {
5965 my ($x) = @_;
5966 goto(ref($x) =~ tr/:/_/rs);
5967  
5968 Math_MPFR: {
5969 my $r = Math::MPFR::Rmpfr_init2($PREC);
5970 Math::MPFR::Rmpfr_cot($r, $x, $ROUND);
5971 return $r;
5972 }
5973  
5974 # cot(x) = 1/tan(x)
5975 Math_MPC: {
5976 my $r = Math::MPC::Rmpc_init2(CORE::int($PREC));
5977 my $t = Math::MPC::Rmpc_init2(CORE::int($PREC));
5978 Math::MPC::Rmpc_tan($r, $x, $ROUND);
5979 Math::MPC::Rmpc_set_ui($t, 1, $ROUND);
5980 Math::MPC::Rmpc_div($r, $t, $r, $ROUND);
5981 return $r;
5982 }
5983 }
5984  
5985 sub cot ($) {
5986 bless \__cot__(_star2mpfr_mpc($_[0]));
5987 }
5988  
5989 sub __coth__ {
5990 my ($x) = @_;
5991 goto(ref($x) =~ tr/:/_/rs);
5992  
5993 Math_MPFR: {
5994 my $r = Math::MPFR::Rmpfr_init2($PREC);
5995 Math::MPFR::Rmpfr_coth($r, $x, $ROUND);
5996 return $r;
5997 }
5998  
5999 # coth(x) = 1/tanh(x)
6000 Math_MPC: {
6001 my $r = Math::MPC::Rmpc_init2(CORE::int($PREC));
6002 my $t = Math::MPC::Rmpc_init2(CORE::int($PREC));
6003 Math::MPC::Rmpc_tanh($r, $x, $ROUND);
6004 Math::MPC::Rmpc_set_ui($t, 1, $ROUND);
6005 Math::MPC::Rmpc_div($r, $t, $r, $ROUND);
6006 return $r;
6007 }
6008 }
6009  
6010 sub coth ($) {
6011 bless \__coth__(_star2mpfr_mpc($_[0]));
6012 }
6013  
6014 sub __acot__ {
6015 my ($x) = @_;
6016 goto(ref($x) =~ tr/:/_/rs);
6017  
6018 # acot(x) = atan(1/x)
6019 Math_MPFR: {
6020 my $r = Math::MPFR::Rmpfr_init2($PREC);
6021 Math::MPFR::Rmpfr_ui_div($r, 1, $x, $ROUND);
6022 Math::MPFR::Rmpfr_atan($r, $r, $ROUND);
6023 return $r;
6024 }
6025  
6026 # acot(x) = atan(1/x)
6027 Math_MPC: {
6028 my $r = Math::MPC::Rmpc_init2(CORE::int($PREC));
6029 Math::MPC::Rmpc_set_ui($r, 1, $ROUND);
6030 Math::MPC::Rmpc_div($r, $r, $x, $ROUND);
6031 Math::MPC::Rmpc_atan($r, $r, $ROUND);
6032 return $r;
6033 }
6034 }
6035  
6036 sub acot ($) {
6037 bless \__acot__(_star2mpfr_mpc($_[0]));
6038 }
6039  
6040 sub __acoth__ {
6041 my ($x) = @_;
6042 goto(ref($x) =~ tr/:/_/rs);
6043  
6044 # acoth(x) = atanh(1/x)
6045 Math_MPFR: {
6046  
6047 # Return a complex number for x > -1 and x < 1
6048 if ( Math::MPFR::Rmpfr_cmp_ui($x, 1) < 0
6049 and Math::MPFR::Rmpfr_cmp_si($x, -1) > 0) {
6050 $x = _mpfr2mpc($x);
6051 goto Math_MPC;
6052 }
6053  
6054 my $r = Math::MPFR::Rmpfr_init2($PREC);
6055 Math::MPFR::Rmpfr_ui_div($r, 1, $x, $ROUND);
6056 Math::MPFR::Rmpfr_atanh($r, $r, $ROUND);
6057 return $r;
6058 }
6059  
6060 # acoth(x) = atanh(1/x)
6061 Math_MPC: {
6062 my $r = Math::MPC::Rmpc_init2(CORE::int($PREC));
6063 Math::MPC::Rmpc_set_ui($r, 1, $ROUND);
6064 Math::MPC::Rmpc_div($r, $r, $x, $ROUND);
6065 Math::MPC::Rmpc_atanh($r, $r, $ROUND);
6066 return $r;
6067 }
6068 }
6069  
6070 sub acoth ($) {
6071 bless \__acoth__(_star2mpfr_mpc($_[0]));
6072 }
6073  
6074 sub deg2rad ($) {
6075 my ($x) = @_;
6076 my $f = Math::MPFR::Rmpfr_init2($PREC);
6077 Math::MPFR::Rmpfr_const_pi($f, $ROUND);
6078 Math::MPFR::Rmpfr_div_ui($f, $f, 180, $ROUND);
6079 bless \__mul__(_star2obj($x), $f);
6080 }
6081  
6082 sub rad2deg ($) {
6083 my ($x) = @_;
6084 my $f = Math::MPFR::Rmpfr_init2($PREC);
6085 Math::MPFR::Rmpfr_const_pi($f, $ROUND);
6086 Math::MPFR::Rmpfr_ui_div($f, 180, $f, $ROUND);
6087 bless \__mul__(_star2obj($x), $f);
6088 }
6089  
6090 #
6091 ## gamma
6092 #
6093  
6094 sub gamma ($) {
6095 my $r = Math::MPFR::Rmpfr_init2($PREC);
6096 Math::MPFR::Rmpfr_gamma($r, _star2mpfr($_[0]), $ROUND);
6097 bless \$r;
6098 }
6099  
6100 #
6101 ## lgamma
6102 #
6103  
6104 sub lgamma ($) {
6105 my $r = Math::MPFR::Rmpfr_init2($PREC);
6106 Math::MPFR::Rmpfr_lgamma($r, _star2mpfr($_[0]), $ROUND);
6107 bless \$r;
6108 }
6109  
6110 #
6111 ## lngamma
6112 #
6113  
6114 sub lngamma ($) {
6115 my $r = Math::MPFR::Rmpfr_init2($PREC);
6116 Math::MPFR::Rmpfr_lngamma($r, _star2mpfr($_[0]), $ROUND);
6117 bless \$r;
6118 }
6119  
6120 #
6121 ## digamma
6122 #
6123  
6124 sub digamma ($) {
6125 my $r = Math::MPFR::Rmpfr_init2($PREC);
6126 Math::MPFR::Rmpfr_digamma($r, _star2mpfr($_[0]), $ROUND);
6127 bless \$r;
6128 }
6129  
6130 #
6131 ## zeta
6132 #
6133  
6134 sub zeta ($) {
6135 my ($x) = @_;
6136  
6137 if (!ref($x) and CORE::int($x) eq $x and $x >= 0 and $x < ULONG_MAX) {
6138 ## $x is an unsigned integer
6139 }
6140 else {
6141 $x = _star2mpfr($x);
6142  
6143 # If $x fits inside an unsigned integer, then unpack it.
6144 if ( Math::MPFR::Rmpfr_integer_p($x)
6145 and Math::MPFR::Rmpfr_fits_ulong_p($x, $ROUND)) {
6146 $x = Math::MPFR::Rmpfr_get_ui($x, $ROUND);
6147 }
6148 }
6149  
6150 my $r = Math::MPFR::Rmpfr_init2($PREC);
6151  
6152 ref($x)
6153 ? Math::MPFR::Rmpfr_zeta($r, $x, $ROUND)
6154 : Math::MPFR::Rmpfr_zeta_ui($r, $x, $ROUND);
6155  
6156 bless \$r;
6157 }
6158  
6159 #
6160 ## Dirichlet eta function
6161 #
6162  
6163 # Implemented as:
6164 # eta(1) = ln(2)
6165 # eta(x) = (1 - 2**(1-x)) * zeta(x)
6166  
6167 sub eta ($) {
6168 my $x = _star2mpfr($_[0]);
6169  
6170 my $r = Math::MPFR::Rmpfr_init2($PREC);
6171 my $x_is_int = Math::MPFR::Rmpfr_integer_p($x);
6172  
6173 # Special case for eta(1) = log(2)
6174 if ($x_is_int and Math::MPFR::Rmpfr_cmp_ui($x, 1) == 0) {
6175 Math::MPFR::Rmpfr_const_log2($r, $ROUND);
6176 return bless \$r;
6177 }
6178  
6179 my $t = Math::MPFR::Rmpfr_init2($PREC);
6180  
6181 Math::MPFR::Rmpfr_ui_sub($r, 1, $x, $ROUND);
6182 Math::MPFR::Rmpfr_ui_pow($r, 2, $r, $ROUND);
6183 Math::MPFR::Rmpfr_ui_sub($r, 1, $r, $ROUND);
6184  
6185 if ($x_is_int and Math::MPFR::Rmpfr_fits_ulong_p($x, $ROUND)) {
6186 Math::MPFR::Rmpfr_zeta_ui($t, Math::MPFR::Rmpfr_get_ui($x, $ROUND), $ROUND);
6187 }
6188 else {
6189 Math::MPFR::Rmpfr_zeta($t, $x, $ROUND);
6190 }
6191  
6192 Math::MPFR::Rmpfr_mul($r, $r, $t, $ROUND);
6193 bless \$r;
6194 }
6195  
6196 #
6197 ## Beta(x,y) function
6198 #
6199  
6200 # Implemented as:
6201 # beta(x,y) = gamma(x)*gamma(y) / gamma(x+y)
6202  
6203 sub beta ($$) {
6204 my $x = _star2mpfr($_[0]);
6205 my $y = _star2mpfr($_[1]);
6206  
6207 state $has_beta = (Math::MPFR::MPFR_VERSION_MAJOR() >= 4);
6208  
6209 if ($has_beta) { # available since mpfr-4.0.0
6210 my $r = Math::MPFR::Rmpfr_init2($PREC);
6211 Math::MPFR::Rmpfr_beta($r, $x, $y, $ROUND);
6212 return bless \$r;
6213 }
6214  
6215 my $t1 = Math::MPFR::Rmpfr_init2($PREC); # gamma(x+y)
6216 my $t2 = Math::MPFR::Rmpfr_init2($PREC); # gamma(y)
6217  
6218 my $r = Math::MPFR::Rmpfr_init2($PREC);
6219  
6220 Math::MPFR::Rmpfr_add($t1, $x, $y, $ROUND);
6221 Math::MPFR::Rmpfr_gamma($t1, $t1, $ROUND);
6222 Math::MPFR::Rmpfr_gamma($r, $x, $ROUND);
6223 Math::MPFR::Rmpfr_gamma($t2, $y, $ROUND);
6224 Math::MPFR::Rmpfr_mul($r, $r, $t2, $ROUND);
6225 Math::MPFR::Rmpfr_div($r, $r, $t1, $ROUND);
6226  
6227 bless \$r;
6228 }
6229  
6230 #
6231 ## Airy function (Ai)
6232 #
6233  
6234 sub Ai ($) {
6235 my $r = Math::MPFR::Rmpfr_init2($PREC);
6236 Math::MPFR::Rmpfr_ai($r, _star2mpfr($_[0]), $ROUND);
6237 bless \$r;
6238 }
6239  
6240 #
6241 ## Exponential integral (Ei)
6242 #
6243  
6244 sub Ei ($) {
6245 my $r = Math::MPFR::Rmpfr_init2($PREC);
6246 Math::MPFR::Rmpfr_eint($r, _star2mpfr($_[0]), $ROUND);
6247 bless \$r;
6248 }
6249  
6250 #
6251 ## Logarithmic integral (Li)
6252 #
6253 sub Li ($) {
6254 my $r = Math::MPFR::Rmpfr_init2($PREC);
6255 Math::MPFR::Rmpfr_log($r, _star2mpfr($_[0]), $ROUND);
6256 Math::MPFR::Rmpfr_eint($r, $r, $ROUND);
6257 bless \$r;
6258 }
6259  
6260 #
6261 ## Dilogarithm function (Li_2)
6262 #
6263 sub Li2 ($) {
6264 my $r = Math::MPFR::Rmpfr_init2($PREC);
6265 Math::MPFR::Rmpfr_li2($r, _star2mpfr($_[0]), $ROUND);
6266 bless \$r;
6267 }
6268  
6269 #
6270 ## Error function
6271 #
6272 sub erf ($) {
6273 my $r = Math::MPFR::Rmpfr_init2($PREC);
6274 Math::MPFR::Rmpfr_erf($r, _star2mpfr($_[0]), $ROUND);
6275 bless \$r;
6276 }
6277  
6278 #
6279 ## Complementary error function
6280 #
6281 sub erfc ($) {
6282 my $r = Math::MPFR::Rmpfr_init2($PREC);
6283 Math::MPFR::Rmpfr_erfc($r, _star2mpfr($_[0]), $ROUND);
6284 bless \$r;
6285 }
6286  
6287 #
6288 ## Lambert W function
6289 #
6290  
6291 sub __LambertW__ {
6292 my ($x) = @_;
6293  
6294 my $p = Math::MPFR::Rmpfr_init2($PREC);
6295 Math::MPFR::Rmpfr_set_str($p, '1e-' . CORE::int($PREC >> 2), 10, $ROUND);
6296  
6297 goto(ref($x) =~ tr/:/_/rs);
6298  
6299 Math_MPFR: {
6300  
6301 # Return a complex number for x < -1/e
6302 if (Math::MPFR::Rmpfr_cmp_d($x, -1 / CORE::exp(1)) < 0) {
6303 $x = _mpfr2mpc($x);
6304 goto Math_MPC;
6305 }
6306  
6307 Math::MPFR::Rmpfr_set_ui((my $r = Math::MPFR::Rmpfr_init2($PREC)), 1, $ROUND);
6308 Math::MPFR::Rmpfr_set_ui((my $y = Math::MPFR::Rmpfr_init2($PREC)), 0, $ROUND);
6309  
6310 my $count = 0;
6311 my $tmp = Math::MPFR::Rmpfr_init2($PREC);
6312  
6313 while (1) {
6314 Math::MPFR::Rmpfr_sub($tmp, $r, $y, $ROUND);
6315 Math::MPFR::Rmpfr_cmpabs($tmp, $p) <= 0 and last;
6316  
6317 Math::MPFR::Rmpfr_set($y, $r, $ROUND);
6318  
6319 Math::MPFR::Rmpfr_log($tmp, $r, $ROUND);
6320 Math::MPFR::Rmpfr_add_ui($tmp, $tmp, 1, $ROUND);
6321  
6322 Math::MPFR::Rmpfr_add($r, $r, $x, $ROUND);
6323 Math::MPFR::Rmpfr_div($r, $r, $tmp, $ROUND);
6324 last if ++$count > $PREC;
6325 }
6326  
6327 Math::MPFR::Rmpfr_log($r, $r, $ROUND);
6328 return $r;
6329 }
6330  
6331 Math_MPC: {
6332 my $r = Math::MPC::Rmpc_init2($PREC);
6333 Math::MPC::Rmpc_sqrt($r, $x, $ROUND);
6334 Math::MPC::Rmpc_add_ui($r, $r, 1, $ROUND);
6335  
6336 my $y = Math::MPC::Rmpc_init2($PREC);
6337 Math::MPC::Rmpc_set_ui($y, 0, $ROUND);
6338  
6339 my $tmp = Math::MPC::Rmpc_init2($PREC);
6340 my $abs = Math::MPFR::Rmpfr_init2($PREC);
6341  
6342 my $count = 0;
6343 while (1) {
6344 Math::MPC::Rmpc_sub($tmp, $r, $y, $ROUND);
6345  
6346 Math::MPC::Rmpc_abs($abs, $tmp, $ROUND);
6347 Math::MPFR::Rmpfr_cmp($abs, $p) <= 0 and last;
6348  
6349 Math::MPC::Rmpc_set($y, $r, $ROUND);
6350  
6351 Math::MPC::Rmpc_log($tmp, $r, $ROUND);
6352 Math::MPC::Rmpc_add_ui($tmp, $tmp, 1, $ROUND);
6353  
6354 Math::MPC::Rmpc_add($r, $r, $x, $ROUND);
6355 Math::MPC::Rmpc_div($r, $r, $tmp, $ROUND);
6356 last if ++$count > $PREC;
6357 }
6358  
6359 Math::MPC::Rmpc_log($r, $r, $ROUND);
6360 return $r;
6361 }
6362 }
6363  
6364 sub LambertW ($) {
6365 bless \__LambertW__(_star2mpfr_mpc($_[0]));
6366 }
6367  
6368 #
6369 ## lgrt -- logarithmic root
6370 #
6371  
6372 sub __lgrt__ {
6373 my ($c) = @_;
6374  
6375 my $p = Math::MPFR::Rmpfr_init2($PREC);
6376 Math::MPFR::Rmpfr_set_str($p, '1e-' . CORE::int($PREC >> 2), 10, $ROUND);
6377  
6378 goto(ref($c) =~ tr/:/_/rs);
6379  
6380 Math_MPFR: {
6381  
6382 # Return a complex number for x < e^(-1/e)
6383 if (Math::MPFR::Rmpfr_cmp_d($c, CORE::exp(-1 / CORE::exp(1))) < 0) {
6384 $c = _mpfr2mpc($c);
6385 goto Math_MPC;
6386 }
6387  
6388 my $r = Math::MPFR::Rmpfr_init2($PREC);
6389 Math::MPFR::Rmpfr_log($r, $c, $ROUND);
6390  
6391 Math::MPFR::Rmpfr_set_ui((my $x = Math::MPFR::Rmpfr_init2($PREC)), 1, $ROUND);
6392 Math::MPFR::Rmpfr_set_ui((my $y = Math::MPFR::Rmpfr_init2($PREC)), 0, $ROUND);
6393  
6394 my $count = 0;
6395 my $tmp = Math::MPFR::Rmpfr_init2($PREC);
6396  
6397 while (1) {
6398 Math::MPFR::Rmpfr_sub($tmp, $x, $y, $ROUND);
6399 Math::MPFR::Rmpfr_cmpabs($tmp, $p) <= 0 and last;
6400  
6401 Math::MPFR::Rmpfr_set($y, $x, $ROUND);
6402  
6403 Math::MPFR::Rmpfr_log($tmp, $x, $ROUND);
6404 Math::MPFR::Rmpfr_add_ui($tmp, $tmp, 1, $ROUND);
6405  
6406 Math::MPFR::Rmpfr_add($x, $x, $r, $ROUND);
6407 Math::MPFR::Rmpfr_div($x, $x, $tmp, $ROUND);
6408 last if ++$count > $PREC;
6409 }
6410  
6411 return $x;
6412 }
6413  
6414 Math_MPC: {
6415 my $d = Math::MPC::Rmpc_init2($PREC);
6416 Math::MPC::Rmpc_log($d, $c, $ROUND);
6417  
6418 my $x = Math::MPC::Rmpc_init2($PREC);
6419 Math::MPC::Rmpc_sqrt($x, $c, $ROUND);
6420 Math::MPC::Rmpc_add_ui($x, $x, 1, $ROUND);
6421 Math::MPC::Rmpc_log($x, $x, $ROUND);
6422  
6423 my $y = Math::MPC::Rmpc_init2($PREC);
6424 Math::MPC::Rmpc_set_ui($y, 0, $ROUND);
6425  
6426 my $tmp = Math::MPC::Rmpc_init2($PREC);
6427 my $abs = Math::MPFR::Rmpfr_init2($PREC);
6428  
6429 my $count = 0;
6430 while (1) {
6431 Math::MPC::Rmpc_sub($tmp, $x, $y, $ROUND);
6432  
6433 Math::MPC::Rmpc_abs($abs, $tmp, $ROUND);
6434 Math::MPFR::Rmpfr_cmp($abs, $p) <= 0 and last;
6435  
6436 Math::MPC::Rmpc_set($y, $x, $ROUND);
6437  
6438 Math::MPC::Rmpc_log($tmp, $x, $ROUND);
6439 Math::MPC::Rmpc_add_ui($tmp, $tmp, 1, $ROUND);
6440  
6441 Math::MPC::Rmpc_add($x, $x, $d, $ROUND);
6442 Math::MPC::Rmpc_div($x, $x, $tmp, $ROUND);
6443 last if ++$count > $PREC;
6444 }
6445  
6446 return $x;
6447 }
6448 }
6449  
6450 sub lgrt ($) {
6451 bless \__lgrt__(_star2mpfr_mpc($_[0]));
6452 }
6453  
6454 #
6455 ## Arithmetic-geometric mean
6456 #
6457  
6458 sub __agm__ {
6459 my ($x, $y) = @_;
6460 goto(join('__', ref($x), ref($y)) =~ tr/:/_/rs);
6461  
6462 Math_MPFR__Math_MPFR: {
6463  
6464 if ( Math::MPFR::Rmpfr_sgn($x) < 0
6465 or Math::MPFR::Rmpfr_sgn($y) < 0) {
6466  
6467 $x = _mpfr2mpc($x);
6468 $y = _mpfr2mpc($y);
6469  
6470 goto Math_MPC__Math_MPC;
6471 }
6472  
6473 my $r = Math::MPFR::Rmpfr_init2($PREC);
6474 Math::MPFR::Rmpfr_agm($r, $x, $y, $ROUND);
6475 return $r;
6476 }
6477  
6478 Math_MPC__Math_MPC: {
6479  
6480 # agm(0, x) = 0
6481 Math::MPC::Rmpc_cmp_si_si($x, 0, 0) || return $x;
6482  
6483 # agm(x, 0) = 0
6484 Math::MPC::Rmpc_cmp_si_si($y, 0, 0) || return $y;
6485  
6486 my $a0 = Math::MPC::Rmpc_init2($PREC);
6487 my $g0 = Math::MPC::Rmpc_init2($PREC);
6488  
6489 my $a1 = Math::MPC::Rmpc_init2($PREC);
6490 my $g1 = Math::MPC::Rmpc_init2($PREC);
6491  
6492 my $t = Math::MPC::Rmpc_init2($PREC);
6493  
6494 Math::MPC::Rmpc_set($a0, $x, $ROUND);
6495 Math::MPC::Rmpc_set($g0, $y, $ROUND);
6496  
6497 my $count = 0;
6498 {
6499 Math::MPC::Rmpc_add($a1, $a0, $g0, $ROUND);
6500 Math::MPC::Rmpc_div_2ui($a1, $a1, 1, $ROUND);
6501  
6502 Math::MPC::Rmpc_mul($g1, $a0, $g0, $ROUND);
6503 Math::MPC::Rmpc_add($t, $a0, $g0, $ROUND);
6504 Math::MPC::Rmpc_sqr($t, $t, $ROUND);
6505 Math::MPC::Rmpc_cmp_si_si($t, 0, 0) || return $t;
6506 Math::MPC::Rmpc_div($g1, $g1, $t, $ROUND);
6507 Math::MPC::Rmpc_sqrt($g1, $g1, $ROUND);
6508 Math::MPC::Rmpc_add($t, $a0, $g0, $ROUND);
6509 Math::MPC::Rmpc_mul($g1, $g1, $t, $ROUND);
6510  
6511 if (Math::MPC::Rmpc_cmp($a0, $a1) and ++$count < $PREC) {
6512 Math::MPC::Rmpc_set($a0, $a1, $ROUND);
6513 Math::MPC::Rmpc_set($g0, $g1, $ROUND);
6514 redo;
6515 }
6516 }
6517  
6518 return $g0;
6519 }
6520  
6521 Math_MPFR__Math_MPC: {
6522 $x = _mpfr2mpc($x);
6523 goto Math_MPC__Math_MPC;
6524 }
6525  
6526 Math_MPC__Math_MPFR: {
6527 $y = _mpfr2mpc($y);
6528 goto Math_MPC__Math_MPC;
6529 }
6530 }
6531  
6532 sub agm ($$) {
6533 bless \__agm__(_star2mpfr_mpc($_[0]), _star2mpfr_mpc($_[1]));
6534 }
6535  
6536 #
6537 ## hypot
6538 #
6539  
6540 # hypot(x, y) = sqrt(x^2 + y^2)
6541  
6542 sub __hypot__ {
6543 my ($x, $y) = @_;
6544 goto(join('__', ref($x), ref($y)) =~ tr/:/_/rs);
6545  
6546 Math_MPFR__Math_MPFR: {
6547 my $r = Math::MPFR::Rmpfr_init2($PREC);
6548 Math::MPFR::Rmpfr_hypot($r, $x, $y, $ROUND);
6549 return $r;
6550 }
6551  
6552 Math_MPFR__Math_MPC: {
6553 my $r = Math::MPFR::Rmpfr_init2($PREC);
6554 Math::MPC::Rmpc_abs($r, $y, $ROUND);
6555 Math::MPFR::Rmpfr_hypot($r, $r, $x, $ROUND);
6556 return $r;
6557 }
6558  
6559 Math_MPC__Math_MPFR: {
6560 my $r = Math::MPFR::Rmpfr_init2($PREC);
6561 Math::MPC::Rmpc_abs($r, $x, $ROUND);
6562 Math::MPFR::Rmpfr_hypot($r, $r, $y, $ROUND);
6563 return $r;
6564 }
6565  
6566 Math_MPC__Math_MPC: {
6567 my $r = Math::MPFR::Rmpfr_init2($PREC);
6568 Math::MPC::Rmpc_abs($r, $x, $ROUND);
6569 my $t = Math::MPFR::Rmpfr_init2($PREC);
6570 Math::MPC::Rmpc_abs($t, $y, $ROUND);
6571 Math::MPFR::Rmpfr_hypot($r, $r, $t, $ROUND);
6572 return $r;
6573 }
6574 }
6575  
6576 sub hypot ($$) {
6577 bless \__hypot__(_star2mpfr_mpc($_[0]), _star2mpfr_mpc($_[1]));
6578 }
6579  
6580 #
6581 ## BesselJ
6582 #
6583  
6584 sub __BesselJ__ {
6585 my ($x, $n) = @_;
6586 goto(join('__', ref($x), ref($n) || 'Scalar') =~ tr/:/_/rs);
6587  
6588 Math_MPFR__Scalar: {
6589 my $r = Math::MPFR::Rmpfr_init2($PREC);
6590  
6591 if ($n == 0) {
6592 Math::MPFR::Rmpfr_j0($r, $x, $ROUND);
6593 }
6594 elsif ($n == 1) {
6595 Math::MPFR::Rmpfr_j1($r, $x, $ROUND);
6596 }
6597 else {
6598 Math::MPFR::Rmpfr_jn($r, $n, $x, $ROUND);
6599 }
6600  
6601 return $r;
6602 }
6603  
6604 Math_MPFR__Math_GMPz: {
6605  
6606 $n = Math::GMPz::Rmpz_get_d($n);
6607  
6608 # Limit goes to zero when n goes to +/-Infinity
6609 if (($n < LONG_MIN or $n > ULONG_MAX)
6610 and Math::MPFR::Rmpfr_number_p($x)) {
6611 my $r = Math::MPFR::Rmpfr_init2($PREC);
6612 Math::MPFR::Rmpfr_set_ui($r, 0, $ROUND);
6613 return $r;
6614 }
6615  
6616 goto Math_MPFR__Scalar;
6617 }
6618 }
6619  
6620 sub BesselJ ($$) {
6621 my ($x, $y) = @_;
6622  
6623 $x = ref($x) eq __PACKAGE__ ? _any2mpfr($$x) : _star2mpfr($x);
6624  
6625 if (!ref($y) and CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
6626 return bless \__BesselJ__($x, $y);
6627 }
6628  
6629 bless \__BesselJ__($x, _star2mpz($y) // (goto &nan));
6630 }
6631  
6632 #
6633 ## BesselY
6634 #
6635  
6636 sub __BesselY__ {
6637 my ($x, $n) = @_;
6638 goto(join('__', ref($x), ref($n) || 'Scalar') =~ tr/:/_/rs);
6639  
6640 Math_MPFR__Scalar: {
6641 my $r = Math::MPFR::Rmpfr_init2($PREC);
6642  
6643 if ($n == 0) {
6644 Math::MPFR::Rmpfr_y0($r, $x, $ROUND);
6645 }
6646 elsif ($n == 1) {
6647 Math::MPFR::Rmpfr_y1($r, $x, $ROUND);
6648 }
6649 else {
6650 Math::MPFR::Rmpfr_yn($r, $n, $x, $ROUND);
6651 }
6652  
6653 return $r;
6654 }
6655  
6656 Math_MPFR__Math_GMPz: {
6657  
6658 $n = Math::GMPz::Rmpz_get_d($n);
6659  
6660 if ($n < LONG_MIN or $n > ULONG_MAX) {
6661  
6662 my $r = Math::MPFR::Rmpfr_init2($PREC);
6663  
6664 if (Math::MPFR::Rmpfr_sgn($x) < 0 or !Math::MPFR::Rmpfr_number_p($x)) {
6665 Math::MPFR::Rmpfr_set_nan($r);
6666 return $r;
6667 }
6668  
6669 if ($n < 0) {
6670 Math::MPFR::Rmpfr_set_inf($r, 1);
6671 }
6672 else {
6673 Math::MPFR::Rmpfr_set_inf($r, -1);
6674 }
6675  
6676 return $r;
6677 }
6678  
6679 goto Math_MPFR__Scalar;
6680 }
6681 }
6682  
6683 sub BesselY ($$) {
6684 my ($x, $y) = @_;
6685  
6686 $x = ref($x) eq __PACKAGE__ ? _any2mpfr($$x) : _star2mpfr($x);
6687  
6688 if (!ref($y) and CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
6689 return bless \__BesselY__($x, $y);
6690 }
6691  
6692 bless \__BesselY__($x, _star2mpz($y) // (goto &nan));
6693 }
6694  
6695 #
6696 ## ROUND
6697 #
6698  
6699 sub __round__ {
6700 my ($x, $prec) = @_;
6701 goto(ref($x) =~ tr/:/_/rs);
6702  
6703 Math_MPFR: {
6704  
6705 my $nth = -CORE::int($prec);
6706  
6707 my $p = Math::MPFR::Rmpfr_init2($PREC);
6708 Math::MPFR::Rmpfr_set_str($p, '1e' . CORE::abs($nth), 10, $ROUND);
6709  
6710 my $r = Math::MPFR::Rmpfr_init2($PREC);
6711  
6712 if ($nth < 0) {
6713 Math::MPFR::Rmpfr_div($r, $x, $p, $ROUND);
6714 }
6715 else {
6716 Math::MPFR::Rmpfr_mul($r, $x, $p, $ROUND);
6717 }
6718  
6719 Math::MPFR::Rmpfr_round($r, $r);
6720  
6721 if ($nth < 0) {
6722 Math::MPFR::Rmpfr_mul($r, $r, $p, $ROUND);
6723 }
6724 else {
6725 Math::MPFR::Rmpfr_div($r, $r, $p, $ROUND);
6726 }
6727  
6728 return $r;
6729 }
6730  
6731 Math_MPC: {
6732  
6733 my $real = Math::MPFR::Rmpfr_init2($PREC);
6734 my $imag = Math::MPFR::Rmpfr_init2($PREC);
6735  
6736 Math::MPC::RMPC_RE($real, $x);
6737 Math::MPC::RMPC_IM($imag, $x);
6738  
6739 $real = __round__($real, $prec);
6740 $imag = __round__($imag, $prec);
6741  
6742 if (Math::MPFR::Rmpfr_zero_p($imag)) {
6743 return $real;
6744 }
6745  
6746 my $r = Math::MPC::Rmpc_init2($PREC);
6747 Math::MPC::Rmpc_set_fr_fr($r, $real, $imag, $ROUND);
6748 return $r;
6749 }
6750  
6751 Math_GMPq: {
6752  
6753 my $nth = -CORE::int($prec);
6754  
6755 my $n = Math::GMPq::Rmpq_init();
6756 Math::GMPq::Rmpq_set($n, $x);
6757  
6758 my $sgn = Math::GMPq::Rmpq_sgn($n);
6759  
6760 if ($sgn < 0) {
6761 Math::GMPq::Rmpq_neg($n, $n);
6762 }
6763  
6764 my $p = Math::GMPz::Rmpz_init_set_str('1' . ('0' x CORE::abs($nth)), 10);
6765  
6766 if ($nth < 0) {
6767 Math::GMPq::Rmpq_div_z($n, $n, $p);
6768 }
6769 else {
6770 Math::GMPq::Rmpq_mul_z($n, $n, $p);
6771 }
6772  
6773 state $half = do {
6774 my $q = Math::GMPq::Rmpq_init_nobless();
6775 Math::GMPq::Rmpq_set_ui($q, 1, 2);
6776 $q;
6777 };
6778  
6779 Math::GMPq::Rmpq_add($n, $n, $half);
6780  
6781 my $z = Math::GMPz::Rmpz_init();
6782 Math::GMPz::Rmpz_set_q($z, $n);
6783  
6784 if (Math::GMPz::Rmpz_odd_p($z) and Math::GMPq::Rmpq_integer_p($n)) {
6785 Math::GMPz::Rmpz_sub_ui($z, $z, 1);
6786 }
6787  
6788 Math::GMPq::Rmpq_set_z($n, $z);
6789  
6790 if ($nth < 0) {
6791 Math::GMPq::Rmpq_mul_z($n, $n, $p);
6792 }
6793 else {
6794 Math::GMPq::Rmpq_div_z($n, $n, $p);
6795 }
6796  
6797 if ($sgn < 0) {
6798 Math::GMPq::Rmpq_neg($n, $n);
6799 }
6800  
6801 if (Math::GMPq::Rmpq_integer_p($n)) {
6802 Math::GMPz::Rmpz_set_q($z, $n);
6803 return $z;
6804 }
6805  
6806 return $n;
6807 }
6808  
6809 Math_GMPz: {
6810 $x = _mpz2mpq($x);
6811 goto Math_GMPq;
6812 }
6813 }
6814  
6815 sub round ($;$) {
6816 my ($x, $y) = @_;
6817  
6818 $x = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
6819  
6820 if (!defined($y)) {
6821 return bless \__round__($x, 0);
6822 }
6823  
6824 $y = _star2si($y) // goto &nan;
6825  
6826 bless \__round__($x, $y);
6827 }
6828  
6829 #
6830 ## RAND / IRAND
6831 #
6832  
6833 sub __irand__ {
6834 my ($x, $y, $state) = @_;
6835  
6836 if (defined($y)) {
6837 my $cmp = Math::GMPz::Rmpz_cmp($y, $x);
6838  
6839 if ($cmp == 0) {
6840 return $x;
6841 }
6842 elsif ($cmp < 0) {
6843 ($x, $y) = ($y, $x);
6844 }
6845  
6846 my $r = Math::GMPz::Rmpz_init();
6847 Math::GMPz::Rmpz_sub($r, $y, $x);
6848 Math::GMPz::Rmpz_add_ui($r, $r, 1);
6849 Math::GMPz::Rmpz_urandomm($r, $state, $r, 1);
6850 Math::GMPz::Rmpz_add($r, $r, $x);
6851 return $r;
6852 }
6853  
6854 my $sgn = Math::GMPz::Rmpz_sgn($x) || return $x;
6855  
6856 my $r = Math::GMPz::Rmpz_init_set($x);
6857  
6858 if ($sgn < 0) {
6859 Math::GMPz::Rmpz_sub_ui($r, $r, 1);
6860 }
6861 else {
6862 Math::GMPz::Rmpz_add_ui($r, $r, 1);
6863 }
6864  
6865 Math::GMPz::Rmpz_urandomm($r, $state, $r, 1);
6866 Math::GMPz::Rmpz_neg($r, $r) if $sgn < 0;
6867 return $r;
6868 }
6869  
6870 {
6871 my $srand = srand();
6872  
6873 {
6874 state $state = Math::MPFR::Rmpfr_randinit_mt_nobless();
6875 Math::MPFR::Rmpfr_randseed_ui($state, $srand);
6876  
6877 sub rand (;$;$) {
6878 my ($x, $y) = @_;
6879  
6880 if (@_ == 0) {
6881 $x = one();
6882 }
6883  
6884 $x = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
6885  
6886 if (!defined($y)) {
6887 my $rand = Math::MPFR::Rmpfr_init2($PREC);
6888 Math::MPFR::Rmpfr_urandom($rand, $state, $ROUND);
6889 return bless \__mul__($rand, $x);
6890 }
6891  
6892 $y = ref($y) eq __PACKAGE__ ? $$y : _star2obj($y);
6893  
6894 my $rand = Math::MPFR::Rmpfr_init2($PREC);
6895 Math::MPFR::Rmpfr_urandom($rand, $state, $ROUND);
6896 $rand = __mul__($rand, __sub__($y, $x));
6897 bless \__add__($rand, $x);
6898 }
6899  
6900 sub seed ($) {
6901 my ($x) = @_;
6902  
6903 $x = $$x if (ref($x) eq __PACKAGE__);
6904  
6905 if (ref($x) ne 'Math::GMPz') {
6906 $x = _star2mpz($x) // do {
6907 require Carp;
6908 Carp::croak("seed(): invalid seed value <<$_[0]>> (expected an integer)");
6909 };
6910 }
6911 Math::MPFR::Rmpfr_randseed($state, $x);
6912 bless \$x;
6913 }
6914 }
6915  
6916 {
6917 state $state = Math::GMPz::zgmp_randinit_mt_nobless();
6918 Math::GMPz::zgmp_randseed_ui($state, $srand);
6919  
6920 sub irand ($;$) {
6921 my ($x, $y) = @_;
6922  
6923 $x = _star2mpz($x) // goto &nan;
6924  
6925 if (!defined($y)) {
6926 return bless \__irand__($x, undef, $state);
6927 }
6928  
6929 $y = _star2mpz($y) // goto &nan;
6930  
6931 bless \__irand__($x, $y, $state);
6932 }
6933  
6934 sub iseed ($) {
6935 my ($x) = @_;
6936  
6937 $x = _star2mpz($x) // do {
6938 require Carp;
6939 Carp::croak("iseed(): invalid seed value <<$_[0]>> (expected an integer)");
6940 };
6941  
6942 Math::GMPz::zgmp_randseed($state, $x);
6943 bless \$x;
6944 }
6945 }
6946 }
6947  
6948 #
6949 ## n-th Fibonacci number of k-th order
6950 #
6951  
6952 sub fibonacci ($;$) {
6953 my ($n, $k) = @_;
6954  
6955 $n = _star2ui($n) // goto &nan;
6956  
6957 # N-th Fibonacci number of k-th order
6958 if (defined($k)) {
6959  
6960 $k = _star2ui($k) // goto &nan;
6961  
6962 if ($k == 2) {
6963 my $z = Math::GMPz::Rmpz_init();
6964 Math::GMPz::Rmpz_fib_ui($z, $n);
6965 return bless \$z;
6966 }
6967  
6968 if ($n < $k - 1) {
6969 return zero();
6970 }
6971  
6972 # Algorithm after M. F. Hasler
6973 # See: https://oeis.org/A302990
6974  
6975 my @f = map {
6976 $_ < $k
6977 ? do {
6978 my $z = Math::GMPz::Rmpz_init();
6979 Math::GMPz::Rmpz_setbit($z, $_);
6980 $z;
6981 }
6982 : Math::GMPz::Rmpz_init_set_ui(1)
6983 } 1 .. ($k + 1);
6984  
6985 my $t = Math::GMPz::Rmpz_init();
6986  
6987 foreach my $i (2 * ++$k - 2 .. $n) {
6988 Math::GMPz::Rmpz_mul_2exp($t, $f[($i - 1) % $k], 1);
6989 Math::GMPz::Rmpz_sub($f[$i % $k], $t, $f[$i % $k]);
6990 }
6991  
6992 my $r = $f[$n % $k];
6993 return bless \$r;
6994 }
6995  
6996 my $r = Math::GMPz::Rmpz_init();
6997 Math::GMPz::Rmpz_fib_ui($r, $n);
6998 bless \$r;
6999 }
7000  
7001 #
7002 ## n-th Lucas number
7003 #
7004  
7005 sub lucas ($) {
7006 my ($n) = @_;
7007  
7008 $n = _star2ui($n) // goto &nan;
7009  
7010 my $r = Math::GMPz::Rmpz_init();
7011 Math::GMPz::Rmpz_lucnum_ui($r, $n);
7012 bless \$r;
7013 }
7014  
7015 sub __lucasV__ {
7016 my ($P, $Q, $n) = @_;
7017  
7018 my ($V1, $V2) = (Math::GMPz::Rmpz_init_set_ui(2), Math::GMPz::Rmpz_init_set($P));
7019 my ($Q1, $Q2) = (Math::GMPz::Rmpz_init_set_ui(1), Math::GMPz::Rmpz_init_set_ui(1));
7020  
7021 foreach my $bit (split(//, Math::GMPz::Rmpz_get_str($n, 2))) {
7022  
7023 Math::GMPz::Rmpz_mul($Q1, $Q1, $Q2);
7024  
7025 if ($bit) {
7026 Math::GMPz::Rmpz_mul($Q2, $Q1, $Q);
7027 Math::GMPz::Rmpz_mul($V1, $V1, $V2);
7028 Math::GMPz::Rmpz_mul($V2, $V2, $V2);
7029 Math::GMPz::Rmpz_submul($V1, $P, $Q1);
7030 Math::GMPz::Rmpz_submul_ui($V2, $Q2, 2);
7031 }
7032 else {
7033 Math::GMPz::Rmpz_set($Q2, $Q1);
7034 Math::GMPz::Rmpz_mul($V2, $V2, $V1);
7035 Math::GMPz::Rmpz_mul($V1, $V1, $V1);
7036 Math::GMPz::Rmpz_submul($V2, $P, $Q1);
7037 Math::GMPz::Rmpz_submul_ui($V1, $Q2, 2);
7038 }
7039 }
7040  
7041 return ($V1, $V2);
7042 }
7043  
7044 sub __lucasVmod__ {
7045 my ($P, $Q, $n, $m) = @_;
7046  
7047 my ($V1, $V2) = (Math::GMPz::Rmpz_init_set_ui(2), Math::GMPz::Rmpz_init_set($P));
7048 my ($Q1, $Q2) = (Math::GMPz::Rmpz_init_set_ui(1), Math::GMPz::Rmpz_init_set_ui(1));
7049  
7050 foreach my $bit (split(//, Math::GMPz::Rmpz_get_str($n, 2))) {
7051  
7052 Math::GMPz::Rmpz_mul($Q1, $Q1, $Q2);
7053 Math::GMPz::Rmpz_mod($Q1, $Q1, $m);
7054  
7055 if ($bit) {
7056 Math::GMPz::Rmpz_mul($Q2, $Q1, $Q);
7057 Math::GMPz::Rmpz_mul($V1, $V1, $V2);
7058 Math::GMPz::Rmpz_powm_ui($V2, $V2, 2, $m);
7059 Math::GMPz::Rmpz_submul($V1, $P, $Q1);
7060 Math::GMPz::Rmpz_submul_ui($V2, $Q2, 2);
7061 Math::GMPz::Rmpz_mod($V1, $V1, $m);
7062 }
7063 else {
7064 Math::GMPz::Rmpz_set($Q2, $Q1);
7065 Math::GMPz::Rmpz_mul($V2, $V2, $V1);
7066 Math::GMPz::Rmpz_powm_ui($V1, $V1, 2, $m);
7067 Math::GMPz::Rmpz_submul($V2, $P, $Q1);
7068 Math::GMPz::Rmpz_submul_ui($V1, $Q2, 2);
7069 Math::GMPz::Rmpz_mod($V2, $V2, $m);
7070 }
7071 }
7072  
7073 Math::GMPz::Rmpz_mod($V1, $V1, $m);
7074  
7075 return ($V1, $V2);
7076 }
7077  
7078 sub __lucasUV__ {
7079 my ($P, $Q, $n) = @_;
7080  
7081 my $U1 = Math::GMPz::Rmpz_init_set_ui(1);
7082  
7083 my ($V1, $V2) = (Math::GMPz::Rmpz_init_set_ui(2), Math::GMPz::Rmpz_init_set($P));
7084 my ($Q1, $Q2) = (Math::GMPz::Rmpz_init_set_ui(1), Math::GMPz::Rmpz_init_set_ui(1));
7085  
7086 my $t = Math::GMPz::Rmpz_init();
7087 my $s = Math::GMPz::Rmpz_scan1($n, 0);
7088  
7089 Math::GMPz::Rmpz_div_2exp($t, $n, $s + 1);
7090  
7091 foreach my $bit (split(//, Math::GMPz::Rmpz_get_str($t, 2))) {
7092  
7093 Math::GMPz::Rmpz_mul($Q1, $Q1, $Q2);
7094  
7095 if ($bit) {
7096 Math::GMPz::Rmpz_mul($Q2, $Q1, $Q);
7097 Math::GMPz::Rmpz_mul($U1, $U1, $V2);
7098 Math::GMPz::Rmpz_mul($V1, $V1, $V2);
7099  
7100 Math::GMPz::Rmpz_mul($V2, $V2, $V2);
7101 Math::GMPz::Rmpz_submul($V1, $Q1, $P);
7102 Math::GMPz::Rmpz_submul_ui($V2, $Q2, 2);
7103 }
7104 else {
7105 Math::GMPz::Rmpz_set($Q2, $Q1);
7106 Math::GMPz::Rmpz_mul($U1, $U1, $V1);
7107 Math::GMPz::Rmpz_mul($V2, $V2, $V1);
7108 Math::GMPz::Rmpz_sub($U1, $U1, $Q1);
7109 Math::GMPz::Rmpz_mul($V1, $V1, $V1);
7110 Math::GMPz::Rmpz_submul($V2, $Q1, $P);
7111 Math::GMPz::Rmpz_submul_ui($V1, $Q2, 2);
7112 }
7113 }
7114  
7115 Math::GMPz::Rmpz_mul($Q1, $Q1, $Q2);
7116 Math::GMPz::Rmpz_mul($Q2, $Q1, $Q);
7117 Math::GMPz::Rmpz_mul($U1, $U1, $V1);
7118 Math::GMPz::Rmpz_mul($V1, $V1, $V2);
7119 Math::GMPz::Rmpz_sub($U1, $U1, $Q1);
7120 Math::GMPz::Rmpz_submul($V1, $Q1, $P);
7121 Math::GMPz::Rmpz_mul($Q1, $Q1, $Q2);
7122  
7123 for (1 .. $s) {
7124 Math::GMPz::Rmpz_mul($U1, $U1, $V1);
7125 Math::GMPz::Rmpz_mul($V1, $V1, $V1);
7126 Math::GMPz::Rmpz_submul_ui($V1, $Q1, 2);
7127 Math::GMPz::Rmpz_mul($Q1, $Q1, $Q1);
7128 }
7129  
7130 return ($U1, $V1);
7131 }
7132  
7133 sub __lucasUVmod__ {
7134 my ($P, $Q, $n, $m) = @_;
7135  
7136 my $U1 = Math::GMPz::Rmpz_init_set_ui(1);
7137  
7138 my ($V1, $V2) = (Math::GMPz::Rmpz_init_set_ui(2), Math::GMPz::Rmpz_init_set($P));
7139 my ($Q1, $Q2) = (Math::GMPz::Rmpz_init_set_ui(1), Math::GMPz::Rmpz_init_set_ui(1));
7140  
7141 my $t = Math::GMPz::Rmpz_init();
7142 my $s = Math::GMPz::Rmpz_scan1($n, 0);
7143  
7144 Math::GMPz::Rmpz_div_2exp($t, $n, $s + 1);
7145  
7146 foreach my $bit (split(//, Math::GMPz::Rmpz_get_str($t, 2))) {
7147  
7148 Math::GMPz::Rmpz_mul($Q1, $Q1, $Q2);
7149 Math::GMPz::Rmpz_mod($Q1, $Q1, $m);
7150  
7151 if ($bit) {
7152 Math::GMPz::Rmpz_mul($Q2, $Q1, $Q);
7153 Math::GMPz::Rmpz_mul($U1, $U1, $V2);
7154 Math::GMPz::Rmpz_mul($V1, $V1, $V2);
7155  
7156 Math::GMPz::Rmpz_powm_ui($V2, $V2, 2, $m);
7157 Math::GMPz::Rmpz_submul($V1, $Q1, $P);
7158 Math::GMPz::Rmpz_submul_ui($V2, $Q2, 2);
7159  
7160 Math::GMPz::Rmpz_mod($V1, $V1, $m);
7161 Math::GMPz::Rmpz_mod($U1, $U1, $m);
7162 }
7163 else {
7164 Math::GMPz::Rmpz_set($Q2, $Q1);
7165 Math::GMPz::Rmpz_mul($U1, $U1, $V1);
7166 Math::GMPz::Rmpz_mul($V2, $V2, $V1);
7167 Math::GMPz::Rmpz_sub($U1, $U1, $Q1);
7168  
7169 Math::GMPz::Rmpz_powm_ui($V1, $V1, 2, $m);
7170 Math::GMPz::Rmpz_submul($V2, $Q1, $P);
7171 Math::GMPz::Rmpz_submul_ui($V1, $Q2, 2);
7172  
7173 Math::GMPz::Rmpz_mod($V2, $V2, $m);
7174 Math::GMPz::Rmpz_mod($U1, $U1, $m);
7175 }
7176 }
7177  
7178 Math::GMPz::Rmpz_mul($Q1, $Q1, $Q2);
7179 Math::GMPz::Rmpz_mul($Q2, $Q1, $Q);
7180 Math::GMPz::Rmpz_mul($U1, $U1, $V1);
7181 Math::GMPz::Rmpz_mul($V1, $V1, $V2);
7182 Math::GMPz::Rmpz_sub($U1, $U1, $Q1);
7183 Math::GMPz::Rmpz_submul($V1, $Q1, $P);
7184 Math::GMPz::Rmpz_mul($Q1, $Q1, $Q2);
7185  
7186 for (1 .. $s) {
7187 Math::GMPz::Rmpz_mul($U1, $U1, $V1);
7188 Math::GMPz::Rmpz_mod($U1, $U1, $m);
7189 Math::GMPz::Rmpz_powm_ui($V1, $V1, 2, $m);
7190 Math::GMPz::Rmpz_submul_ui($V1, $Q1, 2);
7191 Math::GMPz::Rmpz_powm_ui($Q1, $Q1, 2, $m);
7192 }
7193  
7194 Math::GMPz::Rmpz_mod($U1, $U1, $m);
7195 Math::GMPz::Rmpz_mod($V1, $V1, $m);
7196  
7197 return ($U1, $V1);
7198 }
7199  
7200 sub lucasU ($$$) {
7201 my ($P, $Q, $n) = @_;
7202  
7203 $P = _star2mpz($P) // goto &nan;
7204 $Q = _star2mpz($Q) // goto &nan;
7205 $n = _star2mpz($n) // goto &nan;
7206  
7207 # U_0(P, Q) = 0
7208 Math::GMPz::Rmpz_sgn($n) || goto &zero;
7209  
7210 # undefined for n < 0
7211 Math::GMPz::Rmpz_sgn($n) < 0 && goto &nan;
7212  
7213 my $D = Math::GMPz::Rmpz_init();
7214  
7215 Math::GMPz::Rmpz_mul($D, $P, $P);
7216 Math::GMPz::Rmpz_submul_ui($D, $Q, 4);
7217  
7218 # When `P*P - 4*Q != 0`, we can use a faster algorithm
7219 if (Math::GMPz::Rmpz_sgn($D)) {
7220  
7221 my ($V1, $V2) = __lucasV__($P, $Q, $n);
7222  
7223 Math::GMPz::Rmpz_mul_2exp($V2, $V2, 1);
7224 Math::GMPz::Rmpz_submul($V2, $V1, $P);
7225 Math::GMPz::Rmpz_divexact($V2, $V2, $D);
7226  
7227 return bless \$V2;
7228 }
7229  
7230 my ($U) = __lucasUV__($P, $Q, $n);
7231  
7232 bless \$U;
7233 }
7234  
7235 sub lucasUmod ($$$$) {
7236 my ($P, $Q, $n, $m) = @_;
7237  
7238 $P = _star2mpz($P) // goto &nan;
7239 $Q = _star2mpz($Q) // goto &nan;
7240 $n = _star2mpz($n) // goto &nan;
7241 $m = _star2mpz($m) // goto &nan;
7242  
7243 # undefined for m=0
7244 Math::GMPz::Rmpz_sgn($m) || goto &nan;
7245  
7246 # U_0(P, Q) = 0
7247 Math::GMPz::Rmpz_sgn($n) || goto &zero;
7248  
7249 # undefined for n < 0
7250 Math::GMPz::Rmpz_sgn($n) < 0 && goto &nan;
7251  
7252 my $D = Math::GMPz::Rmpz_init();
7253  
7254 Math::GMPz::Rmpz_mul($D, $P, $P);
7255 Math::GMPz::Rmpz_submul_ui($D, $Q, 4);
7256  
7257 # When `gcd(P*P - 4*Q, m) = 1`, we can use a faster algorithm
7258 if (Math::GMPz::Rmpz_invert($D, $D, $m)) {
7259  
7260 my ($V1, $V2) = __lucasVmod__($P, $Q, $n, $m);
7261  
7262 Math::GMPz::Rmpz_mul_2exp($V2, $V2, 1);
7263 Math::GMPz::Rmpz_submul($V2, $V1, $P);
7264 Math::GMPz::Rmpz_mul($V2, $V2, $D);
7265 Math::GMPz::Rmpz_mod($V2, $V2, $m);
7266  
7267 return bless \$V2;
7268 }
7269  
7270 my ($U) = __lucasUVmod__($P, $Q, $n, $m);
7271  
7272 bless \$U;
7273 }
7274  
7275 sub lucasV ($$$) {
7276 my ($P, $Q, $n) = @_;
7277  
7278 $P = _star2mpz($P) // goto &nan;
7279 $Q = _star2mpz($Q) // goto &nan;
7280 $n = _star2mpz($n) // goto &nan;
7281  
7282 # undefined for n < 0
7283 Math::GMPz::Rmpz_sgn($n) < 0 && goto &nan;
7284  
7285 my ($V) = __lucasV__($P, $Q, $n);
7286  
7287 bless \$V;
7288 }
7289  
7290 sub lucasVmod ($$$$) {
7291 my ($P, $Q, $n, $m) = @_;
7292  
7293 $P = _star2mpz($P) // goto &nan;
7294 $Q = _star2mpz($Q) // goto &nan;
7295 $n = _star2mpz($n) // goto &nan;
7296 $m = _star2mpz($m) // goto &nan;
7297  
7298 # undefined for m=0
7299 Math::GMPz::Rmpz_sgn($m) || goto &nan;
7300  
7301 # undefined for n < 0
7302 Math::GMPz::Rmpz_sgn($n) < 0 && goto &nan;
7303  
7304 my ($V) = __lucasVmod__($P, $Q, $n, $m);
7305  
7306 bless \$V;
7307 }
7308  
7309 #
7310 ## fibonacci(n) mod m
7311 #
7312  
7313 sub fibmod ($$) {
7314 my ($n, $m) = @_;
7315  
7316 $n = _star2mpz($n) // goto &nan;
7317 $m = _star2mpz($m) // goto &nan;
7318  
7319 Math::GMPz::Rmpz_sgn($m) == 0 and goto &nan;
7320  
7321 my $sgn = Math::GMPz::Rmpz_sgn($n);
7322  
7323 $sgn < 0 and goto &nan;
7324 $sgn == 0 and goto &zero;
7325  
7326 #<<<
7327 my ($f, $g, $w) = (
7328 Math::GMPz::Rmpz_init_set_ui(0),
7329 Math::GMPz::Rmpz_init_set_ui(1),
7330 );
7331 #>>>
7332  
7333 my $t = Math::GMPz::Rmpz_init();
7334  
7335 foreach my $bit (split(//, substr(Math::GMPz::Rmpz_get_str($n, 2), 1))) {
7336  
7337 Math::GMPz::Rmpz_powm_ui($g, $g, 2, $m);
7338 Math::GMPz::Rmpz_powm_ui($f, $f, 2, $m);
7339  
7340 Math::GMPz::Rmpz_mul_2exp($t, $g, 2);
7341 Math::GMPz::Rmpz_sub($t, $t, $f);
7342  
7343 $w
7344 ? Math::GMPz::Rmpz_add_ui($t, $t, 2)
7345 : Math::GMPz::Rmpz_sub_ui($t, $t, 2);
7346  
7347 Math::GMPz::Rmpz_add($f, $f, $g);
7348  
7349 if ($bit) {
7350 Math::GMPz::Rmpz_sub($f, $t, $f);
7351 Math::GMPz::Rmpz_set($g, $t);
7352 $w = 0;
7353 }
7354 else {
7355 Math::GMPz::Rmpz_sub($g, $t, $f);
7356 $w = 1;
7357 }
7358 }
7359  
7360 Math::GMPz::Rmpz_mod($g, $g, $m);
7361  
7362 return bless \$g;
7363 }
7364  
7365 #
7366 ## lucas(n) mod m
7367 #
7368  
7369 sub lucasmod ($$) {
7370 my ($n, $m) = @_;
7371  
7372 $n = _star2mpz($n) // goto &nan;
7373 $m = _star2mpz($m) // goto &nan;
7374  
7375 Math::GMPz::Rmpz_sgn($m) == 0 and goto &nan;
7376  
7377 my $sgn = Math::GMPz::Rmpz_sgn($n);
7378  
7379 $sgn < 0 and goto &nan;
7380 $sgn == 0 and return bless \Math::GMPz::Rmpz_init_set_ui(2);
7381  
7382 #<<<
7383 my ($f, $g, $w) = (
7384 Math::GMPz::Rmpz_init_set_ui(3),
7385 Math::GMPz::Rmpz_init_set_ui(1),
7386 );
7387 #>>>
7388  
7389 foreach my $bit (split(//, substr(Math::GMPz::Rmpz_get_str($n, 2), 1))) {
7390  
7391 Math::GMPz::Rmpz_powm_ui($g, $g, 2, $m);
7392 Math::GMPz::Rmpz_powm_ui($f, $f, 2, $m);
7393  
7394 if ($w) {
7395 Math::GMPz::Rmpz_sub_ui($g, $g, 2);
7396 Math::GMPz::Rmpz_add_ui($f, $f, 2);
7397 }
7398 else {
7399 Math::GMPz::Rmpz_add_ui($g, $g, 2);
7400 Math::GMPz::Rmpz_sub_ui($f, $f, 2);
7401 }
7402  
7403 if ($bit) {
7404 Math::GMPz::Rmpz_sub($g, $f, $g);
7405 $w = 0;
7406 }
7407 else {
7408 Math::GMPz::Rmpz_sub($f, $f, $g);
7409 $w = 1;
7410 }
7411 }
7412  
7413 Math::GMPz::Rmpz_mod($g, $g, $m);
7414  
7415 bless \$g;
7416 }
7417  
7418 #
7419 ## Chebyshev polynomials: T_n(x)
7420 #
7421  
7422 sub _quadratic_mul {
7423 my ($xa, $xb, $ya, $yb, $w) = @_;
7424 (__add__(__mul__($xa, $ya), __mul__(__mul__($xb, $yb), $w)), __add__(__mul__($xa, $yb), __mul__($xb, $ya)));
7425 }
7426  
7427 sub _quadratic_invmod {
7428 my ($xa, $xb, $w, $m) = @_;
7429  
7430 $xa = __mod__($xa, $m);
7431 $xb = __mod__($xb, $m);
7432  
7433 my $t = invmod(__sub__(__mul__($xa, $xa), __mul__(__mul__($xb, $xb), $w)), $m);
7434 (__mod__(__mul__($xa, $$t), $m), __mod__(__neg__(__mul__($xb, $$t)), $m));
7435 }
7436  
7437 sub _quadratic_pow {
7438 my ($x, $y, $w, $n) = @_;
7439  
7440 my ($c1, $c2) = (Math::GMPz::Rmpz_init_set_ui(1), Math::GMPz::Rmpz_init_set_ui(0));
7441  
7442 for (; $n > 0 ; $n >>= 1) {
7443  
7444 if ($n & 1) {
7445 ($c1, $c2) = _quadratic_mul($c1, $c2, $x, $y, $w);
7446 }
7447  
7448 ($x, $y) = _quadratic_mul($x, $y, $x, $y, $w);
7449 }
7450  
7451 ($c1, $c2);
7452 }
7453  
7454 sub _mpz_quadratic_powmod {
7455 my ($x, $y, $a, $b, $w, $n, $m) = @_;
7456  
7457 state $t = Math::GMPz::Rmpz_init_nobless();
7458  
7459 for my $i (0 .. Math::GMPz::Rmpz_sizeinbase($n, 2) - 1) {
7460  
7461 if (Math::GMPz::Rmpz_tstbit($n, $i)) {
7462  
7463 # (x, y) = ((a*x + b*y*w) % m, (a*y + b*x) % m)
7464 Math::GMPz::Rmpz_mul($t, $b, $w);
7465 Math::GMPz::Rmpz_mul($t, $t, $y);
7466 Math::GMPz::Rmpz_addmul($t, $a, $x);
7467 Math::GMPz::Rmpz_mul($y, $y, $a);
7468 Math::GMPz::Rmpz_addmul($y, $x, $b);
7469 Math::GMPz::Rmpz_mod($x, $t, $m);
7470 Math::GMPz::Rmpz_mod($y, $y, $m);
7471 }
7472  
7473 # (a, b) = ((a*a + b*b*w) % m, (2*a*b) % m)
7474 Math::GMPz::Rmpz_mul($t, $a, $b);
7475 Math::GMPz::Rmpz_mul_2exp($t, $t, 1);
7476 Math::GMPz::Rmpz_powm_ui($a, $a, 2, $m);
7477 Math::GMPz::Rmpz_powm_ui($b, $b, 2, $m);
7478 Math::GMPz::Rmpz_addmul($a, $b, $w);
7479 Math::GMPz::Rmpz_mod($b, $t, $m);
7480 }
7481 }
7482  
7483 sub _quadratic_powmod {
7484 my ($x, $y, $w, $n, $m) = @_;
7485  
7486 my $negative_power = 0;
7487  
7488 if (Math::GMPz::Rmpz_sgn($n) < 0) {
7489 $n = Math::GMPz::Rmpz_init_set($n); # copy
7490 Math::GMPz::Rmpz_abs($n, $n);
7491 $negative_power = 1;
7492 }
7493  
7494 my ($c1, $c2) = (Math::GMPz::Rmpz_init_set_ui(1), Math::GMPz::Rmpz_init_set_ui(0));
7495  
7496 if (ref($x) eq 'Math::GMPz' and ref($y) eq 'Math::GMPz' and ref($w) eq 'Math::GMPz' and ref($m) eq 'Math::GMPz') {
7497 _mpz_quadratic_powmod($c1, $c2, $x, $y, $w, $n, $m);
7498 }
7499 else {
7500 for my $i (0 .. Math::GMPz::Rmpz_sizeinbase($n, 2) - 1) {
7501  
7502 if (Math::GMPz::Rmpz_tstbit($n, $i)) {
7503 ($c1, $c2) = map { __mod__($_, $m) } _quadratic_mul($c1, $c2, $x, $y, $w);
7504 }
7505  
7506 ($x, $y) = map { __mod__($_, $m) } _quadratic_mul($x, $y, $x, $y, $w);
7507 }
7508 }
7509  
7510 if ($negative_power) {
7511 ($c1, $c2) = _quadratic_invmod($c1, $c2, $w, $m);
7512 }
7513  
7514 ($c1, $c2);
7515 }
7516  
7517 sub quadratic_powmod ($$$$$) {
7518 my ($x, $y, $w, $n, $m) = @_;
7519  
7520 $x = _star2obj($x);
7521 $y = _star2obj($y);
7522 $w = _star2obj($w);
7523 $n = _star2mpz($n) // return (nan(), nan());
7524 $m = _star2obj($m);
7525  
7526 my ($r1, $r2) = _quadratic_powmod($x, $y, $w, $n, $m);
7527  
7528 ((bless \$r1), (bless \$r2));
7529 }
7530  
7531 sub chebyshevT ($$) {
7532 my ($n, $x) = @_;
7533  
7534 $n = _star2si($n) // goto &nan;
7535 $x = _star2obj($x);
7536  
7537 $n = -$n if $n < 0;
7538 $n == 0 and goto &one;
7539 $n == 1 and return bless \$x;
7540  
7541 if (ref($x) eq 'Math::GMPz' or (ref($x) eq 'Math::GMPq' and __is_int__($x))) {
7542 return lucasV(2 * $x, 1, $n)->idiv(2);
7543 }
7544  
7545 # T_n(x) = 1/2 * ((x - sqrt(x^2 - 1))^n + (x + sqrt(x^2 - 1))^n)
7546  
7547 my ($r1, $r2) = _quadratic_pow($x, Math::GMPz::Rmpz_init_set_si(-1), __dec__(__mul__($x, $x)), $n);
7548 bless \$r1;
7549 }
7550  
7551 #
7552 ## Modular Chebyshev polynomials: T_n(x) mod m
7553 #
7554  
7555 sub chebyshevTmod ($$$) {
7556 my ($n, $x, $m) = @_;
7557  
7558 $n = _star2mpz($n) // goto &nan;
7559 $x = _star2obj($x);
7560 $m = _star2mpz($m) // goto &nan;
7561  
7562 if (Math::GMPz::Rmpz_sgn($n) < 0) {
7563 $n = Math::GMPz::Rmpz_init_set($n); # copy
7564 Math::GMPz::Rmpz_abs($n, $n);
7565 }
7566  
7567 if (Math::GMPz::Rmpz_odd_p($m) and (ref($x) eq 'Math::GMPz' or (ref($x) eq 'Math::GMPq' and __is_int__($x)))) {
7568 return lucasVmod(2 * $x, 1, $n, $m)->divmod(2, $m);
7569 }
7570  
7571 # T_n(x) = 1/2 * ((x - sqrt(x^2 - 1))^n + (x + sqrt(x^2 - 1))^n)
7572  
7573 my ($r1, $r2) = _quadratic_powmod($x, Math::GMPz::Rmpz_init_set_si(-1), __dec__(__mul__($x, $x)), $n, $m);
7574 my $r = bless \$r1;
7575 $r = $r->mod($m);
7576 $r;
7577 }
7578  
7579 #
7580 ## Chebyshev polynomials: U_n(x)
7581 #
7582  
7583 sub chebyshevU ($$) {
7584 my ($n, $x) = @_;
7585  
7586 $n = _star2si($n) // goto &nan;
7587 $n == 0 and goto &one;
7588  
7589 my $negative = 0;
7590  
7591 if ($n < 0) {
7592  
7593 $n == -1 and goto &zero;
7594 $n == -2 and goto &mone;
7595  
7596 $n = -$n - 2;
7597 $negative = 1;
7598 }
7599  
7600 $x = _star2obj($x);
7601  
7602 if (ref($x) eq 'Math::GMPz' or (ref($x) eq 'Math::GMPz' and __is_int__($x))) {
7603 my $r = lucasU(2 * $x, 1, $n + 1);
7604 $r = $r->neg if $negative;
7605 return $r;
7606 }
7607  
7608 # U_n(x) = ((x + sqrt(x^2 - 1))^(n+1) - (x - sqrt(x^2 - 1))^(n+1)) / (2 * sqrt(x^2 - 1))
7609  
7610 my ($r1, $r2) = _quadratic_pow($x, Math::GMPz::Rmpz_init_set_ui(1), __dec__(__mul__($x, $x)), $n + 1);
7611  
7612 my $r = bless \$r2;
7613 $r = $r->neg if $negative;
7614 $r;
7615 }
7616  
7617 #
7618 ## Modular Chebyshev polynomials: U_n(x) mod m
7619 #
7620  
7621 sub chebyshevUmod {
7622 my ($n, $x, $m) = @_;
7623  
7624 $n = _star2mpz($n) // goto &nan;
7625 $x = _star2obj($x);
7626 $m = _star2mpz($m) // goto &nan;
7627  
7628 my $negative = 0;
7629  
7630 if (Math::GMPz::Rmpz_sgn($n) < 0) {
7631  
7632 if (Math::GMPz::Rmpz_cmp_si($n, -1) == 0) {
7633 return (zero()->mod(bless \$m));
7634 }
7635  
7636 if (Math::GMPz::Rmpz_cmp_si($n, -2) == 0) {
7637 return (mone()->mod(bless \$m));
7638 }
7639  
7640 $n = -$n - 2;
7641 $negative = 1;
7642 }
7643  
7644 if (ref($x) eq 'Math::GMPz' or (ref($x) eq 'Math::GMPq' and __is_int__($x))) {
7645 my $r = lucasUmod(2 * $x, 1, $n + 1, $m);
7646 $r = $r->neg->mod($m) if $negative;
7647 return $r;
7648 }
7649  
7650 # U_n(x) = ((x + sqrt(x^2 - 1))^(n+1) - (x - sqrt(x^2 - 1))^(n+1)) / (2 * sqrt(x^2 - 1))
7651  
7652 my ($r1, $r2) = _quadratic_pow($x, Math::GMPz::Rmpz_init_set_ui(1), __dec__(__mul__($x, $x)), $n + 1, $m);
7653  
7654 my $r = bless \$r2;
7655 $r = $r->neg if $negative;
7656 $r = $r->mod($m);
7657 $r;
7658 }
7659  
7660 #
7661 ## Laguerre polynomials: L_n(x)
7662 #
7663  
7664 sub laguerreL ($$) {
7665 my ($n, $x) = @_;
7666  
7667 $n = _star2ui($n) // goto &nan;
7668 $x = _star2obj($x);
7669  
7670 $n == 0 and goto &one; # L_0(x) = 1
7671 $n == 1 and return bless \__sub__(1, $x); # L_1(x) = 1-x
7672  
7673 my $t = Math::GMPz::Rmpz_init();
7674 my $u = Math::GMPz::Rmpz_init_set_ui(1);
7675  
7676 my @terms;
7677 foreach my $k (0 .. $n) {
7678 Math::GMPz::Rmpz_bin_uiui($t, $n, $k);
7679 Math::GMPz::Rmpz_neg($t, $t) if ($k & 1);
7680 push @terms, __div__(__mul__(__pow__($x, $k), $t), $u);
7681 Math::GMPz::Rmpz_mul_ui($u, $u, $k + 1);
7682 }
7683  
7684 bless \_binsplit(\@terms, \&__add__);
7685 }
7686  
7687 #
7688 ## Legendre polynomials: P_n(x)
7689 #
7690  
7691 sub legendreP ($$) {
7692 my ($n, $x) = @_;
7693  
7694 $n = _star2ui($n) // goto &nan;
7695 $x = _star2obj($x);
7696  
7697 $n == 0 and goto &one;
7698 $n == 1 and return bless \$x;
7699  
7700 my $x1 = __dec__($x);
7701 my $x2 = __inc__($x);
7702  
7703 my $t = Math::GMPz::Rmpz_init();
7704  
7705 my @terms;
7706 foreach my $k (0 .. $n) {
7707 Math::GMPz::Rmpz_bin_uiui($t, $n, $k);
7708 Math::GMPz::Rmpz_mul($t, $t, $t);
7709 push @terms, __mul__(__mul__(__pow__($x1, $n - $k), __pow__($x2, $k)), $t);
7710 }
7711  
7712 my $sum = _binsplit(\@terms, \&__add__);
7713  
7714 Math::GMPz::Rmpz_set_ui($t, 0);
7715 Math::GMPz::Rmpz_setbit($t, $n);
7716  
7717 bless \__div__($sum, $t);
7718 }
7719  
7720 #
7721 ## The physicists' Hermite polynomials H_n(x)
7722 #
7723  
7724 sub hermiteH ($$) {
7725 my ($n, $x) = @_;
7726  
7727 $n = _star2ui($n) // goto &nan;
7728 $x = _star2obj($x);
7729  
7730 $x = __add__($x, $x);
7731  
7732 $n == 0 and goto &one;
7733 $n == 1 and return bless \$x;
7734  
7735 my $t = Math::GMPz::Rmpz_init();
7736 my $u = Math::GMPz::Rmpz_init_set_ui(1);
7737  
7738 my $v = Math::GMPz::Rmpz_init();
7739 Math::GMPz::Rmpz_fac_ui($v, $n);
7740  
7741 my @terms;
7742 foreach my $m (0 .. $n >> 1) {
7743 Math::GMPz::Rmpz_mul($t, $v, $u);
7744 Math::GMPz::Rmpz_neg($t, $t) if ($m & 1);
7745  
7746 push @terms, __div__(__pow__($x, $n - ($m << 1)), $t);
7747  
7748 my $d = ($n - ($m << 1)) * ($n - ($m << 1) - 1);
7749 Math::GMPz::Rmpz_divexact_ui($v, $v, $d) if $d;
7750 Math::GMPz::Rmpz_mul_ui($u, $u, $m + 1);
7751 }
7752  
7753 my $sum = _binsplit(\@terms, \&__add__);
7754 Math::GMPz::Rmpz_fac_ui($v, $n);
7755 bless \__mul__($sum, $v);
7756 }
7757  
7758 #
7759 ## The probabilists' Hermite polynomials He_n(x)
7760 #
7761  
7762 sub hermiteHe ($$) {
7763 my ($n, $x) = @_;
7764  
7765 $n = _star2ui($n) // goto &nan;
7766 $x = _star2obj($x);
7767  
7768 $n == 0 and goto &one;
7769 $n == 1 and return bless \$x;
7770  
7771 my $t = Math::GMPz::Rmpz_init();
7772 my $u = Math::GMPz::Rmpz_init_set_ui(1);
7773  
7774 my $v = Math::GMPz::Rmpz_init();
7775 Math::GMPz::Rmpz_fac_ui($v, $n);
7776  
7777 my @terms;
7778 foreach my $m (0 .. $n >> 1) {
7779 Math::GMPz::Rmpz_mul($t, $v, $u);
7780 Math::GMPz::Rmpz_mul_2exp($t, $t, $m);
7781 Math::GMPz::Rmpz_neg($t, $t) if ($m & 1);
7782  
7783 push @terms, __div__(__pow__($x, $n - ($m << 1)), $t);
7784  
7785 my $d = ($n - ($m << 1)) * ($n - ($m << 1) - 1);
7786 Math::GMPz::Rmpz_divexact_ui($v, $v, $d) if $d;
7787 Math::GMPz::Rmpz_mul_ui($u, $u, $m + 1);
7788 }
7789  
7790 my $sum = _binsplit(\@terms, \&__add__);
7791 Math::GMPz::Rmpz_fac_ui($v, $n);
7792 bless \__mul__($sum, $v);
7793 }
7794  
7795 #
7796 ## Primorial
7797 #
7798 sub primorial ($) {
7799 my ($n) = @_;
7800  
7801 $n = _star2ui($n) // goto &nan;
7802  
7803 my $r = Math::GMPz::Rmpz_init();
7804 Math::GMPz::Rmpz_primorial_ui($r, $n);
7805 bless \$r;
7806 }
7807  
7808 sub min {
7809 my @terms = map { ref($_) eq __PACKAGE__ ? $$_ : _star2obj($_) } @_;
7810  
7811 @terms || return undef;
7812  
7813 my $min = shift(@terms);
7814 foreach my $curr (@terms) {
7815 if ((__cmp__($curr, $min) // return undef) < 0) {
7816 $min = $curr;
7817 }
7818 }
7819  
7820 bless \$min;
7821 }
7822  
7823 sub max {
7824 my @terms = map { ref($_) eq __PACKAGE__ ? $$_ : _star2obj($_) } @_;
7825  
7826 @terms || return undef;
7827  
7828 my $max = shift(@terms);
7829 foreach my $curr (@terms) {
7830 if ((__cmp__($curr, $max) // return undef) > 0) {
7831 $max = $curr;
7832 }
7833 }
7834  
7835 bless \$max;
7836 }
7837  
7838 sub sum {
7839 my @terms = map { ref($_) eq __PACKAGE__ ? $$_ : _star2obj($_) } @_;
7840  
7841 @terms || goto &zero;
7842  
7843 my @non_mpz;
7844 my $sum = Math::GMPz::Rmpz_init_set_ui(0);
7845  
7846 foreach my $n (@terms) {
7847 if (ref($n) eq 'Math::GMPz') {
7848 Math::GMPz::Rmpz_add($sum, $sum, $n);
7849 }
7850 else {
7851 push @non_mpz, $n;
7852 }
7853 }
7854  
7855 if (@non_mpz) {
7856 $sum = __add__($sum, _binsplit(\@non_mpz, \&__add__));
7857 }
7858  
7859 bless \$sum;
7860 }
7861  
7862 sub prod {
7863 my @terms = map { ref($_) eq __PACKAGE__ ? $$_ : _star2obj($_) } @_;
7864 @terms || goto &one;
7865 bless \_binsplit(\@terms, \&__mul__);
7866 }
7867  
7868 sub _secant_numbers {
7869 my ($n) = @_;
7870  
7871 state @cache;
7872  
7873 if ($n <= $#cache) {
7874 return @cache;
7875 }
7876  
7877 $n <<= 1 if ($n <= 512);
7878  
7879 my @S = (Math::GMPz::Rmpz_init_set_ui(1));
7880  
7881 foreach my $k (1 .. $n) {
7882 Math::GMPz::Rmpz_mul_ui($S[$k] = Math::GMPz::Rmpz_init(), $S[$k - 1], $k);
7883 }
7884  
7885 foreach my $k (1 .. $n) {
7886 foreach my $j ($k + 1 .. $n) {
7887 Math::GMPz::Rmpz_addmul_ui($S[$j], $S[$j - 1], ($j - $k) * ($j - $k + 2));
7888 }
7889 }
7890  
7891 push @cache, @S[@cache .. (@S <= 1024 ? $#S : 1024)];
7892  
7893 return @S;
7894 }
7895  
7896 sub _tangent_numbers {
7897 my ($n) = @_;
7898  
7899 state @cache;
7900  
7901 if ($n <= $#cache) {
7902 return @cache;
7903 }
7904  
7905 $n <<= 1 if ($n <= 512);
7906  
7907 my @T = (Math::GMPz::Rmpz_init_set_ui(1));
7908  
7909 foreach my $k (1 .. $n) {
7910 Math::GMPz::Rmpz_mul_ui($T[$k] = Math::GMPz::Rmpz_init(), $T[$k - 1], $k);
7911 }
7912  
7913 foreach my $k (1 .. $n) {
7914 foreach my $j ($k .. $n) {
7915 Math::GMPz::Rmpz_mul_ui($T[$j], $T[$j], $j - $k + 2);
7916 Math::GMPz::Rmpz_addmul_ui($T[$j], $T[$j - 1], $j - $k);
7917 }
7918 }
7919  
7920 push @cache, @T[@cache .. (@T <= 1024 ? $#T : 1024)];
7921  
7922 return @T;
7923 }
7924  
7925 sub _bernoulli_numbers {
7926 my ($n) = @_;
7927  
7928 $n = ($n >> 1) + 1;
7929  
7930 state @cache;
7931  
7932 if ($n <= $#cache) {
7933 return @cache;
7934 }
7935  
7936 my @B;
7937 my @T = _tangent_numbers($n);
7938  
7939 my $t = Math::GMPz::Rmpz_init();
7940  
7941 foreach my $k (scalar(@cache) .. 2 * @T) {
7942  
7943 $k % 2 == 0 or $k == 1 or next;
7944  
7945 my $q = Math::GMPq::Rmpq_init();
7946  
7947 if ($k == 0) {
7948 Math::GMPq::Rmpq_set_ui($q, 1, 1);
7949 $B[$k] = $q;
7950 next;
7951 }
7952  
7953 if ($k == 1) {
7954 Math::GMPq::Rmpq_set_si($q, -1, 2);
7955 $B[$k] = $q;
7956 next;
7957 }
7958  
7959 # T_k
7960 Math::GMPz::Rmpz_mul_ui($t, $T[($k >> 1) - 1], $k);
7961 Math::GMPz::Rmpz_neg($t, $t) if ((($k >> 1) - 1) & 1);
7962 Math::GMPq::Rmpq_set_z($q, $t);
7963  
7964 # (2^k - 1) * 2^k
7965 Math::GMPz::Rmpz_set_ui($t, 0);
7966 Math::GMPz::Rmpz_setbit($t, $k);
7967 Math::GMPz::Rmpz_sub_ui($t, $t, 1);
7968 Math::GMPz::Rmpz_mul_2exp($t, $t, $k);
7969  
7970 # B_k = q
7971 Math::GMPq::Rmpq_div_z($q, $q, $t);
7972  
7973 $B[($k >> 1) + 1] = $q;
7974 }
7975  
7976 push @cache, @B[@cache .. (@B <= 1024 ? $#B : 1024)];
7977  
7978 return (@cache, (@B > @cache ? @B[@cache .. $#B] : ()));
7979 }
7980  
7981 sub bernoulli_polynomial ($$) {
7982 my ($n, $x) = @_;
7983  
7984 #
7985 ## B_n(x) = Sum_{k=0..n} binomial(n, k) * bernoulli(n-k) * x^k
7986 #
7987  
7988 $n = _star2ui($n) // goto &nan;
7989 $x = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
7990  
7991 my @B = _bernoulli_numbers($n);
7992  
7993 my $u = $n + 1;
7994 my $z = Math::GMPz::Rmpz_init();
7995 my $q = Math::GMPq::Rmpq_init();
7996  
7997 my @terms;
7998  
7999 foreach my $k (0 .. $n) {
8000  
8001 --$u & 1 and $u > 1 and next; # B_n = 0 for odd n > 1
8002  
8003 Math::GMPz::Rmpz_bin_uiui($z, $n, $k);
8004 Math::GMPq::Rmpq_mul_z($q, $u <= 1 ? $B[$u] : $B[($u >> 1) + 1], $z);
8005  
8006 push @terms, __mul__(__pow__($x, $k), $q);
8007 }
8008  
8009 bless \_binsplit([reverse @terms], \&__add__);
8010 }
8011  
8012 #
8013 ## Bernoulli number
8014 #
8015  
8016 # Algorithm due to Kevin J. McGown (December 8, 2005).
8017 # Described in his paper: "Computing Bernoulli Numbers Quickly".
8018  
8019 sub __bernfrac__ {
8020 my ($n) = @_; # $n is an unsigned integer
8021  
8022 #<<<
8023 # B(n) = (-1)^(n/2 + 1) * zeta(n)*2*n! / (2*pi)^n
8024  
8025 if ($n == 0) {
8026 goto &_one;
8027 }
8028  
8029 if ($n == 1) {
8030 my $r = Math::GMPq::Rmpq_init();
8031 Math::GMPq::Rmpq_set_ui($r, 1, 2);
8032 return $r;
8033 }
8034  
8035 if (($n & 1) and ($n > 1)) { # Bn = 0 for odd n>1
8036 goto &_zero;
8037 }
8038  
8039 if ($n < 512) {
8040 return ((_bernoulli_numbers($n))[($n>>1)+1]);
8041 }
8042  
8043 state $round = Math::MPFR::MPFR_RNDN();
8044 state $tau = 6.28318530717958647692528676655900576839433879875;
8045  
8046 my $log2B = (CORE::log(4 * $tau * $n) / 2 + $n * (CORE::log($n / $tau) - 1)) / CORE::log(2);
8047  
8048 my $prec = CORE::int($n + $log2B) + ($n <= 90 ? 24 : 0);
8049 state $d = Math::GMPz::Rmpz_init_nobless();
8050 Math::GMPz::Rmpz_fac_ui($d, $n); # d = n!
8051  
8052 my $K = Math::MPFR::Rmpfr_init2($prec);
8053 Math::MPFR::Rmpfr_const_pi($K, $round); # K = pi
8054 Math::MPFR::Rmpfr_pow_si($K, $K, -$n, $round); # K = K^(-n)
8055 Math::MPFR::Rmpfr_mul_z($K, $K, $d, $round); # K = K*d
8056 Math::MPFR::Rmpfr_div_2ui($K, $K, $n - 1, $round); # K = K / 2^(n-1)
8057  
8058 # `d` is the denominator of bernoulli(n)
8059 Math::GMPz::Rmpz_set_ui($d, 2); # d = 2
8060  
8061 my @primes = (2);
8062  
8063 { # Sieve the primes <= n+1
8064 # Sieve of Eratosthenes + Dana Jacobsen's optimizations
8065 my $N = $n + 1;
8066  
8067 my @composite;
8068 my $bound = CORE::int(CORE::sqrt($N));
8069  
8070 for (my $i = 3 ; $i <= $bound ; $i += 2) {
8071 if (!exists($composite[$i])) {
8072 for (my $j = $i * $i ; $j <= $N ; $j += 2 * $i) {
8073 undef $composite[$j];
8074 }
8075 }
8076 }
8077  
8078 foreach my $k (1 .. ($N - 1) >> 1) {
8079 if (!exists($composite[2 * $k + 1])) {
8080  
8081 push(@primes, 2 * $k + 1);
8082  
8083 if ($n % (2 * $k) == 0) { # d = d*p iff (p-1)|n
8084 Math::GMPz::Rmpz_mul_ui($d, $d, 2 * $k + 1);
8085 }
8086 }
8087 }
8088 }
8089  
8090 state $N = Math::MPFR::Rmpfr_init2_nobless(64);
8091  
8092 Math::MPFR::Rmpfr_mul_z($K, $K, $d, $round); # K = K*d
8093 Math::MPFR::Rmpfr_set_ui($N, $n - 1, $round); # N = n-1
8094 Math::MPFR::Rmpfr_ui_div($N, 1, $N, $round); # N = 1/N
8095 Math::MPFR::Rmpfr_pow($N, $K, $N, $round); # N = K^N
8096  
8097 Math::MPFR::Rmpfr_ceil($N, $N); # N = ceil(N)
8098  
8099 my $bound = Math::MPFR::Rmpfr_get_ui($N, $round); # bound = int(N)
8100  
8101 my $z = Math::MPFR::Rmpfr_init2($prec); # zeta(n)
8102 my $u = Math::GMPz::Rmpz_init(); # p^n
8103  
8104 Math::MPFR::Rmpfr_set_ui($z, 1, $round); # z = 1
8105  
8106 # `Math::GMPf` would perform slightly faster here.
8107 for (my $i = 0 ; $primes[$i] <= $bound ; ++$i) { # primes <= bound
8108 Math::GMPz::Rmpz_ui_pow_ui($u, $primes[$i], $n); # u = p^n
8109 Math::MPFR::Rmpfr_mul_z($z, $z, $u, $round); # z = z*u
8110 Math::GMPz::Rmpz_sub_ui($u, $u, 1); # u = u-1
8111 Math::MPFR::Rmpfr_div_z($z, $z, $u, $round); # z = z/u
8112 }
8113  
8114 Math::MPFR::Rmpfr_mul($z, $z, $K, $round); # z = z * K
8115 Math::MPFR::Rmpfr_ceil($z, $z); # z = ceil(z)
8116  
8117 my $q = Math::GMPq::Rmpq_init();
8118  
8119 Math::GMPq::Rmpq_set_den($q, $d); # denominator
8120 Math::MPFR::Rmpfr_get_z($d, $z, $round);
8121 Math::GMPz::Rmpz_neg($d, $d) if $n % 4 == 0; # d = -d, iff 4|n
8122 Math::GMPq::Rmpq_set_num($q, $d); # numerator
8123  
8124 #>>>
8125 return $q; # Bn
8126 }
8127  
8128 sub bernfrac ($;$) {
8129 my ($n, $x) = @_;
8130  
8131 @_ == 2 && goto &bernoulli_polynomial;
8132 $n = _star2ui($n) // goto &nan;
8133  
8134 bless \__bernfrac__($n);
8135 }
8136  
8137 *bernoulli = \&bernfrac;
8138  
8139 sub faulhaber_polynomial {
8140 my ($n, $x) = @_;
8141  
8142 $n = _star2ui($n) // goto &nan;
8143 $x = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
8144  
8145 $n += 1;
8146 $x = __inc__($x);
8147  
8148 bernoulli_polynomial($n, $x)->sub(bernfrac($n))->div($n);
8149 }
8150  
8151 *faulhaber = \&faulhaber_polynomial;
8152  
8153 sub euler_polynomial ($$) {
8154 my ($n, $x) = @_;
8155  
8156 #
8157 ## E_n(x) = Sum_{k=0..n} binomial(n, n-k) * euler_number(n-k) / 2^(n-k) * (x - 1/2)^k
8158 #
8159  
8160 $n = _star2ui($n) // goto &nan;
8161 $x = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
8162  
8163 my @S = _secant_numbers($n >> 1);
8164  
8165 my $u = $n + 1;
8166 my $z = Math::GMPz::Rmpz_init();
8167  
8168 $x = __dec__(__add__($x, $x)); # x = 2*x - 1
8169  
8170 my @terms;
8171  
8172 foreach my $k (0 .. $n) {
8173 --$u & 1 and next; # E_n = 0 for all odd n
8174  
8175 Math::GMPz::Rmpz_bin_uiui($z, $n, $u);
8176 Math::GMPz::Rmpz_mul($z, $z, $S[$u >> 1]);
8177 Math::GMPz::Rmpz_neg($z, $z) if (($u >> 1) & 1);
8178  
8179 push @terms, __mul__(__pow__($x, $k), $z);
8180 }
8181  
8182 Math::GMPz::Rmpz_set_ui($z, 0);
8183 Math::GMPz::Rmpz_setbit($z, $n);
8184  
8185 bless \__div__(_binsplit(\@terms, \&__add__), $z);
8186 }
8187  
8188 sub euler ($;$) {
8189 my ($n, $x) = @_;
8190  
8191 @_ == 2 && goto &euler_polynomial;
8192  
8193 $n = _star2ui($n) // goto &nan;
8194 $n & 1 and goto &zero; # E_n = 0 for all odd indices
8195  
8196 my $e = Math::GMPz::Rmpz_init_set((_secant_numbers($n >> 1))[$n >> 1]);
8197 Math::GMPz::Rmpz_neg($e, $e) if (($n >> 1) & 1);
8198 bless \$e;
8199 }
8200  
8201 sub secant_number ($) {
8202 my ($n) = @_;
8203  
8204 $n = _star2ui($n) // goto &nan;
8205  
8206 my @E = _secant_numbers($n);
8207 bless \Math::GMPz::Rmpz_init_set($E[$n]);
8208 }
8209  
8210 sub tangent_number ($) {
8211 my ($n) = @_;
8212  
8213 $n = _star2ui($n) // goto &nan;
8214  
8215 $n || goto &zero;
8216 my @T = _tangent_numbers($n);
8217 bless \Math::GMPz::Rmpz_init_set($T[$n - 1]);
8218 }
8219  
8220 #
8221 ## The n-th Harmonic number: 1 + 1/2 + ... + 1/n
8222 #
8223  
8224 sub harmfrac ($) {
8225 my ($n) = @_;
8226  
8227 $n = _star2ui($n) // goto &nan;
8228  
8229 $n || goto &zero;
8230 $n < 0 && goto &nan;
8231  
8232 # Use binary splitting for large values of n. (by Fredrik Johansson)
8233 # https://fredrik-j.blogspot.com/2009/02/how-not-to-compute-harmonic-numbers.html
8234 if ($n > 7000) {
8235  
8236 my $num = Math::GMPz::Rmpz_init_set_ui(1);
8237 my $den = Math::GMPz::Rmpz_init_set_ui($n + 1);
8238  
8239 my $temp = Math::GMPz::Rmpz_init();
8240  
8241 # Translation of Dana Jacobsen's code from Math::Prime::Util::{PP,GMP}.
8242 # https://metacpan.org/pod/Math::Prime::Util::PP
8243 # https://metacpan.org/pod/Math::Prime::Util::GMP
8244 sub {
8245 my ($num, $den) = @_;
8246 Math::GMPz::Rmpz_sub($temp, $den, $num);
8247  
8248 if (Math::GMPz::Rmpz_cmp_ui($temp, 1) == 0) {
8249 Math::GMPz::Rmpz_set($den, $num);
8250 Math::GMPz::Rmpz_set_ui($num, 1);
8251 }
8252 elsif (Math::GMPz::Rmpz_cmp_ui($temp, 2) == 0) {
8253 Math::GMPz::Rmpz_set($den, $num);
8254 Math::GMPz::Rmpz_mul_2exp($num, $num, 1);
8255 Math::GMPz::Rmpz_add_ui($num, $num, 1);
8256 Math::GMPz::Rmpz_addmul($den, $den, $den);
8257 }
8258 else {
8259 Math::GMPz::Rmpz_add($temp, $num, $den);
8260 Math::GMPz::Rmpz_div_2exp($temp, $temp, 1);
8261 my $q = Math::GMPz::Rmpz_init_set($temp);
8262 my $r = Math::GMPz::Rmpz_init_set($temp);
8263 __SUB__->($num, $q);
8264 __SUB__->($r, $den);
8265 Math::GMPz::Rmpz_mul($num, $num, $den);
8266 Math::GMPz::Rmpz_mul($temp, $q, $r);
8267 Math::GMPz::Rmpz_add($num, $num, $temp);
8268 Math::GMPz::Rmpz_mul($den, $den, $q);
8269 }
8270 }
8271 ->($num, $den);
8272  
8273 my $q = Math::GMPq::Rmpq_init();
8274 Math::GMPq::Rmpq_set_num($q, $num);
8275 Math::GMPq::Rmpq_set_den($q, $den);
8276 Math::GMPq::Rmpq_canonicalize($q);
8277 return bless \$q;
8278 }
8279  
8280 my $num = Math::GMPz::Rmpz_init_set_ui(1);
8281 my $den = Math::GMPz::Rmpz_init_set_ui(1);
8282  
8283 for (my $k = 2 ; $k <= $n ; ++$k) {
8284 Math::GMPz::Rmpz_mul_ui($num, $num, $k); # num = num * k
8285 Math::GMPz::Rmpz_add($num, $num, $den); # num = num + den
8286 Math::GMPz::Rmpz_mul_ui($den, $den, $k); # den = den * k
8287 }
8288  
8289 my $r = Math::GMPq::Rmpq_init();
8290 Math::GMPq::Rmpq_set_num($r, $num);
8291 Math::GMPq::Rmpq_set_den($r, $den);
8292 Math::GMPq::Rmpq_canonicalize($r);
8293 bless \$r;
8294 }
8295  
8296 *harmonic = \&harmfrac;
8297  
8298 #
8299 ## Bernoulli number as a floating-point value
8300 #
8301  
8302 sub bernreal ($) {
8303 my ($n) = @_;
8304  
8305 $n = _star2ui($n) // goto &nan;
8306  
8307 if ($n == 0) {
8308 my $r = Math::MPFR::Rmpfr_init2($PREC);
8309 Math::MPFR::Rmpfr_set_ui($r, 1, $ROUND);
8310 return bless \$r;
8311 }
8312  
8313 if ($n == 1) {
8314 my $r = Math::MPFR::Rmpfr_init2($PREC);
8315 Math::MPFR::Rmpfr_set_d($r, 0.5, $ROUND);
8316 return bless \$r;
8317 }
8318  
8319 if ($n & 1) { # Bn = 0 for odd n>1
8320 my $r = Math::MPFR::Rmpfr_init2($PREC);
8321 Math::MPFR::Rmpfr_set_ui($r, 0, $ROUND);
8322 return bless \$r;
8323 }
8324  
8325 my $f = Math::MPFR::Rmpfr_init2($PREC);
8326 my $p = Math::MPFR::Rmpfr_init2($PREC);
8327  
8328 Math::MPFR::Rmpfr_zeta_ui($f, $n, $ROUND); # f = zeta(n)
8329 Math::MPFR::Rmpfr_set_ui($p, $n + 1, $ROUND); # p = n+1
8330 Math::MPFR::Rmpfr_gamma($p, $p, $ROUND); # p = gamma(p)
8331  
8332 Math::MPFR::Rmpfr_mul($f, $f, $p, $ROUND); # f = f * p
8333  
8334 Math::MPFR::Rmpfr_const_pi($p, $ROUND); # p = PI
8335 Math::MPFR::Rmpfr_pow_ui($p, $p, $n, $ROUND); # p = p^n
8336  
8337 Math::MPFR::Rmpfr_div_2ui($f, $f, $n - 1, $ROUND); # f = f / 2^(n-1)
8338  
8339 Math::MPFR::Rmpfr_div($f, $f, $p, $ROUND); # f = f/p
8340 Math::MPFR::Rmpfr_neg($f, $f, $ROUND) if $n % 4 == 0;
8341  
8342 bless \$f;
8343 }
8344  
8345 # Natural logarithm of the n-th Bernoulli number
8346  
8347 sub lnbern ($) {
8348 my ($n) = @_;
8349  
8350 $n = _star2mpz($n) // goto &nan;
8351  
8352 # log(|B(n)|) = (1 - n)*log(2) - n*log(Ï€) + log(zeta(n)) + log(n!)
8353  
8354 (Math::GMPz::Rmpz_sgn($n) || goto &zero) < 0 and goto &nan;
8355  
8356 my $L = Math::MPFR::Rmpfr_init2($PREC);
8357 Math::MPFR::Rmpfr_const_log2($L, $ROUND);
8358  
8359 if (Math::GMPz::Rmpz_cmp_ui($n, 1) == 0) {
8360 Math::MPFR::Rmpfr_neg($L, $L, $ROUND);
8361 return bless \$L;
8362 }
8363  
8364 Math::GMPz::Rmpz_odd_p($n) && goto &ninf; # log(Bn) = -Inf for odd n>1
8365  
8366 my $pi = Math::MPFR::Rmpfr_init2($PREC);
8367 Math::MPFR::Rmpfr_const_pi($pi, $ROUND); # pi = π
8368  
8369 my $t = Math::MPFR::Rmpfr_init2($PREC);
8370 Math::MPFR::Rmpfr_log($t, $pi, $ROUND); # t = log(Ï€)
8371 Math::MPFR::Rmpfr_mul_z($t, $t, $n, $ROUND); # t = n*log(Ï€)
8372  
8373 my $s = Math::GMPz::Rmpz_init();
8374 Math::GMPz::Rmpz_ui_sub($s, 1, $n); # s = 1-n
8375  
8376 Math::MPFR::Rmpfr_mul_z($L, $L, $s, $ROUND); # L = (1 - n)*log(2)
8377 Math::MPFR::Rmpfr_sub($L, $L, $t, $ROUND); # L -= n*log(Ï€)
8378  
8379 if (Math::GMPz::Rmpz_fits_ulong_p($n)) { # n is a native unsigned integer
8380 Math::MPFR::Rmpfr_zeta_ui($t, Math::GMPz::Rmpz_get_ui($n), $ROUND);
8381 }
8382 else {
8383 Math::MPFR::Rmpfr_set_z($t, $n, $ROUND); # t = n
8384 Math::MPFR::Rmpfr_zeta($t, $t, $ROUND); # t = zeta(n)
8385 }
8386  
8387 Math::MPFR::Rmpfr_log($t, $t, $ROUND); # t = log(zeta(n))
8388 Math::MPFR::Rmpfr_add($L, $L, $t, $ROUND); # L += log(zeta(n))
8389  
8390 Math::GMPz::Rmpz_add_ui($s, $n, 1); # s = n+1
8391 Math::MPFR::Rmpfr_set_z($t, $s, $ROUND); # t = n+1
8392 Math::MPFR::Rmpfr_lngamma($t, $t, $ROUND); # t = log(gamma(n+1)) = log(n!)
8393  
8394 Math::MPFR::Rmpfr_add($L, $L, $t, $ROUND); # L += log(n!)
8395  
8396 # If 4|n, then B_n is negative; log(-Re(x)) = log(Re(x)) + π*i, for x>0
8397 if (Math::GMPz::Rmpz_divisible_2exp_p($n, 2)) {
8398 my $c = Math::MPC::Rmpc_init2($PREC);
8399 Math::MPC::Rmpc_set_fr_fr($c, $L, $pi, $ROUND);
8400 return bless \$c;
8401 }
8402  
8403 bless \$L;
8404 }
8405  
8406 #
8407 ## The n-th Harmonic number as a floating-point value
8408 #
8409  
8410 # harmreal(x) = digamma(x+1) + EulerGamma
8411  
8412 sub __harmreal__ {
8413 my ($x) = @_; # $x is a Math::MPFR object
8414  
8415 my $r = Math::MPFR::Rmpfr_init2($PREC);
8416 Math::MPFR::Rmpfr_add_ui($r, $x, 1, $ROUND);
8417 Math::MPFR::Rmpfr_digamma($r, $r, $ROUND);
8418  
8419 my $t = Math::MPFR::Rmpfr_init2($PREC);
8420 Math::MPFR::Rmpfr_const_euler($t, $ROUND);
8421 Math::MPFR::Rmpfr_add($r, $r, $t, $ROUND);
8422  
8423 $r;
8424 }
8425  
8426 sub harmreal ($) {
8427 bless \__harmreal__(_star2mpfr($_[0]) // (goto &nan));
8428 }
8429  
8430 #
8431 ## Subfactorial
8432 #
8433  
8434 sub subfactorial ($;$) {
8435 my ($m, $k) = @_;
8436  
8437 $m = _star2ui($m) // goto &nan;
8438  
8439 if (defined($k)) {
8440 $k = _star2si($k) // goto &nan;
8441 }
8442 else {
8443 $k = 0;
8444 }
8445  
8446 my $n = $m - $k;
8447  
8448 goto &zero if ($k < 0);
8449 goto &one if ($n == 0);
8450 goto &zero if ($n < 0);
8451  
8452 my $tau = 6.28318530717958647692528676655900576839433879875;
8453 my $prec = 4 + CORE::int(($n * CORE::log($n) + CORE::log($tau * $n) / 2 - $n) / CORE::log(2));
8454  
8455 my $z = Math::GMPz::Rmpz_init();
8456 Math::GMPz::Rmpz_fac_ui($z, $n);
8457  
8458 state $round_z = Math::MPFR::MPFR_RNDZ();
8459  
8460 my $f = Math::MPFR::Rmpfr_init2($prec);
8461 Math::MPFR::Rmpfr_set_ui($f, 1, $round_z);
8462 Math::MPFR::Rmpfr_exp($f, $f, $round_z);
8463 Math::MPFR::Rmpfr_z_div($f, $z, $f, $round_z);
8464 Math::MPFR::Rmpfr_add_d($f, $f, 0.5, $round_z);
8465 Math::MPFR::Rmpfr_floor($f, $f);
8466 Math::MPFR::Rmpfr_get_z($z, $f, $round_z);
8467  
8468 if ($k != 0) {
8469 my $t = Math::GMPz::Rmpz_init();
8470 Math::GMPz::Rmpz_bin_uiui($t, $m, $k);
8471 Math::GMPz::Rmpz_mul($z, $z, $t);
8472 }
8473  
8474 bless \$z;
8475 }
8476  
8477 sub superfactorial ($) {
8478 my ($n) = @_;
8479  
8480 $n = _star2ui($n) // goto &nan;
8481  
8482 my @list;
8483 foreach my $k (2 .. $n) {
8484 my $z = Math::GMPz::Rmpz_init();
8485 Math::GMPz::Rmpz_ui_pow_ui($z, $k, $n - $k + 1);
8486 push @list, $z;
8487 }
8488  
8489 @list || goto &one;
8490 bless \_binsplit(\@list, \&__mul__);
8491 }
8492  
8493 sub lnsuperfactorial ($) {
8494 my ($n) = @_;
8495  
8496 $n = _star2ui($n) // goto &nan;
8497  
8498 my $r = Math::MPFR::Rmpfr_init2($PREC);
8499 my $t = Math::MPFR::Rmpfr_init2($PREC);
8500  
8501 Math::MPFR::Rmpfr_set_ui($r, 0, $ROUND);
8502  
8503 foreach my $k (2 .. $n) {
8504 Math::MPFR::Rmpfr_set_ui($t, $k, $ROUND);
8505 Math::MPFR::Rmpfr_log($t, $t, $ROUND);
8506 Math::MPFR::Rmpfr_mul_ui($t, $t, $n - $k + 1, $ROUND);
8507 Math::MPFR::Rmpfr_add($r, $r, $t, $ROUND);
8508 }
8509  
8510 bless \$r;
8511 }
8512  
8513 sub hyperfactorial ($) {
8514 my ($n) = @_;
8515  
8516 $n = _star2ui($n) // goto &nan;
8517  
8518 my @list;
8519 foreach my $k (2 .. $n) {
8520 my $z = Math::GMPz::Rmpz_init();
8521 Math::GMPz::Rmpz_ui_pow_ui($z, $k, $k);
8522 push @list, $z;
8523 }
8524  
8525 @list || goto &one;
8526 bless \_binsplit(\@list, \&__mul__);
8527 }
8528  
8529 sub lnhyperfactorial ($) {
8530 my ($n) = @_;
8531  
8532 $n = _star2ui($n) // goto &nan;
8533  
8534 my $r = Math::MPFR::Rmpfr_init2($PREC);
8535 my $t = Math::MPFR::Rmpfr_init2($PREC);
8536  
8537 Math::MPFR::Rmpfr_set_ui($r, 0, $ROUND);
8538  
8539 foreach my $k (2 .. $n) {
8540 Math::MPFR::Rmpfr_set_ui($t, $k, $ROUND);
8541 Math::MPFR::Rmpfr_log($t, $t, $ROUND);
8542 Math::MPFR::Rmpfr_mul_ui($t, $t, $k, $ROUND);
8543 Math::MPFR::Rmpfr_add($r, $r, $t, $ROUND);
8544 }
8545  
8546 bless \$r;
8547 }
8548  
8549 #
8550 ## Factorial
8551 #
8552  
8553 sub factorial ($) {
8554 my ($n) = @_;
8555  
8556 $n = _star2ui($n) // goto &nan;
8557  
8558 my $r = Math::GMPz::Rmpz_init();
8559 Math::GMPz::Rmpz_fac_ui($r, $n);
8560 bless \$r;
8561 }
8562  
8563 #
8564 ## Double-factorial
8565 #
8566  
8567 sub dfactorial ($) {
8568 my ($n) = @_;
8569  
8570 $n = _star2ui($n) // goto &nan;
8571  
8572 my $r = Math::GMPz::Rmpz_init();
8573 Math::GMPz::Rmpz_2fac_ui($r, $n);
8574 bless \$r;
8575 }
8576  
8577 #
8578 ## M-factorial
8579 #
8580  
8581 sub mfactorial ($$) {
8582 my ($n, $k) = @_;
8583  
8584 $n = _star2ui($n) // goto &nan;
8585 $k = _star2ui($k) // goto &nan;
8586  
8587 my $r = Math::GMPz::Rmpz_init();
8588 Math::GMPz::Rmpz_mfac_uiui($r, $n, $k);
8589 bless \$r;
8590 }
8591  
8592 #
8593 ## falling_factorial(x, +y) = binomial(x, y) * y!
8594 ## falling_factorial(x, -y) = 1/falling_factorial(x + y, y)
8595 #
8596 sub falling_factorial ($$) {
8597 my ($x, $y) = @_;
8598  
8599 $x = _star2mpz($x) // goto &nan;
8600 $y = _star2si($y) // goto &nan;
8601  
8602 my $r = Math::GMPz::Rmpz_init_set($x);
8603  
8604 if ($y < 0) {
8605 Math::GMPz::Rmpz_add_ui($r, $r, -$y);
8606 }
8607  
8608 Math::GMPz::Rmpz_fits_ulong_p($r)
8609 ? Math::GMPz::Rmpz_bin_uiui($r, Math::GMPz::Rmpz_get_ui($r), $y < 0 ? -$y : $y)
8610 : Math::GMPz::Rmpz_bin_ui($r, $r, $y < 0 ? -$y : $y);
8611  
8612 Math::GMPz::Rmpz_sgn($r) || do {
8613 $y < 0
8614 ? (goto &nan)
8615 : (goto &zero);
8616 };
8617  
8618 state $t = Math::GMPz::Rmpz_init_nobless();
8619 Math::GMPz::Rmpz_fac_ui($t, $y < 0 ? -$y : $y);
8620 Math::GMPz::Rmpz_mul($r, $r, $t);
8621  
8622 if ($y < 0) {
8623 my $q = Math::GMPq::Rmpq_init();
8624 Math::GMPq::Rmpq_set_z($q, $r);
8625 Math::GMPq::Rmpq_inv($q, $q);
8626 return bless \$q;
8627 }
8628  
8629 bless \$r;
8630 }
8631  
8632 #
8633 ## rising_factorial(x, +y) = binomial(x + y - 1, y) * y!
8634 ## rising_factorial(x, -y) = 1/rising_factorial(x - y, y)
8635 #
8636 sub rising_factorial ($$) {
8637 my ($x, $y) = @_;
8638  
8639 $x = _star2mpz($x) // goto &nan;
8640 $y = _star2si($y) // goto &nan;
8641  
8642 my $r = Math::GMPz::Rmpz_init_set($x);
8643 Math::GMPz::Rmpz_add_ui($r, $r, $y < 0 ? -$y : $y);
8644 Math::GMPz::Rmpz_sub_ui($r, $r, 1);
8645  
8646 if ($y < 0) {
8647 Math::GMPz::Rmpz_sub_ui($r, $r, $y < 0 ? -$y : $y);
8648 }
8649  
8650 Math::GMPz::Rmpz_fits_ulong_p($r)
8651 ? Math::GMPz::Rmpz_bin_uiui($r, Math::GMPz::Rmpz_get_ui($r), $y < 0 ? -$y : $y)
8652 : Math::GMPz::Rmpz_bin_ui($r, $r, $y < 0 ? -$y : $y);
8653  
8654 Math::GMPz::Rmpz_sgn($r) || do {
8655 $y < 0
8656 ? (goto &nan)
8657 : (goto &zero);
8658 };
8659  
8660 state $t = Math::GMPz::Rmpz_init_nobless();
8661 Math::GMPz::Rmpz_fac_ui($t, $y < 0 ? -$y : $y);
8662 Math::GMPz::Rmpz_mul($r, $r, $t);
8663  
8664 if ($y < 0) {
8665 my $q = Math::GMPq::Rmpq_init();
8666 Math::GMPq::Rmpq_set_z($q, $r);
8667 Math::GMPq::Rmpq_inv($q, $q);
8668 return bless \$q;
8669 }
8670  
8671 bless \$r;
8672 }
8673  
8674 #
8675 ## Greatest common multiple
8676 #
8677  
8678 sub gcd {
8679 my ($x, $y) = @_;
8680  
8681 @_ or goto &zero; # By convention, gcd of an empty set is 0.
8682 @_ == 1 and return $x;
8683  
8684 my $r = Math::GMPz::Rmpz_init();
8685  
8686 if (@_ > 2) {
8687 my @terms = map { _star2mpz($_) // goto &nan } @_;
8688  
8689 Math::GMPz::Rmpz_set($r, shift(@terms));
8690  
8691 foreach my $z (@terms) {
8692 Math::GMPz::Rmpz_gcd($r, $r, $z);
8693 Math::GMPz::Rmpz_cmp_ui($r, 1) || last;
8694 }
8695  
8696 return bless \$r;
8697 }
8698  
8699 if (ref($y) and !ref($x)) {
8700 ($x, $y) = ($y, $x);
8701 }
8702  
8703 $x = _star2mpz($x) // goto &nan;
8704  
8705 if (!ref($y) and CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
8706 Math::GMPz::Rmpz_gcd_ui($r, $x, $y < 0 ? -$y : $y);
8707 }
8708 else {
8709 $y = _star2mpz($y) // goto &nan;
8710 Math::GMPz::Rmpz_gcd($r, $x, $y);
8711 }
8712  
8713 bless \$r;
8714 }
8715  
8716 sub gcdext ($$) {
8717 my ($n, $k) = @_;
8718  
8719 $n = _star2mpz($n) // return (nan(), nan());
8720 $k = _star2mpz($k) // return (nan(), nan());
8721  
8722 my $g = Math::GMPz::Rmpz_init();
8723 my $u = Math::GMPz::Rmpz_init();
8724 my $v = Math::GMPz::Rmpz_init();
8725  
8726 Math::GMPz::Rmpz_gcdext($g, $u, $v, $n, $k);
8727  
8728 ((bless \$u), (bless \$v), (bless \$g));
8729 }
8730  
8731 #
8732 ## Least common multiple
8733 #
8734  
8735 sub __lcm__ {
8736 my ($n, $k) = @_;
8737 my $r = Math::GMPz::Rmpz_init();
8738 Math::GMPz::Rmpz_lcm($r, $n, $k);
8739 $r;
8740 }
8741  
8742 sub lcm {
8743 my ($x, $y) = @_;
8744  
8745 @_ or goto &one; # By convention, lcm of an empty set is 1.
8746 @_ == 1 and return $x;
8747  
8748 if (@_ > 2) {
8749 my @terms = map { _star2mpz($_) // goto &nan } @_;
8750 return bless \_binsplit(\@terms, \&__lcm__);
8751 }
8752  
8753 if (ref($y) and !ref($x)) {
8754 ($x, $y) = ($y, $x);
8755 }
8756  
8757 $x = _star2mpz($x) // goto &nan;
8758  
8759 my $r = Math::GMPz::Rmpz_init();
8760  
8761 if (!ref($y) and CORE::int($y) eq $y and $y < ULONG_MAX and $y > LONG_MIN) {
8762 Math::GMPz::Rmpz_lcm_ui($r, $x, $y < 0 ? -$y : $y);
8763 }
8764 else {
8765 $y = _star2mpz($y) // goto &nan;
8766 Math::GMPz::Rmpz_lcm($r, $x, $y);
8767 }
8768  
8769 bless \$r;
8770 }
8771  
8772 #
8773 ## Next prime after `n`.
8774 #
8775  
8776 sub next_prime ($) {
8777 my ($n) = @_;
8778 $n = _star2mpz($n) // goto &nan;
8779 my $r = Math::GMPz::Rmpz_init();
8780 Math::GMPz::Rmpz_nextprime($r, $n);
8781 bless \$r;
8782 }
8783  
8784 #
8785 ## True if `n` is an integer
8786 #
8787  
8788 sub __is_int__ {
8789 my ($x) = @_;
8790  
8791 ref($x) eq 'Math::GMPz' && return 1;
8792 ref($x) eq 'Math::GMPq' && return Math::GMPq::Rmpq_integer_p($x);
8793 ref($x) eq 'Math::MPFR' && return Math::MPFR::Rmpfr_integer_p($x);
8794  
8795 (@_) = _any2mpfr($x);
8796 goto __SUB__;
8797 }
8798  
8799 sub is_int ($) {
8800 my ($n) = @_;
8801 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
8802 ref($n) eq 'Math::GMPz' ? 1 : __is_int__($n);
8803 }
8804  
8805 #
8806 ## True if `n` is a rational number
8807 #
8808  
8809 sub is_rat ($) {
8810 my ($n) = @_;
8811 my $ref = ref(ref($n) eq __PACKAGE__ ? $$n : _star2obj($n));
8812 $ref eq 'Math::GMPz' or $ref eq 'Math::GMPq';
8813 }
8814  
8815 #
8816 ## True if `n` is probably a prime number
8817 #
8818  
8819 sub is_prime ($;$) {
8820 my ($n, $r) = @_;
8821  
8822 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
8823  
8824 if (ref($n) ne 'Math::GMPz') {
8825 __is_int__($n) || return 0;
8826 $n = _any2mpz($n) // return 0;
8827 }
8828  
8829 Math::GMPz::Rmpz_sgn($n) > 0 or return 0;
8830 $r = defined($r) ? (CORE::abs(CORE::int($r)) || 23) : 23;
8831 Math::GMPz::Rmpz_probab_prime_p($n, $r);
8832 }
8833  
8834 #
8835 ## True if `n` is coprime to `k`
8836 #
8837  
8838 sub is_coprime ($$) {
8839 my ($n, $k) = @_;
8840  
8841 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
8842  
8843 if (ref($n) ne 'Math::GMPz') {
8844 __is_int__($n) || return 0;
8845 $n = _any2mpz($n) // return 0;
8846 }
8847  
8848 if (!ref($k) and CORE::int($k) eq $k and $k >= 0 and $k < ULONG_MAX) {
8849 ## `k` is a native integer
8850 }
8851 else {
8852 $k = ref($k) eq __PACKAGE__ ? $$k : _star2obj($k);
8853  
8854 if (ref($k) ne 'Math::GMPz') {
8855 __is_int__($k) || return 0;
8856 $k = _any2mpz($k) // return 0;
8857 }
8858 }
8859  
8860 if (ref($k)) {
8861 state $t = Math::GMPz::Rmpz_init_nobless();
8862 Math::GMPz::Rmpz_gcd($t, $n, $k);
8863 return (Math::GMPz::Rmpz_cmp_ui($t, 1) == 0);
8864 }
8865  
8866 Math::GMPz::Rmpz_gcd_ui($Math::GMPz::NULL, $n, $k) == 1;
8867 }
8868  
8869 #
8870 ## True if all the prime factors of `n` are <= k.
8871 #
8872  
8873 sub is_smooth ($$) {
8874 my ($n, $k) = @_;
8875  
8876 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
8877  
8878 if (ref($n) ne 'Math::GMPz') {
8879 __is_int__($n) || return 0;
8880 $n = _any2mpz($n) // return 0;
8881 }
8882  
8883 return 0 if Math::GMPz::Rmpz_sgn($n) <= 0;
8884  
8885 $k = _star2ui($k) // return 0;
8886  
8887 return 1 if Math::GMPz::Rmpz_cmp_ui($n, 1) == 0;
8888 return 0 if $k <= 1;
8889  
8890 my $B = _cached_primorial($k);
8891  
8892 state $g = Math::GMPz::Rmpz_init_nobless();
8893 Math::GMPz::Rmpz_gcd($g, $n, $B);
8894  
8895 if (Math::GMPz::Rmpz_cmp_ui($g, 1) == 0) {
8896 return 0;
8897 }
8898  
8899 my $t = Math::GMPz::Rmpz_init_set($n);
8900  
8901 while (Math::GMPz::Rmpz_cmp_ui($g, 1) > 0) {
8902 Math::GMPz::Rmpz_remove($t, $t, $g);
8903 return 1 if Math::GMPz::Rmpz_cmp_ui($t, 1) == 0;
8904 Math::GMPz::Rmpz_gcd($g, $t, $g);
8905 }
8906  
8907 return 0;
8908 }
8909  
8910 #
8911 ## True if all the prime factors of `n` are >= k.
8912 #
8913  
8914 sub is_rough ($$) {
8915 my ($n, $k) = @_;
8916  
8917 $n = _star2mpz($n) // return 0;
8918  
8919 return 0 if Math::GMPz::Rmpz_sgn($n) <= 0;
8920 return 1 if Math::GMPz::Rmpz_cmp_ui($n, 1) == 0;
8921  
8922 $k = (_star2ui($k) // return 0) - 1;
8923  
8924 return 1 if $k <= 1;
8925  
8926 my $B = _cached_primorial($k);
8927  
8928 state $g = Math::GMPz::Rmpz_init_nobless();
8929 Math::GMPz::Rmpz_gcd($g, $n, $B);
8930 (Math::GMPz::Rmpz_cmp_ui($g, 1) > 0) ? 0 : 1;
8931 }
8932  
8933 #
8934 ## Smooth part of n, containing all prime factors p|n such that p <= k.
8935 #
8936 sub smooth_part {
8937 my ($n, $k) = @_;
8938  
8939 $n = _star2mpz($n) // goto &nan;
8940  
8941 goto &zero if Math::GMPz::Rmpz_sgn($n) <= 0;
8942  
8943 $k = _star2ui($k) // goto &nan;
8944  
8945 goto &one if $k <= 1;
8946 goto &one if Math::GMPz::Rmpz_cmp_ui($n, 1) == 0;
8947  
8948 my $B = _cached_primorial($k);
8949  
8950 state $g = Math::GMPz::Rmpz_init_nobless();
8951 Math::GMPz::Rmpz_gcd($g, $n, $B);
8952  
8953 if (Math::GMPz::Rmpz_cmp_ui($g, 1) == 0) {
8954 goto &one;
8955 }
8956  
8957 my $t = Math::GMPz::Rmpz_init_set($n);
8958  
8959 while (Math::GMPz::Rmpz_cmp_ui($g, 1) > 0) {
8960 Math::GMPz::Rmpz_remove($t, $t, $g);
8961 return (bless \$n) if Math::GMPz::Rmpz_cmp_ui($t, 1) == 0;
8962 Math::GMPz::Rmpz_gcd($g, $t, $g);
8963 }
8964  
8965 Math::GMPz::Rmpz_divexact($t, $n, $t);
8966 bless \$t;
8967 }
8968  
8969 #
8970 ## Rough part of n, containing all prime factors p|n such that p >= k.
8971 #
8972 sub rough_part {
8973 my ($n, $k) = @_;
8974  
8975 $n = _star2mpz($n) // goto &nan;
8976  
8977 goto &zero if Math::GMPz::Rmpz_sgn($n) <= 0;
8978  
8979 $k = (_star2ui($k) // goto &nan) - 1;
8980  
8981 return (bless \$n) if $k <= 1;
8982 goto &one if Math::GMPz::Rmpz_cmp_ui($n, 1) == 0;
8983  
8984 my $B = _cached_primorial($k);
8985  
8986 state $g = Math::GMPz::Rmpz_init_nobless();
8987 Math::GMPz::Rmpz_gcd($g, $n, $B);
8988  
8989 if (Math::GMPz::Rmpz_cmp_ui($g, 1) == 0) {
8990 return bless \$n;
8991 }
8992  
8993 my $t = Math::GMPz::Rmpz_init_set($n);
8994  
8995 while (Math::GMPz::Rmpz_cmp_ui($g, 1) > 0) {
8996 Math::GMPz::Rmpz_remove($t, $t, $g);
8997 goto &one if Math::GMPz::Rmpz_cmp_ui($t, 1) == 0;
8998 Math::GMPz::Rmpz_gcd($g, $t, $g);
8999 }
9000  
9001 bless \$t;
9002 }
9003  
9004 sub is_smooth_over_prod ($$) {
9005 my ($n, $k) = @_;
9006  
9007 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
9008  
9009 if (ref($n) ne 'Math::GMPz') {
9010 __is_int__($n) || return 0;
9011 $n = _any2mpz($n) // return 0;
9012 }
9013  
9014 return 0 if Math::GMPz::Rmpz_sgn($n) <= 0;
9015  
9016 $k = ref($k) eq __PACKAGE__ ? $$k : _star2obj($k);
9017  
9018 if (ref($k) ne 'Math::GMPz') {
9019 __is_int__($k) || return 0;
9020 $k = _any2mpz($k) // return 0;
9021 }
9022  
9023 return 0 if Math::GMPz::Rmpz_sgn($k) <= 0;
9024 return 1 if Math::GMPz::Rmpz_cmp_ui($n, 1) == 0;
9025  
9026 state $g = Math::GMPz::Rmpz_init_nobless();
9027  
9028 Math::GMPz::Rmpz_gcd($g, $n, $k);
9029  
9030 if (Math::GMPz::Rmpz_cmp_ui($g, 1) == 0) {
9031 return 0;
9032 }
9033  
9034 my $t = Math::GMPz::Rmpz_init_set($n);
9035  
9036 while (Math::GMPz::Rmpz_cmp_ui($g, 1) > 0) {
9037 Math::GMPz::Rmpz_remove($t, $t, $g);
9038 return 1 if Math::GMPz::Rmpz_cmp_ui($t, 1) == 0;
9039 Math::GMPz::Rmpz_gcd($g, $t, $g);
9040 }
9041  
9042 return 0;
9043 }
9044  
9045 #
9046 ## Numerator of a number
9047 #
9048  
9049 sub numerator ($) {
9050 my ($x) = @_;
9051  
9052 my $r = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
9053  
9054 while (1) {
9055  
9056 if (ref($r) eq 'Math::GMPq') {
9057 my $z = Math::GMPz::Rmpz_init();
9058 Math::GMPq::Rmpq_get_num($z, $r);
9059 return bless \$z;
9060 }
9061  
9062 ref($r) eq 'Math::GMPz' and return $x; # is an integer
9063  
9064 $r = _any2mpq($r) // goto &nan;
9065 }
9066 }
9067  
9068 #
9069 ## Denominator of a number
9070 #
9071  
9072 sub denominator ($) {
9073 my ($x) = @_;
9074  
9075 my $r = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
9076  
9077 while (1) {
9078  
9079 if (ref($r) eq 'Math::GMPq') {
9080 my $z = Math::GMPz::Rmpz_init();
9081 Math::GMPq::Rmpq_get_den($z, $r);
9082 return bless \$z;
9083 }
9084  
9085 ref($r) eq 'Math::GMPz' and goto &one; # is an integer
9086  
9087 $r = _any2mpq($r) // goto &nan;
9088 }
9089 }
9090  
9091 #
9092 ## (numerator, denominator)
9093 #
9094  
9095 sub nude ($) {
9096 my ($x) = @_;
9097  
9098 if (ref($x) ne __PACKAGE__) {
9099 $x = __PACKAGE__->new($x);
9100 }
9101  
9102 ($x->numerator, $x->denominator);
9103 }
9104  
9105 #
9106 ## Sign of a number
9107 #
9108  
9109 sub __sgn__ {
9110 my ($x) = @_;
9111 goto(ref($x) =~ tr/:/_/rs);
9112  
9113 Math_MPFR: {
9114 return Math::MPFR::Rmpfr_sgn($x);
9115 }
9116  
9117 Math_GMPq: {
9118 return Math::GMPq::Rmpq_sgn($x);
9119 }
9120  
9121 Math_GMPz: {
9122 return Math::GMPz::Rmpz_sgn($x);
9123 }
9124  
9125 # sgn(x) = x / abs(x)
9126 Math_MPC: {
9127 my $abs = Math::MPFR::Rmpfr_init2($PREC);
9128 Math::MPC::Rmpc_abs($abs, $x, $ROUND);
9129  
9130 if (Math::MPFR::Rmpfr_zero_p($abs)) { # it's zero
9131 return 0;
9132 }
9133  
9134 my $r = Math::MPC::Rmpc_init2($PREC);
9135 Math::MPC::Rmpc_div_fr($r, $x, $abs, $ROUND);
9136 return $r;
9137 }
9138 }
9139  
9140 sub sgn ($) {
9141 my ($x) = @_;
9142 my $r = __sgn__(ref($x) eq __PACKAGE__ ? $$x : _star2obj($x));
9143 ref($r) ? (bless \$r) : $r;
9144 }
9145  
9146 #
9147 ## True if `x` is a real number
9148 #
9149  
9150 sub is_real ($) {
9151 my ($x) = @_;
9152  
9153 my $r = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
9154  
9155 while (1) {
9156 my $ref = ref($r);
9157  
9158 $ref eq 'Math::GMPz' && return 1;
9159 $ref eq 'Math::GMPq' && return 1;
9160 $ref eq 'Math::MPFR' && return Math::MPFR::Rmpfr_number_p($r);
9161  
9162 $r = _any2mpfr($r);
9163 }
9164 }
9165  
9166 #
9167 ## True if `x` is an imaginary number
9168 #
9169  
9170 sub is_imag ($) {
9171 my ($x) = @_;
9172  
9173 my $r = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
9174 ref($r) eq 'Math::MPC' or return 0;
9175  
9176 my $f = Math::MPFR::Rmpfr_init2($PREC);
9177 Math::MPC::RMPC_RE($f, $r);
9178 Math::MPFR::Rmpfr_zero_p($f) || return 0; # is complex
9179 Math::MPC::RMPC_IM($f, $r);
9180 !Math::MPFR::Rmpfr_zero_p($f);
9181 }
9182  
9183 #
9184 ## True if `x` is a complex number
9185 #
9186  
9187 sub is_complex ($) {
9188 my ($x) = @_;
9189  
9190 my $r = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
9191 ref($r) eq 'Math::MPC' or return 0;
9192  
9193 my $f = Math::MPFR::Rmpfr_init2($PREC);
9194 Math::MPC::RMPC_IM($f, $r);
9195 Math::MPFR::Rmpfr_zero_p($f) && return 0; # is real
9196 Math::MPC::RMPC_RE($f, $r);
9197 !Math::MPFR::Rmpfr_zero_p($f);
9198 }
9199  
9200 #
9201 ## True if `x == +Inf`
9202 #
9203  
9204 sub is_inf ($) {
9205 my ($x) = @_;
9206  
9207 my $r = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
9208  
9209 while (1) {
9210 my $ref = ref($r);
9211  
9212 $ref eq 'Math::GMPz' && return 0;
9213 $ref eq 'Math::GMPq' && return 0;
9214 $ref eq 'Math::MPFR' && return (Math::MPFR::Rmpfr_inf_p($r) and Math::MPFR::Rmpfr_sgn($r) > 0);
9215  
9216 $r = _any2mpfr($r);
9217 }
9218 }
9219  
9220 #
9221 ## True if `x == -Inf`
9222 #
9223  
9224 sub is_ninf ($) {
9225 my ($x) = @_;
9226  
9227 my $r = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
9228  
9229 while (1) {
9230 my $ref = ref($r);
9231  
9232 $ref eq 'Math::GMPz' && return 0;
9233 $ref eq 'Math::GMPq' && return 0;
9234 $ref eq 'Math::MPFR' && return (Math::MPFR::Rmpfr_inf_p($r) and Math::MPFR::Rmpfr_sgn($r) < 0);
9235  
9236 $r = _any2mpfr($r);
9237 }
9238 }
9239  
9240 #
9241 ## True if `x` is Not-a-Number (NaN)
9242 #
9243  
9244 sub is_nan ($) {
9245 my ($x) = @_;
9246  
9247 $x = ref($x) eq __PACKAGE__ ? $$x : _star2obj($x);
9248  
9249 ref($x) eq 'Math::GMPz' && return 0;
9250 ref($x) eq 'Math::GMPq' && return 0;
9251 ref($x) eq 'Math::MPFR' && return Math::MPFR::Rmpfr_nan_p($x);
9252  
9253 my $t = Math::MPFR::Rmpfr_init2($PREC);
9254  
9255 Math::MPC::RMPC_RE($t, $x);
9256 Math::MPFR::Rmpfr_nan_p($t) && return 1;
9257  
9258 Math::MPC::RMPC_IM($t, $x);
9259 Math::MPFR::Rmpfr_nan_p($t) && return 1;
9260  
9261 return 0;
9262 }
9263  
9264 #
9265 ## True if `n` is an even integer
9266 #
9267  
9268 sub is_even ($) {
9269 my ($n) = @_;
9270  
9271 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
9272  
9273 if (ref($n) ne 'Math::GMPz') {
9274 __is_int__($n) || return 0;
9275 $n = _any2mpz($n) // return 0;
9276 }
9277  
9278 Math::GMPz::Rmpz_even_p($n);
9279 }
9280  
9281 #
9282 ## True if `n` is an odd integer
9283 #
9284  
9285 sub is_odd ($) {
9286 my ($n) = @_;
9287  
9288 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
9289  
9290 if (ref($n) ne 'Math::GMPz') {
9291 __is_int__($n) || return 0;
9292 $n = _any2mpz($n) // return 0;
9293 }
9294  
9295 Math::GMPz::Rmpz_odd_p($n);
9296 }
9297  
9298 #
9299 ## True if `n == 0`
9300 #
9301  
9302 sub is_zero ($) {
9303 my ($n) = @_;
9304 @_ = ((ref($n) eq __PACKAGE__ ? $$n : _star2obj($n)), 0);
9305 goto &__eq__;
9306 }
9307  
9308 #
9309 ## True if `n == 1`
9310 #
9311  
9312 sub is_one ($) {
9313 my ($n) = @_;
9314 @_ = ((ref($n) eq __PACKAGE__ ? $$n : _star2obj($n)), 1);
9315 goto &__eq__;
9316 }
9317  
9318 #
9319 ## True if `n == -1`
9320 #
9321  
9322 sub is_mone ($) {
9323 my ($n) = @_;
9324 @_ = ((ref($n) eq __PACKAGE__ ? $$n : _star2obj($n)), -1);
9325 goto &__eq__;
9326 }
9327  
9328 #
9329 ## True if `n` is positive
9330 #
9331  
9332 sub is_pos ($) {
9333 my ($n) = @_;
9334 (__cmp__((ref($n) eq __PACKAGE__ ? $$n : _star2obj($n)), 0) // return undef) > 0;
9335 }
9336  
9337 #
9338 ## True if `n` is negative
9339 #
9340  
9341 sub is_neg ($) {
9342 my ($n) = @_;
9343 (__cmp__((ref($n) eq __PACKAGE__ ? $$n : _star2obj($n)), 0) // return undef) < 0;
9344 }
9345  
9346 #
9347 ## True if `n` is a perfect square
9348 #
9349  
9350 sub is_square ($) {
9351 my ($n) = @_;
9352  
9353 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
9354  
9355 if (ref($n) ne 'Math::GMPz') {
9356 __is_int__($n) || return 0;
9357 $n = _any2mpz($n) // return 0;
9358 }
9359  
9360 Math::GMPz::Rmpz_perfect_square_p($n);
9361 }
9362  
9363 #
9364 ## True if `n` is a k-gonal number
9365 #
9366  
9367 sub __is_polygonal__ {
9368 my ($n, $k, $second) = @_;
9369  
9370 Math::GMPz::Rmpz_sgn($n) || return 1;
9371  
9372 # polygonal_root(n, k)
9373 # = ((k - 4) ± sqrt(8 * (k - 2) * n + (k - 4)^2)) / (2 * (k - 2))
9374  
9375 state $t = Math::GMPz::Rmpz_init_nobless();
9376 state $u = Math::GMPz::Rmpz_init_nobless();
9377  
9378 Math::GMPz::Rmpz_sub_ui($u, $k, 2); # u = k-2
9379 Math::GMPz::Rmpz_mul($t, $n, $u); # t = n*u
9380 Math::GMPz::Rmpz_mul_2exp($t, $t, 3); # t = t*8
9381  
9382 Math::GMPz::Rmpz_sub_ui($u, $u, 2); # u = u-2
9383 Math::GMPz::Rmpz_mul($u, $u, $u); # u = u^2
9384  
9385 Math::GMPz::Rmpz_add($t, $t, $u); # t = t+u
9386 Math::GMPz::Rmpz_perfect_square_p($t) || return 0;
9387 Math::GMPz::Rmpz_sqrt($t, $t); # t = sqrt(t)
9388  
9389 Math::GMPz::Rmpz_sub_ui($u, $k, 4); # u = k-4
9390  
9391 $second
9392 ? Math::GMPz::Rmpz_sub($t, $u, $t) # t = u-t
9393 : Math::GMPz::Rmpz_add($t, $t, $u); # t = t+u
9394  
9395 Math::GMPz::Rmpz_add_ui($u, $u, 2); # u = u+2
9396 Math::GMPz::Rmpz_mul_2exp($u, $u, 1); # u = u*2
9397  
9398 Math::GMPz::Rmpz_divisible_p($t, $u); # true iff u|t
9399 }
9400  
9401 sub is_polygonal ($$) {
9402 my ($n, $k) = @_;
9403  
9404 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
9405  
9406 if (ref($n) ne 'Math::GMPz') {
9407 __is_int__($n) || return 0;
9408 $n = _any2mpz($n) // return 0;
9409 }
9410  
9411 $k = _star2mpz($k) // return 0;
9412 __is_polygonal__($n, $k);
9413 }
9414  
9415 #
9416 ## Is a second polygonal number?
9417 #
9418  
9419 sub is_polygonal2 ($$) {
9420 my ($n, $k) = @_;
9421  
9422 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
9423  
9424 if (ref($n) ne 'Math::GMPz') {
9425 __is_int__($n) || return 0;
9426 $n = _any2mpz($n) // return 0;
9427 }
9428  
9429 $k = _star2mpz($k) // return 0;
9430  
9431 __is_polygonal__($n, $k, 1);
9432 }
9433  
9434 #
9435 ## Integer polygonal root
9436 #
9437  
9438 sub __ipolygonal_root__ {
9439 my ($n, $k, $second) = @_;
9440  
9441 # polygonal_root(n, k)
9442 # = ((k - 4) ± sqrt(8 * (k - 2) * n + (k - 4)^2)) / (2 * (k - 2))
9443  
9444 state $t = Math::GMPz::Rmpz_init_nobless();
9445 state $u = Math::GMPz::Rmpz_init_nobless();
9446  
9447 Math::GMPz::Rmpz_sub_ui($u, $k, 2); # u = k-2
9448 Math::GMPz::Rmpz_mul($t, $n, $u); # t = n*u
9449 Math::GMPz::Rmpz_mul_2exp($t, $t, 3); # t = t*8
9450  
9451 Math::GMPz::Rmpz_sub_ui($u, $u, 2); # u = u-2
9452 Math::GMPz::Rmpz_mul($u, $u, $u); # u = u^2
9453 Math::GMPz::Rmpz_add($t, $t, $u); # t = t+u
9454  
9455 Math::GMPz::Rmpz_sgn($t) < 0 && goto &_nan; # `t` is negative
9456  
9457 Math::GMPz::Rmpz_sqrt($t, $t); # t = sqrt(t)
9458 Math::GMPz::Rmpz_sub_ui($u, $k, 4); # u = k-4
9459  
9460 $second
9461 ? Math::GMPz::Rmpz_sub($t, $u, $t) # t = u-t
9462 : Math::GMPz::Rmpz_add($t, $t, $u); # t = t+u
9463  
9464 Math::GMPz::Rmpz_add_ui($u, $u, 2); # u = u+2
9465 Math::GMPz::Rmpz_mul_2exp($u, $u, 1); # u = u*2
9466  
9467 Math::GMPz::Rmpz_sgn($u) || return $n; # `u` is zero
9468  
9469 my $r = Math::GMPz::Rmpz_init();
9470 Math::GMPz::Rmpz_div($r, $t, $u); # r = floor(t/u)
9471 return $r;
9472 }
9473  
9474 sub ipolygonal_root ($$) {
9475 my ($n, $k) = @_;
9476  
9477 $n = _star2mpz($n) // goto &nan;
9478 $k = _star2mpz($k) // goto &nan;
9479  
9480 bless \__ipolygonal_root__($n, $k);
9481 }
9482  
9483 #
9484 ## Second integer polygonal root
9485 #
9486  
9487 sub ipolygonal_root2 ($$) {
9488 my ($n, $k) = @_;
9489  
9490 $n = _star2mpz($n) // goto &nan;
9491 $k = _star2mpz($k) // goto &nan;
9492  
9493 bless \__ipolygonal_root__($n, $k, 1);
9494 }
9495  
9496 #
9497 ## n-th k-gonal number
9498 #
9499  
9500 sub polygonal ($$) {
9501 my ($n, $k) = @_;
9502  
9503 $n = _star2mpz($n) // goto &nan;
9504  
9505 if (!ref($k) and CORE::int($k) eq $k and $k >= 0 and $k < ULONG_MAX) {
9506 ## `k` is a native unsigned integer
9507 }
9508 else {
9509 $k = _star2mpz($k) // goto &nan;
9510 }
9511  
9512 #
9513 ## polygonal(n, k) = n * (k*n - k - 2*n + 4) / 2
9514 #
9515  
9516 my $r = Math::GMPz::Rmpz_init();
9517  
9518 if (!ref($k)) { # `k` is a native unsigned integer
9519 Math::GMPz::Rmpz_mul_ui($r, $n, $k); # r = n*k
9520 Math::GMPz::Rmpz_sub_ui($r, $r, $k); # r = r-k
9521 }
9522 else {
9523 Math::GMPz::Rmpz_mul($r, $n, $k); # r = n*k
9524 Math::GMPz::Rmpz_sub($r, $r, $k); # r = r-k
9525 }
9526  
9527 Math::GMPz::Rmpz_submul_ui($r, $n, 2); # r = r-2*n
9528 Math::GMPz::Rmpz_add_ui($r, $r, 4); # r = r+4
9529 Math::GMPz::Rmpz_mul($r, $r, $n); # r = r*n
9530 Math::GMPz::Rmpz_div_2exp($r, $r, 1); # r = r/2
9531  
9532 bless \$r;
9533 }
9534  
9535 #
9536 ## True if n = c^k for some integer c
9537 #
9538  
9539 sub __is_power__ {
9540 my ($n, $k) = @_;
9541  
9542 # Everything is a first power
9543 $k == 1 and return 1;
9544  
9545 Math::GMPz::Rmpz_cmp_ui($n, 1) == 0 and return 1;
9546  
9547 # Return a true value when $n=-1 and $k is odd
9548 $k % 2 and (Math::GMPz::Rmpz_cmp_si($n, -1) == 0) and return 1;
9549  
9550 # Don't accept a non-positive power
9551 # Also, when $n is negative and $k is even, return faster
9552 if ($k <= 0 or ($k % 2 == 0 and Math::GMPz::Rmpz_sgn($n) < 0)) {
9553 return 0;
9554 }
9555  
9556 # Optimization for perfect squares
9557 $k == 2 and return Math::GMPz::Rmpz_perfect_square_p($n);
9558  
9559 Math::GMPz::Rmpz_perfect_power_p($n) || return 0;
9560 state $t = Math::GMPz::Rmpz_init_nobless();
9561 Math::GMPz::Rmpz_root($t, $n, $k);
9562 }
9563  
9564 sub is_power ($;$) {
9565 my ($n, $k) = @_;
9566  
9567 $n = _star2mpz($n) // return 0;
9568 $k // return Math::GMPz::Rmpz_perfect_power_p($n);
9569 $k = _star2si($k) // return 0;
9570  
9571 __is_power__($n, $k);
9572 }
9573  
9574 sub is_power_of ($$) {
9575 my ($n, $k) = @_;
9576  
9577 $n = _star2obj($n);
9578 $k = _star2obj($k);
9579  
9580 if (ref($n) ne 'Math::GMPz') {
9581 __is_int__($n) || return 0;
9582 $n = _any2mpz($n) // return 0;
9583 }
9584  
9585 if (ref($k) ne 'Math::GMPz') {
9586 $k = _any2mpz($k) // 0;
9587 }
9588  
9589 if (Math::GMPz::Rmpz_cmp_ui($k, 2) == 0) {
9590 return (Math::GMPz::Rmpz_popcount($n) == 1);
9591 }
9592  
9593 my $e = __ilog__($n, $k) // return 0;
9594  
9595 state $t = Math::GMPz::Rmpz_init_nobless();
9596 Math::GMPz::Rmpz_pow_ui($t, $k, $e);
9597  
9598 (Math::GMPz::Rmpz_cmp($t, $n) == 0);
9599 }
9600  
9601 #
9602 ## kronecker
9603 #
9604  
9605 sub kronecker ($$) {
9606 my ($n, $k) = @_;
9607  
9608 if (!ref($n) and CORE::int($n) eq $n and $n < ULONG_MAX and $n > LONG_MIN) {
9609  
9610 if (!ref($k) and CORE::int($k) eq $k and $k < ULONG_MAX and $k > LONG_MIN) {
9611 $k =
9612 ($k < 0)
9613 ? Math::GMPz::Rmpz_init_set_si($k)
9614 : Math::GMPz::Rmpz_init_set_ui($k);
9615 }
9616 else {
9617 $k = _star2mpz($k) // goto &nan;
9618 }
9619  
9620 return (
9621 $n < 0
9622 ? Math::GMPz::Rmpz_si_kronecker($n, $k)
9623 : Math::GMPz::Rmpz_ui_kronecker($n, $k)
9624 );
9625 }
9626  
9627 $n = _star2mpz($n) // goto &nan;
9628  
9629 if (!ref($k) and CORE::int($k) eq $k and $k < ULONG_MAX and $k > LONG_MIN) {
9630 return (
9631 $k < 0
9632 ? Math::GMPz::Rmpz_kronecker_si($n, $k)
9633 : Math::GMPz::Rmpz_kronecker_ui($n, $k)
9634 );
9635 }
9636  
9637 $k = _star2mpz($k) // goto &nan;
9638  
9639 Math::GMPz::Rmpz_kronecker($n, $k);
9640 }
9641  
9642 #
9643 ## valuation
9644 #
9645  
9646 sub __valuation__ { # takes two Math::GMPz objects
9647 my ($x, $y) = @_;
9648 Math::GMPz::Rmpz_sgn($y) || return (0, $x);
9649 Math::GMPz::Rmpz_cmpabs_ui($y, 1) || return (0, $x);
9650 my $r = Math::GMPz::Rmpz_init();
9651 my $v = Math::GMPz::Rmpz_remove($r, $x, $y);
9652 ($v, $r);
9653 }
9654  
9655 sub valuation ($$) {
9656 my ($n, $k) = @_;
9657  
9658 $n = _star2mpz($n) // goto &nan;
9659 $k = _star2mpz($k) // goto &nan;
9660  
9661 (__valuation__($n, $k))[0];
9662 }
9663  
9664 #
9665 ## remdiv
9666 #
9667  
9668 sub remdiv ($$) {
9669 my ($n, $k) = @_;
9670  
9671 $n = _star2mpz($n) // goto &nan;
9672 $k = _star2mpz($k) // goto &nan;
9673  
9674 bless \((__valuation__($n, $k))[1]);
9675 }
9676  
9677 #
9678 ## Make n coprime to k, by removing from n common factors with k
9679 #
9680 sub make_coprime {
9681 my ($n, $k) = @_;
9682  
9683 $n = _star2mpz($n) // goto &nan;
9684 $k = _star2mpz($k) // goto &nan;
9685  
9686 if (Math::GMPz::Rmpz_sgn($n) == 0) {
9687 return bless \$n;
9688 }
9689  
9690 my $r = Math::GMPz::Rmpz_init_set($n);
9691 my $g = Math::GMPz::Rmpz_init();
9692  
9693 Math::GMPz::Rmpz_gcd($g, $r, $k);
9694  
9695 while (Math::GMPz::Rmpz_cmp_ui($g, 1) > 0) {
9696 Math::GMPz::Rmpz_remove($r, $r, $g);
9697 Math::GMPz::Rmpz_gcd($g, $r, $g);
9698 }
9699  
9700 bless \$r;
9701 }
9702  
9703 #
9704 ## Invmod
9705 #
9706  
9707 sub invmod ($$) {
9708 my ($n, $k) = @_;
9709  
9710 $n = _star2mpz($n) // goto &nan;
9711 $k = _star2mpz($k) // goto &nan;
9712  
9713 my $r = Math::GMPz::Rmpz_init();
9714 Math::GMPz::Rmpz_invert($r, $n, $k) || (goto &nan);
9715 bless \$r;
9716 }
9717  
9718 sub _modular_rational {
9719 my ($n, $m) = @_;
9720  
9721 if (ref($n) ne 'Math::GMPq') {
9722 $n = _any2mpq($n) // return;
9723 }
9724  
9725 state $z = Math::GMPz::Rmpz_init_nobless();
9726  
9727 my $t = Math::GMPz::Rmpz_init();
9728 Math::GMPq::Rmpq_get_den($z, $n);
9729 Math::GMPz::Rmpz_invert($t, $z, $m) or return;
9730 Math::GMPq::Rmpq_get_num($z, $n);
9731 Math::GMPz::Rmpz_mul($t, $t, $z);
9732  
9733 return $t;
9734 }
9735  
9736 #
9737 ## Ratmod
9738 #
9739  
9740 sub ratmod ($$) {
9741 my ($n, $m) = @_;
9742  
9743 $n = _star2obj($n) // goto &nan;
9744 $m = _star2mpz($m) // goto &nan;
9745  
9746 my $r = Math::GMPz::Rmpz_init();
9747  
9748 if (ref($n) eq 'Math::GMPz') {
9749 Math::GMPz::Rmpz_mod($r, $n, $m);
9750 }
9751 else {
9752 $r = _modular_rational($n, $m) // goto &nan;
9753 Math::GMPz::Rmpz_mod($r, $r, $m);
9754 }
9755  
9756 bless \$r;
9757 }
9758  
9759 #
9760 ## Powmod
9761 #
9762  
9763 sub powmod ($$$) {
9764 my ($n, $k, $m) = @_;
9765  
9766 $n = _star2obj($n) // goto &nan;
9767 $k = _star2mpz($k) // goto &nan;
9768 $m = _star2mpz($m) // goto &nan;
9769  
9770 Math::GMPz::Rmpz_sgn($m) || goto &nan;
9771  
9772 if (ref($n) ne 'Math::GMPz') {
9773 if (__is_int__($n)) {
9774 $n = _any2mpz($n) // goto &nan;
9775 }
9776 else {
9777 $n = _modular_rational($n, $m) // goto &nan;
9778 }
9779 }
9780  
9781 my $r = Math::GMPz::Rmpz_init();
9782  
9783 if (Math::GMPz::Rmpz_sgn($k) < 0) {
9784 Math::GMPz::Rmpz_invert($r, $n, $m) or goto &nan;
9785 }
9786  
9787 Math::GMPz::Rmpz_fits_ulong_p($k)
9788 ? Math::GMPz::Rmpz_powm_ui($r, $n, Math::GMPz::Rmpz_get_ui($k), $m)
9789 : Math::GMPz::Rmpz_powm($r, $n, $k, $m);
9790  
9791 bless \$r;
9792 }
9793  
9794 #
9795 ## Geometric summation formula
9796 ## https://en.wikipedia.org/wiki/Geometric_series
9797 #
9798  
9799 sub geometric_sum ($$) {
9800 my ($n, $r) = @_;
9801  
9802 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
9803 $r = ref($r) eq __PACKAGE__ ? $$r : _star2obj($r);
9804  
9805 bless \__div__(__sub__(__pow__($r, __add__($n, 1)), 1), __sub__($r, 1));
9806 }
9807  
9808 #
9809 ## Faulhaber's summation formula
9810 ## https://en.wikipedia.org/wiki/Faulhaber%27s_formula
9811 #
9812  
9813 sub faulhaber_sum ($$) {
9814 my ($n, $p) = @_;
9815  
9816 $n = _star2mpz($n) // goto &nan;
9817 $p = _star2ui($p) // goto &nan;
9818  
9819 if ($p == 0) {
9820 return bless \$n;
9821 }
9822  
9823 if ($p == 1 or $p == 3) {
9824 my $r = Math::GMPz::Rmpz_init();
9825 Math::GMPz::Rmpz_add_ui($r, $n, 1);
9826 Math::GMPz::Rmpz_mul($r, $r, $n);
9827 Math::GMPz::Rmpz_div_2exp($r, $r, 1);
9828 Math::GMPz::Rmpz_mul($r, $r, $r) if ($p == 3);
9829 return bless \$r;
9830 }
9831  
9832 state $z = Math::GMPz::Rmpz_init_nobless();
9833  
9834 if ($p == 2) { # n*(n+1)*(2*n+1)/6
9835 my $r = Math::GMPz::Rmpz_init();
9836 Math::GMPz::Rmpz_add_ui($z, $n, 1);
9837 Math::GMPz::Rmpz_mul($r, $z, $n);
9838 Math::GMPz::Rmpz_mul_2exp($z, $z, 1);
9839 Math::GMPz::Rmpz_sub_ui($z, $z, 1);
9840 Math::GMPz::Rmpz_mul($r, $r, $z);
9841 Math::GMPz::Rmpz_divexact_ui($r, $r, 6);
9842 return bless \$r;
9843 }
9844  
9845 # When p >= n, sum the powers directly.
9846 if (Math::GMPz::Rmpz_cmp_ui($n, $p) <= 0) {
9847 my $r = Math::GMPz::Rmpz_init_set_ui(0);
9848 foreach my $k (1 .. Math::GMPz::Rmpz_get_ui($n)) {
9849 Math::GMPz::Rmpz_ui_pow_ui($z, $k, $p);
9850 Math::GMPz::Rmpz_add($r, $r, $z);
9851 }
9852 return bless \$r;
9853 }
9854  
9855 my @B = _bernoulli_numbers($p);
9856  
9857 my $q = Math::GMPq::Rmpq_init();
9858 my $u = Math::GMPz::Rmpz_init_set_ui(1);
9859  
9860 my $sum = Math::GMPq::Rmpq_init();
9861 Math::GMPq::Rmpq_set_ui($sum, 0, 1);
9862  
9863 # Sum_{k=1..n} k^p = 1/(p+1) * Sum_{j=0..p} binomial(p+1, j) * n^(p-j+1) * bernoulli(j)
9864 # = 1/(p+1) * Sum_{j=0..p} binomial(p+1, p-j) * n^(j+1) * bernoulli(p-j)
9865  
9866 foreach my $j (0 .. $p - 2) {
9867  
9868 Math::GMPz::Rmpz_mul($u, $u, $n);
9869  
9870 # Skip when bernoulli(p-j) == 0
9871 ($p - $j) % 2 == 0 or next;
9872  
9873 Math::GMPz::Rmpz_bin_uiui($z, $p + 1, $p - $j);
9874 Math::GMPz::Rmpz_mul($z, $z, $u);
9875 Math::GMPq::Rmpq_mul_z($q, $B[(($p - $j) >> 1) + 1], $z);
9876 Math::GMPq::Rmpq_add($sum, $sum, $q);
9877 }
9878  
9879 # sum += (1/2) * n^p * (2*n + p + 1)
9880 Math::GMPz::Rmpz_mul($u, $u, $n);
9881 Math::GMPz::Rmpz_mul_2exp($z, $n, 1);
9882 Math::GMPz::Rmpz_add_ui($z, $z, $p + 1);
9883 Math::GMPz::Rmpz_mul($u, $u, $z);
9884 Math::GMPq::Rmpq_set_ui($q, 1, 2);
9885 Math::GMPq::Rmpq_mul_z($q, $q, $u);
9886 Math::GMPq::Rmpq_add($sum, $sum, $q);
9887  
9888 # z = sum/(p+1)
9889 Math::GMPq::Rmpq_get_num($u, $sum);
9890 Math::GMPz::Rmpz_divexact_ui($u, $u, $p + 1);
9891 bless \$u;
9892 }
9893  
9894 #
9895 ## Dirichlet hyperbola method
9896 ## https://en.wikipedia.org/wiki/Dirichlet_hyperbola_method
9897 #
9898  
9899 sub dirichlet_sum ($$$$$) {
9900 my ($n, $f, $g, $F, $G) = @_;
9901  
9902 $n = _star2mpz($n) // goto &nan;
9903 Math::GMPz::Rmpz_sgn($n) > 0 or goto &zero;
9904  
9905 $f //= sub { 1 };
9906 $g //= sub { 1 };
9907  
9908 $F //= sub { $_[0] };
9909 $G //= sub { $_[0] };
9910  
9911 my $s = Math::GMPz::Rmpz_init();
9912 Math::GMPz::Rmpz_sqrt($s, $n);
9913  
9914 my $sum = Math::GMPz::Rmpz_init_set_ui(0);
9915  
9916 my $t = bless \Math::GMPz::Rmpz_init_set_ui(0);
9917 my $u = bless \Math::GMPz::Rmpz_init_set_ui(0);
9918  
9919 Math::GMPz::Rmpz_fits_slong_p($s) || goto &nan;
9920  
9921 foreach my $k (1 .. Math::GMPz::Rmpz_get_ui($s)) {
9922  
9923 Math::GMPz::Rmpz_set_ui($$t, $k);
9924 Math::GMPz::Rmpz_div_ui($$u, $n, $k);
9925  
9926 my $f_r = $f->($t);
9927 my $g_r = $g->($t);
9928 my $F_r = $F->($u);
9929 my $G_r = $G->($u);
9930  
9931 $f_r = _star2mpz($f_r) // goto &nan;
9932 $g_r = _star2mpz($g_r) // goto &nan;
9933 $F_r = _star2mpz($F_r) // goto &nan;
9934 $G_r = _star2mpz($G_r) // goto &nan;
9935  
9936 Math::GMPz::Rmpz_addmul($sum, $f_r, $G_r);
9937 Math::GMPz::Rmpz_addmul($sum, $g_r, $F_r);
9938 }
9939  
9940 $sum = __sub__($sum, __mul__(_star2mpz($F->(bless \$s)), _star2mpz($G->(bless \$s))));
9941 bless \$sum;
9942 }
9943  
9944 #
9945 ## Catalan numbers
9946 #
9947  
9948 sub catalan ($;$) {
9949 my ($n, $k) = @_;
9950  
9951 # Catalan's triangle
9952 # catalan(n, k) = binomial(n+k, k) - binomial(n+k, k-1)
9953 if (scalar(@_) == 2) {
9954  
9955 $n = _star2mpz($n) // goto &nan;
9956 $k = _star2ui($k) // goto &nan;
9957  
9958 my $t = Math::GMPz::Rmpz_init();
9959 my $u = Math::GMPz::Rmpz_init();
9960  
9961 Math::GMPz::Rmpz_add_ui($t, $n, $k);
9962 Math::GMPz::Rmpz_bin_ui($u, $t, $k);
9963 ($k > 0)
9964 ? Math::GMPz::Rmpz_bin_ui($t, $t, $k - 1)
9965 : Math::GMPz::Rmpz_bin_si($t, $t, $k - 1);
9966 Math::GMPz::Rmpz_sub($u, $u, $t);
9967  
9968 return bless \$u;
9969 }
9970  
9971 $n = _star2ui($n) // goto &nan;
9972  
9973 my $r = Math::GMPz::Rmpz_init();
9974 Math::GMPz::Rmpz_bin_uiui($r, $n << 1, $n);
9975 Math::GMPz::Rmpz_divexact_ui($r, $r, $n + 1);
9976 bless \$r;
9977 }
9978  
9979 #
9980 ## Bell numbers
9981 #
9982  
9983 sub bell ($) {
9984 my ($n) = @_;
9985  
9986 $n = _star2ui($n) // goto &nan;
9987  
9988 my @acc;
9989  
9990 my $t = Math::GMPz::Rmpz_init();
9991 my $bell = Math::GMPz::Rmpz_init_set_ui(1);
9992  
9993 foreach my $k (1 .. $n) {
9994  
9995 Math::GMPz::Rmpz_set($t, $bell);
9996  
9997 foreach my $item (@acc) {
9998 Math::GMPz::Rmpz_add($t, $t, $item);
9999 Math::GMPz::Rmpz_set($item, $t);
10000 }
10001  
10002 unshift @acc, $bell;
10003 $bell = Math::GMPz::Rmpz_init_set($acc[-1]);
10004 }
10005  
10006 bless \$bell;
10007 }
10008  
10009 #
10010 ## Binomial coefficient
10011 #
10012  
10013 sub binomial ($$) {
10014 my ($n, $k) = @_;
10015  
10016 # `n` and `k` are native unsigned integers
10017 if ( !ref($n)
10018 and !ref($k)
10019 and CORE::int($n) eq $n
10020 and CORE::int($k) eq $k
10021 and $n >= 0
10022 and $k >= 0
10023 and $n < 1e6
10024 and $k < ULONG_MAX) {
10025 my $r = Math::GMPz::Rmpz_init();
10026 Math::GMPz::Rmpz_bin_uiui($r, $n, $k);
10027 return bless \$r;
10028 }
10029  
10030 $n = _star2mpz($n) // goto &nan;
10031 $k = _star2si($k) // goto &nan;
10032  
10033 my $r = Math::GMPz::Rmpz_init();
10034  
10035 if ($k >= 0 and Math::GMPz::Rmpz_fits_ulong_p($n) and Math::GMPz::Rmpz_cmp_ui($n, 1e6) <= 0) {
10036 Math::GMPz::Rmpz_bin_uiui($r, Math::GMPz::Rmpz_get_ui($n), $k);
10037 }
10038 else {
10039 $k < 0
10040 ? Math::GMPz::Rmpz_bin_si($r, $n, $k)
10041 : Math::GMPz::Rmpz_bin_ui($r, $n, $k);
10042 }
10043  
10044 bless \$r;
10045 }
10046  
10047 #
10048 ## Multinomial coefficient
10049 #
10050  
10051 sub multinomial {
10052 my ($n, @mset) = @_;
10053  
10054 $n = _star2mpz($n) // goto &nan;
10055  
10056 my $bin = Math::GMPz::Rmpz_init();
10057 my $sum = Math::GMPz::Rmpz_init_set($n);
10058 my $prod = Math::GMPz::Rmpz_init_set_ui(1);
10059  
10060 foreach my $k (@mset) {
10061  
10062 $k = _star2si($k) // goto &nan;
10063  
10064 $k < 0
10065 ? Math::GMPz::Rmpz_sub_ui($sum, $sum, -$k)
10066 : Math::GMPz::Rmpz_add_ui($sum, $sum, $k);
10067  
10068 if ($k >= 0 and Math::GMPz::Rmpz_fits_ulong_p($sum)) {
10069 Math::GMPz::Rmpz_bin_uiui($bin, Math::GMPz::Rmpz_get_ui($sum), $k);
10070 }
10071 else {
10072 $k < 0
10073 ? Math::GMPz::Rmpz_bin_si($bin, $sum, $k)
10074 : Math::GMPz::Rmpz_bin_ui($bin, $sum, $k);
10075 }
10076  
10077 Math::GMPz::Rmpz_mul($prod, $prod, $bin);
10078 }
10079  
10080 bless \$prod;
10081 }
10082  
10083 #
10084 ## AND
10085 #
10086  
10087 sub and { # used in overloading
10088 my ($n, $k) = @_;
10089  
10090 $n = _star2mpz($n) // goto &nan;
10091 $k = _star2mpz($k) // goto &nan;
10092  
10093 my $r = Math::GMPz::Rmpz_init();
10094 Math::GMPz::Rmpz_and($r, $n, $k);
10095 bless \$r;
10096 }
10097  
10098 #
10099 ## OR
10100 #
10101  
10102 sub or { # used in overloading
10103 my ($n, $k) = @_;
10104  
10105 $n = _star2mpz($n) // goto &nan;
10106 $k = _star2mpz($k) // goto &nan;
10107  
10108 my $r = Math::GMPz::Rmpz_init();
10109 Math::GMPz::Rmpz_ior($r, $n, $k);
10110 bless \$r;
10111 }
10112  
10113 #
10114 ## XOR
10115 #
10116  
10117 sub xor { # used in overloading
10118 my ($n, $k) = @_;
10119  
10120 $n = _star2mpz($n) // goto &nan;
10121 $k = _star2mpz($k) // goto &nan;
10122  
10123 my $r = Math::GMPz::Rmpz_init();
10124 Math::GMPz::Rmpz_xor($r, $n, $k);
10125 bless \$r;
10126 }
10127  
10128 #
10129 ## NOT
10130 #
10131  
10132 sub not { # used in overloading
10133 my ($n) = @_;
10134  
10135 $n = _star2mpz($n) // goto &nan;
10136  
10137 my $r = Math::GMPz::Rmpz_init();
10138 Math::GMPz::Rmpz_com($r, $n);
10139 bless \$r;
10140 }
10141  
10142 #
10143 ## Get k-th bit of integer `n`
10144 #
10145  
10146 sub getbit ($$) {
10147 my ($n, $k) = @_;
10148  
10149 $n = _star2mpz($n) // return undef;
10150 $k = _star2ui($k) // return undef;
10151  
10152 Math::GMPz::Rmpz_tstbit($n, $k);
10153 }
10154  
10155 #
10156 ## Set k-th bit of integer `n` to 1
10157 #
10158  
10159 sub setbit ($$) {
10160 my ($n, $k) = @_;
10161  
10162 $n = _star2mpz($n) // return undef;
10163 $k = _star2ui($k) // return undef;
10164  
10165 my $r = Math::GMPz::Rmpz_init_set($n);
10166 Math::GMPz::Rmpz_setbit($r, $k);
10167 bless \$r;
10168 }
10169  
10170 #
10171 ## FLIP-BIT (XOR)
10172 #
10173  
10174 sub flipbit ($$) {
10175 my ($n, $k) = @_;
10176  
10177 $n = _star2mpz($n) // goto &nan;
10178 $k = _star2ui($k) // goto &nan;
10179  
10180 my $r = Math::GMPz::Rmpz_init_set($n);
10181  
10182 Math::GMPz::Rmpz_tstbit($r, $k)
10183 ? Math::GMPz::Rmpz_clrbit($r, $k)
10184 : Math::GMPz::Rmpz_setbit($r, $k);
10185  
10186 bless \$r;
10187 }
10188  
10189 #
10190 ## CLEAR BIT (set bit $y to 0)
10191 #
10192  
10193 sub clearbit ($$) {
10194 my ($n, $k) = @_;
10195  
10196 $n = _star2mpz($n) // goto &nan;
10197 $k = _star2ui($k) // goto &nan;
10198  
10199 my $r = Math::GMPz::Rmpz_init_set($n);
10200 Math::GMPz::Rmpz_clrbit($r, $k);
10201 bless \$r;
10202 }
10203  
10204 #
10205 ## Scan n starting from bit index k, towards more
10206 ## significant bits, until the first 0 is found.
10207 #
10208  
10209 sub bit_scan0 ($;$) {
10210 my ($n, $k) = @_;
10211  
10212 $n = _star2mpz($n) // return undef;
10213  
10214 if (defined($k)) {
10215 $k = _star2ui($k) // return undef;
10216 }
10217 else {
10218 $k = 0;
10219 }
10220  
10221 Math::GMPz::Rmpz_scan0($n, $k);
10222 }
10223  
10224 #
10225 ## Scan n starting from bit index k, towards more
10226 ## significant bits, until the first 1 is found.
10227 #
10228  
10229 sub bit_scan1 ($;$) {
10230 my ($n, $k) = @_;
10231  
10232 $n = _star2mpz($n) // return undef;
10233  
10234 if (defined($k)) {
10235 $k = _star2ui($k) // return undef;
10236 }
10237 else {
10238 $k = 0;
10239 }
10240  
10241 Math::GMPz::Rmpz_scan1($n, $k);
10242 }
10243  
10244 #
10245 ## LEFT SHIFT
10246 #
10247  
10248 sub lsft { # used in overloading
10249 my ($n, $k) = @_;
10250  
10251 $n = _star2mpz($n) // goto &nan;
10252 $k = _star2si($k) // goto &nan;
10253  
10254 my $r = Math::GMPz::Rmpz_init();
10255  
10256 $k < 0
10257 ? Math::GMPz::Rmpz_div_2exp($r, $n, -$k)
10258 : Math::GMPz::Rmpz_mul_2exp($r, $n, CORE::int($k));
10259  
10260 bless \$r;
10261 }
10262  
10263 #
10264 ## RIGHT SHIFT
10265 #
10266  
10267 sub rsft { # used in overloading
10268 my ($n, $k) = @_;
10269  
10270 $n = _star2mpz($n) // goto &nan;
10271 $k = _star2si($k) // goto &nan;
10272  
10273 my $r = Math::GMPz::Rmpz_init();
10274  
10275 $k < 0
10276 ? Math::GMPz::Rmpz_mul_2exp($r, $n, -$k)
10277 : Math::GMPz::Rmpz_div_2exp($r, $n, CORE::int($k));
10278  
10279 bless \$r;
10280 }
10281  
10282 #
10283 ## Population count: number of 1's in the binary representation of `n`
10284 #
10285  
10286 sub popcount ($) {
10287 my ($n) = @_;
10288  
10289 $n = _star2mpz($n) // return undef;
10290  
10291 if (Math::GMPz::Rmpz_sgn($n) < 0) {
10292 $n = Math::GMPz::Rmpz_init_set($n);
10293 Math::GMPz::Rmpz_neg($n, $n);
10294 }
10295  
10296 Math::GMPz::Rmpz_popcount($n);
10297 }
10298  
10299 #
10300 ## Hamming distance
10301 #
10302  
10303 sub hamdist ($$) {
10304 my ($n, $k) = @_;
10305  
10306 $n = _star2mpz($n) // return undef;
10307 $k = _star2mpz($k) // return undef;
10308  
10309 Math::GMPz::Rmpz_hamdist($n, $k);
10310 }
10311  
10312 #
10313 ## Conversions
10314 #
10315  
10316 sub as_bin ($) {
10317 my ($n) = @_;
10318 $n = _star2mpz($n) // return undef;
10319 Math::GMPz::Rmpz_get_str($n, 2);
10320 }
10321  
10322 sub as_oct ($) {
10323 my ($n) = @_;
10324 $n = _star2mpz($n) // return undef;
10325 Math::GMPz::Rmpz_get_str($n, 8);
10326 }
10327  
10328 sub as_hex ($) {
10329 my ($n) = @_;
10330 $n = _star2mpz($n) // return undef;
10331 Math::GMPz::Rmpz_get_str($n, 16);
10332 }
10333  
10334 sub as_int ($;$) {
10335 my ($n, $k) = @_;
10336  
10337 my $base = 10;
10338 if (defined($k)) {
10339  
10340 $base = _star2ui($k) // 0;
10341  
10342 if ($base < 2 or $base > 62) {
10343 require Carp;
10344 Carp::croak("base must be between 2 and 62, got $k");
10345 }
10346 }
10347  
10348 $n = _star2mpz($n) // return undef;
10349 Math::GMPz::Rmpz_get_str($n, $base);
10350 }
10351  
10352 sub __as_rat__ {
10353 my ($n, $k) = @_;
10354  
10355 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
10356  
10357 my $base = 10;
10358 if (defined($k)) {
10359  
10360 $base = _star2ui($k) // 0;
10361  
10362 if ($base < 2 or $base > 62) {
10363 require Carp;
10364 Carp::croak("base must be between 2 and 62, got $k");
10365 }
10366 }
10367  
10368 my $ref = ref($n);
10369 if ($ref eq 'Math::GMPq' or $ref eq 'Math::GMPz') {
10370 return (
10371 $ref eq 'Math::GMPq'
10372 ? Math::GMPq::Rmpq_get_str($n, $base)
10373 : Math::GMPz::Rmpz_get_str($n, $base)
10374 );
10375 }
10376  
10377 $n = _any2mpq($n) // return undef;
10378 Math::GMPq::Rmpq_get_str($n, $base);
10379 }
10380  
10381 sub as_rat ($;$) {
10382 my ($n, $k) = @_;
10383 __as_rat__($n, $k) // return undef;
10384 }
10385  
10386 sub as_frac ($;$) {
10387 my ($n, $k) = @_;
10388 my $rat = __as_rat__($n, $k) // return undef;
10389 $rat .= '/1' if (index($rat, '/') == -1);
10390 $rat;
10391 }
10392  
10393 sub as_dec ($;$) {
10394 my ($n, $d) = @_;
10395  
10396 my $prec = $PREC;
10397 if (defined($d)) {
10398  
10399 $prec = _star2ui($d) // 0;
10400 $prec <<= 2;
10401  
10402 state $min_prec = Math::MPFR::RMPFR_PREC_MIN();
10403 state $max_prec = Math::MPFR::RMPFR_PREC_MAX();
10404  
10405 if ($prec < $min_prec or $prec > $max_prec) {
10406 require Carp;
10407 Carp::croak("precision must be between $min_prec and $max_prec, got ", $prec);
10408 }
10409 }
10410  
10411 local $PREC = $prec;
10412 __stringify__(_star2mpfr_mpc($n));
10413 }
10414  
10415 sub __base__ {
10416 my ($x, $base) = @_;
10417 goto(ref($x) =~ tr/:/_/rs);
10418  
10419 Math_GMPz: {
10420 return Math::GMPz::Rmpz_get_str($x, $base);
10421 }
10422  
10423 Math_GMPq: {
10424 return Math::GMPq::Rmpq_get_str($x, $base);
10425 }
10426  
10427 Math_MPFR: {
10428 return Math::MPFR::Rmpfr_get_str($x, $base, 0, $ROUND);
10429 }
10430  
10431 Math_MPC: {
10432  
10433 # return Math::MPC::Rmpc_get_str($base, 0, $x, $ROUND); # not OK
10434  
10435 my $fr = Math::MPFR::Rmpfr_init2($PREC);
10436 Math::MPC::RMPC_RE($fr, $x);
10437 my $real = __base__($fr, $base);
10438 Math::MPC::RMPC_IM($fr, $x);
10439 return $real if Math::MPFR::Rmpfr_zero_p($fr);
10440 my $imag = __base__($fr, $base);
10441 return "($real $imag)";
10442 }
10443 }
10444  
10445 sub base {
10446 my ($n, $k) = @_;
10447  
10448 $n = ref($n) eq __PACKAGE__ ? $$n : _star2obj($n);
10449  
10450 my $base = 10;
10451 if (defined($k)) {
10452  
10453 $base = _star2ui($k) // 0;
10454  
10455 if ($base < 2 or $base > 62) {
10456 require Carp;
10457 Carp::croak("base must be between 2 and 62, got $k");
10458 }
10459 }
10460  
10461 __base__($n, $base);
10462 }
10463  
10464 sub rat_approx ($) {
10465 my ($x) = @_;
10466  
10467 $x = _star2mpfr($x);
10468  
10469 Math::MPFR::Rmpfr_number_p($x) || goto &nan;
10470  
10471 my $n1 = Math::GMPz::Rmpz_init_set_ui(0);
10472 my $n2 = Math::GMPz::Rmpz_init_set_ui(1);
10473  
10474 my $d1 = Math::GMPz::Rmpz_init_set_ui(1);
10475 my $d2 = Math::GMPz::Rmpz_init_set_ui(0);
10476  
10477 my $q = Math::GMPq::Rmpq_init();
10478 my $z = Math::GMPz::Rmpz_init();
10479  
10480 my $s = __stringify__($x);
10481  
10482 my $f1 = Math::MPFR::Rmpfr_init2($PREC);
10483 my $f2 = Math::MPFR::Rmpfr_init2($PREC);
10484 my $f3 = Math::MPFR::Rmpfr_init2($PREC);
10485  
10486 Math::MPFR::Rmpfr_set($f1, $x, $ROUND);
10487  
10488 while (1) {
10489 Math::MPFR::Rmpfr_floor($f2, $f1);
10490 Math::MPFR::Rmpfr_get_z($z, $f2, $ROUND);
10491  
10492 Math::GMPz::Rmpz_addmul($n1, $n2, $z); # n1 += n2 * z
10493 Math::GMPz::Rmpz_addmul($d1, $d2, $z); # d1 += d2 * z
10494  
10495 ($n1, $n2) = ($n2, $n1);
10496 ($d1, $d2) = ($d2, $d1);
10497  
10498 # q = n2 / d2
10499 Math::GMPq::Rmpq_set_num($q, $n2);
10500 Math::GMPq::Rmpq_set_den($q, $d2);
10501 Math::GMPq::Rmpq_canonicalize($q);
10502  
10503 Math::MPFR::Rmpfr_set_q($f3, $q, $ROUND);
10504 CORE::index(__stringify__($f3), $s) == 0 and last;
10505  
10506 # f1 = 1 / (f1 - f2)
10507 Math::MPFR::Rmpfr_sub($f1, $f1, $f2, $ROUND);
10508 Math::MPFR::Rmpfr_zero_p($f1) && last;
10509 Math::MPFR::Rmpfr_ui_div($f1, 1, $f1, $ROUND);
10510 }
10511  
10512 bless \$q;
10513 }
10514  
10515 my %DIGITS_36;
10516 @DIGITS_36{0 .. 9, 'a' .. 'z'} = (0 .. 35);
10517  
10518 my %DIGITS_62;
10519 @DIGITS_62{0 .. 9, 'A' .. 'Z', 'a' .. 'z'} = (0 .. 61);
10520  
10521 sub digits ($;$) {
10522 my ($n, $k) = @_;
10523  
10524 $n = _star2mpz($n) // return;
10525  
10526 $k //= 10;
10527  
10528 my $sgn = Math::GMPz::Rmpz_sgn($n);
10529  
10530 if ($sgn == 0) { # n = 0
10531 return (0);
10532 }
10533 elsif ($sgn < 0) { # n < 0; make it absolute
10534 $n = Math::GMPz::Rmpz_init_set($n);
10535 Math::GMPz::Rmpz_abs($n, $n);
10536 }
10537  
10538 if (!ref($k) and CORE::int($k) eq $k and $k > 1 and $k < ULONG_MAX) {
10539 return if ($k <= 1); # not defined for k <= 1
10540 }
10541 else {
10542 $k = _star2mpz($k) // return;
10543 return if (Math::GMPz::Rmpz_cmp_ui($k, 1) <= 0); # not defined for k <= 1
10544 }
10545  
10546 # Return faster for k=2..62
10547 if (ref($k) ? (Math::GMPz::Rmpz_cmp_ui($k, 62) <= 0) : ($k <= 62)) {
10548 $k = Math::GMPz::Rmpz_get_ui($k) if ref($k);
10549 return map { $k <= 36 ? $DIGITS_36{$_} : $DIGITS_62{$_} }
10550 split(//, scalar reverse scalar(Math::GMPz::Rmpz_get_str($n, $k)));
10551 }
10552  
10553 # Subquadratic algorithm from "Modern Computer Arithmetic" by Richard P. Brent and Paul Zimmermann
10554 if (!ref($k) || Math::GMPz::Rmpz_fits_ulong_p($k)) {
10555  
10556 my $A = $n;
10557 my $B = ref($k) ? Math::GMPz::Rmpz_get_ui($k) : $k;
10558  
10559 # Find r such that B^(2r - 2) <= A < B^(2r)
10560 my $r = (__ilog__($A, $B) >> 1) + 1;
10561  
10562 state $Q = Math::GMPz::Rmpz_init_nobless();
10563 state $R = Math::GMPz::Rmpz_init_nobless();
10564  
10565 return sub {
10566 my ($A, $r) = @_;
10567  
10568 # Cut the recursion early
10569 if (Math::GMPz::Rmpz_fits_ulong_p($A)) {
10570 my $v = Math::GMPz::Rmpz_init_set($A);
10571 my $m = Math::GMPz::Rmpz_init();
10572  
10573 my @digits;
10574 while (Math::GMPz::Rmpz_sgn($v)) {
10575 push @digits, Math::GMPz::Rmpz_divmod_ui($v, $m, $v, $B);
10576 }
10577 return @digits;
10578 }
10579  
10580 #~ if (Math::GMPz::Rmpz_cmp_ui($A, $B) < 0) {
10581 #~ return Math::GMPz::Rmpz_get_ui($A);
10582 #~ }
10583  
10584 my $t = Math::GMPz::Rmpz_init();
10585 Math::GMPz::Rmpz_ui_pow_ui($t, $B, 2 * ($r - 1)); # can this be optimized away?
10586  
10587 if (Math::GMPz::Rmpz_cmp($t, $A) > 0) {
10588 --$r;
10589 }
10590  
10591 Math::GMPz::Rmpz_ui_pow_ui($t, $B, $r);
10592 Math::GMPz::Rmpz_divmod($Q, $R, $A, $t);
10593  
10594 my $w = ($r + 1) >> 1;
10595 Math::GMPz::Rmpz_set($t, $Q);
10596  
10597 my @right = __SUB__->($R, $w);
10598 my @left = __SUB__->($t, $w);
10599  
10600 (@right, (0) x ($r - scalar(@right)), @left);
10601 }
10602 ->($A, $r);
10603 }
10604  
10605 # This algorithm will be used only when base > ULONG_MAX
10606 my @digits;
10607  
10608 $n = Math::GMPz::Rmpz_init_set($n); # copy
10609  
10610 while (Math::GMPz::Rmpz_sgn($n) > 0) {
10611 my $m = Math::GMPz::Rmpz_init();
10612 Math::GMPz::Rmpz_divmod($n, $m, $n, $k);
10613 push @digits, bless \$m;
10614 }
10615  
10616 return @digits;
10617 }
10618  
10619 sub sumdigits ($;$) {
10620 my ($n, $k) = @_;
10621  
10622 $n = _star2mpz($n) // goto &nan;
10623  
10624 $k //= 10;
10625  
10626 my $sgn = Math::GMPz::Rmpz_sgn($n);
10627  
10628 if ($sgn == 0) { # n = 0
10629 goto &zero;
10630 }
10631 elsif ($sgn < 0) { # n < 0; make it absolute
10632 $n = Math::GMPz::Rmpz_init_set($n);
10633 Math::GMPz::Rmpz_abs($n, $n);
10634 }
10635  
10636 if (!ref($k) and CORE::int($k) eq $k and $k > 1 and $k < ULONG_MAX) {
10637 goto &nan if ($k <= 1); # not defined for k <= 1
10638 }
10639 else {
10640 $k = _star2mpz($k) // goto &nan;
10641 goto &nan if (Math::GMPz::Rmpz_cmp_ui($k, 1) <= 0); # not defined for k <= 1
10642 }
10643  
10644 # Return faster for k=2..62
10645 #<<<
10646 if (ref($k) ? (Math::GMPz::Rmpz_cmp_ui($k, 62) <= 0) : ($k <= 62)) {
10647 $k = Math::GMPz::Rmpz_get_ui($k) if ref($k);
10648  
10649 return bless \Math::GMPz::Rmpz_init_set_ui(Math::GMPz::Rmpz_popcount($n)) if $k == 2;
10650  
10651 if (Math::GMPz::Rmpz_sizeinbase($n, $k) <= 1e6) {
10652 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))));
10653 }
10654 }
10655 #>>>
10656  
10657 # Subquadratic algorithm from "Modern Computer Arithmetic" by Richard P. Brent and Paul Zimmermann
10658 if (!ref($k) || Math::GMPz::Rmpz_fits_ulong_p($k)) {
10659  
10660 my $A = $n;
10661 my $B = ref($k) ? Math::GMPz::Rmpz_get_ui($k) : $k;
10662  
10663 # Find r such that B^(2r - 2) <= A < B^(2r)
10664 my $r = (__ilog__($A, $B) >> 1) + 1;
10665  
10666 state $Q = Math::GMPz::Rmpz_init_nobless();
10667 state $R = Math::GMPz::Rmpz_init_nobless();
10668  
10669 my $total = sub {
10670 my ($A, $r) = @_;
10671  
10672 # Cut the recursion early
10673 if (Math::GMPz::Rmpz_fits_ulong_p($A)) {
10674 my $v = Math::GMPz::Rmpz_init_set($A);
10675 my $m = Math::GMPz::Rmpz_init();
10676  
10677 my $sum = 0;
10678 while (Math::GMPz::Rmpz_sgn($v)) {
10679 $sum += Math::GMPz::Rmpz_divmod_ui($v, $m, $v, $B);
10680 }
10681 return $sum;
10682 }
10683  
10684 #~ if (Math::GMPz::Rmpz_cmp_ui($A, $B) < 0) {
10685 #~ return Math::GMPz::Rmpz_get_ui($A);
10686 #~ }
10687  
10688 my $w = ($r + 1) >> 1;
10689 my $t = Math::GMPz::Rmpz_init();
10690  
10691 Math::GMPz::Rmpz_ui_pow_ui($t, $B, $r);
10692 Math::GMPz::Rmpz_divmod($Q, $R, $A, $t);
10693 Math::GMPz::Rmpz_set($t, $Q);
10694  
10695 __SUB__->($R, $w) + __SUB__->($t, $w);
10696 }
10697 ->($A, $r);
10698  
10699 ($total < ULONG_MAX)
10700 && return bless \Math::GMPz::Rmpz_init_set_ui($total);
10701 }
10702  
10703 # This algorithm will be used only for very large bases,
10704 # base > ULONG_MAX, or when the sum of digits exceeds ULONG_MAX.
10705 $k = Math::GMPz::Rmpz_init_set_ui($k) if !ref($k);
10706 $n = Math::GMPz::Rmpz_init_set($n); # copy
10707  
10708 my $m = Math::GMPz::Rmpz_init();
10709 my $sum = Math::GMPz::Rmpz_init_set_ui(0);
10710  
10711 while (Math::GMPz::Rmpz_sgn($n) > 0) {
10712 Math::GMPz::Rmpz_divmod($n, $m, $n, $k);
10713 Math::GMPz::Rmpz_add($sum, $sum, $m);
10714 }
10715  
10716 bless \$sum;
10717 }
10718  
10719 my %FROM_DIGITS_36;
10720 @FROM_DIGITS_36{0 .. 35} = (0 .. 9, 'a' .. 'z');
10721  
10722 my %FROM_DIGITS_62;
10723 @FROM_DIGITS_62{0 .. 61} = (0 .. 9, 'A' .. 'Z', 'a' .. 'z');
10724  
10725 sub digits2num {
10726 my ($digits, $base) = @_;
10727  
10728 ref($digits) eq 'ARRAY' or goto &nan;
10729  
10730 $base //= 10;
10731  
10732 if ($base <= 1) {
10733 goto &nan;
10734 }
10735  
10736 @$digits || goto &zero;
10737  
10738 #<<<
10739 if ($base <= 62) {
10740 $base = ref($base) ? Math::GMPz::Rmpz_get_ui(_star2mpz($base) // goto &nan) : CORE::int($base);
10741 return bless \Math::GMPz::Rmpz_init_set_str(scalar reverse(join('', map { ($base <= 36 ? $FROM_DIGITS_36{$_} : $FROM_DIGITS_62{$_}) // goto &nan } @$digits)), $base);
10742 }
10743 #>>>
10744  
10745 if (!ref($base) and CORE::int($base) eq $base and $base > 1 and $base < ULONG_MAX) {
10746 $base = Math::GMPz::Rmpz_init_set_ui($base);
10747 }
10748 else {
10749 $base = Math::GMPz::Rmpz_init_set(_star2mpz($base) // goto &nan);
10750 }
10751  
10752 my @digits = map {
10753 (!ref($_) and CORE::int($_) eq $_ and $_ >= 0 and $_ < ULONG_MAX)
10754 ? Math::GMPz::Rmpz_init_set_ui($_)
10755 : Math::GMPz::Rmpz_init_set(_star2mpz($_) // goto &nan);
10756 } @$digits;
10757  
10758 my $L = \@digits;
10759  
10760 # Algorithm from "Modern Computer Arithmetic"
10761 # by Richard P. Brent and Paul Zimmermann
10762  
10763 for (my $k = scalar(@digits) ; $k > 1 ; $k = ($k >> 1) + ($k & 1)) {
10764  
10765 my @T;
10766 for (0 .. ($k >> 1) - 1) {
10767 Math::GMPz::Rmpz_addmul($L->[$_ << 1], $L->[($_ << 1) + 1], $base);
10768 push @T, $L->[$_ << 1];
10769 }
10770  
10771 push(@T, $L->[-1]) if ($k & 1);
10772 $L = \@T;
10773 Math::GMPz::Rmpz_mul($base, $base, $base);
10774 }
10775  
10776 bless \($L->[0]);
10777 }
10778  
10779 sub bsearch ($$;$) {
10780 my ($left, $right, $block) = @_;
10781  
10782 if (@_ == 3) {
10783 $left = Math::GMPz::Rmpz_init_set(_star2mpz($left) // return undef);
10784 $right = Math::GMPz::Rmpz_init_set(_star2mpz($right) // return undef);
10785 }
10786 else {
10787 $block = $right;
10788 $right = Math::GMPz::Rmpz_init_set(_star2mpz($left) // return undef);
10789 $left = Math::GMPz::Rmpz_init_set_ui(0);
10790 }
10791  
10792 my $middle = Math::GMPz::Rmpz_init();
10793  
10794 while (Math::GMPz::Rmpz_cmp($left, $right) <= 0) {
10795  
10796 Math::GMPz::Rmpz_add($middle, $left, $right);
10797 Math::GMPz::Rmpz_div_2exp($middle, $middle, 1);
10798  
10799 my $cmp = do {
10800 local $_ = bless \Math::GMPz::Rmpz_init_set($middle);
10801 $block->($_) || return $_;
10802 };
10803  
10804 if ($cmp > 0) {
10805 Math::GMPz::Rmpz_sub_ui($right, $middle, 1);
10806 }
10807 else {
10808 Math::GMPz::Rmpz_add_ui($left, $middle, 1);
10809 }
10810 }
10811  
10812 return undef;
10813 }
10814  
10815 sub bsearch_ge ($$;$) {
10816 my ($left, $right, $block) = @_;
10817  
10818 if (@_ == 3) {
10819 $left = Math::GMPz::Rmpz_init_set(_star2mpz($left) // return undef);
10820 $right = Math::GMPz::Rmpz_init_set(_star2mpz($right) // return undef);
10821 }
10822 else {
10823 $block = $right;
10824 $right = Math::GMPz::Rmpz_init_set(_star2mpz($left) // return undef);
10825 $left = Math::GMPz::Rmpz_init_set_ui(0);
10826 }
10827  
10828 my $middle = Math::GMPz::Rmpz_init();
10829  
10830 while (1) {
10831  
10832 Math::GMPz::Rmpz_add($middle, $left, $right);
10833 Math::GMPz::Rmpz_div_2exp($middle, $middle, 1);
10834  
10835 my $cmp = do {
10836 local $_ = bless \Math::GMPz::Rmpz_init_set($middle);
10837 $block->($_) || return $_;
10838 };
10839  
10840 if ($cmp < 0) {
10841 Math::GMPz::Rmpz_add_ui($left, $middle, 1);
10842  
10843 if (Math::GMPz::Rmpz_cmp($left, $right) > 0) {
10844 Math::GMPz::Rmpz_add_ui($middle, $middle, 1);
10845 last;
10846 }
10847 }
10848 else {
10849 Math::GMPz::Rmpz_sub_ui($right, $middle, 1);
10850 Math::GMPz::Rmpz_cmp($left, $right) > 0 and last;
10851 }
10852 }
10853  
10854 bless \$middle;
10855 }
10856  
10857 sub bsearch_le ($$;$) {
10858 my ($left, $right, $block) = @_;
10859  
10860 if (@_ == 3) {
10861 $left = Math::GMPz::Rmpz_init_set(_star2mpz($left) // return undef);
10862 $right = Math::GMPz::Rmpz_init_set(_star2mpz($right) // return undef);
10863 }
10864 else {
10865 $block = $right;
10866 $right = Math::GMPz::Rmpz_init_set(_star2mpz($left) // return undef);
10867 $left = Math::GMPz::Rmpz_init_set_ui(0);
10868 }
10869  
10870 my $middle = Math::GMPz::Rmpz_init();
10871  
10872 while (1) {
10873  
10874 Math::GMPz::Rmpz_add($middle, $left, $right);
10875 Math::GMPz::Rmpz_div_2exp($middle, $middle, 1);
10876  
10877 my $cmp = do {
10878 local $_ = bless \Math::GMPz::Rmpz_init_set($middle);
10879 $block->($_) || return $_;
10880 };
10881  
10882 if ($cmp < 0) {
10883 Math::GMPz::Rmpz_add_ui($left, $middle, 1);
10884 Math::GMPz::Rmpz_cmp($left, $right) > 0 and last;
10885 }
10886 else {
10887 Math::GMPz::Rmpz_sub_ui($right, $middle, 1);
10888 if (Math::GMPz::Rmpz_cmp($left, $right) > 0) {
10889 Math::GMPz::Rmpz_sub_ui($middle, $middle, 1);
10890 last;
10891 }
10892 }
10893 }
10894  
10895 bless \$middle;
10896 }
10897  
10898 1; # End of Math::AnyNum
↑ Summary