# arrange an expression in powers of a variable

I have the following code:

f0 = function('f0')(x)
f1 = function('f1')(x)
var('ep')
y = f0+ep*f1
de=ep*diff(y,x,2)+diff(y,x)
expand(de)


which gives the output:

ep^2*diff(f1(x), x, x) + ep*diff(f0(x), x, x) + ep*diff(f1(x), x) + diff(f0(x), x)


How can I rearrange this expression in powers of "ep" parameter? i.e

ep^2*diff(f1(x), x, x) + ep*( diff(f0(x), x, x) + diff(f1(x), x) ) + diff(f0(x), x)


Then I want to get the coefficient for each power (which is a differential eq) and then pass it to the desolve.

edit retag close merge delete

Sort by » oldest newest most voted

Alternatively, you can use collect():

sage: de.collect(ep)
ep^2*diff(f1(x), x, x) + ep*(diff(f0(x), x, x) + diff(f1(x), x)) + diff(f0(x), x)


To get the coefficient of ep^2 you can do:

sage: de.coefficients(ep)
diff(f1(x), x, x)


To get all the coefficients at once:

sage: de.coefficients()
[[diff(f0(x), x), 0],
[diff(f0(x), x, x) + diff(f1(x), x), 1],
[diff(f1(x), x, x), 2]]


If you define

sage: epcoeff = [de.coefficients(ep)[i] for i in range(len(de.coefficients()))]


you can access the coefficient of ep^n using epcoeff[n], so that you can feed it to desolve etc.

more

You can use the power series expansion as follows:

sage: de.series(ep)
(diff(f0(x), x)) + (diff(f0(x), x, x) + diff(f1(x), x))*ep + (diff(f1(x), x, x))*ep^2 + Order(ep^20)


And then truncate it to remove the big oh:

sage: de.series(ep).truncate()
ep^2*diff(f1(x), x, x) + ep*(diff(f0(x), x, x) + diff(f1(x), x)) + diff(f0(x), x)

more

1

Thanks! How to access the coefficients of each power? If you noticed, I am trying to use Sage for asymptotic analysis of DE's. Is there any guide/doc for this topic?