Programowanie w systemie UNIX/pari

Z Wikibooks, biblioteki wolnych podręczników.

Jak używać biblioteki libpari w C ?

instalacja[edytuj]

  • sudo apt-get install libpari-dev
  • sudo apt install pari-gp

sposoby użycia[edytuj]

  • samodzielny program gp ( shell kalkulator)
  • biblioteka pari ( = libpari )
    • wywołanie w programie
  • tłumaczenie programu gp do kodu w c za pomocą kompilatora gp2c

użycie biblioteki[edytuj]

  • opcja kompilatora -lpari
  • dyrektywa linkera: #include <pari/pari.h>
  • bibliotek skompilowana: libpari.so

typy[edytuj]

  • GEN = podstawowy typ dla wszystkich obiektów PARI, jest wskaźnikiem do typu long.

pierwszy program[edytuj]

/*
gcc t.c -lpari -Wall

- fatal error: pari/pari.h: Nie ma takiego pliku ani katalogu
compilation terminated.
- solution : install libpari

gun -> gen_1 

./a.out



*/

#include<stdio.h>
#include<pari/pari.h>
int main(void)
{
 GEN i,j,k;
 pari_init(500000,2);
 i=gen_1;
 j=stoi(3);
 k=gadd(i,j);
 printf("1+3=%s\n ",GENtostr(k));

  pari_close(); 

  printf("paricfg_version_code = %ld\n",   paricfg_version_code);
 return 0;
}

Wynik:


 1+3=4
paricfg_version_code = 134401

nie jest imponujący ale program działa (:-))[1]

Przykłady[edytuj]

Są w katalogu:

  /usr/share/doc/pari-doc/examples

Lista plików:

  • bench.gp
  • classno.gp
  • cl.gp
  • contfrac.gp
  • EXPLAIN.gz
  • extgcd.c
  • Inputrc
  • lucas.gp
  • Makefile
  • Makefile.linux-x86_64
  • minigp.c
  • openmp.c
  • pari-mt.c
  • rho.gp
  • squfof.gp
  • taylor.gp
  • thread.c



wersja[edytuj]

Jak wydrukować numer wersji biblioteki?[2]

  • najpierw użyć funkcji pari_init() aby utworzyć stos pari, na który funkce będą zwracać swoje wartości

Zmienne:

  • paricfg_version_code encodes in a single long, the Major and minor version numbers as well as the patchlevel.
  • PARI_VERSION_SHIFT is the number of bits used to store each of the integers M, m, p in the version code.
  • paricfg_vcsversion is a version string related to the revision control system used to handle your sources, if any. For instance git-commit hash if compiled from a git repository.
  • The two character strings paricfg_version and paricfg_buildinfo, correspond to the first two lines printed by gp just before the Copyright message. The character string paricfg_compiledate is the date of compilation which appears on the next line. The character string paricfg_mt_engine is the name of the threading engine on the next line.

Funkcje:

  • GEN pari_version() returns the version number as a PARI object, a t_VEC with three t_INT and one t_STR components.
  • long PARI_VERSION(long M, long m, long p) produces the version code attached to release M.m.p. Each code id a unique PARI release, and corresponds to the natural total order on the set of releases (bigger code number means more recent release).
  • pari_print_version();


pari_print_version powoduje :

 GP/PARI CALCULATOR Version 2.9.4 (released)
          amd64 running linux (x86-64/GMP-6.1.2 kernel) 64-bit version
        compiled: Dec 19 2017, gcc version 7.3.0 (Ubuntu 7.3.0-1ubuntu1)
                           threading engine: pthread
                (readline v7.0 disabled, extended help enabled)

gp2c = tworzenie kodu C z pliku gp[edytuj]

Używamy programu gp2c: [3]

 gp2c file.gp > file.gp.c


przykład[edytuj]

Mamy funkcję obliczającą liczbę składowych zbioru Mandelbrota odpowiadających zbiorom Julia z przyciągającym cyklem: [4]

  a(n) = sumdiv(n, d, moebius(n/d)*2^(d-1))

