Python NumPy에서 르장드르(Legendre) 다항식의 의사 Vandermonde 행렬을 생성하려면 legendre.legvander2d() 메서드를 사용하면 됩니다. 이 메서드는 의사 Vandermonde 행렬을 반환하며, 반환된 행렬의 shape은 x.shape + (deg + 1,) 형태입니다. 마지막 인덱스는 해당 르장드르 다항식의 차수를 나타내고, dtype은 변환된 x와 동일하게 설정됩니다.
x, y 매개변수는 점 좌표를 담은 배열로, 두 배열은 모두 동일한 shape이어야 합니다. 요소 중 하나라도 복소수가 포함되어 있으면 dtype은 complex128로, 그렇지 않으면 float64로 자동 변환됩니다. 스칼라 값이 입력되면 1차원 배열로 변환됩니다. 또한 deg 매개변수는 [x_deg, y_deg] 형태의 최대 차수 리스트를 받습니다.
단계별 구현 방법
먼저 필요한 라이브러리를 임포트합니다.
import numpy as np from numpy.polynomial import legendre as L
numpy.array() 메서드를 사용해 모두 동일한 shape을 가진 점 좌표 배열을 생성합니다.
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)
배열의 데이터 타입(dtype)을 확인합니다.
print("\nArray1 datatype...\n",x.dtype)
print("\nArray2 datatype...\n",y.dtype)
두 배열의 차원(ndim)을 확인합니다.
print("\nDimensions of Array1...\n",x.ndim)
print("\nDimensions of Array2...\n",y.ndim)
두 배열의 shape을 확인합니다.
print("\nShape of Array1...\n",x.shape)
print("\nShape of Array2...\n",y.shape)
르장드르 다항식의 의사 Vandermonde 행렬을 생성하려면 legendre.legvander2d() 메서드를 사용합니다. 여기서는 x 방향 최대 차수를 2, y 방향 최대 차수를 3으로 지정했습니다.
x_deg, y_deg = 2, 3
print("\nResult...\n",L.legvander2d(x,y, [x_deg, y_deg]))
전체 예제 코드
import numpy as np
from numpy.polynomial import legendre as L
# numpy.array() 메서드를 사용해 모두 동일한 shape을 가진 점 좌표 배열 생성
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)
# 두 배열의 shape 확인
print("\nShape of Array1...\n",x.shape)
print("\nShape of Array2...\n",y.shape)
# legendre.legvander2d() 메서드로 르장드르 다항식의 의사 Vandermonde 행렬 생성
x_deg, y_deg = 2, 3
print("\nResult...\n",L.legvander2d(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 -5. +6.j -29. -8.j -2. +2.j -6. -2.j
-2. -22.j 74. -42.j -0.5 -12.j 23.5 -13.j 74.5 +57.j -81.5 +352.j]
[ 1. +0.j 2. +2.j -0.5 +12.j -43. +37.j -1. +2.j -6. +2.j
-23.5 -13.j -31. -123.j -5. -6.j 2. -22.j 74.5 -57.j 437. +73.j]]