Ask Your Question
2

Substitute differential operators in an expression.

asked 2018-12-06 09:15:21 -0600

ablmf gravatar image

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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2018-12-06 19:09:46 -0600

nbruin gravatar image

updated 2018-12-06 19:19:41 -0600

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.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2018-12-06 09:15:21 -0600

Seen: 52 times

Last updated: Dec 06