ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 07 Dec 2018 02:09:46 +0100Substitute differential operators in an expression.https://ask.sagemath.org/question/44598/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?
Thu, 06 Dec 2018 16:15:21 +0100https://ask.sagemath.org/question/44598/substitute-differential-operators-in-an-expression/Answer by nbruin for <p>Let's say I am taking derivatives of an expression involving unknow function.</p>
<pre><code>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
</code></pre>
<p>This gives the output</p>
<pre><code>(a + b)*x^(a + b - 1)*D[0](h)(x^(a + b))
</code></pre>
<p>How do I replace <code>D[0](h)(x^(a + b))</code> with something like <code>direvative_of_h(x^(a + b))</code>?
In this simple case, I can just do this manually using <code>.operands</code>, but if I have a rather complicated equation, how I can I do it?</p>
https://ask.sagemath.org/question/44598/substitute-differential-operators-in-an-expression/?answer=44605#post-id-44605Ideally 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.Fri, 07 Dec 2018 02:09:46 +0100https://ask.sagemath.org/question/44598/substitute-differential-operators-in-an-expression/?answer=44605#post-id-44605