Interface to GP/Pari

Type gp.[tab] for a list of all the functions available from your Gp install. Type gp.[tab]? for Gp’s help about a given function. Type gp(...) to create a new Gp object, and gp.eval(...) to run a string using Gp (and get the result back as a string).

EXAMPLES: We illustrate objects that wrap GP objects (gp is the PARI interpreter):

sage: M = gp('[1,2;3,4]')
sage: M
[1, 2; 3, 4]
sage: M * M
[7, 10; 15, 22]
sage: M + M
[2, 4; 6, 8]
sage: M.matdet()
-2
sage: E = gp.ellinit([1,2,3,4,5])
sage: E.ellglobalred()
[10351, [1, -1, 0, -1], 1]
sage: E.ellan(20)
[1, 1, 0, -1, -3, 0, -1, -3, -3, -3, -1, 0, 1, -1, 0, -1, 5, -3, 4, 3]
sage: primitive_root(7)
3
sage: x = gp("znlog( Mod(2,7), Mod(3,7))")
sage: 3^x % 7
2
sage: print gp("taylor(sin(x),x)")
x - 1/6*x^3 + 1/120*x^5 - 1/5040*x^7 + 1/362880*x^9 - 1/39916800*x^11 + 1/6227020800*x^13 - 1/1307674368000*x^15 + O(x^16)

GP has a powerful very efficient algorithm for numerical computation of integrals.

sage: gp("a = intnum(x=0,6,sin(x))")
0.03982971334963397945434770208               # 32-bit
0.039829713349633979454347702077075594548     # 64-bit
sage: gp("a")
0.03982971334963397945434770208               # 32-bit
0.039829713349633979454347702077075594548     # 64-bit
sage: gp.kill("a")
sage: gp("a")
a

Note that gp ASCII plots do work in Sage, as follows:

sage: print gp.eval("plot(x=0,6,sin(x))")
<BLANKLINE>
0.9988963 |''''''''''''_x"...x_''''''''''''''''''''''''''''''''''''''''''|
          |          x"        "x                                        |
          |        _"            "_                                      |
          |       x                x                                     |
          |      "                  "                                    |
          |     "                    "                                   |
          |   _"                      "_                                 |
          |  _                          _                                |
          | _                            _                               |
          |_                              _                              |
          _,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
          |                                "                             |
          |                                 "                            |
          |                                  "                           |
          |                                   "                          "
          |                                    "_                      _"|
          |                                      _                    _  |
          |                                       _                  _   |
          |                                        x                x    |
          |                                         "_            _"     |
          |                                           x_        _x       |
-0.998955 |............................................."x____x".........|
          0                                                              6

The GP interface reads in even very long input (using files) in a robust manner, as long as you are creating a new object.

sage: t = '"%s"'%10^10000   # ten thousand character string.
sage: a = gp.eval(t)            
sage: a = gp(t)

In Sage, the PARI large galois groups datafiles should be installed by default:

sage: f = gp('x^9 - x - 2')
sage: f.polgalois()
[362880, -1, 34, "S9"]

AUTHORS:

  • William Stein
  • David Joyner: some examples
  • William Stein (2006-03-01): added tab completion for methods: gp.[tab] and x = gp(blah); x.[tab]
  • William Stein (2006-03-01): updated to work with PARI 2.2.12-beta
  • William Stein (2006-05-17): updated to work with PARI 2.2.13-beta
class sage.interfaces.gp.Gp(stacksize=10000000, maxread=100000, script_subdirectory=None, logfile=None, server=None, server_tmpdir=None, init_list_length=1024)

Bases: sage.interfaces.expect.Expect

Interface to the PARI gp interpreter.

Type gp.[tab] for a list of all the functions available from your Gp install. Type gp.[tab]? for Gp’s help about a given function. Type gp(...) to create a new Gp object, and gp.eval(...) to run a string using Gp (and get the result back as a string).

console()

Spawn a new GP command-line session.

EXAMPLES:

sage: gp.console() #not tested
GP/PARI CALCULATOR Version 2.3.3 (released)
amd64 running linux (x86-64/GMP-4.2.1 kernel) 64-bit version
compiled: Feb 22 2008, gcc-4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)
(readline v5.2 enabled, extended help available)
...
cputime(t=None)

cputime for pari - cputime since the pari process was started.

INPUT:

  • t - (default: None); if not None, then returns time since t

Warning

