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
>>> from sage.all import *
>>> M = gp('[1,2;3,4]')
>>> M
[1, 2; 3, 4]
>>> M * M
[7, 10; 15, 22]
>>> M + M
[2, 4; 6, 8]
>>> M.matdet()
-2
sage: E = gp.ellinit([1,2,3,4,5])
sage: E.ellglobalred()
[10351, [1, -1, 0, -1], 1, [11, 1; 941, 1], [[1, 5, 0, 1], [1, 5, 0, 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]
>>> from sage.all import *
>>> E = gp.ellinit([Integer(1),Integer(2),Integer(3),Integer(4),Integer(5)])
>>> E.ellglobalred()
[10351, [1, -1, 0, -1], 1, [11, 1; 941, 1], [[1, 5, 0, 1], [1, 5, 0, 1]]]
>>> E.ellan(Integer(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
>>> from sage.all import *
>>> primitive_root(Integer(7))
3
>>> x = gp("znlog( Mod(2,7), Mod(3,7))")
>>> Integer(3)**x % Integer(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)
>>> from sage.all import *
>>> 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.039829713349633979454347702077075594548
sage: gp("a")
0.039829713349633979454347702077075594548
sage: gp.kill("a")
sage: gp("a")
a
>>> from sage.all import *
>>> gp("a = intnum(x=0,6,sin(x))")
0.039829713349633979454347702077075594548
>>> gp("a")
0.039829713349633979454347702077075594548
>>> gp.kill("a")
>>> 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
>>> from sage.all import *
>>> 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)
>>> from sage.all import *
>>> t = '"%s"'%Integer(10)**Integer(10000)   # ten thousand character string.
>>> a = gp.eval(t)
>>> 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"]
>>> from sage.all import *
>>> f = gp('x^9 - x - 2')
>>> 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=None, script_subdirectory=None, logfile=None, server=None, server_tmpdir=None, init_list_length=1024, seed=None)[source]¶
- Bases: - ExtraTabCompletion,- 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 evaluate a string using Gp (and get the result back as a string).- INPUT: - stacksize– integer (default: 10000000); the initial PARI stacksize in bytes (default: 10MB)
- script_subdirectory– string (default:- None); name of the subdirectory of- SAGE_EXTCODE/parifrom 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– integer (default: 1024); length of initial list of local variables
- seed– integer (default: random); random number generator seed for pari
 - EXAMPLES: - sage: Gp() PARI/GP interpreter - >>> from sage.all import * >>> Gp() PARI/GP interpreter - console()[source]¶
- 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) - >>> from sage.all import * >>> 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)[source]¶
- 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 - >>> from sage.all import * >>> gp.cputime() # random output 0.0080000000000000002 >>> gp.factor('2^157-1') [852133201, 1; 60726444167, 1; 1654058017289, 1; 2134387368610417, 1] >>> gp.cputime() # random output 0.26900000000000002 
 - get(var)[source]¶
- 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' - >>> from sage.all import * >>> gp.set('x', '2') >>> gp.get('x') '2' 
 - get_default(var)[source]¶
- Return the current value of a PARI gp configuration variable. - INPUT: - var– string; 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 sage: gp.get_default('datadir') '.../share/pari' sage: gp.get_default('seriesprecision') 16 sage: gp.get_default('realprecision') 38 - >>> from sage.all import * >>> gp.get_default('log') 0 >>> gp.get_default('datadir') '.../share/pari' >>> gp.get_default('seriesprecision') 16 >>> gp.get_default('realprecision') 38 
 - get_precision()[source]¶
- Return the current PARI precision for real number computations. - EXAMPLES: - sage: gp.get_precision() 38 - >>> from sage.all import * >>> gp.get_precision() 38 
 - get_real_precision()[source]¶
- Return the current PARI precision for real number computations. - EXAMPLES: - sage: gp.get_precision() 38 - >>> from sage.all import * >>> gp.get_precision() 38 
 - get_series_precision()[source]¶
- Return the current PARI power series precision. - EXAMPLES: - sage: gp.get_series_precision() 16 - >>> from sage.all import * >>> gp.get_series_precision() 16 
 - help(command)[source]¶
- Return GP’s help for - command.- EXAMPLES: - sage: gp.help('gcd') 'gcd(x,{y}): greatest common divisor of x and y.' - >>> from sage.all import * >>> gp.help('gcd') 'gcd(x,{y}): greatest common divisor of x and y.' 
 - kill(var)[source]¶
- 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' - >>> from sage.all import * >>> gp.set('xx', '22') >>> gp.get('xx') '22' >>> gp.kill('xx') >>> gp.get('xx') 'xx' 
 - new_with_bits_prec(s, precision=0)[source]¶
- Create a GP object from s with - precisionbits 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: # needs sage.symbolic sage: pi_def = gp(pi); pi_def 3.1415926535897932384626433832795028842 sage: pi_def.precision() 38 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 38 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() 38 - >>> from sage.all import * >>> # needs sage.symbolic >>> pi_def = gp(pi); pi_def 3.1415926535897932384626433832795028842 >>> pi_def.precision() 38 >>> pi_150 = gp.new_with_bits_prec(pi, Integer(150)) >>> new_prec = pi_150.precision(); new_prec 48 # 32-bit 57 # 64-bit >>> old_prec = gp.set_precision(new_prec); old_prec 38 >>> pi_150 3.14159265358979323846264338327950288419716939938 # 32-bit 3.14159265358979323846264338327950288419716939937510582098 # 64-bit >>> gp.set_precision(old_prec) 48 # 32-bit 57 # 64-bit >>> gp.get_precision() 38 
 - set(var, value)[source]¶
