12.07.2015 Views

Sage Developer's Guide - Mirrors

Sage Developer's Guide - Mirrors

Sage Developer's Guide - Mirrors

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Sage</strong> Developer’s <strong>Guide</strong>, Release 6.1.1return self.am()else:return self.am().change_ring(R)def adjacency_matrix(self, sparse=None, boundary_first=False):...Similarly, provide a _vector_ method for an object that can be coerced to a vector over a ringR. Then the <strong>Sage</strong> function vector will work for this object. The following is from the fileSAGE_ROOT/sage/sage/modules/free_module_element.pyx:cdef class FreeModuleElement(element_Vector):...def _vector_(self, R):return self.change_ring(R)# abstract base class4.2.3 <strong>Sage</strong> PreparsingTo make Python even more usable interactively, there are a number of tweaks to the syntax made when you use<strong>Sage</strong> from the commandline or via the notebook (but not for Python code in the <strong>Sage</strong> library). Technically, this isimplemented by a preparse() function that rewrites the input string. Most notably, the following replacements aremade:• <strong>Sage</strong> supports a special syntax for generating rings or, more generally, parents with named generators:sage: R. = QQ[]sage: preparse(’R. = QQ[]’)"R = QQ[’x, y’]; (x, y,) = R._first_ngens(2)"• Integer and real literals are <strong>Sage</strong> integers and <strong>Sage</strong> floating point numbers. For example, in pure Python thesewould be an attribute error:sage: 16.sqrt()4sage: 87.factor()3 * 29• Raw literals are not preparsed, which can be useful from an efficiency point of view. Just like Python ints aredenoted by an L, in <strong>Sage</strong> raw integer and floating literals are followed by an “r” (or “R”) for raw, meaning notpreparsed. For example:sage: a = 393939rsage: a393939sage: type(a)sage: b = 393939sage: type(b)sage: a == bTrue• Raw literals can be very useful in certain cases. For instance, Python integers can be more efficient than <strong>Sage</strong>integers when they are very small. Large <strong>Sage</strong> integers are much more efficient than Python integers since theyare implemented using the GMP C library.Consult the file preparser.py for more details about <strong>Sage</strong> preparsing, more examples involving raw literals, etc.4.2. Coding in Python for <strong>Sage</strong> 35

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!