# Revision history [back]

It would help if you'd post the code you are having problems with. I don't experience the problem you seem to run into:

sage: var('a,b,c')
(a, b, c)
sage: function('f')
f
sage: function('g')
g
sage: E=a*f(x)+b*diff(f(x),x)+c*diff(f(x),x,x)
sage: E
a*f(x) + b*D(f)(x) + c*D[0, 0](f)(x)
sage: E.substitute_function(f,g)
a*g(x) + b*D(g)(x) + c*D[0, 0](g)(x)


f gets beautifully replaced by g. It seems from your question that you might be trying

sage: E.substitute_function(f(x),g(x))
a*f(x) + b*D(f)(x) + c*D[0, 0](f)(x)


which is not according to the documentation of substitute_function and indeed doesn't work: the arguments should be functions, not expressions.

It would help if you'd post the code you are having problems with. I don't experience the problem you seem to run into:

sage: var('a,b,c')
(a, b, c)
sage: function('f')
f
sage: function('g')
g
sage: E=a*f(x)+b*diff(f(x),x)+c*diff(f(x),x,x)
sage: E
a*f(x) + b*D(f)(x) + c*D[0, 0](f)(x)
sage: E.substitute_function(f,g)
a*g(x) + b*D(g)(x) + c*D[0, 0](g)(x)


f gets beautifully replaced by g. It seems from your question that you might be trying

sage: E.substitute_function(f(x),g(x))
a*f(x) + b*D(f)(x) + c*D[0, 0](f)(x)


which is not according to the documentation of substitute_function and indeed doesn't work: the arguments should be functions, not expressions.

edit: Thank you for clarifying (editing your question would have been better than posting the additional information as an answer)

You're a victim of erroneous documentation. The command f=function('f',x) doesn't give you a function, it gives you an expression "function f evaluated at x". This is misused in a lot of places, particularly the DE code. It means that the argument that you give to substitute_function isn't a function. Try and print it:

sage: f=function('f',x)
sage: f
f(x)
sage: f(x)
DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...)
See http://trac.sagemath.org/5930 for details.
f(x)


The DeprecationWarning is already an indication that typing in f(x) isn't what you'd expect it to be.

If instead you do the following, things works as you'd expect.

sage: var('x,a,b')
(x, a, b)
sage: function('f')(x)
f(x)
sage: assume(a>0); assume(b<0)
sage: eqn = a*diff(f(x),x,2) == b*f(x)
sage: g(x) = desolve_laplace(eqn, f(x), ivar=x)
sage: eqn.substitute_function(f,g)
b*cos(sqrt(-a*b)*x/a)*f(0) - sqrt(-a*b)*sin(sqrt(-a*b)*x/a)*D(f)(0) == (a^2*sin(sqrt(-a*b)*x/a)*D(f)(0)/sqrt(-a*b) + a*cos(sqrt(-a*b)*x/a)*f(0))*b/a


You just did what is in the documentation of desolve_laplace, but as you found out, the code there doesn't quite do what it claims, so that substitute_function doesn't work as expected. You're not the first one to fall in this trap. There are some tickets about it, see http://trac.sagemath.org/ticket/17447 for instance.

It would help if you'd post the code you are having problems with. I don't experience the problem you seem to run into:

sage: var('a,b,c')
(a, b, c)
sage: function('f')
f
sage: function('g')
g
sage: E=a*f(x)+b*diff(f(x),x)+c*diff(f(x),x,x)
sage: E
a*f(x) + b*D(f)(x) + c*D[0, 0](f)(x)
sage: E.substitute_function(f,g)
a*g(x) + b*D(g)(x) + c*D[0, 0](g)(x)


f gets beautifully replaced by g. It seems from your question that you might be trying

sage: E.substitute_function(f(x),g(x))
a*f(x) + b*D(f)(x) + c*D[0, 0](f)(x)


which is not according to the documentation of substitute_function and indeed doesn't work: the arguments should be functions, not expressions.

edit: Thank you for clarifying (editing your question would have been better than posting the additional information as an answer)

You're a victim of erroneous documentation. The command f=function('f',x) doesn't give you a function, it gives you an expression "function f evaluated at x". This is misused in a lot of places, particularly the DE code. It means that the argument that you give to substitute_function isn't a function. Try and print it:

sage: f=function('f',x)
f=function('f',x)   # This doesn't do what you might think it does. Don't use it.
sage: f
f(x)
sage: f(x)
DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...)
See http://trac.sagemath.org/5930 for details.
f(x)


The DeprecationWarning is already an indication that typing in f(x) isn't what you'd expect it to be.

If instead you do the following, things works as you'd expect.

sage: var('x,a,b')
(x, a, b)
sage: function('f')(x)
f(x)
sage: assume(a>0); assume(b<0)
sage: eqn = a*diff(f(x),x,2) == b*f(x)
sage: g(x) = desolve_laplace(eqn, f(x), ivar=x)
sage: eqn.substitute_function(f,g)
b*cos(sqrt(-a*b)*x/a)*f(0) - sqrt(-a*b)*sin(sqrt(-a*b)*x/a)*D(f)(0) == (a^2*sin(sqrt(-a*b)*x/a)*D(f)(0)/sqrt(-a*b) + a*cos(sqrt(-a*b)*x/a)*f(0))*b/a


You just did what is in the documentation of desolve_laplace, but as you found out, the code there doesn't quite do what it claims, so that substitute_function doesn't work as expected. You're not the first one to fall in this trap. There are some tickets about it, see http://trac.sagemath.org/ticket/17447 for instance.