If you call gettime explicitly, e.g., gp.eval(‘gettime’), you will throw off this clock.

EXAMPLES:

sage: gp.cputime()          # random output 
0.0080000000000000002
sage: gp.factor('2^157-1')
[852133201, 1; 60726444167, 1; 1654058017289, 1; 2134387368610417, 1]
sage: gp.cputime()          # random output 
0.26900000000000002
get(var)

Get the value of the variable var.

EXAMPLES:

sage: gp.set('x', '2')
sage: gp.get('x')
'2'
get_precision()

Return the current PARI precision for real number computations.

EXAMPLES:

sage: gp.get_precision()
28              # 32-bit
38              # 64-bit
get_real_precision()

Return the current PARI precision for real number computations.

EXAMPLES:

sage: gp.get_precision()
28              # 32-bit
38              # 64-bit
help(command)

Returns GP’s help for command.

EXAMPLES:

sage: gp.help('gcd')
'gcd(x,{y}): greatest common divisor of x and y.'
kill(var)

EXAMPLES:

sage: gp.set('xx', '22')
sage: gp.get('xx')
'22'
sage: gp.kill('xx')
sage: gp.get('xx')
'xx'
new_with_bits_prec(s, precision=0)

Creates a GP object from s with precision bits of precision. GP actually automatically increases this precision to the nearest word (i.e. the next multiple of 32 on a 32-bit machine, or the next multiple of 64 on a 64-bit machine).

EXAMPLES:

sage: pi_def = gp(pi); pi_def
3.141592653589793238462643383                  # 32-bit
3.1415926535897932384626433832795028842        # 64-bit
sage: pi_def.precision()
28                                             # 32-bit
38                                             # 64-bit
sage: pi_150 = gp.new_with_bits_prec(pi, 150)
sage: new_prec = pi_150.precision(); new_prec
48                                             # 32-bit
57                                             # 64-bit
sage: old_prec = gp.get_precision()
sage: gp.set_precision(new_prec)
28                                             # 32-bit
38                                             # 64-bit
sage: pi_150
3.14159265358979323846264338327950288419716939937  # 32-bit
3.14159265358979323846264338327950288419716939937510582098  # 64-bit
sage: gp.set_precision(old_prec)
48                                             # 32-bit
57                                             # 64-bit
quit(verbose=False, timeout=0.25)

EXAMPLES:

sage: a = gp('10'); a
10
sage: gp.quit()
sage: a
...
ValueError: The pari session in which this object was defined is no longer running.
sage: gp(pi)
3.1415926535897932384626433832795028842    # 64-bit
3.141592653589793238462643383              # 32-bit
set(var, value)

Set the variable var to the given value.

EXAMPLES:

sage: gp.set('x', '2')
sage: gp.get('x')
'2'
set_precision(prec=None)

Sets the current PARI precision (in decimal digits) for real number computations, and returns the old one.

EXAMPLES:

sage: old_prec = gp.set_precision(53)
sage: gp.get_precision()
57
sage: gp.set_precision(old_prec)
57
sage: gp.get_precision()
28              # 32-bit
38              # 64-bit
set_real_precision(prec=None)

Sets the current PARI precision (in decimal digits) for real number computations, and returns the old one.

EXAMPLES:

sage: old_prec = gp.set_precision(53)
sage: gp.get_precision()
57
sage: gp.set_precision(old_prec)
57
sage: gp.get_precision()
28              # 32-bit
38              # 64-bit
trait_names()

EXAMPLES:

sage: c = gp.trait_names()
sage: len(c) > 100
True
sage: 'gcd' in c
True
version()

Returns the version of GP being used.

EXAMPLES:

sage: gp.version()
((2, 3, 5), 'GP/PARI CALCULATOR Version 2.3.5 (released)')
class sage.interfaces.gp.GpElement(parent, value, is_name=False, name=None)

Bases: sage.interfaces.expect.ExpectElement

EXAMPLES: This example illustrates dumping and loading GP elements to compressed strings.

sage: a = gp(39393)
sage: loads(a.dumps()) == a
True

Since dumping and loading uses the string representation of the object, it need not result in an identical object from the point of view of PARI:

sage: E = gp('ellinit([1,2,3,4,5])')
sage: loads(E.dumps()) == E
False
sage: loads(E.dumps())
[1, 2, 3, 4, 5, 9, 11, 29, 35, -183, -3429, -10351, 6128487/10351, [-1.618909932267371342378000940, -0.3155450338663143288109995302 - 2.092547096911958607981689447*I, -0.3155450338663143288109995302 + 2.092547096911958607981689447*I]~, 2.780740013766729771063197627, -1.390370006883364885531598814 + 1.068749776356193066159263547*I, -1.554824121162190164275074561 + 3.415713103000000000000000000 E-29*I, 0.7774120605810950821375372806 - 1.727349756386839866714149879*I, 2.971915267817909670771647951] # 32-bit
[1, 2, 3, 4, 5, 9, 11, 29, 35, -183, -3429, -10351, 6128487/10351, [-1.6189099322673713423780009396072169750, -0.31554503386631432881099953019639151248 - 2.0925470969119586079816894466366945829*I, -0.31554503386631432881099953019639151248 + 2.0925470969119586079816894466366945829*I]~, 2.7807400137667297710631976271813584994, -1.3903700068833648855315988135906792497 + 1.0687497763561930661592635474375038788*I, -1.5548241211621901642750745610982915039 + 7.9528267991764473360000000000000000000 E-39*I, 0.77741206058109508213753728054914575197 - 1.7273497563868398667141498789110695181*I, 2.9719152678179096707716479509361896060]  # 64-bit
sage: E
[1, 2, 3, 4, 5, 9, 11, 29, 35, -183, -3429, -10351, 6128487/10351, [-1.618909932267371342378000940, -0.3155450338663143288109995302 - 2.092547096911958607981689447*I, -0.3155450338663143288109995302 + 2.092547096911958607981689447*I]~, 2.780740013766729771063197627, -1.390370006883364885531598814 + 1.068749776356193066159263547*I, -1.554824121162190164275074561 + 3.415713103 E-29*I, 0.7774120605810950821375372806 - 1.727349756386839866714149879*I, 2.971915267817909670771647951]   # 32-bit
[1, 2, 3, 4, 5, 9, 11, 29, 35, -183, -3429, -10351, 6128487/10351, [-1.6189099322673713423780009396072169750, -0.31554503386631432881099953019639151248 - 2.0925470969119586079816894466366945829*I, -0.31554503386631432881099953019639151248 + 2.0925470969119586079816894466366945829*I]~, 2.7807400137667297710631976271813584994, -1.3903700068833648855315988135906792497 + 1.0687497763561930661592635474375038788*I, -1.5548241211621901642750745610982915039 + 7.952826799176447336 E-39*I, 0.77741206058109508213753728054914575197 - 1.7273497563868398667141498789110695181*I, 2.9719152678179096707716479509361896060]  # 64-bit

The two elliptic curves look the same, but internally the floating point numbers are slightly different.

bool()

EXAMPLES:

sage: gp(2).bool()
True
sage: bool(gp(2))
True
sage: bool(gp(0))
False
trait_names()

EXAMPLES:

sage: 'gcd' in gp(2).trait_names()
True
class sage.interfaces.gp.GpFunction(parent, name)
Bases: sage.interfaces.expect.ExpectFunction
class sage.interfaces.gp.GpFunctionElement(obj, name)
Bases: sage.interfaces.expect.FunctionElement
sage.interfaces.gp.gp_console()

Spawn a new GP command-line session.

EXAMPLES:

sage: gp.console() #not tested
GP/PARI CALCULATOR Version 2.3.3 (released)
amd64 running linux (x86-64/GMP-4.2.1 kernel) 64-bit version
compiled: Feb 22 2008, gcc-4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)
(readline v5.2 enabled, extended help available)
...
sage.interfaces.gp.gp_version()

EXAMPLES:

sage: gp.version()      # random output
((2, 3, 1), 'GP/PARI CALCULATOR Version 2.3.1 (0)')
sage.interfaces.gp.is_GpElement(x)

Returns True of x is a GpElement.

EXAMPLES:

sage: from sage.interfaces.gp import is_GpElement
sage: is_GpElement(gp(2))
True
sage: is_GpElement(2)
False
sage.interfaces.gp.reduce_load_GP()

Returns the GP interface object defined in sage.interfaces.gp.

EXAMPLES:

sage: from sage.interfaces.gp import reduce_load_GP
sage: reduce_load_GP()
GP/PARI interpreter

Previous topic

Interface to GAP3

Next topic

Interface to the Gnuplot interpreter.

This Page