Zapisujemy jako plik tekstowy "c.gp" i przetwarzamy na plik c

 gp2c c.gp > c.c

Otrzymujemy:

/*-*- compile-command: "cc -c -o c.gp.o -g -O3 -Wall -fomit-frame-pointer -fno-strict-aliasing -fPIC -I"/usr/include/x86_64-linux-gnu" c.gp.c && cc -o c.gp.so -shared -g -O3 -Wall -fomit-frame-pointer -fno-strict-aliasing -fPIC -Wl,-shared -Wl,-z,relro c.gp.o -lc -lm -L/usr/lib/x86_64-linux-gnu -lpari"; -*-*/
#include <pari/pari.h>
/*
GP;install("init_c","v","init_c","./c.gp.so");
GP;install("a","D0,G,","a","./c.gp.so");
*/
void init_c(void);
GEN a(GEN n);
/*End of prototype*/

void
init_c(void)	  /* void */
{
  return;
}

GEN
a(GEN n)
{
  GEN p1;	  /* vec */
  GEN p2;
  p1 = divisors(n);
  p2 = gen_0;
  {
    long l3;
    GEN d;	  /* int */
    for (l3 = 1; l3 < lg(p1); ++l3)
    {
      d = icopy(gel(p1, l3));
      p2 = gadd(p2, gmulsg(moebius(gdiv(n, d)), powgi(gen_2, subis(d, 1))));
    }
  }
  return p2;
}

Kompilujemy:

gcc c.c -lpari -Wall

wynik:

/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

Po dodaniu funkcji main i usunięciu niepotrzebnego kodu mamy:

/*


https://oeis.org/A000740


 gcc c.c -lpari -Wall
 ./a.out
number of components of Mandelbrot set corresponding to Julia sets with an attractive 3-cycle = 3

*/


#include <pari/pari.h>


// a(n) = sumdiv(n, d, moebius(n/d)*2^(d-1))
GEN
a(GEN n)
{
  GEN p1;	  /* vec */
  GEN p2;
  p1 = divisors(n);
  p2 = gen_0;
  {
    long l3;
    GEN d;	  /* int */
    for (l3 = 1; l3 < lg(p1); ++l3)
    {
      d = icopy(gel(p1, l3));
      p2 = gadd(p2, gmulsg(moebius(gdiv(n, d)), powgi(gen_2, subis(d, 1))));
    }
  }
  return p2;
} // end of the a function 


// manually

int main(void)
{
 GEN n, k;
 pari_init(500000,2);
 n=stoi(9);
 
 k = a(n);

 printf("number of components of Mandelbrot set corresponding to Julia sets with an attractive %s-cycle = %s\n ",GENtostr(n), GENtostr(k));

 pari_close(); 
 return 0;
}



gp[edytuj]

gp
Reading GPRC: /etc/gprc ...Done.

                  GP/PARI CALCULATOR Version 2.9.4 (released)
          amd64 running linux (x86-64/GMP-6.1.2 kernel) 64-bit version
        compiled: Dec 19 2017, gcc version 7.3.0 (Ubuntu 7.3.0-1ubuntu1)
                           threading engine: pthread
                 (readline v7.0 enabled, extended help enabled)

                     Copyright (C) 2000-2017 The PARI Group

PARI/GP is free software, covered by the GNU General Public License, and comes 
WITHOUT ANY WARRANTY WHATSOEVER.

Type ? for help, \q to quit.
Type ?15 for how to get moral (and possibly technical) support.

parisize = 8000000, primelimit = 500000, nbthreads = 8
? 

Pomoc[edytuj]

Problemy[edytuj]

gun[edytuj]

gun został zastąpiony przez gen_1[5]

dodatkowe biblioteki[edytuj]


Źródła[edytuj]

  1. stackoverflow question : how-to-run-c-program-using-pari-library-with-gcc
  2. stackoverflow question: how-to-print-libpari-version-in-c
  3. manual gp2c
  4. The On-Line Encyclopedia of Integer Sequences : A000740
  5. stackoverflow question : gen-variables-not-identified-pari-library-c