from sympy import Symbol
x = Symbol('x')
equation = x**2 2**x - 2*x - 5**x 1
這里,在這個方程中,例如,多項式部分是x**2 - 2*x 1,而非多項式部分是2**x - 5**x。
給定一個方程,如何提取它的多項式和非多項式部分?
uj5u.com熱心網友回復:
您可以使用該as_poly方法查找給定符號中的多項式項:
In [1]: from sympy import Symbol
...:
...: x = Symbol('x')
...: equation = x**2 2**x - 2*x - 5**x 1
In [2]: poly, nonpoly = [], []
In [3]: for term in Add.make_args(equation):
...: if term.as_poly(x) is not None:
...: poly.append(term)
...: else:
...: nonpoly.append(term)
...:
In [4]: poly
Out[4]:
? 2 ?
?1, x , -2?x?
In [5]: nonpoly
Out[5]:
? x x?
?2 , -5 ?
In [6]: Add(*poly)
Out[6]:
2
x - 2?x 1
In [7]: Add(*nonpoly)
Out[7]:
x x
2 - 5
https://docs.sympy.org/latest/modules/core.html#sympy.core.expr.Expr.as_poly
uj5u.com熱心網友回復:
先把所有的詞分開,
lst = equation.args
使用degree()sympy 模塊中的函式查找lst. PolynomialError如果項不是多項式,則給出。
錯誤可以使用try ... except陳述句處理。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/312893.html
