Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to Using GP with Math::Pari by hv

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-25 20:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found