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

Python NumPy로 복소수 점 배열을 활용해 Hermite_e 다항식의 Vandermonde 행렬 생성하기

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

주요 매개변수

  • x: 점(point)들의 배열입니다. 요소 중 하나라도 복소수가 포함되어 있으면 dtype이 complex128로 변환되고, 모두 실수라면 float64로 변환됩니다. 만약 x가 스칼라 값으로 입력되면 자동으로 1차원 배열로 변환됩니다.
  • deg: 결과 행렬의 최대 차수를 지정합니다.

단계별 구현 과정

1단계: 필요한 라이브러리 임포트

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

2단계: 복소수 점 배열 생성

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

3단계: 배열 정보 확인

생성한 배열의 값, 차원 수, 데이터 타입, 형태를 순서대로 출력하여 확인합니다.

print("Our Array...\n",x)
print("\nDimensions of our Array...\n",x.ndim)
print("\nDatatype of our Array object...\n",x.dtype)
print("\nShape of our Array object...\n",x.shape)

4단계: Vandermonde 행렬 생성

H.hermevander() 함수에 배열과 차수(deg)를 인자로 전달하여 Hermite_e 다항식의 Vandermonde 행렬을 생성합니다.

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

전체 예제 코드

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

# 복소수를 포함하는 점 배열 생성
x = np.array([-2.+2.j, -1.+2.j, 0.+2.j, 1.+2.j, 2.+2.j])

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

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

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

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

# hermevander()로 Hermite_e 다항식의 Vandermonde 행렬 생성
print("\nResult...\n",H.hermevander(x, 2))

실행 결과

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

Dimensions of our Array...
1

Datatype of our Array object...
complex128

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

Result...
   [[ 1.+0.j -2.+2.j -1.-8.j]
   [ 1.+0.j -1.+2.j -4.-4.j]
   [ 1.+0.j 0.+2.j -5.+0.j]
   [ 1.+0.j 1.+2.j -4.+4.j]
   [ 1.+0.j 2.+2.j -1.+8.j]]

실행 결과를 보면 입력 배열의 dtype이 복소수 포함 여부에 따라 complex128로 자동 변환되었으며, 차수 2의 Hermite_e 다항식에 대한 Vandermonde 행렬이 (5, 3) 형태로 정상적으로 생성된 것을 확인할 수 있습니다.