Ask Your Question
2

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)
dg

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

dg.subs(g==h*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).

Thanks

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
3

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)
dgx

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

m(x)=h(x)*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?

Thanks

edit flag offensive delete link more
2

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

g(x)=function('g',x)
h=function('h',x)
function('j',x)
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'

Output:

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

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

Comments

1

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
0

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)
dg
sage output: D[0](g)(x)

dg.subs(g==h*x)
sage output: D[0](g)(x)
edit flag offensive delete link more

Comments

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

Stats

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

Seen: 3,068 times

Last updated: Apr 07 '13