1Pari(3) User Contributed Perl Documentation Pari(3)
2
3
4
6 Math::Pari - Perl interface to PARI.
7
9 use Math::Pari;
10 $a = PARI 2;
11 print $a**10000;
12
13 or
14
15 use Math::Pari qw(Mod);
16 $a = Mod(3,5);
17 print $a**10000;
18
20 This package is a Perl interface to famous library PARI for
21 numerical/scientific/number-theoretic calculations. It allows use of
22 most PARI functions as Perl functions, and (almost) seamless merging of
23 PARI and Perl data. In what follows we suppose prior knowledge of what
24 PARI is (see <ftp://megrez.math.u-bordeaux.fr/pub/pari>, or
25 Math::libPARI).
26
28 DEFAULT
29 By default the package exports functions PARI(), PARIcol(),
30 PARIvar(), PARImat(), PARImat_tr() and parse_as_gp() which convert
31 their argument(s) to a PARI object. (In fact PARI() is just an
32 alias for "new Math::Pari"). The function PARI() accepts following
33 data as its arguments
34
35 One integer Is converted to a PARI integer.
36
37 One float Is converted to a PARI float.
38
39 One string Is executed as a PARI expression (so should not
40 contain whitespace).
41
42 PARI object Is passed unchanged.
43
44 Reference to a Perl array
45 Each element is converted using the same rules,
46 PARI vector-row with these elements is returned.
47
48 Several of above The same as with a reference to array.
49
50 Conflicts of rules in PARI()
51 In deciding what rule of the above to apply the preference is given
52 to the uppermost choice of those available now. If none matches,
53 then the string rule is used. So PARI(1) returns integer,
54 "PARI(1.)" returns float, "PARI("1")" evaluates 1 as a PARI
55 expression (well, the result is the same as PARI(1), only slower).
56
57 Note that for Perl these data are synonymous, since Perl freely
58 converts between integers, float and strings. However, to PARI()
59 only what the argument is now is important. If $v is 1 in the Perl
60 world, "PARI($v)" may convert it to an integer, float, or to the
61 result of evaluating the PARI program 1 (all depending on how $v
62 was created and accessed in Perl).
63
64 This is a fundamental limitation of creating an interface between
65 two systems, both with polymorphic objects, but with subtly
66 different semantic of the flavors of these objects. In reality,
67 however, this is rarely a problem.
68
69 PARIcol(), PARImat() and PARImat_tr()
70 PARIcol() behaves in the same way as PARI() unless given several
71 arguments. In the latter case it returns a vector-column instead of
72 a vector-row.
73
74 PARImat() constructs a matrix out of the given arguments. It will
75 work if PARI() will construct a vector of vectors given the same
76 arguments. The internal vectors become columns of the matrix.
77 PARImat_tr() behaves similarly, but the internal vectors become
78 rows of the matrix.
79
80 Since PARI matrices are similar to vector-rows of vector-columns,
81 PARImat() is quicker, but PARImat_tr() better corresponds to the
82 PARI input and output forms of matrices:
83
84 print PARImat [[1,2], [3,4]]; # prints [1,3;2,4]
85 print PARImat_tr [[1,2], [3,4]]; # prints [1,2;3,4]
86
87 parse_as_gp()
88 Did you notice that when taking a string, PARI() requires that
89 there is no whitespace there (outside of string constants)? This
90 is exactly as the "PARI" library parses strings. However, to
91 simplify human interaction, the "gp" calculator allows whitespace,
92 comments, breaking into multiple lines, many independent
93 expressions (such as function definitions).
94
95 We do not include the corresponding C code from the calculator, but
96 provide a Perl clone. It supports whitespace, "\\"-comments, and,
97 for multi-line arguments, it supports trailing "\" for line-
98 continuation, trailing binary ops, comma, opening
99 parenthesis/bracket indicate lines with continuation, group of
100 lines in "{}" joined into one line.
101
102 Keep in mind that this is just a convenience function, and no
103 attempt was performed to make it particularly quick. Moreover, the
104 PARI user functions (or maybe it is better to call them user
105 macros?) are currently not automatically importable into Perl, so
106 to access functions defined in parse_as_gp()' argument may be
107 awkward. (The temporary fix is to use a temporary convenience
108 function __wrap_PARI_macro():
109
110 parse_as_gp <<EOP;
111 add2(x) = x + 2
112 EOP
113 *add2 = Math::Pari::__wrap_PARI_macro 'add2';
114 print add2(17);
115
116 but keep in mind that the generated this way wrapper is also not
117 designed to be quick.)
118
119 "use" with arguments
120 If arguments are specified in the "use Math::Pari" directive, the
121 PARI functions appearing as arguments are exported in the caller
122 context. In this case the function PARI() and friends is not
123 exported, so if you need them, you should include them into export
124 list explicitly, or include ":DEFAULT" tag:
125
126 use Math::Pari qw(factorint PARI);
127 use Math::Pari qw(:DEFAULT factorint);
128
129 or simply do it in two steps
130
131 use Math::Pari;
132 use Math::Pari 'factorint';
133
134 The other tags recognized are ":PARI", ":all", "prec=NUMBER",
135 number tags (e.g., ":4"), overloaded constants tags (":int",
136 ":float", ":hex") and section names tags. The number tags export
137 functions from the PARI library from the given class (except for
138 ":PARI", which exports all of the classes). Tag ":all" exports all
139 of the exportable symbols and ":PARI".
140
141 Giving "?" command to "gp" (PARI calculator) lists the following
142 classes:
143
144 1: Standard monadic or dyadic OPERATORS
145 2: CONVERSIONS and similar elementary functions
146 3: TRANSCENDENTAL functions
147 4: NUMBER THEORETICAL functions
148 5: Functions related to ELLIPTIC CURVES
149 6: Functions related to general NUMBER FIELDS
150 7: POLYNOMIALS and power series
151 8: Vectors, matrices, LINEAR ALGEBRA and sets
152 9: SUMS, products, integrals and similar functions
153 10: GRAPHIC functions
154 11: PROGRAMMING under GP
155
156 One can use section names instead of number tags. Recognized names
157 are
158
159 :standard :conversions :transcendental :number :elliptic
160 :fields :polynomials :vectors :sums :graphic :programming
161
162 One can get the list of all of the functions accessible by
163 "Math::Pari", or the accessible functions from the given section
164 using listPari() function.
165
166 Starting from version 5.005 of Perl, three constant-overload tags
167 are supported: ":int", ":float", ":hex". If used, all the
168 integer/float/hex-or-octal-or-binary literals in Perl will be
169 automatically converted to became PARI objects. For example,
170
171 use Math::Pari ':int';
172 print 2**1000;
173
174 is equivalent to
175
176 print PARI(2)**PARI(1000);
177
178 (The support for this Perl feature is buggy before the Perl version
179 5.005_57 - unless Perl uses mymalloc options; you can check for
180 this with "perl -V:usemymalloc".) Note also that (at least with
181 some versions of Perl) one should enable ':float' for conversion of
182 long integer literals (Perl may consider them as floats, since they
183 won't fit into Perl integers); note that it is PARI which
184 determines which PARI subtype is assigned to each such literal:
185
186 use Math::Pari ':float', 'type_name';
187 print type_name 22222222222222222222222;
188
189 prints "t_INT".
190
192 Directly accessible from Perl
193 This package supports all the functions from the PARI library with a
194 signature which can be recognized by Math::Pari. This means that when
195 you update the PARI library, the newly added functions will we
196 available without any change to this package; only a recompile is
197 needed. In fact no recompile will be needed if you link libPARI
198 dynamically (you need to modify the Makefile manually to do this).
199
200 You can "reach" unsupported functions via going directly to PARI parser
201 using the string flavor of PARI() function, as in
202
203 3 + PARI('O(x^17)');
204
205 For some "unreachable" functions there is a special wrapper functions,
206 such as "O(variable,power)").
207
208 The following functions are specific to GP calculator, thus are not
209 available to Math::Pari in any way:
210
211 default error extern input print print1 printp printp1
212 printtex quit read system whatnow write write1 writetex
213
214 whatnow() function is useless, since Math::Pari does not support the
215 "compatibility" mode (with older PARI library). The functionality of
216 print(), write() and variants is available via automatic string
217 translation, and pari_print() function and its variants (see "Printout
218 functions").
219
220 default() is the only important function with functionality not
221 supported by the current interface. Note however, that four most
222 important default() actions are supported by allocatemem(),
223 setprimelimit(), setprecision() and setseriesprecision() functions.
224 (When called without arguments, these functions return the current
225 values.)
226
227 allocatemem($bytes) should not be called from inside Math::Pari
228 functions (such as forprimes()).
229
230 Arguments
231 Arguments to PARI functions are automatically converted to "long" or a
232 PARI object depending on the signature of the actual library function.
233 The arguments are forced into the given type, so even if "gp" rejects
234 your code similar to
235
236 func(2.5); # func() takes a long in C
237
238 arguing that a particular argument should be of "type T_INT" (i.e., a
239 Pari integer), the corresponding code will work in "Math::Pari", since
240 2.5 is silently converted to "long", per the function signature.
241
242 Return values
243 PARI functions return a PARI object or a Perl's integer depending on
244 what the actual library function returns.
245
246 Additional functions
247 Some PARI functions are available in "gp" (i.e., in "PARI" calculator)
248 via infix notation only. In "Math::Pari" these functions are available
249 in functional notations too. Some other convenience functions are also
250 made available.
251
252 Infix, prefix and postfix operations
253 are available under names
254
255 gneg, gadd, gsub, gmul, gdiv, gdivent, gmod, gpui,
256 gle, gge, glt, ggt, geq, gne, gegal, gor, gand,
257 gcmp, gcmp0, gcmp1, gcmp_1.
258
259 "gdivent" means euclidean quotient, "gpui" is power, "gegal"
260 checks whether two objects are equal, "gcmp" is applicable to two
261 real numbers only, "gcmp0", "gcmp1", "gcmp_1" compare with 0, 1
262 and -1 correspondingly (see PARI user manual for details, or
263 Math::libPARI). Note that all these functions are more readily
264 available via operator overloading, so instead of
265
266 gadd(gneg($x), $y)
267
268 one can write
269
270 -$x+$y
271
272 (as far as overloading may be triggered, see overload, so we
273 assume that at least one of $x or $y is a PARI object).
274
275 Conversion functions
276 pari2iv, pari2nv, pari2num, pari2pv, pari2bool
277
278 convert a PARI object to an integer, float, integer/float
279 (whatever is better), string, and a boolean value correspondingly.
280 Most the time you do not need these functions due to automatic
281 conversions.
282
283 Printout functions
284 pari_print, pari_pprint, pari_texprint
285
286 perform the same conversions to strings as their PARI
287 counterparts, but do not print the result. The difference of
288 pari_print() with pari2pv() is the number of significant digits
289 they output, and whitespace in the output. pari2pv(), which is
290 intended for "computer-readable strings", outputs as many digits
291 as is supported by the current precision of the number; while
292 pari_print(), which targets human-readable strings, takes into
293 account the currently specified output precision too.
294
295 Constant functions
296 Some mathematical constants appear as function without arguments
297 in PARI. These functions are available in Math::Pari too. If you
298 export them as in
299
300 use Math::Pari qw(:DEFAULT Pi I Euler);
301
302 they can be used as barewords in your program:
303
304 $x = Pi ** Euler;
305
306 Low-level functions
307 For convenience of low-level PARI programmers some low-level
308 functions are made available as well (all except type_name() and
309 changevalue() are not exportable):
310
311 typ($x)
312 lg($x)
313 lgef($x)
314 lgefint($x)
315 longword($x, $n)
316 type_name($x)
317 changevalue($name,$newvalue)
318
319 Here longword($x,$n) returns $n-th word in the memory
320 representation of $x (including non-code words). type_name()
321 differs from the PARI function type(): type() returns a PARI
322 object, while type_name() returns a Perl string. (PARI objects of
323 string type behave very non-intuitive w.r.t. string comparison
324 functions; remember that they are compared using lex() to the
325 results of evaluation of other argument of comparison!)
326
327 The function listPari($number) outputs a list of names of PARI
328 functions in the section $number. Use listPari(-1) to get the
329 list across all of the sections.
330
331 Uncompatible functions
332 O
333
334 Since implementing "O(7**6)" would be very tedious, we provide a
335 two-argument form "O(7,6)" instead (meaning the same as "O(7^6)"
336 in PARI). Note that with polynomials there is no problem like
337 this one, both "O($x,6)" and "O($x**6)" work.
338
339 ifact(n)
340
341 integer factorial functions, available from "gp" as "n!".
342
343 Looping functions
344 PARI has a big collection of functions which loops over some set. Such
345 a function takes two special arguments: loop variable, and the code to
346 execute in the loop.
347
348 The code can be either a string (which contains PARI code to execute -
349 thus should not contain whitespace), or a Perl code reference. The
350 loop variable can be a string giving the name of PARI variable (as in
351
352 fordiv(28, 'j', 'a=a+j+j^2');
353
354 or
355
356 $j= 'j';
357 fordiv(28, $j, 'a=a+j+j^2');
358
359 ), a PARI monomial (as in
360
361 $j = PARI 'j';
362 fordiv(28, $j, sub { $a += $j + $j**2 });
363
364 ), or a "delayed Math::Pari variable" (as in
365
366 $j = PARIvar 'j';
367 fordiv(28, $j, 'a=a+j+j^2');
368
369 ). If none of these applies, as in
370
371 my $j; # Have this in a separate statement
372 fordiv(28, $j, sub { $a += $j + $j**2 });
373
374 then during the execution of the "sub", Math::Pari would autogenerate a
375 PARI variable, and would put its value in $j; this value of $j is
376 temporary only, the old contents of $j is restored when fordiv()
377 returns.
378
379 Note that since you have no control over this name, you will not be
380 able to use this variable from your PARI code; e.g.,
381
382 $j = 7.8;
383 fordiv(28, $j, 'a=a+j+j^2');
384
385 will not make "j" mirror $j (unless you explicitly set up "j" to be a
386 no-argument PARI function mirroring $j, see "Accessing Perl functions
387 from PARI code").
388
389 Caveats. There are 2 flavors of the "code" arguments (string/"sub"),
390 and 4 types of the "variable" arguments
391 (string/monomial/"PARIvar"/other). However, not all 8 combinations
392 make sense. As we already explained, an "other" variable cannot work
393 with a "string" code.
394
395 Useless musing alert! Do not read the rest of this section! Do not use
396 "string" variables with "sub" code, and do not ask why!
397
398 Additionally, the following code will not do what you expect
399
400 $x = 0;
401 $j = PARI 'j';
402 fordiv(28, 'j', sub { $x += $j } ); # Use $j as a loop variable!
403
404 since the PARI function "fordiv" localizes the PARI variable "j" inside
405 the loop, but $j will still reference the old value; the old value is a
406 monomial, not the index of the loop (which is an integer each time
407 "sub" is called). The simplest workaround is not to use the above
408 syntax (i.e., not mixing literal loop variable with Perl loop code,
409 just using $j as the second argument to "fordiv" is enough):
410
411 $x = 0;
412 $j = PARI 'j';
413 fordiv(28, $j, sub { $x += $j } );
414
415 Alternately, one can make a delayed variable $j which will always
416 reference the same thing "j" references in PARI now by using "PARIvar"
417 constructor
418
419 $x = 0;
420 $j = PARIvar 'j';
421 fordiv(28, 'j', sub { $x += $j } );
422
423 (This problem is similar to
424
425 $ref = \$_; # $$ref is going to be old value even after
426 # localizing $_ in Perl's grep/map
427
428 not accessing localized values of $_ in the plain Perl.)
429
430 Another possible quirk is that
431
432 fordiv(28, my $j, sub { $a += $j + $j**2 });
433
434 will not work too - by a different reason. "my" declarations change
435 the meaning of $j only after the end of the current statement; thus $j
436 inside "sub" will access a different variable $j (typically a non-
437 lexical, global variable $j) than one you declared on this line.
438
439 Accessing Perl functions from PARI code
440 Use the same name inside PARI code:
441
442 sub counter { $i += shift; }
443 $i = 145;
444 PARI 'k=5' ;
445 fordiv(28, 'j', 'k=k+counter(j)');
446 print PARI('k'), "\n";
447
448 prints
449
450 984
451
452 Due to a difference in the semantic of variable-number-of-parameters-
453 functions between PARI and Perl, if the Perl subroutine takes a
454 variable number of arguments (via "@" in the prototype or a missing
455 prototype), up to 6 arguments are supported when this function is
456 called from PARI. If called from PARI with fewer arguments, the rest
457 of arguments will be set to be integers "PARI 0".
458
459 Note also that no direct import of Perl variables is available yet (but
460 you can write a function wrapper for this):
461
462 sub getv () {$v}
463
464 There is an unsupported (and undocumented ;-) function for explicitly
465 importing Perl functions into PARI, possibly with a different name, and
466 possibly with explicitly specifying number of arguments.
467
469 Functions from PARI library may take as arguments and/or return values
470 the objects of C type "GEN". In Perl these data are encapsulated into
471 special kind of Perl variables: PARI objects. You can check for a
472 variable $obj to be a PARI object using
473
474 ref $obj and $obj->isa('Math::Pari');
475
476 Most the time you do not need this due to automatic conversions and
477 overloading.
478
480 If very lazy, one can code in Perl the same way one does it in PARI.
481 Variables in PARI are denoted by barewords, as in "x", and in the
482 default configuration (no warnings, no strict) Perl allows the same -
483 up to some extent. Do not do this, since there are many surprising
484 problems.
485
486 Some bareletters denote Perl operators, like "q", "x", "y", "s". This
487 can lead to errors in Perl parsing your expression. E.g.,
488
489 print sin(tan(t))-tan(sin(t))-asin(atan(t))+atan(asin(t));
490
491 may parse OK after "use Math::Pari qw(sin tan asin atan)". Why?
492
493 After importing, the word "sin" will denote the PARI function sin(),
494 not Perl operator sin(). The difference is subtle: the PARI function
495 implicitly forces its arguments to be converted PARI objects; it gets
496 't' as the argument, which is a string, thus is converted to what "t"
497 denotes in PARI - a monomial. While the Perl operator sin() grants
498 overloading (i.e., it will call PARI function sin() if the argument is
499 a PARI object), it does not force its argument; given 't' as argument,
500 it converts it to what sin() understands, a float (producing 0.), so
501 will give 0. as the answer.
502
503 However
504
505 print sin(tan(y))-tan(sin(y))-asin(atan(y))+atan(asin(y));
506
507 would not compile. You should avoid lower-case barewords used as PARI
508 variables, e.g., do
509
510 $y = PARI 'y';
511 print sin(tan($y))-tan(sin($y))-asin(atan($y))+atan(asin($y));
512
513 to get
514
515 -1/18*y^9+26/4725*y^11-41/1296*y^13+328721/16372125*y^15+O(y^16)
516
517 (BTW, it is a very good exercise to get the leading term by hand).
518
519 Well, the same advice again: do not use barewords anywhere in your
520 program!
521
523 Whenever an arithmetic operation includes at least one PARI object, the
524 other arguments are converted to a PARI object and the corresponding
525 PARI library functions is used to implement the operation. Currently
526 the following arithmetic operations are overloaded:
527
528 unary -
529 + - * / % ** abs cos sin exp log sqrt
530 << >>
531 <= == => < > != <=>
532 le eq ge lt gt ne cmp
533 | & ^ ~
534
535 Numeric comparison operations are converted to "gcmp" and friends,
536 string comparisons compare in lexicographical order using "lex".
537
538 Additionally, whenever a PARI object appears in a situation that
539 requires integer, numeric, boolean or string data, it is converted to
540 the corresponding type. Boolean conversion is subject to usual PARI
541 pitfalls related to imprecise zeros (see documentation of "gcmp0" in
542 PARI reference).
543
544 For details on overloading, see overload.
545
546 Note that a check for equality is subject to same pitfalls as in PARI
547 due to imprecise values. PARI may also refuse to compare data of
548 different types for equality if it thinks this may lead to
549 counterintuitive results.
550
551 Note also that in PARI the numeric ordering is not defined for some
552 types of PARI objects. For string comparison operations we use PARI-
553 lexicographical ordering.
554
556 Perl
557 In the versions of perl earlier than 5.003 overloading used a different
558 interface, so you may need to convert "use overload" line to %OVERLOAD,
559 or, better, upgrade.
560
561 PARI
562 Starting from version 2.0, this module comes without a PARI library
563 included.
564
565 For the source of PARI library see
566 <ftp://megrez.math.u-bordeaux.fr/pub/pari>.
567
569 Note that the PARI notations should be used in the string arguments to
570 PARI() function, while the Perl notations should be used otherwise.
571
572 "^" Power is denoted by "**" in Perl.
573
574 "\" and "\/"
575 There are no such operators in Perl, use the word forms
576 "gdivent(x,y)" and "gdivround(x,y)" instead.
577
578 "~" There is no postfix "~" Perl operator. Use mattranspose() instead.
579
580 "'" There is no postfix "'" Perl operator. Use deriv() instead.
581
582 "!" There is no postfix "!" Perl operator. Use factorial()/ifact()
583 instead (returning a real or an integer correspondingly).
584
585 big integers
586 Perl converts big literal integers to doubles if they could not be
587 put into C integers (the particular flavor can be found in the
588 output of "perl -V" in newer version of Perl, look for
589 "ivtype"/"ivsize"). If you want to input such an integer, use
590
591 while ($x < PARI('12345678901234567890')) ...
592
593 instead of
594
595 while ($x < 12345678901234567890) ...
596
597 Why? Because conversion to double leads to precision loss
598 (typically above 1e15, see perlnumber), and you will get something
599 like 12345678901234567168 otherwise.
600
601 Starting from version 5.005 of Perl, if the tag ":int" is used on
602 the 'use Math::Pari' line, all of the integer literals in Perl will
603 be automatically converted to became PARI objects. E.g.,
604
605 use Math::Pari ':int';
606 print 2**1000;
607
608 is equivalent to
609
610 print PARI(2)**PARI(1000);
611
612 Similarly, large integer literals do not lose precision.
613
614 This directive is lexically scoped. There is a similar tag ":hex"
615 which affects hexadecimal, octal and binary constants. One may
616 also need to use tag ":float" for auto-conversion of large integer
617 literals which Perl considers as floating point literals (see
618 ""use" with arguments" for details).
619
620 doubles
621 Doubles in Perl are typically of precision approximately 15 digits
622 (see perlnumber). When you use them as arguments to PARI
623 functions, they are converted to PARI real variables, and due to
624 intermediate 15-digits-to-binary conversion of Perl variables the
625 result may be different than with the PARI many-digits-to-binary
626 conversion. E.g., "PARI(0.01)" and "PARI('0.01')" differ at 19-th
627 place, as
628
629 setprecision(38);
630 print pari_print(0.01), "\n",
631 pari_print('0.01'), "\n";
632
633 shows.
634
635 Note that setprecision() changes the output format of pari_print()
636 and friends, as well as the default internal precision. The
637 generic PARI===>string conversion does not take into account the
638 output format, thus
639
640 setprecision(38);
641 print PARI(0.01), "\n",
642 PARI('0.01'), "\n",
643 pari_print(0.01), "\n";
644
645 will print all the lines with different number of digits after the
646 point: the first one with 22, since the double 0.01 was converted
647 to a low-precision PARI object, the second one with 41, since
648 internal form for precision 38 requires that many digits for
649 representation, and the last one with 39 to have 38 significant
650 digits.
651
652 Starting from version 5.005 of Perl, if the tag ":float" is used on
653 the "use Math::Pari" line, all the float literals in Perl will be
654 automatically converted to became PARI objects. E.g.,
655
656 use Math::Pari ':float';
657 print atan(1.);
658
659 is equivalent to
660
661 print atan(PARI('1.'));
662
663 Similarly, large float literals do not lose precision.
664
665 This directive is lexically scoped.
666
667 array base
668 Arrays are 1-based in PARI, are 0-based in Perl. So while array
669 access is possible in Perl, you need to use different indices:
670
671 $nf = PARI 'nf'; # assume that PARI variable nf contains a number field
672 $a = PARI('nf[7]');
673 $b = $nf->[6];
674
675 Now $a and $b contain the same value.
676
677 matrices
678 Note that "PARImat([[...],...,[...])" constructor creates a matrix
679 with specified columns, while in PARI the command "[1,2,3;4,5,6]"
680 creates a matrix with specified rows. Use a convenience function
681 PARImat_tr() which will transpose a matrix created by PARImat() to
682 use the same order of elements as in PARI.
683
684 builtin perl functions
685 Some PARI functions, like "length" and "eval", are Perl
686 (semi-)reserved words. To reach these functions, one should either
687 import them:
688
689 use Math::Pari qw(length eval);
690
691 or call them with prefix (like &length) or the full name (like
692 "Math::Pari::length").
693
695 If you have Term::Gnuplot Perl module installed, you may use high-
696 resolution graphic primitives of PARI. Before the usage you need to
697 establish a link between Math::Pari and Term::Gnuplot by calling
698 link_gnuplot(). You can change the output filehandle by calling
699 set_plot_fh(), and output terminal by calling plotterm(), as in
700
701 use Math::Pari qw(:graphic asin);
702
703 link_gnuplot(); # automatically loads Term::Gnuplot
704 plotterm('emtex');
705 plot_outfile_set('out.tex'); # better do after plotterm()
706 ploth($x, .5, .999, sub {asin $x});
707 close FH or die;
708
710 libPARI documentation is included, see Math::libPARI. It is converted
711 from Chapter 3 of PARI/GP documentation by the gphelp script of
712 GP/PARI.
713
715 No environment variables are used.
716
718 · A few of PARI functions are available indirectly only.
719
720 · Using overloading constants with the Perl versions below 5.005_57
721 could lead to segfaults (at least without "-D usemymalloc"), as
722 in:
723
724 use Math::Pari ':int';
725 for ( $i = 0; $i < 10 ; $i++ ) { print "$i\n" }
726
727 · It may be possible that conversion of a Perl value which has both
728 the integer slot and the floating slot set may create a PARI
729 integer, even if the actual value is not an integer.
730
731 · problems with refcounting of array elements and Mod().
732
733 Workaround: make the modulus live longer than the result of Mod().
734 Until Perl version 5.6.1, one should exercise a special care so
735 that the modulus goes out of scope on a different statement than
736 the result:
737
738 { my $modulus = 125;
739 { my $res = Mod(34, $modulus);
740 print $res;
741 }
742 $fake = 1; # A (fake) statement here is required
743 }
744
745 Here $res is destructed before the "$fake = 1" statement, $modulus
746 is destructed before the first statement after the provided block.
747 However, if you remove the "$fake = 1" statement, both these
748 variables are destructed on the first statement after the provided
749 block (and in a wrong order!).
750
751 In 5.6.1 declaring $modulus before $res is all that is needed to
752 circumvent the same problem:
753
754 { my $modulus = 125;
755 my $res = Mod(34, $modulus);
756 print $res;
757 } # destruction will happen in a correct order.
758
759 Access to array elements may result in similar problems. Hard to
760 fix since in PARI the data is not refcounted.
761
762 · Legacy implementations of dynalinking require the code of DLL to
763 be compiled to be "position independent" code (PIC). This slows
764 down the execution, while allowing sharing the loaded copy of the
765 DLL between different processes. [On contemporary architectures
766 the same effect is allowed without the position-independent hack.]
767
768 Currently, PARI assembler files are not position-independent.
769 When compiled for the dynamic linking on legacy systems, this
770 creates a DLL which cannot be shared between processes. Some
771 legacy systems are reported to recognize this situation, and load
772 the DLL as a non-shared module. However, there may be systems
773 (are there?) on which this can cause some "problems".
774
775 Summary: if the dynaloading on your system requires some kind of
776 "-fPIC" flag, using "assembler" compiles (anything but
777 "machine=none") *may* force you to do a static build (i.e.,
778 creation of a custom Perl executable with
779
780 perl Makefile.PL static
781 make perl
782 make test_static
783
784 ).
785
786 · isprime() is a misnomer before PARI version 2.3
787
788 In older versions of PARI, the one-argument variant of the
789 function isprime() is actually checking for probable primes.
790 Moreover, it has certain problems.
791
792 POSSIBLE WORKAROUND (not needed for newer PARI): before version
793 2.3 of PARI, to get probability of misdetecting a prime below
794 1e-12, call isprime() twice; below 1e-18, call it 3 times; etc.
795 (The algorithm is probabilistic, and the implementation is such
796 that the result depends on which calls to isprime() were performed
797 ealier.)
798
799 The problems: first, while the default algorithm (before version
800 2.3) gives practically acceptable results in non-adversarial
801 situations, the worst-case behaviour is significantly worse than
802 the average behaviour. The algorithm is looking for so-called
803 "witnesses" (with up to 10 tries) among random integers; usually,
804 witnesses are abundant. However, there are non-prime numbers for
805 which the fraction of witnesses is close to the theoretical
806 mininum, 0.75; with 10 random tries, the probability of missing a
807 witness for such numbers is close to 1e-6. (The known worst-case
808 numbers M have phi(M)/4 non-witnesses, with M=P(2P-1), prime P,
809 2P-1 and 4|P+1; the proportion of such numbers near K is expected
810 to be const/sqrt(K)log(K)^2. Note that numbers which have more
811 than about 5% non-witnesses may also be candidates for false
812 positives. Conjecturally, they are of the form (aD+1)(bD+1) with
813 a<b, ab <= const, prime aD+1, and bD+1, and D not divisible by
814 high power of 2 (above a=1, b=2 and D is odd); the proportion of
815 such numbers may have a similar asymptotic const/sqrt(K)log(K)^2.)
816
817 Second, the random number generator is "reset to known state" when
818 PARI library is initialized. That means that the behaviour is
819 actually predictable if one knows which calls to isprime() are
820 performed; an adversary can find non-primes M which will trigger a
821 false positive exactly on the Nth call to isprime(M) (for
822 particular values of N). With enough computing resources, one can
823 find non-primes M for which N is relatively small (with M about
824 1e9, one can achieve N as low as 1000). Compare with similar (but
825 less abundant) examples for simpler algorithm, Carmichael numbers
826 <http://en.wikipedia.org/wiki/Carmichael_numbers>; see also
827 numbers with big proportion of non-witnesses
828 <http://oeis.org/A090659> and numbers with many non-witnesses
829 <http://oeis.org/A141768>, and the conjecture about proportion
830 <http://web.archive.org/web/*/http://www.ma.iup.edu/MAA/proceedings/vol1/higgins.pdf>.
831
832 See the discussion of isprime()
833 <https://rt.cpan.org/Public/Bug/Display.html?id=93652>.
834
836 When Math::Pari is loaded, it examines variables $Math::Pari::initmem
837 and $Math::Pari::initprimes. They specify up to which number the
838 initial list of primes should be precalculated, and how large should be
839 the arena for PARI calculations (in bytes). (These values have safe
840 defaults.)
841
842 Since setting these values before loading requires either a "BEGIN"
843 block, or postponing the loading ("use" vs. "require"), it may be more
844 convenient to set them via Math::PariInit:
845
846 use Math::PariInit qw( primes=12000000 stack=1e8 );
847
848 "use Math::PariInit" also accepts arbitrary Math::Pari import
849 directives, see Math::PariInit.
850
851 These values may be changed at runtime too, via allocatemem() and
852 setprimelimit(), with performance penalties for
853 recalculation/reallocation.
854
856 Ilya Zakharevich, ilyaz@cpan.org
857
859 Hey! The above document had some coding errors, which are explained
860 below:
861
862 Around line 848:
863 Expected '=item *'
864
865
866
867perl v5.28.0 2018-08-10 Pari(3)