QB64 Phoenix Edition
using libpari - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Programs (https://qb64phoenix.com/forum/forumdisplay.php?fid=7)
+---- Thread: using libpari (/showthread.php?tid=536)



using libpari - Jack - 06-10-2022

here's small demo on how you can use libpari http://pari.math.u-bordeaux.fr/
this demo show but a tiny bit of what you can do with the lib
note, some functions will only work on a real console window hence my use of $Console:Only and _Dest _Console
attached below is the libpari dll if you don't want to install Pari/gp
Code: (Select All)
_Title "libpari-demo"
$Console:Only
_Dest _Console

Declare Dynamic Library "libpari"
    Sub pari_init (ByVal parisize~&&, Byval maxprime~&&)
    Sub pari_close()
    Sub pari_print_version ()
    Function setdefault~&& (s As String, v As String, Byval flag&&)
    Function strtoGENstr~&& (s As String)
    Function setrealprecision&& (ByVal n&&, prec&&)
    Function GENtostr$ (ByVal x~&&)
    Function geval~&& (ByVal x~&&)
    Sub outmat (ByVal x~&&)
    Function gp_input~&& ()
    Function stoi~&& (ByVal x&&)
    Function abscmpiu& (ByVal x~&&, Byval y As _Unsigned _Integer64)
    Function abscmpui& (ByVal x As _Unsigned _Integer64, Byval y~&&)
    Function absequaliu& (ByVal x~&&, Byval y As _Unsigned _Integer64)
    Function gequal& (ByVal x~&&, Byval y~&&)
    Function gcmp& (ByVal x~&&, Byval y~&&)
End Declare

Dim As _Unsigned _Integer64 a, b, c, d, e, f
Dim As String s1, s2, s3
Dim As _Integer64 flag, prec

pari_init 80000000, 500000
pari_print_version
Print
prec = setrealprecision(50, prec)
s1 = "seriesprecision" + Chr$(0): s2 = "12" + Chr$(0)
f = setdefault(s1, s2, flag)
s1 = "fibonacci(300)" + Chr$(0)
a = strtoGENstr(s1) 'convert to GEN string
b = geval(a) 'eval the string in a
'outmat b
s3 = GENtostr(b) 'convert GEN to string
Print "fibonacci(300) = "; s3

s1 = "50!" + Chr$(0)
a = strtoGENstr(s1) 'convert to GEN string
b = geval(a) 'eval the string in a
'outmat b
s3 = GENtostr(b) 'convert GEN to string
Print "50! = "; s3

s1 = "sin(x)" + Chr$(0)
a = strtoGENstr(s1) 'convert to GEN string
b = geval(a) 'eval the string in a
'outmat b
s3 = GENtostr(b)
Print "sin(x) = "; s3

Print "evaluate a tiny program: for(i=2, 10, print(sqrt(i)))"
s1 = "for(i=2, 10, print(sqrt(i)))" + Chr$(0)
a = strtoGENstr(s1)
c = geval(a)
Print
Print "enter an expression or simply press return to exit the loop"

s1 = " "
While s1 <> ""
    Input "enter an expression ", s1
    s2 = s1 + Chr$(0)
    c = strtoGENstr(s2)
    a = geval(c)
    outmat a
Wend

'Print "enter an A ";: a = gp_input
'Print "enter an B ";: b = gp_input
'Print gequal(a, b)
'Print gcmp(a, b)
'Print abscmpiu(a, 0)
'Print abscmpui(0, a)
'Print absequaliu(a, 0)

s1 = "plot(x=-Pi,Pi,sin(x))" + Chr$(0)
a = strtoGENstr(s1) 'convert to GEN string
Print "sin(x) = ";
b = geval(a) 'eval the string in a

pari_close
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(300) = 222232244629420445529739893461909967206666939096499764990979600
50! = 30414093201713378043612608166064768844377641568960512000000000000
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)
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
[edit] the plot looked real bad in the post


RE: using libpari - Ed Davis - 09-05-2023

(06-10-2022, 04:03 AM)Jack Wrote: here's small demo on how you can use libpari http://pari.math.u-bordeaux.fr/
this demo show but a tiny bit of what you can do with the lib
note, some functions will only work on a real console window hence my use of $Console:Only and _Dest _Console
attached below is the libpari dll if you don't want to install Pari/gp
So, if it was me, I would change the expression parser to not solve the equation, but just do a syntax check.
If the syntax is ok, then pass the string to the library routine.

Does that make sense?


RE: using libpari - Jack - 09-05-2023

yes, but I don't think it's worth the trouble


RE: using libpari - grymmjack - 09-06-2023

@jack Thanks for this share!


RE: using libpari - Jack - 09-06-2023

hi grymmjack
libpari has many number theory functions, the only problem is that there's no way to catch syntax errors, your program will silently crash if there's a syntax error.
 I was talking with Ed Davis about adapting one of his parsers to parse the string for syntax errors before feeding it to libpari, but there are so many functions in that lib that it would take a considerable amount of work.