Ask Your Question

How to substitute a function within derivatives?

asked 2013-03-21 23:59:28 +0100

sgia gravatar image

updated 2015-07-31 17:58:15 +0100

FrédéricC gravatar image

I want to simplify an ODE by making a substitution, say g(x) -> h(x)*x, but can't get it to work. I tried:

g=function('g', x)
h=function('h', x)
dg = g.diff(x)

sage output: D[0](g)(x)


sage output: D[0](g)(x)

The substitution is not done for the g function within derivatives. I tried dg.subs(g(x)==h(x)*x) too, got deprecation warnings and the same results. How can I make this work? This is a simplified example, in reality, instead of dg, I have the lhs of an ODE defined interms of g(x).


edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted

answered 2013-04-07 00:25:45 +0100

sgia gravatar image

I figured this out. Sage's behavior is confusing to say the least. To make it work, I need the following:

gx=function('g', x)
dgx = gx.diff(x)

sage output: D[0](g)(x)

dgx.substitute_function(g, m)

sage output: x*D[0](h)(x) + h(x)

What's happening, as I understand, is:

  • function('g', x) has a side effect of creating a variable g with type : class 'sage.symbolic.function_factory.NewSymbolicFunction' which is important for my purpose, but the statement also returns an Express g(x). My original version assigned the returned Express to g, which overrided the NewSymbolicFunction that I need

  • I need to use .substitute_function() method instead of .subs(), and for that to work, I also need to first create another function m(x)

This seems unnecessarily complex and unintuitive. Is there a better way?


edit flag offensive delete link more

answered 2013-04-07 07:56:48 +0100

ndomes gravatar image

The following code shows the differences in type and parent of the names g , h , j and k

k = function('k')

L = [g,h,j,k]
for func in L: print func,' : ',type(func)
print '---'
for func in L: 
    try: print func,' : ', func.parent()
    except: print func, 'has no parent'


x |--> g(x)  :  <type 'sage.symbolic.expression.Expression'>
h(x)  :  <type 'sage.symbolic.expression.Expression'>
j  :  <class 'sage.symbolic.function_factory.NewSymbolicFunction'>
k  :  <class 'sage.symbolic.function_factory.NewSymbolicFunction'>
x |--> g(x)  :  Callable function ring with arguments (x,)
h(x)  :  Symbolic Ring
j  :  j has no parent
k  :  k has no parent

IMHO the following doesn't look complicated

z = h(x)*x^2
dg = g(x).diff(x)
dg ; dg.substitute_function(g,z)
edit flag offensive delete link more



This is highly confusing. I would have expected the last three things `h, j, k` to be of the same type.

ppurka gravatar imageppurka ( 2013-04-07 09:34:10 +0100 )edit

answered 2013-03-22 00:03:00 +0100

sgia gravatar image

The code does not show up correctly, trying again:

g=function('g', x)
h=function('h', x)
dg = g.diff(x)
sage output: D[0](g)(x)

sage output: D[0](g)(x)
edit flag offensive delete link more


fixed your question.

ppurka gravatar imageppurka ( 2013-03-22 04:46:13 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2013-03-21 23:59:28 +0100

Seen: 2,739 times

Last updated: Apr 07 '13