Python NumPy에서 르장드르(Legendre) 다항식의 유사 방데르몽드(pseudo-Vandermonde) 행렬을 생성하려면 polynomial.legvander() 메서드를 사용합니다. 이 메서드는 유사 방데르몽드 행렬을 반환하며, 반환되는 행렬의 형태(shape)는 x.shape + (deg + 1,)입니다. 마지막 인덱스는 해당 르장드르 다항식의 차수(degree)를 나타냅니다. 데이터 타입(dtype)은 입력값 x가 변환된 타입과 동일하게 유지됩니다.
주요 매개변수
x: 점(point)들의 배열입니다. 요소 중 하나라도 복소수가 포함되어 있으면 dtype은 complex128로, 그렇지 않으면 float64로 변환됩니다. 만약 x가 스칼라 값이라면 자동으로 1차원 배열로 변환됩니다.
deg: 생성될 결과 행렬의 차수를 지정합니다.
구현 단계
먼저 필요한 라이브러리를 임포트합니다.
import numpy as np from numpy.polynomial import legendre as L
복소수를 포함하는 배열을 생성합니다.
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)이제 polynomial.legvander() 메서드를 사용하여 르장드르 다항식의 유사 방데르몽드 행렬을 생성합니다.
print("\nResult...\n",L.legvander(x, 2))전체 예제 코드
import numpy as np
from numpy.polynomial import legendre as L
# 복소수를 포함하는 배열 생성
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)
# legvander() 메서드로 르장드르 다항식의 유사 방데르몽드 행렬 생성
print("\nResult...\n",L.legvander(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 -0.5-12.j]
[ 1. +0.j -1. +2.j -5. -6.j]
[ 1. +0.j 0. +2.j -6.5 +0.j]
[ 1. +0.j 1. +2.j -5. +6.j]
[ 1. +0.j 2. +2.j -0.5+12.j]]위 결과에서 확인할 수 있듯이, 입력 배열의 모든 요소가 복소수이므로 dtype이 complex128로 변환되었으며, 차수(deg)를 2로 지정했기 때문에 각 점마다 3개의 열(0차, 1차, 2차 르장드르 다항식 값)을 가진 행렬이 생성되었습니다.