# Function returning graphic object

I have a function that I want to plot, to be exact that function is like:

dn(k, n, theta, y) = ...


I want to plot it for k, with all other parameters fixed. What seemed the most logic to me was to have a function returning it:

plot_dn(n, theta, y) = plot(dn(k, n, theta, y), (k, 0, 1))

# Idea: simplify plotting by just calling (for example):
plot_dn(100, 0.4, 0.5)
plot_dn(100, 0.6, 0.5)
plot_dn(100, 0.6, 0.8)


plot_dn should be a function creating a graphic object, not a graphic object (ie, plot should be called only when plot_dn is). Problem is that this throw a really large callstack (side point: a callstack is not an error report, sagemath should really have proper error reporting), which ends up with ValueError: Variable 'k' not found.

If I put the function in a list, the error changes:

plot_dn(n, theta, y) = plot([dn(k, n, theta, y)], (k, 0, 1))
# Error is: TypeError: unable to simplify to float approximation


How can I make it work ?

edit retag close merge delete

Sort by » oldest newest most voted

The way you define dn is a symbolic function, that is an expression given by a mathematical formula, not something that produces an object.

For plot_dn if you want to produce a graphical object, you need to define is a Python function, not a symbolic. You can try the following:

sage: plot_dn = lambda n, theta, y : plot(dn(k, n, theta, y), (k, 0, 1))


It is equivalent to:

sage: def plot_dn(n, theta, y):
....:     return plot(dn(k, n, theta, y), (k, 0, 1))

more