Python 中的 numpy.poly1d()

| | | | | | | | | | |
注意

這是舊多項式 API 的一部分。從 1.4 版開始,首選 numpy.polynomial 中定義的新多項式 API。可以在轉換指南中找到差異摘要。

numpy.poly1d() 函數允許定義多項式函數。因此,它可以直接在多項式上使用自然運算

它是一個便利類,用於封裝“多項式上的自然運算”,以便所述運算可以採用它們的代碼中的慣用形式。

語法:numpy.poly1d(arr, root, var)
參數:
arr: [array_like] 多項式係數按冪的降序給出。如果第二個參數 (root) 設置為 True,則數組值為多項式方程的根。

root: [bool, optional] True 表示多項式根。默認為 False。
var: 變量,如我們在多項式中需要的 x、y、z [默認是 x]。

參數:
c:多項式係數。
coef:多項式係數。
coefficients:多項式係數。
order:多項式的階或次數。
o: 多項式的階或次數。
r: 多項式根。
roots: 多項式根。

返回:多項式和應用的操作


Numpy poly1d 示例

np.poly1d 示例#1

def _break_points(num, den): " ""在給定這些位置的情況下提取實軸上的斷點和增益""" # type: (np.poly1d, np.poly1d) -> (np.array, np.array) dnum = num.deriv(m=1) dden = den.deriv(m=1) polynom = den * dnum - num * dden real_break_pts = polynom.r # 不關心無限斷點 real_break_pts = real_break_pts[num(real_break_pts) != 0] k_break = -den(real_break_pts ) / num(real_break_pts) idx = k_break >= 0 # 只有正面收益 k_break = k_break[idx] real_break_pts = real_break_pts[idx] if len(k_break) == 0: k_break = [0] real_break_pts = den.roots return k_break, real_break_pts 

np.poly1d example #2

def test_poly1d_math(self): # 這裡我們使用一些簡單的係數來簡化計算 p = np.poly1d([1., 2, 4]) q = np.poly1d([4., 2, 1]) assert_equal(p/q, (np.poly1d([0.25]), np.poly1d([1.5, 3.75]))) assert_equal(p.整數(),np.p oly1d([1/3, 1., 4., 0.])) assert_equal(p.integ(1), np.poly1d([1/3, 1., 4., 0.])) p = np .poly1d([1., 2, 3]) q = np.poly1d([3., 2, 1]) assert_equal(p * q, np.poly1d([3., 8., 14., 8., 3.])) assert_equal(p + q, np.poly1d([4., 4., 4.])) assert_equal(p - q, np.poly1d([-2., 0., 2.])) assert_equal(p ** 4, np.poly1d([1., 8., 36., 104., 214., 312., 324., 216., 81.])) assert_equal(p(q), np. poly1d([9., 12., 16., 8., 6.])) assert_equal(q(p), np.poly1d([3., 12., 32., 40., 34.])) assert_equal (p.deriv(), np.poly1d([2., 2.])) assert_equal(p.deriv(2), np.poly1d([2.])) assert_equal(np.polydiv(np.poly1d([ 1, 0, -1]), np.poly1d([1, 1])), (np.poly1d([1., -1.]), np.poly1d([0.]))) 

np.poly1d 示例 #3

def test_poly1d_str_and_repr(self): p = np.poly1d([1., 2, 3]) assert_equal(repr(p), `poly1d([1 ., 2., 3.])`) assert_equal(str(p), `2` `1 x + 2 x + 3`) q = np.poly1d([3., 2, 1]) assert_equal(repr( q), `poly1d([3., 2., 1.])`) assert_equal(str(q), `2` `3 x + 2 x + 1`) r = np.poly1d([1.89999 + 2j, -3j, -5.12345678, 2 + 1j]) assert_equal(str(r), `3 2` `(1.9 + 2j) x - 3j x - 5.123 x + (2 + 1j)`) assert_equal(str(np) .poly1d([-3, -2, -1])), `2` `-3 x - 2 x - 1`) 

