# 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?

edit retag close merge delete

Sort by » oldest newest most voted

You 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.

more

Mmmh... 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 ...(more)

( 2012-07-14 14:13:59 +0100 )edit

I'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.

( 2012-07-14 22:13:48 +0100 )edit

I'll see if this is a bug, thank you :)

( 2012-07-15 07:31:18 +0100 )edit