Interface to Sage - Interpreter Interfaces

Skip to content
Back to top
View this page
Edit this page

# Interface to Sage

This is an expect interface to another copy of the Sage interpreter.

class sage.interfaces.sage0.Sage(logfile=None, preparse=True, init_code=None, server=None, server_tmpdir=None, remote_cleaner=True, **kwds)

  Bases: `ExtraTabCompletion`, `Expect`

  Expect interface to the Sage interpreter itself.

  INPUT:

  - `server` – (optional) if specified runs Sage on a remote machine with address. You must have ssh keys setup so you can login to the remote machine by typing “ssh remote_machine” and no password, call _install_hints_ssh() for hints on how to do that.

  The version of Sage should be the same as on the local machine, since pickling is used to move data between the two Sage process.

  EXAMPLES: We create an interface to a copy of Sage. This copy of Sage runs as an external process with its own memory space, etc.

  Sage

      sage: s = Sage()

  Python

      >>> from sage.all import *
      >>> s = Sage()

  Create the element 2 in our new copy of Sage, and cube it.

  Sage

      sage: a = s(2)
      sage: a^3
      8

  [Python]

      >>> from sage.all import *
      >>> a = s(Integer(2))
      >>> a**Integer(3)
      8

  Create a vector space of dimension \(4\), and compute its generators:

  Sage

      sage: V = s('QQ^4')
      sage: V.gens()
      ((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1))

  Python

      >>> from sage.all import *
      >>> V = s('QQ^4')
      >>> V.gens()
      ((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1))

  Note that V is a not a vector space, it’s a wrapper around an object (which happens to be a vector space), in another running instance of Sage.

  Sage

      sage: type(V)
      <class 'sage.interfaces.sage0.SageElement'>
      sage: V.parent()
      Sage
      sage: g = V.0;  g
      (1, 0, 0, 0)
      sage: g.parent()
      Sage

  [Python]

      >>> from sage.all import *
      >>> type(V)
      <class 'sage.interfaces.sage0.SageElement'>
      >>> V.parent()
      Sage
      >>> g = V.gen(0);  g
      (1, 0, 0, 0)
      >>> g.parent()
      Sage

  We can still get the actual parent by using the name attribute of g, which is the variable name of the object in the child process.

  Sage

      sage: s('%s.parent()' % g.name())
      Vector space of dimension 4 over Rational Field

  Python

      >>> from sage.all import *
      >>> s('%s.parent()' % g.name())
      Vector space of dimension 4 over Rational Field

  Note that the memory space is completely different.

  Sage

      sage: x = 10
      sage: s('x = 5')
      5
      sage: x
      10
      sage: s('x')
      5

  [Python]

      >>> from sage.all import *
      >>> x = Integer(10)
      >>> s('x = 5')
      5
      >>> x
      10
      >>> s('x')
      5

  We can have the child interpreter itself make another child Sage process, so now three copies of Sage are running:

  Sage

      sage: s3 = s('Sage()')
      sage: a = s3(10)
      sage: a
      10

  Python

      >>> from sage.all import *
      >>> s3 = s('Sage()')
      >>> a = s3(Integer(10))
      >>> a
      10

  This \(a=10\) is in a subprocess of a subprocess of your original Sage.

  Sage

      sage: _ = s.eval('%s.eval("x=8")' % s3.name())
      sage: s3('"x"')
      8
      sage: s('x')
      5
      sage: x
      10

  [Python]

      >>> from sage.all import *
      >>> _ = s.eval('%s.eval("x=8")' % s3.name())
      >>> s3('"x"')
      8
      >>> s('x')
      5
      >>> x
      10

  The double quotes are needed because the call to s3 first evaluates its arguments using the s interpreter, so the call to s3 is passed `s('"x"')`, which is the string `'x'` in the s interpreter.

  clear(var)

    Clear the variable named var.

    Note that the exact format of the `NameError` for a cleared variable is slightly platform dependent, see Issue #10539.

    EXAMPLES:

    Sage

        sage: sage0.set('x', '2')
        sage: sage0.get('x')
        '2'
        sage: sage0.clear('x')
        sage: 'NameError' in sage0.get('x')
        True

    Python

        >>> from sage.all import *
        >>> sage0.set('x', '2')
        >>> sage0.get('x')
        '2'
        >>> sage0.clear('x')
        >>> 'NameError' in sage0.get('x')
        True

  console()

    Spawn a new Sage command-line session.

    EXAMPLES:

    Sage

        sage: sage0.console() #not tested
        ----------------------------------------------------------------------
        | SageMath version ..., Release Date: ...                            |
        | Using Python ....   Type "help()" for help.                        |
        ----------------------------------------------------------------------
        ...

    Python

        >>> from sage.all import *
        >>> sage0.console() #not tested
        ----------------------------------------------------------------------
        | SageMath version ..., Release Date: ...                            |
        | Using Python ....   Type "help()" for help.                        |
        ----------------------------------------------------------------------
        ...

  cputime(t=None)

    Return cputime since this Sage subprocess was started.

    EXAMPLES:

    Sage

        sage: sage0.cputime()     # random output
        1.3530439999999999
        sage: sage0('factor(2^157-1)')
        852133201 * 60726444167 * 1654058017289 * 2134387368610417
        sage: sage0.cputime()     # random output
        1.6462939999999999

    Python

        >>> from sage.all import *
        >>> sage0.cputime()     # random output
        1.3530439999999999
        >>> sage0('factor(2^157-1)')
        852133201 * 60726444167 * 1654058017289 * 2134387368610417
        >>> sage0.cputime()     # random output
        1.6462939999999999

  eval(line, strip=True, **kwds)

    Send the code x to a second instance of the Sage interpreter and return the output as a string.

    This allows you to run two completely independent copies of Sage at the same time in a unified way.

    INPUT:

    - `line` – input line of code

    - `strip` – ignored

    EXAMPLES:

    Sage

        sage: sage0.eval('2+2')
        '4'

    Python

        >>> from sage.all import *
        >>> sage0.eval('2+2')
        '4'

  get(var)

    Get the value of the variable var.

    EXAMPLES:

    Sage

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

    Python

        >>> from sage.all import *
        >>> sage0.set('x', '2')
        >>> sage0.get('x')
        '2'

  new(x)

    EXAMPLES:

    Sage

        sage: sage0.new(2)
        2
        sage: _.parent()
        Sage

    Python

        >>> from sage.all import *
        >>> sage0.new(Integer(2))
        2
        >>> _.parent()
        Sage

  preparse(x)

    Return the preparsed version of the string s.

    EXAMPLES:

    Sage

        sage: sage0.preparse('2+2')
        'Integer(2)+Integer(2)'

    Python

        >>> from sage.all import *
        >>> sage0.preparse('2+2')
        'Integer(2)+Integer(2)'

  set(var, value)

    Set the variable var to the given value.

    EXAMPLES:

    Sage

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

    Python

        >>> from sage.all import *
        >>> sage0.set('x', '2')
        >>> sage0.get('x')
        '2'

  version()

    EXAMPLES:

    Sage

        sage: sage0.version()
        'SageMath version ..., Release Date: ...'
        sage: sage0.version() == version()
        True

    Python

        >>> from sage.all import *
        >>> sage0.version()
        'SageMath version ..., Release Date: ...'
        >>> sage0.version() == version()
        True

