Common Interface Functionality#
See the examples in the other sections for how to use specific interfaces. The interface classes all derive from the generic interface that is described in this section.
AUTHORS:
William Stein (2005): initial version
William Stein (2006-03-01): got rid of infinite loop on startup if client system missing
Felix Lawrence (2009-08-21): edited ._sage_() to support lists and float exponents in foreign notation.
Simon King (2010-09-25): Expect._local_tmpfile() depends on Expect.pid() and is cached; Expect.quit() clears that cache, which is important for forking.
Jean-Pierre Flori (2010,2011): Split non Pexpect stuff into a parent class.
Simon King (2015): Improve pickling for InterfaceElement
- class sage.interfaces.interface.Interface(name)[source]#
Bases:
WithEqualityById
,ParentWithBase
Interface interface object.
Note
Two interfaces compare equal if and only if they are identical objects (this is a critical constraint so that caching of representations of objects in interfaces works correctly). Otherwise they are never equal.
- eval(code, **kwds)[source]#
Evaluate code in an interface.
This method needs to be implemented in sub-classes.
Note that it is not always to be expected that it returns a non-empty string. In contrast,
get()
is supposed to return the result of applying a print command to the object so that the output is easier to parse.Likewise, the method
_eval_line()
for evaluation of a single line, often makes sense to be overridden.
- function_call(function, args=None, kwds=None)[source]#
EXAMPLES:
sage: maxima.quad_qags(x, x, 0, 1, epsrel=1e-4) [0.5,5.5511151231257...e-15,21,0] sage: maxima.function_call('quad_qags', [x, x, 0, 1], {'epsrel':'1e-4'}) [0.5,5.5511151231257...e-15,21,0]
>>> from sage.all import * >>> maxima.quad_qags(x, x, Integer(0), Integer(1), epsrel=RealNumber('1e-4')) [0.5,5.5511151231257...e-15,21,0] >>> maxima.function_call('quad_qags', [x, x, Integer(0), Integer(1)], {'epsrel':'1e-4'}) [0.5,5.5511151231257...e-15,21,0]
- get(var)[source]#
Get the value of the variable var.
Note that this needs to be overridden in some interfaces, namely when getting the string representation of an object requires an explicit print command.
- get_seed()[source]#
Return the seed used to set the random number generator in this interface.
The seed is initialized as
None
but should be set when the interface starts.EXAMPLES:
sage: s = Singular() sage: s.set_seed(107) 107 sage: s.get_seed() 107
>>> from sage.all import * >>> s = Singular() >>> s.set_seed(Integer(107)) 107 >>> s.get_seed() 107
- get_using_file(var)[source]#
Return the string representation of the variable var in self, possibly using a file. Use this if var has a huge string representation, since it may be way faster.
Warning
In fact unless a special derived class implements this, it will not be any faster. This is the case for this class if you’re reading it through introspection and seeing this.
- interact()[source]#
This allows you to interactively interact with the child interpreter.
Press Ctrl + D or type ‘quit’ or ‘exit’ to exit and return to Sage.
Note
This is completely different than the console() member function. The console function opens a new copy of the child interpreter, whereas the interact function gives you interactive access to the interpreter that is being used by Sage. Use sage(xxx) or interpretername(xxx) to pull objects in from sage to the interpreter.
- rand_seed()[source]#
Return a random seed that can be put into
set_seed
function for any interpreter.This should be overridden if the particular interface needs something other than a small positive integer.
EXAMPLES:
sage: from sage.interfaces.interface import Interface sage: i = Interface("") sage: i.rand_seed() # random 318491487 sage: s = Singular() sage: s.rand_seed() # random 365260051
>>> from sage.all import * >>> from sage.interfaces.interface import Interface >>> i = Interface("") >>> i.rand_seed() # random 318491487 >>> s = Singular() >>> s.rand_seed() # random 365260051
- read(filename)[source]#
EXAMPLES:
sage: filename = tmp_filename() sage: f = open(filename, 'w') sage: _ = f.write('x = 2\n') sage: f.close() sage: octave.read(filename) # optional - octave sage: octave.get('x') # optional - octave ' 2' sage: import os sage: os.unlink(filename)
>>> from sage.all import * >>> filename = tmp_filename() >>> f = open(filename, 'w') >>> _ = f.write('x = 2\n') >>> f.close() >>> octave.read(filename) # optional - octave >>> octave.get('x') # optional - octave ' 2' >>> import os >>> os.unlink(filename)
- set_seed(seed=None)[source]#
Set the random seed for the interpreter and return the new value of the seed.
This is dependent on which interpreter so must be implemented in each separately. For examples see gap.py or singular.py.
If seed is
None
then should generate a random seed.EXAMPLES:
sage: s = Singular() sage: s.set_seed(1) 1 sage: [s.random(1,10) for i in range(5)] [8, 10, 4, 9, 1] sage: from sage.interfaces.interface import Interface sage: i = Interface("") sage: i.set_seed() Traceback (most recent call last): ... NotImplementedError: This interpreter did not implement a set_seed function
>>> from sage.all import * >>> s = Singular() >>> s.set_seed(Integer(1)) 1 >>> [s.random(Integer(1),Integer(10)) for i in range(Integer(5))] [8, 10, 4, 9, 1] >>> from sage.interfaces.interface import Interface >>> i = Interface("") >>> i.set_seed() Traceback (most recent call last): ... NotImplementedError: This interpreter did not implement a set_seed function
- class sage.interfaces.interface.InterfaceElement(parent, value, is_name=False, name=None)[source]#
Bases:
Element
Interface element.
- attribute(attrname)[source]#
If this wraps the object x in the system, this returns the object x.attrname. This is useful for some systems that have object oriented attribute access notation.
EXAMPLES:
sage: g = gap('SO(1,4,7)') sage: k = g.InvariantQuadraticForm() sage: k.attribute('matrix') [ [ 0*Z(7), Z(7)^0, 0*Z(7), 0*Z(7) ], [ 0*Z(7), 0*Z(7), 0*Z(7), 0*Z(7) ], [ 0*Z(7), 0*Z(7), Z(7), 0*Z(7) ], [ 0*Z(7), 0*Z(7), 0*Z(7), Z(7)^0 ] ]
>>> from sage.all import * >>> g = gap('SO(1,4,7)') >>> k = g.InvariantQuadraticForm() >>> k.attribute('matrix') [ [ 0*Z(7), Z(7)^0, 0*Z(7), 0*Z(7) ], [ 0*Z(7), 0*Z(7), 0*Z(7), 0*Z(7) ], [ 0*Z(7), 0*Z(7), Z(7), 0*Z(7) ], [ 0*Z(7), 0*Z(7), 0*Z(7), Z(7)^0 ] ]
sage: e = gp('ellinit([0,-1,1,-10,-20])') sage: e.attribute('j') -122023936/161051
>>> from sage.all import * >>> e = gp('ellinit([0,-1,1,-10,-20])') >>> e.attribute('j') -122023936/161051
- bool()[source]#
Convert this element to a boolean.
EXAMPLES:
sage: singular(0).bool() False sage: singular(1).bool() True
>>> from sage.all import * >>> singular(Integer(0)).bool() False >>> singular(Integer(1)).bool() True
- get_using_file()[source]#
Return this element’s string representation using a file. Use this if self has a huge string representation. It’ll be way faster.
EXAMPLES:
sage: a = maxima(str(2^1000)) sage: a.get_using_file() '10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376'
>>> from sage.all import * >>> a = maxima(str(Integer(2)**Integer(1000))) >>> a.get_using_file() '10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376'
- hasattr(attrname)[source]#
Returns whether the given attribute is already defined by this object, and in particular is not dynamically generated.
EXAMPLES:
sage: m = maxima('2') sage: m.hasattr('integral') True sage: m.hasattr('gcd') False
>>> from sage.all import * >>> m = maxima('2') >>> m.hasattr('integral') True >>> m.hasattr('gcd') False
- name(new_name=None)[source]#
Returns the name of self. If new_name is passed in, then this function returns a new object identical to self whose name is new_name.
Note that this can overwrite existing variables in the system.
EXAMPLES:
sage: # optional - rpy2 sage: x = r([1,2,3]); x [1] 1 2 3 sage: x.name() 'sage...' sage: x = r([1,2,3]).name('x'); x [1] 1 2 3 sage: x.name() 'x'
>>> from sage.all import * >>> # optional - rpy2 >>> x = r([Integer(1),Integer(2),Integer(3)]); x [1] 1 2 3 >>> x.name() 'sage...' >>> x = r([Integer(1),Integer(2),Integer(3)]).name('x'); x [1] 1 2 3 >>> x.name() 'x'
sage: s5 = gap.SymmetricGroup(5).name('s5') sage: s5 SymmetricGroup( [ 1 .. 5 ] ) sage: s5.name() 's5'
>>> from sage.all import * >>> s5 = gap.SymmetricGroup(Integer(5)).name('s5') >>> s5 SymmetricGroup( [ 1 .. 5 ] ) >>> s5.name() 's5'
- sage(*args, **kwds)[source]#
Attempt to return a Sage version of this object.
This method does nothing more than calling
_sage_()
, simply forwarding any additional arguments.EXAMPLES:
sage: gp(1/2).sage() 1/2 sage: _.parent() Rational Field sage: singular.lib("matrix") sage: R = singular.ring(0, '(x,y,z)', 'dp') sage: singular.matrix(2,2).sage() [0 0] [0 0]
>>> from sage.all import * >>> gp(Integer(1)/Integer(2)).sage() 1/2 >>> _.parent() Rational Field >>> singular.lib("matrix") >>> R = singular.ring(Integer(0), '(x,y,z)', 'dp') >>> singular.matrix(Integer(2),Integer(2)).sage() [0 0] [0 0]
- class sage.interfaces.interface.InterfaceFunction(parent, name)[source]#
Bases:
SageObject
Interface function.
- class sage.interfaces.interface.InterfaceFunctionElement(obj, name)[source]#
Bases:
SageObject
Interface function element.
- sage.interfaces.interface.is_InterfaceElement(x)[source]#
Return True if
x
is of typeInterfaceElement
.EXAMPLES:
sage: from sage.interfaces.interface import is_InterfaceElement sage: is_InterfaceElement(2) doctest:...: DeprecationWarning: the function is_InterfaceElement is deprecated; use isinstance(x, sage.interfaces.abc.InterfaceElement) instead See https://github.com/sagemath/sage/issues/34804 for details. False
>>> from sage.all import * >>> from sage.interfaces.interface import is_InterfaceElement >>> is_InterfaceElement(Integer(2)) doctest:...: DeprecationWarning: the function is_InterfaceElement is deprecated; use isinstance(x, sage.interfaces.abc.InterfaceElement) instead See https://github.com/sagemath/sage/issues/34804 for details. False