Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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:
1. a callable sage function (of potentially more than one variable) as the first argument (henceforth called func), and
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.
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:

def fracintegral(func,xsub,n,a=0):
    return integrate((x-t)^(n-1)*func(t),t,a,x)

The last line should look something like:

    return integrate((x-t)^(n-1)*func(x=t),t,a,x)
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:

    return integrate((x-t)^(n-1)*func(xsub=t),t,a,x)

then substitution of func(x=t) doesn't occur (and the integrate function effectively treats func as a constant with respect to dt).


    return integrate((x-t)^(n-1)*func.subs(xsub==t),t,a,x)
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?