파이썬의 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와 같은 변수 [기본값 is x].

인수:
c: 다항식 계수.
coef: 다항식 계수.
계수: 다항식 계수.
차수: 다항식의 차수 또는 차수.
o: 다항식의 차수 또는 차수.
r: 다항식 근.
근: 다항식 근.

반환: 다항식 및 연산 적용


Numpy poly1d 예제

np.poly1d 예제 #1

def _break_points(num, den): " ""실제 축에 대한 중단점을 추출하고 이 위치에서 이득을 얻습니다.""" # 유형: (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가 k_break를 반환합니다. real_break_pts 

np.poly1d 예제 #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(polynom, m=1) indx = np.argmax(np.abs(pd(x))) eph_c = x [indx] pd2 = np.polyder(polynom, m=2) p2_roots = np.roots(pd2) p2_roots = p2_roots[p2_roots[:].imag == 0] p2_roots = np.real(p2_roots) Eph_fin = find_rootsnearest( ,eph_c) Eph_fin, polynom elif method == "new method" 반환: #plt.plot(Etotal, total, "ro") #plt.plot(x, polynom(x)) 

np 전달 .poly1d example #5

def _systopoly1d(sys): """시스템의 분자 및 분모 폴리노메일 추출""" # 신호 처리 도구 상자에서 입력 허용 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 또는 sys.outputs > 1): raise ControlMIMONotImplemented() # 시작 시스템 객체에서 분자와 분모 추출 nump = 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 example #6

def quadraticInterpolation(valueList2d, numDegrees, n, startTime=None, endTime=None): ``` 주어진 점 numDegrees를 교차하는 부드러운 곡선에 일련의 점을 생성합니다. 피팅된 다항식의 각도 - 이 값이 입력에 비해 너무 높으면 곡선이 이상해집니다. n - 출력할 포인트의 수 startTime/endTime/n - n 포인트가 startTime과 endTime 사이에 균등한 간격으로 생성됩니다. ``` _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 예제 #7

 def fit_y(self, X, Y, x1, x2): len(X) != 0 # X가 한 점만 포함하는 경우 함수 on은 np.sum(X == X[0]) == len(X)인 경우 y=Y[0] 행을 얻습니다. 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 ): """프리에지 평균을 기반으로 선형 bg를 제거해야 합니다.""" preedge_spectrum = get_preedge_spectrum(process_parameters, xmcd_data) preedge_poly = np.poly1d( np.polyfit(preedge_spectrum["에너지"], preedge_spectrum["XAS") ], 1) ) xas_bg = preedge_poly(xmcd_data["에너지"]) ["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가 한 점만 포함하는 경우 함수는 y=Y[0] 행을 얻습니다. if np.sum(X == X[0]) == len(X): return Y[0 ], Y[0] p = np.poly1d(np.polyfit(X, Y, 1)) return p(x1), p(x2) 

np.poly1d 예제 #10

<사전>def __init__( self, roots, weights=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 = 람다 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