ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 30 Mar 2015 13:15:49 -0500Handle variable namehttp://ask.sagemath.org/question/26382/handle-variable-name/Dear all,
I would like to pass a function as a parameter and also one of its variable. The simplest situation would just to evaluate a function f.
def test(f,myvar,myval):
return f(myvar=myval)
f = x^2
print(test(f,x,3)
It returns `x^2`, but I would like `9`. Of course I don't know a priori what would be the name of the variable and f may have several variables.
The long question is that I would like to define something that `plot(f,(x,xmin,xmax))`. Is there's a standard way to handle this triple `(x,xmin,xmax)`? Is there's a web page for the source code for such functions to study the standard implementation?
Thanks a lot!
Arnaud
Mon, 30 Mar 2015 08:41:21 -0500http://ask.sagemath.org/question/26382/handle-variable-name/Answer by rws for <p>Dear all,</p>
<p>I would like to pass a function as a parameter and also one of its variable. The simplest situation would just to evaluate a function f. </p>
<pre><code>def test(f,myvar,myval):
return f(myvar=myval)
f = x^2
print(test(f,x,3)
</code></pre>
<p>It returns <code>x^2</code>, but I would like <code>9</code>. Of course I don't know a priori what would be the name of the variable and f may have several variables.</p>
<p>The long question is that I would like to define something that <code>plot(f,(x,xmin,xmax))</code>. Is there's a standard way to handle this triple <code>(x,xmin,xmax)</code>? Is there's a web page for the source code for such functions to study the standard implementation?</p>
<p>Thanks a lot!
Arnaud</p>
http://ask.sagemath.org/question/26382/handle-variable-name/?answer=26386#post-id-26386As to the first question, use ``subs``:
sage: def test(f,myvar,myval):
return f.subs(myvar==myval)
....:
sage: f = x^2
sage: print(test(f,x,3))
9
In general, Sage comes with source, see https://github.com/sagemath/sage
Regarding the ``plot`` question, the triple is a standard Python tuple. I would
need a more specific question to elaborate further.Mon, 30 Mar 2015 11:06:31 -0500http://ask.sagemath.org/question/26382/handle-variable-name/?answer=26386#post-id-26386Answer by Arnaud1418 for <p>Dear all,</p>
<p>I would like to pass a function as a parameter and also one of its variable. The simplest situation would just to evaluate a function f. </p>
<pre><code>def test(f,myvar,myval):
return f(myvar=myval)
f = x^2
print(test(f,x,3)
</code></pre>
<p>It returns <code>x^2</code>, but I would like <code>9</code>. Of course I don't know a priori what would be the name of the variable and f may have several variables.</p>
<p>The long question is that I would like to define something that <code>plot(f,(x,xmin,xmax))</code>. Is there's a standard way to handle this triple <code>(x,xmin,xmax)</code>? Is there's a web page for the source code for such functions to study the standard implementation?</p>
<p>Thanks a lot!
Arnaud</p>
http://ask.sagemath.org/question/26382/handle-variable-name/?answer=26387#post-id-26387Compiling the answers above, here is a simple mean value function taking as parameters (i) a function and (ii) a triple defining the variable and the min and max values.
def meanvalue(f,mytriple):
myvar,mymin,mymax = mytriple
return f.subs(myvar==(mymin+mymax)/2)
var('z')
f = z^2
meanvalue(f,(z,0,1))
Thanks to everyone for answers!Mon, 30 Mar 2015 13:15:49 -0500http://ask.sagemath.org/question/26382/handle-variable-name/?answer=26387#post-id-26387Answer by calc314 for <p>Dear all,</p>
<p>I would like to pass a function as a parameter and also one of its variable. The simplest situation would just to evaluate a function f. </p>
<pre><code>def test(f,myvar,myval):
return f(myvar=myval)
f = x^2
print(test(f,x,3)
</code></pre>
<p>It returns <code>x^2</code>, but I would like <code>9</code>. Of course I don't know a priori what would be the name of the variable and f may have several variables.</p>
<p>The long question is that I would like to define something that <code>plot(f,(x,xmin,xmax))</code>. Is there's a standard way to handle this triple <code>(x,xmin,xmax)</code>? Is there's a web page for the source code for such functions to study the standard implementation?</p>
<p>Thanks a lot!
Arnaud</p>
http://ask.sagemath.org/question/26382/handle-variable-name/?answer=26385#post-id-26385 Here is a way to pass the triple you mentioned.
def f(*params):
p=plot(x^2,*params)
return(p)
interv=(x,-3,3)
show(f(interv) )
Mon, 30 Mar 2015 11:06:11 -0500http://ask.sagemath.org/question/26382/handle-variable-name/?answer=26385#post-id-26385Answer by tmonteil for <p>Dear all,</p>
<p>I would like to pass a function as a parameter and also one of its variable. The simplest situation would just to evaluate a function f. </p>
<pre><code>def test(f,myvar,myval):
return f(myvar=myval)
f = x^2
print(test(f,x,3)
</code></pre>
<p>It returns <code>x^2</code>, but I would like <code>9</code>. Of course I don't know a priori what would be the name of the variable and f may have several variables.</p>
<p>The long question is that I would like to define something that <code>plot(f,(x,xmin,xmax))</code>. Is there's a standard way to handle this triple <code>(x,xmin,xmax)</code>? Is there's a web page for the source code for such functions to study the standard implementation?</p>
<p>Thanks a lot!
Arnaud</p>
http://ask.sagemath.org/question/26382/handle-variable-name/?answer=26384#post-id-26384You could try with passing a dictionary to the `.subs()` method:
sage: def test(f,myvar,myval):
sage: return f.subs({myvar:myval})
sage: print(test(f,x,3))
9
Mon, 30 Mar 2015 11:01:47 -0500http://ask.sagemath.org/question/26382/handle-variable-name/?answer=26384#post-id-26384