Abstract base class for Sage objects¶
Base class for all (user-visible) objects in Sage
Every object that can end up being returned to the user should inherit from
Return an ASCII art representation.
To implement multi-line ASCII art output in a derived class you must override this method. Unlike
_repr_(), which is sometimes used for the hash key, the output of
_ascii_art_()may depend on settings and is allowed to change during runtime.
You can use the
ascii_art()function to get the ASCII art representation of any object in Sage:
sage: ascii_art(integral(exp(x+x^2)/(x+1), x)) / | | 2 | x + x | e | ------- dx | x + 1 | /
Alternatively, you can use the
%display ascii_art/simplemagic to switch all output to ASCII art and back:
sage: from sage.repl.interpreter import get_test_shell sage: shell = get_test_shell() sage: shell.run_cell('tab = StandardTableaux(3); tab') [[1, 2], ] sage: shell.run_cell('%display ascii_art') sage: shell.run_cell('tab') 1 2 3 sage: shell.run_cell('Tableaux.options(ascii_art="table", convention="French")') sage: shell.run_cell('tab') +---+ | 3 | +---+---+ | 1 | 2 | +---+---+ sage: shell.run_cell('%display plain') sage: shell.run_cell('Tableaux.options._reset()') sage: shell.quit()
Return a hashable key which identifies this objects for caching. The output must be hashable itself, or a tuple of objects which are hashable or define a
This method will only be called if the object itself is not hashable.
Some immutable objects (such as \(p\)-adic numbers) cannot implement a reasonable hash function because their
==operator has been modified to return
Truefor objects which might behave differently in some computations:
sage: K.<a> = Qq(9) sage: b = a + O(3) sage: c = a + 3 sage: b a + O(3) sage: c a + 3 + O(3^20) sage: b == c True sage: b == a True sage: c == a False
If such objects defined a non-trivial hash function, this would break caching in many places. However, such objects should still be usable in caches. This can be achieved by defining an appropriate
sage: hash(b) Traceback (most recent call last): ... TypeError: unhashable type: 'sage.rings.padics.qadic_flint_CR.qAdicCappedRelativeElement' sage: @cached_method ....: def f(x): return x==a sage: f(b) True sage: f(c) # if b and c were hashable, this would return True False sage: b._cache_key() (..., ((0, 1),), 0, 1) sage: c._cache_key() (..., ((0, 1), (1,)), 0, 20)
An implementation must make sure that for elements
a != b, then also
a._cache_key() != b._cache_key(). In practice this means that the
_cache_keyshould always include the parent as its first argument:
sage: S.<a> = Qq(4) sage: d = a + O(2) sage: b._cache_key() == d._cache_key() # this would be True if the parents were not included False
Same as self.save(filename, compress)
selfto a string
s, which can later be reconstituted as
There is an optional boolean argument
compresswhich defaults to
sage: from sage.misc.persist import comp sage: O = SageObject() sage: p_comp = O.dumps() sage: p_uncomp = O.dumps(compress=False) sage: comp.decompress(p_comp) == p_uncomp True sage: import pickletools sage: pickletools.dis(p_uncomp) 0: \x80 PROTO 2 2: c GLOBAL 'sage.structure.sage_object SageObject' 41: q BINPUT ... 43: ) EMPTY_TUPLE 44: \x81 NEWOBJ 45: q BINPUT ... 47: . STOP highest protocol among opcodes = 2
Return the type of
selfto support the coercion framework.
sage: t = log(sqrt(2) - 1) + log(sqrt(2) + 1); t log(sqrt(2) + 1) + log(sqrt(2) - 1) sage: u = t.maxima_methods() sage: u.parent() <class 'sage.symbolic.maxima_wrapper.MaximaWrapper'>
Change self so it prints as x, where x is a string.
This is only supported for Python classes that derive from SageObject.
sage: x = PolynomialRing(QQ, 'x', sparse=True).gen() sage: g = x^3 + x - 5 sage: g x^3 + x - 5 sage: g.rename('a polynomial') sage: g a polynomial sage: g + x x^3 + 2*x - 5 sage: h = g^100 sage: str(h)[:20] 'x^300 + 100*x^298 - ' sage: h.rename('x^300 + ...') sage: h x^300 + ...
Real numbers are not Python classes, so rename is not supported:
sage: a = 3.14 sage: type(a) <... 'sage.rings.real_mpfr.RealLiteral'> sage: a.rename('pi') Traceback (most recent call last): ... NotImplementedError: object does not support renaming: 3.14000000000000
The reason C-extension types are not supported by default is if they were then every single one would have to carry around an extra attribute, which would be slower and waste a lot of memory.
To support them for a specific class, add a
cdef public __custom_nameattribute.
Remove the custom name of an object.
sage: P.<x> = QQ sage: P Univariate Polynomial Ring in x over Rational Field sage: P.rename('A polynomial ring') sage: P A polynomial ring sage: P.reset_name() sage: P Univariate Polynomial Ring in x over Rational Field
Save self to the given filename.
sage: f = x^3 + 5 sage: f.save(os.path.join(SAGE_TMP, 'file')) sage: load(os.path.join(SAGE_TMP, 'file.sobj')) x^3 + 5