# Interface to the GP calculator of PARI/GP¶

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 evaluate 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))")

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"]


TESTS:

Test error recovery:

sage: x = gp('1/0')
Traceback (most recent call last):
...
TypeError: Error executing code in GP:
CODE:
sage[...]=1/0;
PARI/GP ERROR:
***   at top-level: sage[...]=1/0
***                          ^--
*** _/_: division by zero


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)

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 evaluate a string using Gp (and get the result back as a string).

INPUT:

• stacksize (int, default 10000000) – the initial PARI stacksize in bytes (default 10MB)
• maxread (int, default 100000) – ??
• script_subdirectory (string, default None) – name of the subdirectory of SAGE_EXTCODE/pari from which to read scripts
• logfile (string, default None) – log file for the pexpect interface
• server – name of remote server
• server_tmpdir – name of temporary directory on remote server
• init_list_length (int, default 1024) – length of initial list of local variables.

EXAMPLES:

sage: Gp()
PARI/GP interpreter

console()

Spawn a new GP command-line session.

EXAMPLES:

sage: gp.console()  # not tested
GP/PARI CALCULATOR Version 2.4.3 (development svn-12577)
amd64 running linux (x86-64/GMP-4.2.1 kernel) 64-bit version
compiled: Jul 21 2010, gcc-4.6.0 20100705 (experimental) (GCC)
(readline v6.0 enabled, extended help enabled)

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 GP variable var.

INPUT:

• var (string) – a valid GP variable identifier

EXAMPLES:

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

get_default(var=None)

Return the current value of a PARI gp configuration variable.

INPUT:

• var (string, default None) – the name of a PARI gp configuration variable. (See gp.default() for a list.)

OUTPUT:

(string) the value of the variable.

EXAMPLES:

sage: gp.get_default('log')
0
'.../local/share/pari'
sage: gp.get_default('seriesprecision')
16
sage: gp.get_default('realprecision')
28              # 32-bit
38              # 64-bit

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

get_series_precision()

Return the current PARI power series precision.

EXAMPLES:

sage: gp.get_series_precision()
16

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)

Kill the value of the GP variable var.

INPUT:

• var (string) – a valid GP variable identifier

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.set_precision(new_prec); old_prec
28                                             # 32-bit
38                                             # 64-bit
sage: pi_150
3.14159265358979323846264338327950288419716939938  # 32-bit
3.14159265358979323846264338327950288419716939937510582098  # 64-bit
sage: gp.set_precision(old_prec)
48                                             # 32-bit
57                                             # 64-bit
sage: gp.get_precision()
28                                             # 32-bit
38                                             # 64-bit

quit(verbose=False, timeout=0.25)

Terminate the GP process.

EXAMPLES:

sage: a = gp('10'); a
10
sage: gp.quit()
sage: a
Traceback (most recent call last):
...
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 GP variable var to the given value.

INPUT:

• var (string) – a valid GP variable identifier
• value – a value for the variable

EXAMPLES:

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

set_default(var=None, value=None)

Set a PARI gp configuration variable, and return the old value.

INPUT:

• var (string, default None) – the name of a PARI gp configuration variable. (See gp.default() for a list.)
• value – the value to set the variable to.

EXAMPLES:

sage: old_prec = gp.set_default('realprecision',100); old_prec
28              # 32-bit
38              # 64-bit
sage: gp.get_default('realprecision')
100
sage: gp.set_default('realprecision',old_prec)
100
sage: gp.get_default('realprecision')
28              # 32-bit
38              # 64-bit

set_precision(prec=None)

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

EXAMPLES:

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

set_real_precision(prec=None)

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

EXAMPLES:

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

set_series_precision(prec=None)

Sets the PARI power series precision, and returns the old precision.

EXAMPLES:

sage: old_prec = gp.set_series_precision(50); old_prec
16
sage: gp.get_series_precision()
50
sage: gp.set_series_precision(old_prec)
50
sage: gp.get_series_precision()
16

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()  # not tested
((2, 4, 3), 'GP/PARI CALCULATOR Version 2.4.3 (development svn-12577)')

class sage.interfaces.gp.GpElement(parent, value, is_name=False, name=None)

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

sage: a = gp(39393)
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])')
False
[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.068749776356193066159263548*I, 3.109648242324380328550149122 + 1.009741959000000000000000000 E-28*I, 1.554824121162190164275074561 + 1.064374745210273756943885994*I, 2.971915267817909670771647951] # 32-bit
[1, 2, 3, 4, 5, 9, 11, 29, 35, -183, -3429, -10351, 6128487/10351, [-1.6189099322673713423780009396072169751, -0.31554503386631432881099953019639151248 - 2.0925470969119586079816894466366945829*I, -0.31554503386631432881099953019639151248 + 2.0925470969119586079816894466366945829*I]~, 2.7807400137667297710631976271813584994, 1.3903700068833648855315988135906792497 - 1.0687497763561930661592635474375038788*I, 3.1096482423243803285501491221965830079 + 2.3509887016445750160000000000000000000 E-38*I, 1.5548241211621901642750745610982915040 + 1.0643747452102737569438859937299427442*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.068749776356193066159263548*I, 3.109648242324380328550149122 + 1.009741959 E-28*I, 1.554824121162190164275074561 + 1.064374745210273756943885994*I, 2.971915267817909670771647951] # 32-bit
[1, 2, 3, 4, 5, 9, 11, 29, 35, -183, -3429, -10351, 6128487/10351, [-1.6189099322673713423780009396072169751, -0.31554503386631432881099953019639151248 - 2.0925470969119586079816894466366945829*I, -0.31554503386631432881099953019639151248 + 2.0925470969119586079816894466366945829*I]~, 2.7807400137667297710631976271813584994, 1.3903700068833648855315988135906792497 - 1.0687497763561930661592635474375038788*I, 3.1096482423243803285501491221965830079 + 2.350988701644575016 E-38*I, 1.5548241211621901642750745610982915040 + 1.0643747452102737569438859937299427442*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)
class sage.interfaces.gp.GpFunctionElement(obj, name)
sage.interfaces.gp.gp_console()

Spawn a new GP command-line session.

EXAMPLES:

sage: gp.console()  # not tested
GP/PARI CALCULATOR Version 2.4.3 (development svn-12577)
amd64 running linux (x86-64/GMP-4.2.1 kernel) 64-bit version
compiled: Jul 21 2010, gcc-4.6.0 20100705 (experimental) (GCC)
(readline v6.0 enabled, extended help enabled)

sage.interfaces.gp.gp_version()

EXAMPLES:

sage: gp.version()  # not tested
((2, 4, 3), 'GP/PARI CALCULATOR Version 2.4.3 (development svn-12577)')

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


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

EXAMPLES:

sage: from sage.interfaces.gp import reduce_load_GP
`