Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use PARI C library

Tags:

pari-gp

pari

I have already searched for some tutorial on how to use the PARI library in a C program, but unfortunatelly I found nothing but generic tutorials of using the GP package in command line interface.

Could anyone help? For instance, I would like to initialize a 3x3 matrix and find it's 1000-th power. In gp (the CLI), this could be achieved just by typing something like:

? A=[1,2,3;4,5,6;7,8,9]
? A^1000

but I found no help about doing this quiet simple thing in a C source file. Is there a comprehensive tutorial, or a documentation that provides such examples? Any comment will be extremely appreciated!

like image 988
nullgeppetto Avatar asked Dec 03 '13 09:12

nullgeppetto


1 Answers

This is covered in the manual libpari ("User's Guide to the PARI library"). An easy way to use it for simple cases is to save the associated GP script and run gp2c -g on it, which will produce a C file doing the calculations with the PARI library. You can then edit to suit your tastes.

For this particular case:

#include <pari/pari.h>
GEN test(void);

GEN
test(void)
{
  GEN A = cgetg(4, t_MAT);
  gel(A, 1) = cgetg(4, t_COL);
  gel(A, 2) = cgetg(4, t_COL);
  gel(A, 3) = cgetg(4, t_COL);
  /* Create matrix A */

  gcoeff(A, 1, 1) = gen_1;
  gcoeff(A, 1, 2) = gen_2;
  gcoeff(A, 1, 3) = stoi(3);
  gcoeff(A, 2, 1) = stoi(4);
  gcoeff(A, 2, 2) = stoi(5);
  gcoeff(A, 2, 3) = stoi(6);
  gcoeff(A, 3, 1) = stoi(7);
  gcoeff(A, 3, 2) = stoi(8);
  gcoeff(A, 3, 3) = stoi(9);
  /* Fill matrix A with values */

  return gpowgs(A, 1000); /* Return A^1000 */
}
like image 151
Charles Avatar answered Oct 06 '22 19:10

Charles