blib/lib/Math/AnyNum.pm

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