using libpari

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

using libpari

Post by srvaldez »

get pari/gp from http://pari.math.u-bordeaux.fr/
translating the header using FBfrog produces a huge bi file that needs many corrections but using libpari is very simple
you can use functions like mpadd simply enough but there's no need, you can simply evaluate a string with your functions
I only post a tiny fraction of the include file with example usage

Code: Select all

#Pragma Once
#Inclib "pari"

Extern "C"

Type pari_ulong As Ulongint
Type GEN As Longint Ptr

Declare Sub pari_print_version()
Declare Function geval(Byval x As GEN) As GEN
Declare Function setdefault(Byval s As Const Zstring Ptr, Byval v As Const Zstring Ptr, Byval flag As Longint) As GEN
Declare Function strtoGENstr(Byval s As Const Zstring Ptr) As GEN
Declare Function setrealprecision(Byval n As Longint, Byval prec As Longint Ptr) As Longint
Declare Sub pari_init(Byval parisize As Uinteger, Byval maxprime As pari_ulong)
Declare Function GENtostr(Byval x As GEN) As Zstring Ptr
Declare Function stoi(Byval x As Longint) As GEN
Declare Function mpadd(Byval x As GEN, Byval y As GEN) As GEN
Declare Function mpsub(Byval x As GEN, Byval y As GEN) As GEN
Declare Function mpmul(Byval x As GEN, Byval y As GEN) As GEN
Declare Function mpdiv(Byval x As GEN, Byval y As GEN) As GEN
End Extern

Dim As GEN a, b, c, e, f
Dim As Zstring Ptr s=Callocate(1024*1024)
Dim As Longint prec, flag

pari_init(8000000,500000)
pari_print_version()
setrealprecision(50, @prec)
f=setdefault("seriesprecision", "12", flag)
*s="fibonacci(50)"
a=strtoGENstr(s)    'convert to GEN string
b=geval(a)          'eval the string in a
s=GENtostr(b)       'convert GEN to string
Print "fibonacci(50) = ";*s

*s="sin(x)"
a=strtoGENstr(s)
c=geval(a)
s=GENtostr(c)
Print "sin(x) = ";*s

*s="Pi"
a=strtoGENstr(s)
c=geval(a)
s=GENtostr(c)
Print "Pi = ";*s
Print
Print "evaluate a tiny program: for(i=2, 10, print(sqrt(i)))"
*s="for(i=2, 10, print(sqrt(i)))"
a=strtoGENstr(s)
c=geval(a)

Deallocate(s)
output

Code: Select all

                                      GP/PARI CALCULATOR Version 2.13.4 (released)
                              amd64 running mingw (x86-64/GMP-6.1.2 kernel) 64-bit version
                              compiled: Mar 25 2022, gcc version 8.3-posix 20190406 (GCC)
                                                threading engine: single
                                    (readline v8.0 disabled, extended help enabled)
fibonacci(50) = 12586269025
sin(x) = x - 1/6*x^3 + 1/120*x^5 - 1/5040*x^7 + 1/362880*x^9 - 1/39916800*x^11 + O(x^13)
Pi = 3.1415926535897932384626433832795028841971693993751

evaluate a tiny program: for(i=2, 10, print(sqrt(i)))
1.4142135623730950488016887242096980785696718753769
1.7320508075688772935274463415058723669428052538104
2.0000000000000000000000000000000000000000000000000
2.2360679774997896964091736687312762354406183596115
2.4494897427831780981972840747058913919659474806567
2.6457513110645905905016157536392604257102591830825
2.8284271247461900976033774484193961571393437507539
3.0000000000000000000000000000000000000000000000000
3.1622776601683793319988935444327185337195551393252
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: using libpari

Post by dodicat »

Hi srvaldez.
I didn't install anything, but 7zip let me extract the .exe.
I got the libpari.dll from it and used it.
no problems.
geval I think is quite powerful.
Wherever there is a fibonacci, there is usually a factorial.
I just did

*s="factorial(50)"
a=strtoGENstr(s) 'convert to GEN string
b=geval(a) 'eval the string in a
s=GENtostr(b) 'convert GEN to string
Print "factorial(50) = ";*s
...
factorial(50) = 30414093201713378043612608166064768844377641568960512000000000000 (when I adjust the decimal output and make the precision 100)
So it is very easy to use.

Aside:
I notice that in the FB distribution
bin/libex/gcc/. . ./9.3.0 a libgmp-10.dll is present (among others).
It is only 446 kb, it gives
GMP version 6.2.0 when asked for the version.
But it works just fine.
I wonder why a gmp dll is in that location?

Anyway, thanks for libpari, there are many c includes for this as you say, I haven't done any speed tests on it (YET).
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: using libpari

Post by srvaldez »

hi didicat :)
thank you for giving it a try, as for why libgmp is in that folder my guess is that cc1.exe depends on it
as for the factorial you can also use 50! for example
I did not include the file functions in my example, but you can also read files that contain expressions or programs
example, suppose that you have a file pari.gp then you read that file

Code: Select all

*s="read(""pari.gp"")"
a=strtoGENstr(s)
c=geval(a)
you can also do ASCII plots

Code: Select all

*s="plot(X=-Pi,Pi,sin(X))"
a=strtoGENstr(s)
c=geval(a)
Post Reply