Ask Your Question

Revision history [back]

Suppose we have an ordinary function

sage: def f(x,y,z):
....:     return x^2 - y + z^3

or (which is the same, but shorter)

sage: f = lambda x,y,z: x^2 - y + z^3

Evaluating the function at arguments of the desired type, we can work with the result, e.g. symbolically:

sage: var('x,y,z')
sage: f(x,y,z).coefficient(y)
-1

or with polynomials:

sage: R.<x,y,z> = QQ[]
sage: f(x,y,z).coefficient({y : 1})
-1

However, the latter variant does not work when f is instead a SageMath callable symbolic expression:

sage: f(x,y,z) = x^2 - y + z^3

because it will always output a symbolic expression. So in order to work with polynomials, you have to do:

sage: R(f(x,y,z)).coefficient({y : 1})
-1

instead (or better: define an ordinary function, as above).

In my opinion, this is one of the reasons why having this notation for callable symbolic expressions is bad.

Side note: in SageMath it is often very reasonable to use expressions instead of functions. Many facilities are designed around this, accepting an expression and a list of variables, so the expression will be interpreted as a function of the specified variables. For example, to take a derivative with diff you can specify the variable with respect to which you want to differentiate.