Python의 NumPy 라이브러리에서 chebyshev.chebvander() 함수를 사용하면 체비셰프(Chebyshev) 다항식의 Vandermonde 행렬을 손쉽게 생성할 수 있습니다. 이 메서드는 Vandermonde 행렬을 반환하며, 반환되는 행렬의 형태(shape)는 x.shape + (deg + 1,)입니다. 여기서 마지막 인덱스는 해당 체비셰프 다항식의 차수를 나타냅니다. 데이터 타입(dtype)은 변환된 x와 동일하게 유지됩니다.
첫 번째 매개변수인 a는 점(point)들의 배열입니다. 요소 중 하나라도 복소수가 포함되어 있으면 dtype은 complex128로, 그렇지 않으면 float64로 자동 변환됩니다. 만약 x가 스칼라 값이라면 1차원 배열로 변환됩니다. 두 번째 매개변수인 deg는 결과 행렬의 차수를 지정합니다.
구현 단계
먼저 필요한 라이브러리를 임포트합니다.
import numpy as np from numpy.polynomial import chebyshev as C
복소수를 포함한 배열을 생성합니다.
x = np.array([-2.+2.j, -1.+2.j, 0.+2.j, 1.+2.j, 2.+2.j])
생성된 배열을 화면에 출력합니다.
print("Our Array...\n",x)배열의 차원(dimension)을 확인합니다.
print("\nDimensions of our Array...\n",x.ndim)
배열 객체의 데이터 타입(dtype)을 확인합니다.
print("\nDatatype of our Array object...\n",x.dtype)배열 객체의 형태(shape)를 확인합니다.
print("\nShape of our Array object...\n",x.shape)이제 chebyshev.chebvander() 함수를 사용하여 체비셰프 다항식의 Vandermonde 행렬을 생성합니다.
print("\nResult...\n",C.chebvander(x, 2))전체 예제 코드
import numpy as np
from numpy.polynomial import chebyshev as C
# 복소수를 포함한 배열 생성
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)
# 형태(shape) 확인
print("\nShape of our Array object...\n",x.shape)
# chebvander() 함수로 체비셰프 다항식의 Vandermonde 행렬 생성
# 반환되는 행렬의 shape는 x.shape + (deg + 1,)이며,
# 마지막 인덱스는 해당 체비셰프 다항식의 차수를 의미합니다.
# dtype은 변환된 x와 동일합니다.
print("\nResult...\n",C.chebvander(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.-16.j] [ 1. +0.j -1. +2.j -7. -8.j] [ 1. +0.j 0. +2.j -9. +0.j] [ 1. +0.j 1. +2.j -7. +8.j] [ 1. +0.j 2. +2.j -1.+16.j]]