|Andreas Enge on Tue, 04 Feb 2014 14:08:26 +0100|
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
|Re: Interfacing pari and gmp/mpfr/mpc|
Hello, finally I finished some work started during the Atelier Pari/GP in January. The attached package allows to switch between pari and gmp/mpfr/mpc types in a C program, to use the pari stack functions for allocating the mantissae of mpfr/mpc numbers, and to call functions from libraries relying on mpfr/mpc (in particular, the libraries themselves) in the GP command interpreter. Comments are welcome. I tested against the current git of Pari/GP; the package does not work with the stable version of Pari/GP. I kept names such as mpz_set_GEN and mpz_get_GEN, thus invading foreign name spaces: These would be the names used inside GMP if it depended on libpari and contained the corresponding functionality. There is little risk the names would be used for anything else. For further information, I am attaching the README of the package below. Andreas --- Pari-gnump allows to easily switch between number types from the GNU multiprecision ecosystem (mpz, mpq, mpfr, mpc) and corresponding types in libpari, as well as to use functions from the GNU libraries in GP. INSTALLATION ------------ Adapt the location and version numbers of Pari/GP, GNU MP (GMP), GNU MPFR and GNU MPC in the first lines of Makefile. If you wish to use functions relying on GMP, GNU MPFR or GNU MPC in your GP session, you need to add them to pari-gnump-user.h and pari-gnump-user.c (see below). Execute make This creates the library libpari-gnump.so. There is no need to install this library, it may simply be copied to the location of your Pari/GP project. To test if everything went well, execute make check NUMBER TRANSFORMATIONS ---------------------- The exported functions are given in pari-gnump.h. For each GNU multiprecision type X from mpz, mpq. mpfr and mpc, there are functions X_set_GEN X_get_GEN The first one takes an argument x of type X and g of type GEN, and assigns the value of g to x if the types are compatible. The second one takes as argument x of type X and returns a GEN with the same value, allocated on the Pari stack. The functions pari_mpfr_init2, pari_mpc_init2 and pari_mpc_init3 work exactly as their counterparts without the pari prefix, except that they allocate the mantissae on the Pari stack instead of using the GMP memory allocation functions. They should not be freed with calls to mpfr_clear or mpc_clear, but with the usual Pari stack handling ("avma"). pari_mpfr_init_set_GEN and pari_mpc_init_set_GEN combine such an initialisation with an assignment; as precision, they use the precision of the source variable if it is of a floating point type, or the default precision if it is an integer or a rational number. If you are unsure what is meant by this paragraph, you probably do not wish to use these functions. USER FUNCTIONS IN GP -------------------- If you wish to use functions relying on GMP, GNU MPFR or GNU MPC in a GP session, you need to write a wrapper function in pari-gnump-user.c and declare it in pari-gnump-user.h. The function should take GEN arguments, transform them to mpz, mpq, mpfr or mpc, compute with them in the desired way, and transform the result back into a GEN. By calling make this function will be automatically included into the library libpari-gnump.so. The functions must then be made available inside your GP session using the "install" command. As examples, pari-gnump-user.c implements correctly rounded multiplications of real and complex numbers using MPFR and MPC, as well as the erf and zeta functions from MPFR. The file example.gp shows how to install and use these functions in a GP script.