### Navigation

- index
- modules |
- next |
- previous |
- Interpreter Interfaces »

# 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, [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]

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

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
      ***                          ^--
      *** _/_: impossible inverse in gdiv: 0.

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

    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')
        '.../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
        <repr(<sage.interfaces.gp.GpElement at 0x...>) failed: 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, value)

    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')
        28              # 32-bit
        38              # 64-bit

  `set_precision`(prec)

    Sets 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
        28              # 32-bit
        38              # 64-bit
        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)

    Sets 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
        28              # 32-bit
        38              # 64-bit
        sage: gp.get_precision()
        57
        sage: gp.set_precision(old_prec)
        57
        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)

  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(dumps(E)) == E
      True
      sage: x = gp.Pi()/3
      sage: loads(dumps(x)) == x
      False
      sage: x
      1.047197551196597746154214461            # 32-bit
      1.0471975511965977461542144610931676281  # 64-bit
      sage: loads(dumps(x))
      1.047197551196597746154214461            # 32-bit
      1.0471975511965977461542144610931676281  # 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.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

`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()
      PARI/GP interpreter

### Navigation

- index
- modules |
- next |
- previous |
- Interpreter Interfaces »