Python NumPy에서 polynomial.legendre.leggrid2d() 메서드를 사용하면 x와 y의 데카르트 곱(Cartesian product) 위에서 2차원 르장드르(Legendre) 급수를 평가할 수 있습니다. 이 메서드는 x와 y의 데카르트 곱에 있는 점들에서 2차원 르장드르 급수의 값을 반환합니다.
만약 계수 배열 c가 2차원 미만이라면, 자동으로 형태(shape)에 1이 추가되어 2차원 배열로 변환됩니다. 결과의 형태는 c.shape[2:] + x.shape + y.shape가 됩니다.
주요 매개변수
x, y (첫 번째 매개변수)
2차원 급수가 평가될 점들의 집합입니다. 급수는 x와 y의 데카르트 곱에 있는 각 점에서 계산됩니다. 만약 x나 y가 리스트(list) 또는 튜플(tuple)이면 먼저 ndarray로 변환되며, 그렇지 않으면 그대로 유지됩니다. ndarray가 아닌 경우에는 스칼라(scalar)로 취급됩니다.
c (두 번째 매개변수)
계수들이 담긴 배열입니다. 다중 차수(multi-degree) i, j에 해당하는 항의 계수가 c[i,j]에 저장되도록 정렬되어야 합니다. 만약 c의 차원이 2보다 크다면, 나머지 인덱스들은 여러 개의 계수 집합을 나타냅니다.
구현 단계
먼저 필요한 라이브러리를 임포트합니다.
import numpy as np
from numpy.polynomial import legendre as L
2차원 계수 배열을 생성합니다.
c = np.arange(4).reshape(2,2)
배열을 출력해 확인합니다.
print("Our Array...\n",c)배열의 차원을 확인합니다.
print("\nDimensions of our Array...\n",c.ndim)배열의 데이터 타입을 확인합니다.
print("\nDatatype of our Array object...\n",c.dtype)배열의 형태(shape)를 확인합니다.
print("\nShape of our Array object...\n",c.shape)이제 x와 y의 데카르트 곱 위에서 2D 르장드르 급수를 평가하기 위해 polynomial.legendre.leggrid2d() 메서드를 호출합니다.
print("\nResult...\n",L.leggrid2d([1,2],[1,2],c))전체 예제 코드
import numpy as np
from numpy.polynomial import legendre as L
# 2차원 계수 배열 생성
c = np.arange(4).reshape(2,2)
# 배열 출력
print("Our Array...\n",c)
# 차원 확인
print("\nDimensions of our Array...\n",c.ndim)
# 데이터 타입 확인
print("\nDatatype of our Array object...\n",c.dtype)
# 형태 확인
print("\nShape of our Array object...\n",c.shape)
# leggrid2d() 메서드로 2D 르장드르 급수 평가
print("\nResult...\n",L.leggrid2d([1,2],[1,2],c))
실행 결과
Our Array...
[[0 1]
[2 3]]
Dimensions of our Array...
2
Datatype of our Array object...
int64
Shape of our Array object...
(2, 2)
Result...
[[ 6. 10.]
[11. 18.]]
위 실행 결과를 보면, 2x2 형태의 계수 배열 c가 생성되었고, int64 데이터 타입과 (2, 2) 형태를 가지는 것을 확인할 수 있습니다. 최종적으로 leggrid2d() 메서드를 통해 x=[1,2]와 y=[1,2]의 데카르트 곱 지점에서 평가된 2차원 르장드르 급수의 값인 [[6., 10.], [11., 18.]]이 출력됩니다.