Arrays 哪种方法是处理多项式数组的有效方法?
我有一个函数,其中给出了两个多项式,p和q,计算实数a和b之间p/q的积分。我的函数是$\phi$:Arrays 哪种方法是处理多项式数组的有效方法?,arrays,numpy,Arrays,Numpy,我有一个函数,其中给出了两个多项式,p和q,计算实数a和b之间p/q的积分。我的函数是$\phi$: import numpy as np def integrate_pdivq(a, b, P, Q, r): """ Calculates the definite integral of P(x)/Q(x) between a and b, assuming gr P<gr Q and that Q have all different roots, all of which have
import numpy as np
def integrate_pdivq(a, b, P, Q, r):
"""
Calculates the definite integral of P(x)/Q(x) between a and b, assuming gr P<gr Q and that Q have all different roots, all of which have nonzero imaginary part.
Parameters:
P: list with coefficients of polynomial P.
Q: list with coefficients of polynomial Q.
r: list with the roots of Q.
"""
Q_deriv = np.polyder(Q)
fracc = ( np.polyval(P, r)/np.polyval(Q_deriv, r) )
inte = ( 0.5 * np.log((b-r.real)**2+(r.imag)**2) + 1j * np.arctan((b-r.real)/(r.imag)) ) - ( 0.5 * np.log((a-r.real)**2+(r.imag)**2) + 1j * np.arctan((a-r.real)/(r.imag)) )
return np.sum(fracc*inte)
将numpy导入为np
def集成pdivq(a、b、P、Q、r):
"""
计算a和b之间P(x)/Q(x)的定积分,假设gr P我刚才在另一个问题中演示过(我想是你问的)如何将
polyder
的逻辑应用于二维系数数组。通过快速浏览polyval
的代码,可以很容易地将其应用于二维系数数组
“polyval”的核心是:
for i in range(len(p)):
y = x * y + p[i]
对于ndp
(即系数的“行”),执行以下操作:
for i in range(p.shape[-1]):
y = x * y + p[...,i]
您可能需要调整尺寸,以便x
、y
和p
能够很好地广播
numpy中的多项式函数没有什么深奥或神秘之处。它只是对系数和数值变量进行简单的数学运算。它们看起来像是方便函数,而不是一个全面的多项式包