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.Tue, 18 Jul 2017 14:55:53 +0200Collecting coefficients of derivativeshttps://ask.sagemath.org/question/38214/collecting-coefficients-of-derivatives/ I have a complicated expression involving derivatives of a function, and I'd like to collect all of the first, second, third, etc. derivatives. For example, my expression starts:
a1*a2*a3*x*f(x) + a1*a2*x^2*diff(f(x), x) + a1*a3*x^2*diff(f(x), x) + a2*a3*x^2*diff(f(x), x)
and I would like to rewrite that part as
a1*a2*a3*x*f(x) + (a1*a2*x^2*+ a1*a3*x^2+ a2*a3*x^2)*diff(f(x), x)
How do I tell Sage to do this?Mon, 10 Jul 2017 16:41:23 +0200https://ask.sagemath.org/question/38214/collecting-coefficients-of-derivatives/Answer by calc314 for <p>I have a complicated expression involving derivatives of a function, and I'd like to collect all of the first, second, third, etc. derivatives. For example, my expression starts:</p>
<pre><code>a1*a2*a3*x*f(x) + a1*a2*x^2*diff(f(x), x) + a1*a3*x^2*diff(f(x), x) + a2*a3*x^2*diff(f(x), x)
</code></pre>
<p>and I would like to rewrite that part as</p>
<pre><code> a1*a2*a3*x*f(x) + (a1*a2*x^2*+ a1*a3*x^2+ a2*a3*x^2)*diff(f(x), x)
</code></pre>
<p>How do I tell Sage to do this?</p>
https://ask.sagemath.org/question/38214/collecting-coefficients-of-derivatives/?answer=38216#post-id-38216Try this:
p=a1*a2*a3*x*f(x) + a1*a2*x^2*diff(f(x), x) + a1*a3*x^2*diff(f(x), x) + a2*a3*x^2*diff(f(x), x)
p.collect(diff(f(x),x))Mon, 10 Jul 2017 19:25:14 +0200https://ask.sagemath.org/question/38214/collecting-coefficients-of-derivatives/?answer=38216#post-id-38216Comment by Ursula for <p>Try this:</p>
<pre><code>p=a1*a2*a3*x*f(x) + a1*a2*x^2*diff(f(x), x) + a1*a3*x^2*diff(f(x), x) + a2*a3*x^2*diff(f(x), x)
p.collect(diff(f(x),x))
</code></pre>
https://ask.sagemath.org/question/38214/collecting-coefficients-of-derivatives/?comment=38303#post-id-38303Note that this only collects first derivatives: to collect the second, third, etc. derivatives you'd have to do that explicitly (and know how high your derivatives go).Tue, 18 Jul 2017 14:55:53 +0200https://ask.sagemath.org/question/38214/collecting-coefficients-of-derivatives/?comment=38303#post-id-38303