12.07.2015 Views

Sage Developer's Guide - Mirrors

Sage Developer's Guide - Mirrors

Sage Developer's Guide - Mirrors

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Sage</strong> Developer’s <strong>Guide</strong>, Release 6.1.14.2.1 DesignIf you are planning to develop some new code for <strong>Sage</strong>, design is important. So think about what your program will doand how that fits into the structure of <strong>Sage</strong>. In particular, much of <strong>Sage</strong> is implemented in the object-oriented languagePython, and there is a hierarchy of classes that organize code and functionality. For example, if you implement elementsof a ring, your class should derive from sage.structure.element.RingElement, rather than startingfrom scratch. Try to figure out how your code should fit in with other <strong>Sage</strong> code, and design it accordingly.4.2.2 Special <strong>Sage</strong> FunctionsFunctions with leading and trailing double underscores __XXX__ are all predefined by Python. Functions with leadingand trailing single underscores _XXX_ are defined for <strong>Sage</strong>. Functions with a single leading underscore are meant tobe semi-private, and those with a double leading underscore are considered really private. Users can create functionswith leading and trailing underscores.Just as Python has many standard special methods for objects, <strong>Sage</strong> also has special methods. They are typically ofthe form _XXX_. In a few cases, the trailing underscore is not included, but this will eventually be changed so that thetrailing underscore is always included. This section describes these special methods.All objects in <strong>Sage</strong> should derive from the Cython extension class <strong>Sage</strong>Object:from sage.ext.sage_object import <strong>Sage</strong>Objectclass MyClass(<strong>Sage</strong>Object,...):...or from some other already existing <strong>Sage</strong> class:from sage.rings.ring import Algebraclass MyFavoriteAlgebra(Algebra):...You should implement the _latex_ and _repr_ method for every object. The other methods depend on the natureof the object.LaTeX RepresentationEvery object x in <strong>Sage</strong> should support the command latex(x), so that any <strong>Sage</strong> object can be easily and accuratelydisplayed via LaTeX. Here is how to make a class (and therefore its instances) support the command latex.1. Define a method _latex_(self) that returns a LaTeX representation of your object. It should be somethingthat can be typeset correctly within math mode. Do not include opening and closing $’s.2. Often objects are built up out of other <strong>Sage</strong> objects, and these components should be typeset using the latexfunction. For example, if c is a coefficient of your object, and you want to typeset c using LaTeX, uselatex(c) instead of c._latex_(), since c might not have a _latex_ method, and latex(c) knowshow to deal with this.3. Do not forget to include a docstring and an example that illustrates LaTeX generation for your object.4. You can use any macros included in amsmath, amssymb, or amsfonts, or the ones defined inSAGE_ROOT/doc/commontex/macros.tex.An example template for a _latex_ method follows:4.2. Coding in Python for <strong>Sage</strong> 33

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

Saved successfully!

Ooh no, something went wrong!