Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The normal way to define a function in Sagemath is exactly the same as in Python :

sage: def myplot1(r):
....:     return plot(x^r, (x, -1,1))
....:

Using the lambda syntax is a shortcut fulfilling the same goal for functions returning the result of the expression of a single Python (Sage) expression :

sage: myplot2 = lambda r: plot(x^r, (x, -1, 1))

An important Sage-specific way to define symbolic functions (a. k. a. callable symbolic expressions) can be used to define functions taking symbolic arguments and returning symbolic expressions :

sympsimp(x) = x._sympy_().simplify()._sage_()

Other, more exotic ways of defining functions include :

  • Cython (Python code +°optional type declarations, compiled to C code) ;

  • C/Fortran library code with interface to the Python interpreter.

But these ways need more work and are more useful for library/package development or Sage extension.