The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Math::Pari - Perl interface to PARI.

SYNOPSIS

  use Math::Pari;
  $a = PARI 2;
  print $a**10000;

or

  use Math::Pari qw(mod);
  $a = mod(3,5);
  print $a**10000;

DESCRIPTION

This package allows use of most PARI functions as Perl functions. In what follows we suppose prior knowledge of what PARI is (see ftp://megrez.math.u-bordeaux.fr/pub/pari).

EXPORT

DEFAULT

By default the package exports functions PARI(), PARIcol() and PARImat() that converts its argument(s) to a PARI object. (In fact PARI() is just an alias for new Math::Pari). The function PARI() accepts following data as its arguments

One integer

Is converted to a PARI integer.

One float

Is converted to a PARI float.

One string

Is executed as a PARI expresion (so should not contain whitespace).

PARI object

Is passed unchanged.

Reference to a Perl array

Each element is converted using the same rules, PARI vector-row with these elements is returned.

Several of above

The same as with a reference to array.

Conflicts of rules in PARI()

In deciding what rule of the above to apply the preference is given to the uppermost choice of available now, if none matches, then the string rule is used. So PARI(1) returns integer, PARI(1.) returns float, PARI("1") evaluates "1" as a PARI expression, though all these data can be converted inside Perl into integer, float or string. Only what the argument is now is important.

PARIcol() and PARImat()

PARIcol() behaves in the same way as PARI() unless given several arguments. In the latter case it returns a vector-column instead of vector-row.

PARImat() constructs a matrix out of the given arguments. It will work if PARI() will construct a vector of vectors given the same arguments.

use with arguments

If arguments are specified in the use Math::Pari directive, the PARI functions appearing as arguments are exported in the caller context. In this case the function PARI() and friends is not exported, so if you need them, you should include them into export list explicitely, or include :DEFAULT tag.

The other tags recognized are :all, and number tags :4. These tags export functions from the PARI library from the given class (except for :all, which exports all the classes). Note that :all does not include :DEFAULT.

Available functions

Directly accessible from Perl

This package supports all the functions from the PARI library with a signature from a short list. This means that when you update the PARI library, the newly added function will we available without any change to this package (provided their signature is in the supported list). You can reach unsupported functions using string argument of PARI() function, as in

  3 + PARI('o(x^17)')

A perl script parifonc is provided that lists the functions from the current release of PARI that are unavailable with the current release of this glue code. You should specify two arguments to this script: a path to anal.c file of PARI/GP distribution, and path to the file Pari.xs from your truely package.

Arguments

Arguments to PARI functions are converted to long or PARI type depending on what type the actual library function requires. No error checking on arguments is done, so if gp rejects your code since a particular argument should be of type 1 (i.e., a Pari integer), Math::Pari will silently convert it to long. Each argument is converted by the rules applicable to PARI().

Return values

PARI functions return PARI type or a long depending on what the actual library function returns.

Additional functions

Some PARI functions are available in gp (i.e., in PARI calculator) via infix notation only. In Math::Pari these functions are available in functional notations too. Some other convenience functions are also made available.

Infix, prefix and postfix operations

are available under names

  gneg, gadd, gsub, gmul, gdiv, gdivent, gmod, gpui,
  gle, gge, glt, ggt, geq, gne, gegal, gor, gand,
  gcmp, gcmp0, gcmp1, gcmp_1.

gdivent means euclidean quotient, gpui is power, gegal checks whether two objects are equal, gcmp is applicable to two real numbers only, gcmp0, gcmp1, gcmp_1 compare with 0, 1 and -1 correspondingly (see PARI user manual for details).

Convenience functions
  pari2iv, pari2nv, pari2num, pari2pv, pari2bool

convert a PARI object to an integer, float, integer/float (whatever is better), string, and a boolean value correspondingly. Most the time you do not need these functions due to automatic conversions.

Printout functions
  pari_print, pari_pprint, pari_texprint

perform conversions to strings as their PARI counterparts, but do not print the result. The difference of pari_print() with pari2pv() is the number of significant digits they print.)

Constant functions

Some mathematical constant appear as function without arguments in PARI. Corresponding functions are made available under Perl. If you export them like in

  use Math::Pari wq(:DEFAULT pi i euler);

they can be used as barewords in your program, with the usual restrictions that sometimes you should disambiguate the call like in

  $negOne = exp(pi() * i);

The parentheses after pi are needed since Perl thinks you want to call pi with argument *i otherwise (since *i is a legal Perl expression, and function calls are greedy on the right).

PARI objects

Functions from PARI library take as arguments and/or return objects of type GEN (in C notations). In Perl these data are encapsulated into special kind of Perl variables: PARI objects. You can check for a variable $obj to be a PARI object using

  ref $obj eq 'Math::Pari';

Most the time you do not need this due to automatic conversions.

PARI polynomials and Perl barewords

Some bareletters denote Perl operators, like q, x, y, s. This can lead to errors in Perl parsing your expression. Say, while

  print sin(tan(x))-tan(sin(x))-asin(atan(x))+atan(asin(x));

parses OK (after use Math::Pari qw(sin tan asin atan)),

  print sin(tan(y))-tan(sin(y))-asin(atan(y))+atan(asin(y));

does not. You should avoid lower-case barewords used as PARI variables.

Overloading and automatic conversion

Whenever an arithmetic operation includes a PARI object the other arguments are converted to a PARI type and the corresponding PARI library functions is used to implement the operation. Numeric comparison operations use gcmp and friends, string comparisons compare in lexicographical order using lex. Currently the following arithmetic operations are overloaded:

  unary -, +, -, *, /, %, **, abs, cos, sin, exp, log, sqrt.

Whenever a PARI object appears in a situation that requires integer, numeric, boolean or string data, it is converted to the corresponding type. Boolean conversion is subject to usual PARI pitfalls related to imprecise zeros (see documentation of gcmp0 in PARI reference).

PREREQUISITES

Perl

To compile the extension you need to have at least MakeMaker version 3.7. In the versions of perl earlier than 5.001 negative constants were converted to floats, so to use PARI operations that do different things on integers and floats you would like to use a more recent version of perl. Overloading was buggy in 5.000.

PARI

You need at least version 1.39 of PARI. (See ftp://megrez.math.u-bordeaux.fr/pub/pari.)

Perl vs. PARI: different syntax

Note that the PARI notations should be used in string arguments to PARI() function, while Perl notations should be used otherwise.

^

Power is denoted by ** in Perl.

\ and \/

There are no such operators in Perl, use the word forms gdivent(x,y) and gdivround(x,y) instead.

~

There is no postfix ~ Perl operator. Use trans() instead.

!

There is no postfix ! Perl operator. Use fact() instead (note that the semantics of fact(x) and x! is different in PARI, one is real, another integer).

Currently there is no direct way to get integer factorial in Math::Pari.

doubles

Doubles in Perl are of precision approximately 15 digits. When you use them as arguments to PARI functions, they are converted to PARI real variables, and due to intermediate 15-decimal-to-binary conversion of Perl variables the result may be different than with the PARI many-decimal-to-binary conversion. Say, PARI(0.01) and PARI('0.01') differ at 19-th place, as

  setprecision(38); print pari_print(0.01), "\n", pari_print('0.01'), "\n";

shows.

ENVIRONMENT

No environment variables are used.

BUGS

  • Not all the PARI functions are directly available.

  • Many others...

AUTHOR

Ilya Zakharevich, ilya@math.mps.ohio-state.edu