주어진 차수의 Pseudo-Vandermonde 행렬을 생성하려면 Python NumPy의 polynomial.polyvander2d() 함수를 사용합니다. 이 함수는 지정된 차수(deg)와 샘플 포인트 (x, y)에 대한 의사 Vandermonde 행렬을 반환합니다.
polyvander2d() 함수의 주요 매개변수
- x, y: 점 좌표 배열로, 모두 동일한 형태(shape)를 가져야 합니다. 요소 중 하나라도 복소수가 포함되어 있으면 dtype은 float64 또는 complex128로 자동 변환됩니다. 스칼라 값은 1차원 배열로 변환됩니다.
- deg: [x_deg, y_deg] 형태의 최대 차수 리스트입니다.
구현 단계
먼저 필요한 라이브러리를 임포트합니다.
import numpy as np from numpy.polynomial.polynomial import polyvander2d
numpy.array() 메서드를 사용하여 동일한 형태의 점 좌표 배열을 생성합니다.
x = np.array([-2.+2.j, -1.+2.j]) y = np.array([1.+2.j, 2.+2.j])
생성된 배열과 데이터 타입을 출력해 확인합니다.
print("Array1...\n",x)
print("\nArray2...\n",y)
print("\nArray1 datatype...\n",x.dtype)
print("\nArray2 datatype...\n",y.dtype)두 배열의 차원(dimension)과 형태(shape)를 확인합니다.
print("\nDimensions of Array1...\n",x.ndim)
print("\nDimensions of Array2...\n",y.ndim)
print("\nShape of Array1...\n",x.shape)
print("\nShape of Array2...\n",y.shape)지정된 차수의 Pseudo-Vandermonde 행렬을 생성하기 위해 polyvander2d()를 호출합니다.
x_deg, y_deg = 2, 3
print("\nResult...\n",polyvander2d(x,y, [x_deg, y_deg]))전체 예제 코드
import numpy as np
from numpy.polynomial.polynomial import polyvander2d
# numpy.array() 메서드로 동일한 형태의 점 좌표 배열 생성
x = np.array([-2.+2.j, -1.+2.j])
y = np.array([1.+2.j, 2.+2.j])
# 배열 출력
print("Array1...\n",x)
print("\nArray2...\n",y)
# 데이터 타입 출력
print("\nArray1 datatype...\n",x.dtype)
print("\nArray2 datatype...\n",y.dtype)
# 두 배열의 차원 확인
print("\nDimensions of Array1...\n",x.ndim)
print("\nDimensions of Array2...\n",y.ndim)
# 두 배열의 형태 확인
print("\nShape of Array1...\n",x.shape)
print("\nShape of Array2...\n",y.shape)
# Pseudo-Vandermonde 행렬 생성
x_deg, y_deg = 2, 3
print("\nResult...\n",polyvander2d(x,y, [x_deg, y_deg]))실행 결과
Array1...
[-2.+2.j -1.+2.j]
Array2...
[1.+2.j 2.+2.j]
Array1 datatype...
complex128
Array2 datatype...
complex128
Dimensions of Array1...
1
Dimensions of Array2...
1
Shape of Array1...
(2,)
Shape of Array2...
(2,)
Result...
[[ 1. +0.j 1. +2.j -3. +4.j -11. -2.j -2. +2.j -6. -2.j -2.-14.j 26.-18.j
0. -8.j 16. -8.j 32.+24.j -16.+88.j]
[ 1. +0.j 2. +2.j 0. +8.j -16.+16.j -1. +2.j -6. +2.j -16. -8.j -16.-48.j
-3. -4.j 2.-14.j 32.-24.j 112.+16.j]]실행 결과에서 확인할 수 있듯이, 복소수 좌표가 포함된 경우 dtype이 complex128로 자동 변환되며, x의 최대 차수가 2이고 y의 최대 차수가 3이므로 각 점마다 (2+1)×(3+1) = 12개의 열을 가진 행렬이 생성됩니다. 이러한 Pseudo-Vandermonde 행렬은 다항식 최소제곱 피팅(fitting) 등 다양한 수치 계산에 활용됩니다.