Ask Your Question

Function returning graphic object

asked 2015-04-07 07:57:27 -0500

TL gravatar image

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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-04-07 08:24:24 -0500

tmonteil gravatar image

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))
edit flag offensive delete link more


That seems to work, thanks !

TL gravatar imageTL ( 2015-04-07 08:35:40 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2015-04-07 07:57:27 -0500

Seen: 98 times

Last updated: Apr 07 '15