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.Sat, 24 Mar 2012 21:51:48 +0100How to dynamically substitute a variable in a callable function?https://ask.sagemath.org/question/8825/how-to-dynamically-substitute-a-variable-in-a-callable-function/Hi guys, this is a little problem I came across a week ago.
I'm trying to define a python function that accepts:<br>
1. a callable sage function (of potentially more than one variable) as the first argument (henceforth called func), and <br>
2. a symbolic variable as the second argument (henceforth called xsub)
My function then needs to define a dummy symbolic variable (t), and substitute xsub with t in func. <br>I can do this for one variable equations in current 4.8 sagemath, by ignoring the new substitution syntax, but it throws up a depreciation warning (Which I'm assuming will become an error in 5.0).
Here's what my code looks like:
<pre>def fracintegral(func,xsub,n,a=0):
var('t')
assume(x>a)
assume(t>a)
return integrate((x-t)^(n-1)*func(t),t,a,x)</pre>
The last line should look something like:<pre>
return integrate((x-t)^(n-1)*func(x=t),t,a,x)</pre>
in order to avoid Depreciation Warnings, but this hardcodes x as the variable to be substituted. (Useless if my func is a y function.)
If I try:<pre>
return integrate((x-t)^(n-1)*func(xsub=t),t,a,x)</pre>
then substitution of func(x=t) doesn't occur (and the integrate function effectively treats func as a constant with respect to dt).
Trying:<pre>
return integrate((x-t)^(n-1)*func.subs(xsub==t),t,a,x)</pre>
doesn't work either, same result as func(xsub=t).
So, any idea how a function can accept a symbolic variable, and dynamically substitute out that variable in a callable function?JoalHeagneySat, 24 Mar 2012 21:51:48 +0100https://ask.sagemath.org/question/8825/