Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python에서 주어진 복소수 근을 사용하여 다항식의 근을 계산합니다.

<시간/>

다항식의 근을 계산하려면 Python Numpy에서 polynomial.polyroots() 메서드를 사용합니다. 이 메서드는 다항식 근의 배열을 반환합니다. 모든 근이 실수이면 out도 실수이고 그렇지 않으면 복소수입니다. 매개변수 c는 다항식 계수의 1차원 배열입니다.

근 추정치는 컴패니언 행렬의 고유값으로 얻어지며, 복소 평면의 원점에서 멀리 떨어진 근은 이러한 값에 대한 거듭제곱 급수의 수치적 불안정성으로 인해 큰 오차를 가질 수 있습니다. 다중도가 1보다 큰 근은 또한 그러한 점 근처의 계열 값이 근의 오류에 상대적으로 둔감하기 때문에 더 큰 오류를 표시합니다. 원점 근처의 고립된 뿌리는 Newton의 방법을 몇 번 반복하여 개선할 수 있습니다.

단계

먼저 필요한 라이브러리를 가져옵니다-

from numpy.polynomial import polynomial as P

다항식의 근을 계산하려면 Python Numpy에서 polynomial.polyroots() 메서드를 사용하십시오 -

j = complex(0,1)
print("Result (roots of a polynomial)...\n",P.polyroots((-j,j)))

데이터 유형 가져오기 -

print("\nType...\n",P.polyroots((-j, j)).dtype)

모양 가져오기 -

print("\nShape...\n",P.polyroots((-j, j)).shape)

예시

from numpy.polynomial import polynomial as P

# To compute the roots of a polynomials, use the polynomial.polyroots() method in Python Numpy.

# The method returns an array of the roots of the polynomial. If all the roots are real, then out is also real, otherwise it is complex.

# The parameter, c is a 1-D array of polynomial coefficients.
j = complex(0,1)
print("Result (roots of a polynomial)...\n",P.polyroots((-j,j)))

# Get the datatype
print("\nType...\n",P.polyroots((-j, j)).dtype)

# Get the shape
print("\nShape...\n",P.polyroots((-j, j)).shape)
가져오기

출력

Result (roots of a polynomial)...
[1.+0.j]

Type...
complex128

Shape...
(1,)