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.AsciiArtString

Bases: str

class sage.interfaces.interface.Interface(name)

Bases: sage.misc.fast_methods.WithEqualityById, sage.structure.parent_base.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.

call(function_name, *args, **kwds)
clear(var)

Clear the variable named var.

console()
cputime()

CPU time since this process started running.

eval(code, **kwds)

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.

execute(*args, **kwds)
function_call(function, args=None, kwds=None)

EXAMPLES:

sage: maxima.quad_qags(x, x, 0, 1, epsrel=1e-4)
[0.5,0.55511151231257...e-14,21,0]
sage: maxima.function_call('quad_qags', [x, x, 0, 1], {'epsrel':'1e-4'})
[0.5,0.55511151231257...e-14,21,0]
get(var)

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

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
get_using_file(var)

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.

help(s)
interact()

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.

name(new_name=None)
new(code)
rand_seed()

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
318491487L

sage: s = Singular()
sage: s.rand_seed() # random
365260051L
read(filename)

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)
set(var, value)

Set the variable var to the given value.

set_seed(seed=None)

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
class sage.interfaces.interface.InterfaceElement(parent, value, is_name=False, name=None)

Bases: sage.structure.element.Element

Interface element.

attribute(attrname)

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 ] ]
sage: e = gp('ellinit([0,-1,1,-10,-20])')
sage: e.attribute('j')
-122023936/161051
bool()

Return whether this element is equal to True.

NOTE:

This method needs to be overridden if the subprocess would not return a string representation of a boolean value unless an explicit print command is used.

EXAMPLES:

sage: singular(0).bool()
False
sage: singular(1).bool()
True
gen(n)
get_using_file()

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'
hasattr(attrname)

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

Tell whether this element is a string.

By default, the answer is negative.

name(new_name=None)

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: 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'
sage: s5 = gap.SymmetricGroup(5).name('s5')
sage: s5
SymmetricGroup( [ 1 .. 5 ] )
sage: s5.name()
's5'
sage(*args, **kwds)

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]
class sage.interfaces.interface.InterfaceFunction(parent, name)

Bases: sage.structure.sage_object.SageObject

Interface function.

class sage.interfaces.interface.InterfaceFunctionElement(obj, name)

Bases: sage.structure.sage_object.SageObject

Interface function element.

help()
sage.interfaces.interface.is_InterfaceElement(x)