- 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' - >>> from sage.all import * >>> gp.set('x', '2') >>> gp.get('x') '2' 
 - set_default(var, value)[source]¶
- Set a PARI gp configuration variable, and return the old value. - INPUT: - var– string; 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', 110) sage: gp.get_default('realprecision') 115 sage: gp.set_default('realprecision', old_prec) 115 sage: gp.get_default('realprecision') 38 - >>> from sage.all import * >>> old_prec = gp.set_default('realprecision', Integer(110)) >>> gp.get_default('realprecision') 115 >>> gp.set_default('realprecision', old_prec) 115 >>> gp.get_default('realprecision') 38 
 - set_precision(prec)[source]¶
- Set the PARI precision (in decimal digits) for real computations, and returns the old value. - Note - PARI/GP rounds up precisions to the nearest machine word, so the result of - get_precision()is not always the same as the last value inputted to- set_precision().- EXAMPLES: - sage: old_prec = gp.set_precision(53); old_prec 38 sage: gp.get_precision() 57 sage: gp.set_precision(old_prec) 57 sage: gp.get_precision() 38 - >>> from sage.all import * >>> old_prec = gp.set_precision(Integer(53)); old_prec 38 >>> gp.get_precision() 57 >>> gp.set_precision(old_prec) 57 >>> gp.get_precision() 38 
 - set_real_precision(prec)[source]¶
- Set the PARI precision (in decimal digits) for real computations, and returns the old value. - Note - PARI/GP rounds up precisions to the nearest machine word, so the result of - get_precision()is not always the same as the last value inputted to- set_precision().- EXAMPLES: - sage: old_prec = gp.set_precision(53); old_prec 38 sage: gp.get_precision() 57 sage: gp.set_precision(old_prec) 57 sage: gp.get_precision() 38 - >>> from sage.all import * >>> old_prec = gp.set_precision(Integer(53)); old_prec 38 >>> gp.get_precision() 57 >>> gp.set_precision(old_prec) 57 >>> gp.get_precision() 38 
 - set_seed(seed=None)[source]¶
- Set the seed for gp interpreter. - The seed should be an integer. - EXAMPLES: - sage: g = Gp() sage: g.set_seed(1) 1 sage: [g.random() for i in range(5)] [1546275796, 879788114, 1745191708, 771966234, 1247963869] - >>> from sage.all import * >>> g = Gp() >>> g.set_seed(Integer(1)) 1 >>> [g.random() for i in range(Integer(5))] [1546275796, 879788114, 1745191708, 771966234, 1247963869] 
 - set_series_precision(prec=None)[source]¶
- Set 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 - >>> from sage.all import * >>> old_prec = gp.set_series_precision(Integer(50)); old_prec 16 >>> gp.get_series_precision() 50 >>> gp.set_series_precision(old_prec) 50 >>> gp.get_series_precision() 16 
 
- class sage.interfaces.gp.GpElement(parent, value, is_name=False, name=None)[source]¶
- Bases: - ExpectElement,- GpElement- EXAMPLES: This example illustrates dumping and loading GP elements to compressed strings. - sage: a = gp(39393) sage: loads(a.dumps()) == a True - >>> from sage.all import * >>> a = gp(Integer(39393)) >>> 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(dumps(E)) == E True sage: x = gp.Pi()/3 sage: loads(dumps(x)) == x False sage: x 1.0471975511965977461542144610931676281 sage: loads(dumps(x)) 1.0471975511965977461542144610931676281 - >>> from sage.all import * >>> E = gp('ellinit([1,2,3,4,5])') >>> loads(dumps(E)) == E True >>> x = gp.Pi()/Integer(3) >>> loads(dumps(x)) == x False >>> x 1.0471975511965977461542144610931676281 >>> loads(dumps(x)) 1.0471975511965977461542144610931676281 - The two elliptic curves look the same, but internally the floating point numbers are slightly different. 
- sage.interfaces.gp.gp_console()[source]¶
- 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) - >>> from sage.all import * >>> 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()[source]¶
- EXAMPLES: - sage: gp.version() # not tested ((2, 4, 3), 'GP/PARI CALCULATOR Version 2.4.3 (development svn-12577)') - >>> from sage.all import * >>> gp.version() # not tested ((2, 4, 3), 'GP/PARI CALCULATOR Version 2.4.3 (development svn-12577)') 
- sage.interfaces.gp.is_GpElement(x)[source]¶
- Return - Trueif- xis of type- GpElement.- This function is deprecated; use - isinstance()(of- sage.interfaces.abc.GpElement) instead.- EXAMPLES: - sage: from sage.interfaces.gp import is_GpElement sage: is_GpElement(gp(2)) doctest:...: DeprecationWarning: the function is_GpElement is deprecated; use isinstance(x, sage.interfaces.abc.GpElement) instead See https://github.com/sagemath/sage/issues/34804 for details. True sage: is_GpElement(2) False - >>> from sage.all import * >>> from sage.interfaces.gp import is_GpElement >>> is_GpElement(gp(Integer(2))) doctest:...: DeprecationWarning: the function is_GpElement is deprecated; use isinstance(x, sage.interfaces.abc.GpElement) instead See https://github.com/sagemath/sage/issues/34804 for details. True >>> is_GpElement(Integer(2)) False 
- sage.interfaces.gp.reduce_load_GP()[source]¶
- Return the GP interface object defined in - sage.interfaces.gp.- EXAMPLES: - sage: from sage.interfaces.gp import reduce_load_GP sage: reduce_load_GP() PARI/GP interpreter - >>> from sage.all import * >>> from sage.interfaces.gp import reduce_load_GP >>> reduce_load_GP() PARI/GP interpreter