Your skill will accomplishwhat the force of many cannot PerlMonks

### Using GP with Math::Pari

by hv (Parson)
 on Dec 18, 2007 at 14:00 UTC Need Help??
hv has asked for the wisdom of the Perl Monks concerning the following question:

I was asking this the other day, here's what I found out.

I wanted to know how to use Math::Pari to declare and then use a new function written in GP. After a brief exchange with the author, I discovered that the only problem was that Math::Pari hiccups on braces and spaces - and these are in any case not needed (at least for dealing with numbers), they are pure "syntactic sugar".

So I knocked up a local function:

```use Math::Pari qw/ PARI /;
sub PARIdo {
my \$s = shift;
\$s =~ s/[{}\s]+//g;
PARI(\$s);
}
.. and was then able to use it to write nice clean GP function declarations:
```PARIdo(q{
mygcd(a, b) = {
if (a > b, {
mygcd(b, a);
}, {
if (a == 0, {
b;
}, {
mygcd(b % a, a);
});
});
}
});
print PARI("mygcd(12,8)");

I don't yet have a good solution for passing arguments in. You can interpolate them:

```my \$v1 = PARI(12);
my \$v2 = PARI(8);
print PARI(qq{mygcd(\$v1,\$v2)});
.. but it is distressingly inefficient to convert large numbers to string and back. I've done some experiments with setting up a PARIvar, but haven't worked out yet if it is possible to assign to such a variable from perl space.

I'd welcome any other tips and tricks for getting the most out of perl/PARI hybrid code.

Hugo

Create A New User
Node Status?
node history
Node Type: perlquestion [id://657677]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
As of 2017-07-23 08:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I came, I saw, I ...

Results (346 votes). Check out past polls.