class sage.interfaces.sage0.SageElement(parent, value, is_name=False, name=None)

  Bases: `ExpectElement`

class sage.interfaces.sage0.SageFunction(obj, name)

  Bases: `FunctionElement`

sage.interfaces.sage0.reduce_load_Sage()

  EXAMPLES:

  Sage

      sage: from sage.interfaces.sage0 import reduce_load_Sage
      sage: reduce_load_Sage()
      Sage

  Python

      >>> from sage.all import *
      >>> from sage.interfaces.sage0 import reduce_load_Sage
      >>> reduce_load_Sage()
      Sage

sage.interfaces.sage0.reduce_load_element(s)

  EXAMPLES:

  Sage

      sage: from sage.interfaces.sage0 import reduce_load_element
      sage: s = dumps(1/2)
      sage: half = reduce_load_element(s); half
      1/2
      sage: half.parent()
      Sage

  Python

      >>> from sage.all import *
      >>> from sage.interfaces.sage0 import reduce_load_element
      >>> s = dumps(Integer(1)/Integer(2))
      >>> half = reduce_load_element(s); half
      1/2
      >>> half.parent()
      Sage

sage.interfaces.sage0.sage0_console()

  Spawn a new Sage command-line session.

  EXAMPLES:

  Sage

      sage: sage0_console() #not tested
      ----------------------------------------------------------------------
      | SageMath version ..., Release Date: ...                            |
      | Using Python ....   Type "help()" for help.                        |
      ----------------------------------------------------------------------
      ...

  Python

      >>> from sage.all import *
      >>> sage0_console() #not tested
      ----------------------------------------------------------------------
      | SageMath version ..., Release Date: ...                            |
      | Using Python ....   Type "help()" for help.                        |
      ----------------------------------------------------------------------
      ...

sage.interfaces.sage0.sage0_version()

  EXAMPLES:

  Sage

      sage: from sage.interfaces.sage0 import sage0_version
      sage: sage0_version() == version()
      True

  Python

      >>> from sage.all import *
      >>> from sage.interfaces.sage0 import sage0_version
      >>> sage0_version() == version()
      True