# Substitute differential operators in an expression.

Let's say I am taking derivatives of an expression involving unknow function.

var('x,a,b');

f=(x^(a+b)).function(x,a,b);

h=function('h',nargs=1)(x);

g=h(f(x,a,b));

dg=diff(g,x);

dg


This gives the output

(a + b)*x^(a + b - 1)*D[0](h)(x^(a + b))


How do I replace D[0](h)(x^(a + b)) with something like direvative_of_h(x^(a + b))? In this simple case, I can just do this manually using .operands, but if I have a rather complicated equation, how I can I do it?

edit retag close merge delete

Sort by ยป oldest newest most voted

Ideally you should be able to do something like

Dh=h(x).diff(x).operator()
dg.substitute_function(Dh,f)


but unfortunately that doesn't work. I think it's a bug that it doesn't. Perhaps it can be fixed in the future.

A workaround is to define your function h so that it knows what its derivative is. For instance, you could just define h to be an antiderivative:

hprime=function('hprime')
h=hprime(x).integrate(x).function(x)


Then you can just execute your original code:

g=h(f(x,a,b));
dg=diff(g,x);


and then dg will be:

(a + b)*x^(a + b - 1)*hprime(x^(a + b))


Alternatively, you could define g as a symbolic function that knows what its derivative is called. This is a little technical and poorly documented, because it's probably intended for internal use:

hprime=function('hprime')
h=function('h',derivative_func=lambda self,*args,**kwargs: hprime(*args))


This produces the same result for dg, while preserving a concise form for g.

more