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, 17 Sep 2013 08:09:08 +0200How 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?Sat, 24 Mar 2012 21:51:48 +0100https://ask.sagemath.org/question/8825/how-to-dynamically-substitute-a-variable-in-a-callable-function/Comment by kcrisman for <p>Hi guys, this is a little problem I came across a week ago.</p>
<p>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)</p>
<p>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).</p>
<p>Here's what my code looks like:</p>
<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>
<p>The last line should look something like:</p><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.)<p></p>
<p>If I try:</p><pre> return integrate((x-t)^(n-1)*func(xsub=t),t,a,x)</pre><p></p>
<p>then substitution of func(x=t) doesn't occur (and the integrate function effectively treats func as a constant with respect to dt).</p>
<p>Trying:</p><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).<p></p>
<p>So, any idea how a function can accept a symbolic variable, and dynamically substitute out that variable in a callable function?</p>
https://ask.sagemath.org/question/8825/how-to-dynamically-substitute-a-variable-in-a-callable-function/?comment=20044#post-id-20044On an unrelated note, you may want to see if there is a way to do this without redeclaring `t` each time. Does that remove the assumptions on `t`? You can debug this with `assumptions()`. My first guess would be that it doesn't... but I could be wrong. Even after having spent a lot of time with our assumptions code, I still don't fully understand how it interacts with Maxima.Mon, 26 Mar 2012 23:25:42 +0200https://ask.sagemath.org/question/8825/how-to-dynamically-substitute-a-variable-in-a-callable-function/?comment=20044#post-id-20044Answer by DSM for <p>Hi guys, this is a little problem I came across a week ago.</p>
<p>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)</p>
<p>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).</p>
<p>Here's what my code looks like:</p>
<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>
<p>The last line should look something like:</p><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.)<p></p>
<p>If I try:</p><pre> return integrate((x-t)^(n-1)*func(xsub=t),t,a,x)</pre><p></p>
<p>then substitution of func(x=t) doesn't occur (and the integrate function effectively treats func as a constant with respect to dt).</p>
<p>Trying:</p><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).<p></p>
<p>So, any idea how a function can accept a symbolic variable, and dynamically substitute out that variable in a callable function?</p>
https://ask.sagemath.org/question/8825/how-to-dynamically-substitute-a-variable-in-a-callable-function/?answer=13391#post-id-13391Are you thinking of something like this?
def fracintegral(func,xsub,n,a=0):
t = var('t')
assume(x>a)
assume(t>a)
return integrate((x-t)^(n-1)*func.subs({xsub:t}),t,a,x)
which gives a deprecation-free
sage: var("x y")
(x, y)
sage: f1 = function("f", x)
sage: fracintegral(f1, x, 4)
-integrate((t - x)^3*f(t), t, 0, x)
sage: f2 = function("f", x, y)
sage: fracintegral(f2, y, 4)
-integrate((t - x)^3*f(x, t), t, 0, x)
sage: f3(x,y) = x^2+sin(y)
sage: fracintegral(f3, y, 4)
(x, y) |--> 1/4*x^6 + x^3 - 6*x + 6*sin(x)
Sat, 24 Mar 2012 23:23:29 +0100https://ask.sagemath.org/question/8825/how-to-dynamically-substitute-a-variable-in-a-callable-function/?answer=13391#post-id-13391Comment by Szilard SZALAY for <p>Are you thinking of something like this?</p>
<pre><code>def fracintegral(func,xsub,n,a=0):
t = var('t')
assume(x>a)
assume(t>a)
return integrate((x-t)^(n-1)*func.subs({xsub:t}),t,a,x)
</code></pre>
<p>which gives a deprecation-free</p>
<pre><code>sage: var("x y")
(x, y)
sage: f1 = function("f", x)
sage: fracintegral(f1, x, 4)
-integrate((t - x)^3*f(t), t, 0, x)
sage: f2 = function("f", x, y)
sage: fracintegral(f2, y, 4)
-integrate((t - x)^3*f(x, t), t, 0, x)
sage: f3(x,y) = x^2+sin(y)
sage: fracintegral(f3, y, 4)
(x, y) |--> 1/4*x^6 + x^3 - 6*x + 6*sin(x)
</code></pre>
https://ask.sagemath.org/question/8825/how-to-dynamically-substitute-a-variable-in-a-callable-function/?comment=17008#post-id-17008This solution is working for subs() but not for limit(). Could you give us please some explanation or link about what is really going in the background?Tue, 17 Sep 2013 08:09:08 +0200https://ask.sagemath.org/question/8825/how-to-dynamically-substitute-a-variable-in-a-callable-function/?comment=17008#post-id-17008Comment by JoalHeagney for <p>Are you thinking of something like this?</p>
<pre><code>def fracintegral(func,xsub,n,a=0):
t = var('t')
assume(x>a)
assume(t>a)
return integrate((x-t)^(n-1)*func.subs({xsub:t}),t,a,x)
</code></pre>
<p>which gives a deprecation-free</p>
<pre><code>sage: var("x y")
(x, y)
sage: f1 = function("f", x)
sage: fracintegral(f1, x, 4)
-integrate((t - x)^3*f(t), t, 0, x)
sage: f2 = function("f", x, y)
sage: fracintegral(f2, y, 4)
-integrate((t - x)^3*f(x, t), t, 0, x)
sage: f3(x,y) = x^2+sin(y)
sage: fracintegral(f3, y, 4)
(x, y) |--> 1/4*x^6 + x^3 - 6*x + 6*sin(x)
</code></pre>
https://ask.sagemath.org/question/8825/how-to-dynamically-substitute-a-variable-in-a-callable-function/?comment=19967#post-id-19967Maybe something to do with hashing of the xsub key in the dictionary?Mon, 09 Apr 2012 23:26:04 +0200https://ask.sagemath.org/question/8825/how-to-dynamically-substitute-a-variable-in-a-callable-function/?comment=19967#post-id-19967Comment by kcrisman for <p>Are you thinking of something like this?</p>
<pre><code>def fracintegral(func,xsub,n,a=0):
t = var('t')
assume(x>a)
assume(t>a)
return integrate((x-t)^(n-1)*func.subs({xsub:t}),t,a,x)
</code></pre>
<p>which gives a deprecation-free</p>
<pre><code>sage: var("x y")
(x, y)
sage: f1 = function("f", x)
sage: fracintegral(f1, x, 4)
-integrate((t - x)^3*f(t), t, 0, x)
sage: f2 = function("f", x, y)
sage: fracintegral(f2, y, 4)
-integrate((t - x)^3*f(x, t), t, 0, x)
sage: f3(x,y) = x^2+sin(y)
sage: fracintegral(f3, y, 4)
(x, y) |--> 1/4*x^6 + x^3 - 6*x + 6*sin(x)
</code></pre>
https://ask.sagemath.org/question/8825/how-to-dynamically-substitute-a-variable-in-a-callable-function/?comment=20045#post-id-20045Clever. So why doesn't `func.subs(xsub=t)` work, since that should be functionally equivalent?Mon, 26 Mar 2012 23:24:14 +0200https://ask.sagemath.org/question/8825/how-to-dynamically-substitute-a-variable-in-a-callable-function/?comment=20045#post-id-20045