ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 07 Apr 2015 15:35:40 +0200Function returning graphic objecthttps://ask.sagemath.org/question/26450/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 ?**Tue, 07 Apr 2015 14:57:27 +0200https://ask.sagemath.org/question/26450/function-returning-graphic-object/Answer by tmonteil for <p>I have a function that I want to plot, to be exact that function is like:</p>
<pre><code>dn(k, n, theta, y) = ...
</code></pre>
<p>I want to plot it for <code>k</code>, with all other parameters fixed. What seemed the most logic to me was to have a function returning it:</p>
<pre><code>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)
</code></pre>
<p><code>plot_dn</code> should be a function creating a graphic object, not a graphic object (ie, <code>plot</code> should be called only when <code>plot_dn</code> 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 <code>ValueError: Variable 'k' not found</code>.</p>
<p>If I put the function in a list, the error changes:</p>
<pre><code>plot_dn(n, theta, y) = plot([dn(k, n, theta, y)], (k, 0, 1))
# Error is: TypeError: unable to simplify to float approximation
</code></pre>
<p><strong>How can I make it work ?</strong></p>
https://ask.sagemath.org/question/26450/function-returning-graphic-object/?answer=26452#post-id-26452The 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))
Tue, 07 Apr 2015 15:24:24 +0200https://ask.sagemath.org/question/26450/function-returning-graphic-object/?answer=26452#post-id-26452Comment by TL for <p>The way you define <code>dn</code> is a <em>symbolic function</em>, that is an expression given by a mathematical formula, not something that produces an object.</p>
<p>For <code>plot_dn</code> if you want to produce a graphical object, you need to define is a Python function, not a symbolic. You can try the following:</p>
<pre><code>sage: plot_dn = lambda n, theta, y : plot(dn(k, n, theta, y), (k, 0, 1))
</code></pre>
<p>It is equivalent to:</p>
<pre><code>sage: def plot_dn(n, theta, y):
....: return plot(dn(k, n, theta, y), (k, 0, 1))
</code></pre>
https://ask.sagemath.org/question/26450/function-returning-graphic-object/?comment=26454#post-id-26454That seems to work, thanks !Tue, 07 Apr 2015 15:35:40 +0200https://ask.sagemath.org/question/26450/function-returning-graphic-object/?comment=26454#post-id-26454