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

다항식을 평가하고 r의 모든 계수 열은 Python에서 x의 모든 요소에 대해 평가됩니다.

<시간/>

점 x에서 근으로 지정된 다항식을 평가하려면 Python Numpy에서 polynomial.polyvalfromroots() 메서드를 사용합니다. 첫 번째 매개변수는 x입니다. x가 목록이나 튜플이면 ndarray로 변환되고, 그렇지 않으면 변경되지 않고 스칼라로 처리됩니다. 두 경우 모두 x 또는 해당 요소는 r의 요소와 함께 덧셈과 곱셈을 지원해야 합니다.

두 번째 매개변수인 r은 근의 배열입니다. r이 다차원인 경우 첫 번째 인덱스는 루트 인덱스이고 나머지 인덱스는 여러 다항식을 열거합니다. 예를 들어, 2차원의 경우 각 다항식의 근은 r의 열에 저장된 것으로 생각할 수 있습니다.

세 번째 매개변수는 텐서입니다. True이면 루트 배열의 모양이 x의 각 차원에 대해 하나씩 오른쪽으로 확장됩니다. 스칼라는 이 작업의 차원이 0입니다. 결과는 r의 모든 계수 열이 x의 모든 요소에 대해 평가된다는 것입니다. False인 경우 x는 평가를 위해 r의 열에 브로드캐스트됩니다. 이 키워드는 r이 다차원일 때 유용합니다. 기본값은 True입니다.

단계

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

from numpy.polynomial.polynomial import polyvalfromroots
import numpy as np

다차원 계수의 배열 생성 -

c = np.arange(-2, 2).reshape(2,2)

배열 표시 -

print("Our Array...\n",c)

치수 확인 -

print("\nDimensions of our Array...\n",c.ndim)

데이터 유형 가져오기 -

print("\nDatatype of our Array object...\n",c.dtype)

모양 가져오기 -

print("\nShape of our Array object...\n",c.shape)

점 x에서 근으로 지정된 다항식을 평가하려면 Python Numpy에서 polynomial.polyvalfromroots() 메서드를 사용하십시오 -

print("\nResult...\n",polyvalfromroots([-2, 1], c, tensor=True))

예시

from numpy.polynomial.polynomial import polyvalfromroots
import numpy as np

# Create an array of multidimensional coefficients
c = np.arange(-2, 2).reshape(2,2)

# Display the array
print("Our Array...\n",c)

# Check the Dimensions
print("\nDimensions of our Array...\n",c.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",c.dtype)

# Get the Shape
print("\nShape of our Array object...\n",c.shape)

# To evaluate a polynomial specified by its roots at points x, use the polynomial.polyvalfromroots() method in Python Numpy
print("\nResult...\n",polyvalfromroots([-2, 1], c, tensor=True))

출력

Our Array...
[[-2 -1]
[ 0 1]]

Dimensions of our Array...
2

Datatype of our Array object...
int64

Shape of our Array object...
(2, 2)

Result...
[[-0. 3.]
[ 3. 0.]]