Computer >> 컴퓨터 >  >> 프로그래밍 >> Python

Python NumPy로 에르미트(Hermite) 다항식의 Vandermonde 행렬 생성하기

Python NumPy에서 hermite.hermvander() 함수를 사용하면 에르미트(Hermite) 다항식의 Vandermonde 행렬을 생성할 수 있습니다. 이 메서드는 의사 Vandermonde(pseudo-Vandermonde) 행렬을 반환하며, 반환되는 행렬의 형태(shape)는 x.shape + (deg + 1,)입니다. 여기서 마지막 인덱스는 해당 에르미트 다항식의 차수를 나타냅니다. 데이터 타입(dtype)은 변환된 x와 동일하게 유지됩니다.

매개변수 설명

x: 점(point)들의 배열입니다. 요소 중 하나라도 복소수가 포함되어 있으면 dtype은 complex128로, 그렇지 않으면 float64로 변환됩니다. x가 스칼라 값인 경우에는 1차원 배열로 변환됩니다.

deg: 생성될 행렬의 차수입니다.

구현 단계

먼저 필요한 라이브러리를 임포트합니다.

import numpy as np
from numpy.polynomial import hermite as H

배열을 생성합니다.

x = np.array([0, 1, -1, 2])

배열을 출력합니다.

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

에르미트 다항식의 Vandermonde 행렬을 생성하려면 Python NumPy의 hermite.hermvander()를 사용합니다.

print("\nResult...\n",H.hermvander(x, 2))

전체 예제 코드

import numpy as np
from numpy.polynomial import hermite as H

# 배열 생성
x = np.array([0, 1, -1, 2])

# 배열 출력
print("Our Array...\n",x)

# 차원 확인
print("\nDimensions of our Array...\n",x.ndim)

# 데이터 타입 확인
print("\nDatatype of our Array object...\n",x.dtype)

# 형태(shape) 확인
print("\nShape of our Array object...\n",x.shape)

# 에르미트 다항식의 Vandermonde 행렬 생성
print("\nResult...\n",H.hermvander(x, 2))

실행 결과

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

Dimensions of our Array...
1

Datatype of our Array object...
int64

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

Result...
    [[ 1. 0. -2.]
    [ 1. 2. 2.]
    [ 1. -2. 2.]
    [ 1. 4. 14.]]

위 결과에서 볼 수 있듯이, 각 행은 입력 배열의 각 점에 대해 0차부터 지정한 차수(여기서는 2차)까지의 에르미트 다항식 값을 나열한 것입니다. 이러한 Vandermonde 행렬은 에르미트 다항식 기반의 최소제곱 근사나 다항식 피팅에 활용됩니다.