np.poly1d 例子#4

 def data_analysis(e_ph,flux,method="least"): if method == "least": coeffs = np.polyfit(x=e_ph, y=flux, deg=11) polynom = np.poly1d(coeffs) x = np.linspace(e_ph[0], e_ph[-1], num=100) pd = np.polyder(多項式, m=1) indx = np.argmax(np.abs(pd(x))) eph_c = x [indx] pd2 = np.polyder(多項式,m=2) p2_roots = np.roots(pd2) p2_roots = p2_roots[p2_roots[:].imag == 0] p2_roots = np.real(p2_roots) Eph_fin = find_nearest(p2_roots ,eph_c) return Eph_fin, polynom elif method == "new method": pass #plt.plot(Etotal, total, "ro") #plt.plot(x, polynom(x)) 

np .poly1d example #5

def _systopoly1d(sys): """Extract numerator and denominator polynomails for a system""" # Allow inputs from the signal processing toolbox if (isinstance(sys, scipy.signal. lti)): num = sys.num denp = s ys.den else: # 如果需要,轉換為傳遞函數 sys = _convert_to_transfer_function(sys) # 確保我們有一個 SISO 系統 if (sys.inputs > 1 or sys.outputs > 1): raise ControlMIMONotImplemented() # Start by從系統對像中提取分子和分母 num = sys.num[0][0] denp = sys.den[0][0] # 檢查 num, den 是否已經是多項式;否則轉換 if (not isinstance(nump, poly1d)): nump = poly1d(nump) if (not isinstance(denp, poly1d)): denp = poly1d(denp) return (nump, denp) 

np .poly1d 示例 #6

def quadraticInterpolation(valueList2d, numDegrees, n, startTime=None, endTime=None): ``` 在平滑曲線上生成一系列點,這些點與給定點 numDegrees - 相交擬合多項式的度數 - 如果此值對於輸入 n 太高,曲線會變得很奇怪 - 輸出 startTime/endTime/n 的點數 - 將在 startTime 和 endTime 之間以均勻間隔生成 n 個點 ``` _numpyCheck( ) x, y = zip(*valueList2d) 如果 startTime 為 None: startTime = x[0] 如果 endTime 為 None: endTime = x[-1] polyFunc = np.poly1d(np.polyfit(x, y, numDegrees)) newX = np.linspace(startTime, endTime, n) retList = [(n, polyFunc(n)) for n in newX] return retList 

np.poly1d example #7

 def fit_y(self, X, Y, x1, x2): len(X) != 0 # 如果 X 只包含一個點,函數on 將得到行 y=Y[0] 如果 np.sum(X == X[0]) == len(X): return Y[0], Y[0] p = np.poly1d(np.polyfit( X, Y, 1)) 返回 p(x1), p(x2) 

np.poly1d 示例 #8

def remove_linear_BG_XAS_preedge( xmcd_data, scanparams, process_parameters=None, process_number= -1 ): """應該根據前邊緣平均值移除一個線性背景""" preedge_spectrum = get_preedge_spectrum(process_parameters, xmcd_data) preedge_poly = np.poly1d( np.polyfit(preedge_spectrum["Energy"], preedge_spectrum["XAS" ], 1) ) xas_bg = preedge_poly(xmcd_data["Energy"]) for xas in ["XAS+", "XAS-", "XAS"]: xmcd_data[xas] -= xas_bg return (xmcd_data, {"xas_bg_poly_coeffs": " ".join(map(str, preedge_poly.coeffs))}) 

np.poly1d 示例 #9

def fit_y(self, X, Y, x1, x2): len(X) != 0 # 如果 X 只包含一個點,函數將得到 line y=Y[0] if np.sum(X == X[0]) == len(X): return Y[0 ], Y[0] p = np.poly1d(np.polyfit(X, Y, 1)) 返回 p(x1), p(x2) 

np.poly1d 示例 #10

def __init__(自我,根,權重=None,hn=1.0,kn=1.0,wfunc=None,limits=None,monic=0,eval_func=None):np.poly1d.__init__(self,roots,r=1)equiv_weights = [ weights[k] / wfunc(roots[k]) for k in range(len(roots))] self.__dict__[`weights`] = np.array(list(zip(roots,weights,equiv_weights))) self. __dict__[`weight_func`] = wfunc self.__dict__[`limits`] = 限制 mu =  sqrt(hn) if monic: evf = eval_func if evf: eval_func = lambda x: evf(x)/kn mu = mu / abs( kn) kn = 1.0 self.__dict__[`normcoef`] = mu self.__dict__[`coeffs`] *= kn # 注意:eval_func 將在算術上被丟棄 self.__dict__[`_eval_func`] = eval_func 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method