Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Using modular numbers with Math::Pari

by hv (Parson)
on Oct 29, 2009 at 12:40 UTC ( #803944=perlquestion: print w/replies, xml ) Need Help??
hv has asked for the wisdom of the Perl Monks concerning the following question:

I can get a modular reciprocal with Pari easily enough, eg:

use Math::Pari qw/ PARI Mod /; my($n, $p) = map PARI($_), (3, 17); my $recip = PARI(1) / Mod($n, $p); print "1 / $n == $recip\n";
1 / 3 == Mod(6,17)

The result "Mod(6,17)" is a modular value, representing the equivalence class of integers equivalent to 6 (mod 17), and that's great.

However I now want to get the "6" out of there as an integer for further work, i.e. to get the "common residue" of that modular number. I haven't found anything in the Math::Pari or Pari/GP docs that tells how to do that, and everything I've tried gives an error:

$int = int($recip); PARI: *** incorrect type in comparison. at -e line 1. $int = pari2num($recip); PARI: *** forbidden assignment t_INTMOD --> t_REAL. at -e line 1. $int = pari2iv($recip); PARI: *** incorrect type in gtolong. at -e line 1.

I could pull the value out with a regexp, but that would be silly. Surely there is a simple, efficient way to do this?


Replies are listed 'Best First'.
Re: Using modular numbers with Math::Pari
by blokhead (Monsignor) on Oct 29, 2009 at 14:24 UTC
    Pari's lift function "lifts" a modular residue back into the integers.
    ? lift( 1 / Mod(3,17) ) %1 = 6
    Presumably it is available from the Math::Pari bindings somehow (I only know pari, not the Perl bindings).



      perl -MMath::Pari=PARI,Mod,lift -wle 'print lift(PARI(1)/Mod(3,17))' 6

      Perfect - not sure I'd ever have found that searching the docs myself.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://803944]
Approved by Corion
Front-paged by moritz
[stevieb]: in a C++ header file, if a function declaration is listed twice with each listing containing different params, that's just essentially allowing one function accept two different arg lists, yes?
[stevieb]: int func(int x, int y);
[stevieb]: int func(int x);
[stevieb]: For example

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2017-06-28 23:52 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (653 votes). Check out past polls.