ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 15 Jul 2012 00:31:18 -0500How to extract coefficient terms from a differential equation, as for polynomial?https://ask.sagemath.org/question/9154/how-to-extract-coefficient-terms-from-a-differential-equation-as-for-polynomial/Hello all. As title, I want to extract from a differential equation the term coefficients with respect to a variable. For example:
var('x, u')
y = function('y', x)
DE = y.diff(x)*u^2 + 2*u + x
from DE I want to create a new symbolic expression with the coefficient of u^2, in this case y.diff(x).
Must I convert DE to a polynomial ring? And how can I do it?Sat, 14 Jul 2012 01:59:09 -0500https://ask.sagemath.org/question/9154/how-to-extract-coefficient-terms-from-a-differential-equation-as-for-polynomial/Answer by calc314 for <p>Hello all. As title, I want to extract from a differential equation the term coefficients with respect to a variable. For example:</p>
<pre><code>var('x, u')
y = function('y', x)
DE = y.diff(x)*u^2 + 2*u + x
</code></pre>
<p>from DE I want to create a new symbolic expression with the coefficient of u^2, in this case y.diff(x).
Must I convert DE to a polynomial ring? And how can I do it?</p>
https://ask.sagemath.org/question/9154/how-to-extract-coefficient-terms-from-a-differential-equation-as-for-polynomial/?answer=13820#post-id-13820You can use `DE.coeff(u,2)` to extract the coefficient of $u^2$.
You can see a list of commands you can use with your expression `DE` by typing `DE.` and hitting the tab key.
Sat, 14 Jul 2012 04:15:14 -0500https://ask.sagemath.org/question/9154/how-to-extract-coefficient-terms-from-a-differential-equation-as-for-polynomial/?answer=13820#post-id-13820Comment by calc314 for <p>You can use <code>DE.coeff(u,2)</code> to extract the coefficient of $u^2$.</p>
<p>You can see a list of commands you can use with your expression <code>DE</code> by typing <code>DE.</code> and hitting the tab key.</p>
https://ask.sagemath.org/question/9154/how-to-extract-coefficient-terms-from-a-differential-equation-as-for-polynomial/?comment=19406#post-id-19406I've had a similar situation with `simplify_full` where I had to apply the command twice to get the desired result. Sage uses Maxima to do these symbolic computations, but I'm not sure why the iteration is needed and whether you'd have to do this iteration if you were working in Maxima.Sat, 14 Jul 2012 15:13:48 -0500https://ask.sagemath.org/question/9154/how-to-extract-coefficient-terms-from-a-differential-equation-as-for-polynomial/?comment=19406#post-id-19406Comment by Lucas_Malor for <p>You can use <code>DE.coeff(u,2)</code> to extract the coefficient of $u^2$.</p>
<p>You can see a list of commands you can use with your expression <code>DE</code> by typing <code>DE.</code> and hitting the tab key.</p>
https://ask.sagemath.org/question/9154/how-to-extract-coefficient-terms-from-a-differential-equation-as-for-polynomial/?comment=19408#post-id-19408Mmmh... it really works with the simple example above, but not in the more complicated (and bad) code I was working on:
var('x, u, u1, u2)
f = -3*u2*(u1/u + 1/x) - 3*(u1)^2/(x*u)
eta = function('eta', x, u, u1)
def D(f, g) :
return f.diff(x) + u1*f.diff(u) + u2*f.diff(u1) + g*f.diff(u2)
D1_eta = D(eta, f)
D2_eta = D(D1_eta, f)
D3_eta = D(D2_eta, f)
DE = D3_eta - f.diff(u)*eta - f.diff(u1)*D1_eta - f.diff(u2)*D2_eta
DE.coeff(u2,3)
I must do
DE.collect(u2).coeff(u2,3)
Furthemore if you print
DE.collect(u2)
terms are expanded, but not collected with respect to u2. I must do
DE.collect(u2).collect(u2)
It's really strange... it's a bug or my fault? I have Sage 5.1 running on Ubuntu 12.04 32bit, installed from PPA.Sat, 14 Jul 2012 07:13:59 -0500https://ask.sagemath.org/question/9154/how-to-extract-coefficient-terms-from-a-differential-equation-as-for-polynomial/?comment=19408#post-id-19408Comment by Lucas_Malor for <p>You can use <code>DE.coeff(u,2)</code> to extract the coefficient of $u^2$.</p>
<p>You can see a list of commands you can use with your expression <code>DE</code> by typing <code>DE.</code> and hitting the tab key.</p>
https://ask.sagemath.org/question/9154/how-to-extract-coefficient-terms-from-a-differential-equation-as-for-polynomial/?comment=19404#post-id-19404I'll see if this is a bug, thank you :)Sun, 15 Jul 2012 00:31:18 -0500https://ask.sagemath.org/question/9154/how-to-extract-coefficient-terms-from-a-differential-equation-as-for-polynomial/?comment=19404#post-id-19404