Sympy: Drop higher order terms in polynomial

▼魔方 西西 提交于 2019-12-07 01:35:50

问题


Using Sympy, say we have an expression f, which is a polynomial of the Symbol "x" (and of potentially other symbols).

I would like to know what if there is an efficient way to drop all terms in f of order greater than some integer n.

As a special case I have a very complicated function but i want to only keep terms up to 2nd order in x. What's the efficient way to do this?

The obvious, not-very-efficient way to do it would be for each m less than n, take m derivatives and set x to 0 to obtain the coefficient of x^m. We obtain each coefficient this way then reconstruct the polynomial. But taking derivatives is not the most efficient thing.


回答1:


An easy way to do this is to add O(x**n) to the expression, like

In [23]: x + x**2 + x**4 + x**10 + O(x**3)
Out[23]:
     2    ⎛ 3⎞
x + x  + O⎝x ⎠

If you want to later remove it, use the removeO method

In [24]: (x + x**2 + x**4 + x**10 + O(x**3)).removeO()
Out[24]:
 2
x  + x

You can also use series to take the series expansion of the expression. The difference here is the behavior if a non-polynomial term ends up in the expression:

In [25]: x + sin(x) + O(x**3)
Out[25]:
              ⎛ 3⎞
sin(x) + x + O⎝x ⎠

In [26]: (x + sin(x)).series(x, 0, 3)
Out[26]:
       ⎛ 3⎞
2⋅x + O⎝x ⎠



回答2:


If you take a look at the polynomial module docs:

http://docs.sympy.org/latest/modules/polys/reference.html

there will be plenty of ways to go about it, depending on the specifics of your situation. A couple different ways that would work:

Using .coeffs():

>>> f = 3 * x**3 + 2 * x**2 + x * y + y**3 + 1
>>> order = 2

>>> coeffs = Poly(f, x).coeffs()
>>> f_new = sum(x**n * coeffs[-(n+1)] for n in range(order+1)) # the +1 is to get 0th order
>>> f_new
2*x**2 + x*y + y**3 + 1

Alternatively, you could iterate over items in .all_terms():

>>> all_terms = Poly(f, x).all_terms()
>>> sum(x**n * term for (n,), term in all_terms() if n <= order)

There are plenty of manipulation functions in the module that you should be able to work with the expression directly rather than doing calculations/taking derivatives/etc.



来源:https://stackoverflow.com/questions/25001896/sympy-drop-higher-order-terms-in-polynomial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!