14.14 Interface to SAGE

Module: sage.interfaces.sage0

Interface to SAGE

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

Module-level Functions

reduce_load_Sage( )

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

reduce_load_element( s)

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

sage0_console( )

Spawn a new Sage command-line session.

sage: sage0_console() #not tested
----------------------------------------------------------------------
| SAGE Version ..., Release Date: ...                                |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------
...

sage0_version( )

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

Class: Sage

class Sage
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.

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: s = Sage()

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

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

Create a vector space of dimension $ 4$ , and compute its generators:

sage: V = s('QQ^4')
sage: 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: type(V)
<class 'sage.interfaces.sage0.SageElement'>
sage: V.parent()
Sage
sage: g = V.0;  g
(1, 0, 0, 0)
sage: 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: s('%s.parent()'%g.name())
Vector space of dimension 4 over Rational Field

Note that the memory space is completely different.

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

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

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

This $ a=10$ is in a subprocess of a subprocesses of your original Sage.

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

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

Sage( self, [logfile=None], [preparse=True], [python=False], [init_code=None], [server=None], [server_tmpdir=None], [remote_cleaner=True])

sage: sage0 == loads(dumps(sage0))
True

Functions: clear,$ \,$ console,$ \,$ cputime,$ \,$ eval,$ \,$ get,$ \,$ new,$ \,$ preparse,$ \,$ quit,$ \,$ set,$ \,$ trait_names,$ \,$ version

clear( self, var)

Clear the variable named var.

sage: sage0.set('x', '2')
sage: sage0.get('x')
'2'
sage: sage0.clear('x')
sage: sage0.get('x')
"...NameError: name 'x' is not defined"

console( self)

Spawn a new Sage command-line session.

sage: sage0.console() #not tested
----------------------------------------------------------------------
| SAGE Version ..., Release Date: ...                                |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------
...

cputime( self, [t=None])

Return cputime since this Sage subprocess was started.

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

eval( self, line, [strip=True])

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

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

get( self, var)

Get the value of the variable var.

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

new( self, x)

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

preparse( self, x)

Returns the preparsed version of the string s.

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

quit( self, [verbose=False])

sage: s = Sage()
sage: s.eval('2+2')
'4'
sage: s.quit()

set( self, var, value)

Set the variable var to the given value.

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

trait_names( self)

sage: t = sage0.trait_names()
sage: len(t) > 100
True
sage: 'gcd' in t
True

version( self)

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

Special Functions: __call__,$ \,$ __init__,$ \,$ __reduce__,$ \,$ _contains,$ \,$ _object_class,$ \,$ _quit_string

__call__( self, x)

sage: a = sage0(4)
sage: a.parent()
Sage
sage: a is sage0(a)
True

__reduce__( self)

sage: sage0.__reduce__()
(<function reduce_load_Sage at 0x...>, ())

_contains( self, v1, v2)

sage: sage0._contains('2', 'QQ')
True

_object_class( self)

sage: sage0._object_class()
<class 'sage.interfaces.sage0.SageElement'>

_quit_string( self)

sage: sage0._quit_string()
'from sage.misc.misc import delete_tmpfiles; delete_tmpfiles()'

Class: SageElement

class SageElement

Special Functions: _sage_

_sage_( self)

Return local copy of self.

sage: sr = mq.SR(allow_zero_inversions=True)
sage: F,s = sr.polynomial_system()
sage: F == sage0(F)._sage_()
True

Class: SageFunction

class SageFunction

Special Functions: __call__,$ \,$ __repr__

__call__( self)

sage: four_gcd = sage0(4).gcd
sage: four_gcd(6)
2

__repr__( self)

sage: sage0(4).gcd
<built-in method gcd of sage.rings.integer.Integer object at 0x...>

See About this document... for information on suggesting changes.