Python의 NumPy 라이브러리에서 제공하는 polynomial.legendre.legval2d() 메서드를 사용하면 주어진 점(x, y)에서 2차원 르장드르(Legendre) 급수를 손쉽게 평가할 수 있습니다. 이 메서드는 x와 y의 대응되는 값들로 구성된 좌표 쌍마다 2D 르장드르 급수의 계산 결과를 반환합니다.
legval2d() 메서드의 매개변수
첫 번째 매개변수 — x, y: 2차원 급수를 평가할 점 (x, y)입니다. 이때 x와 y는 반드시 동일한 형태(shape)를 가져야 합니다. 만약 x 또는 y가 리스트(list)나 튜플(tuple)로 전달되면 먼저 ndarray로 변환되며, 이미 ndarray인 경우 그대로 사용됩니다. ndarray가 아닌 값은 스칼라(scalar)로 처리됩니다.
두 번째 매개변수 — c: 계수(coefficient) 배열입니다. 다중 차수(multidegree) i, j에 해당하는 항의 계수는 c[i, j]에 저장됩니다. 만약 c의 차원이 2보다 크다면, 나머지 인덱스들은 여러 개의 계수 집합을 나타내는 데 사용됩니다.
구현 단계
먼저 필요한 라이브러리를 임포트합니다.
import numpy as np from numpy.polynomial import legendre as L
1차원 계수 배열을 생성합니다.
c = np.array([3, 5])
생성된 배열을 화면에 출력합니다.
print("Our Array...\n",c)배열의 차원(dimension)을 확인합니다.
print("\nDimensions of our Array...\n",c.ndim)배열의 데이터 타입(dtype)을 확인합니다.
print("\nDatatype of our Array object...\n",c.dtype)배열의 형태(shape)를 확인합니다.
print("\nShape of our Array object...\n",c.shape)이제 polynomial.legendre.legval2d() 메서드를 사용하여 점 (x, y)에서 2D 르장드르 급수를 평가합니다.
print("\nResult...\n",L.legval2d([1,2],[1,2],c))전체 예제 코드
import numpy as np
from numpy.polynomial import legendre as L
# 1차원 계수 배열 생성
c = np.array([3, 5])
# 배열 출력
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)
# legval2d() 메서드로 점 (x, y)에서 2D 르장드르 급수 평가
print("\nResult...\n",L.legval2d([1,2],[1,2],c))실행 결과
Our Array...
[3 5]
Dimensions of our Array...
1
Datatype of our Array object...
int64
Shape of our Array object...
(2,)
Result...
[21. 34.]위 실행 결과에서 볼 수 있듯이, 계수 배열 [3, 5]를 사용하여 점 (1, 1)과 (2, 2)에서 2D 르장드르 급수를 평가하면 각각 21.0과 34.0이라는 결과값이 반환됩니다. 이처럼 legval2d() 메서드를 활용하면 복잡한 다항식 연산을 간단한 코드 한 줄로 처리할 수 있습니다.