Python NumPy에서 하나의 르장드르(Legendre) 급수를 다른 급수에 더하려면 polynomial.legendre.legadd() 메서드를 사용하면 됩니다. 이 메서드는 두 급수의 합을 나타내는 르장드르 급수 계수 배열을 반환합니다.
legadd()는 두 르장드르 급수 c1 + c2의 합을 계산합니다. 인자로 전달되는 계수 시퀀스는 낮은 차수 항부터 높은 차수 항 순으로 정렬됩니다. 예를 들어 [1, 2, 3]은 P_0 + 2*P_1 + 3*P_2라는 급수를 의미합니다. 매개변수 c1과 c2는 각각 르장드르 급수의 계수를 담고 있는 1차원 배열이며, 역시 낮은 차수부터 높은 차수 순으로 정렬되어 있어야 합니다.
단계별 진행 방법
1단계: 필요한 라이브러리 임포트
먼저 필요한 라이브러리를 가져옵니다.
import numpy as np
from numpy.polynomial import legendre as L
2단계: 르장드르 급수 계수 배열 생성
르장드르 급수의 계수를 담은 1차원 배열을 생성합니다.
c1 = np.array([2,3,4])
c2 = np.array([4,3,2])
3단계: 계수 배열 출력
생성된 계수 배열을 화면에 표시합니다.
print("Array1...\n",c1)
print("\nArray2...\n",c2)4단계: 데이터 타입 확인
두 배열의 데이터 타입(dtype)을 확인합니다.
print("\nArray1 datatype...\n",c1.dtype)
print("\nArray2 datatype...\n",c2.dtype)5단계: 차원 확인
두 배열의 차원(ndim)을 확인합니다.
print("\nDimensions of Array1...\n",c1.ndim)
print("\nDimensions of Array2...\n",c2.ndim)6단계: 형상(Shape) 확인
두 배열의 형상(shape)을 확인합니다.
print("\nShape of Array1...\n",c1.shape)
print("\nShape of Array2...\n",c2.shape)7단계: legadd()로 두 급수 더하기
numpy.polynomial.legendre.legadd() 메서드를 사용해 두 르장드르 급수를 더합니다. 이 메서드는 두 급수의 합을 나타내는 르장드르 급수 배열을 반환합니다.
print("\nResult (sum)....\n",L.legadd(c1, c2))전체 예제 코드
import numpy as np
from numpy.polynomial import legendre as L
# 르장드르 급수 계수 1차원 배열 생성
c1 = np.array([2,3,4])
c2 = np.array([4,3,2])
# 계수 배열 출력
print("Array1...\n",c1)
print("\nArray2...\n",c2)
# 데이터 타입 출력
print("\nArray1 datatype...\n",c1.dtype)
print("\nArray2 datatype...\n",c2.dtype)
# 배열의 차원 확인
print("\nDimensions of Array1...\n",c1.ndim)
print("\nDimensions of Array2...\n",c2.ndim)
# 배열의 형상 확인
print("\nShape of Array1...\n",c1.shape)
print("\nShape of Array2...\n",c2.shape)
# legadd() 메서드로 두 르장드르 급수의 합 계산
# 결과는 두 급수의 합을 나타내는 르장드르 급수 배열입니다.
print("\nResult (sum)....\n",L.legadd(c1, c2))
실행 결과
Array1...
[2 3 4]
Array2...
[4 3 2]
Array1 datatype...
int64
Array2 datatype...
int64
Dimensions of Array1...
1
Dimensions of Array2...
1
Shape of Array1...
(3,)
Shape of Array2...
(3,)
Result (sum)....
[6. 6. 6.]
정리
위 실행 결과에서 볼 수 있듯이, 계수 [2, 3, 4]와 [4, 3, 2]를 갖는 두 르장드르 급수를 legadd()로 더하면 대응하는 계수끼리 단순히 더해진 [6, 6, 6]이 결과로 반환됩니다. 즉, 르장드르 급수의 덧셈은 동일한 차수의 계수끼리 더하는 것과 같으므로, 결과적으로 일반적인 배열 덧셈과 같은 효과를 냅니다. 다만 legadd()를 사용하면 서로 길이가 다른 계수 배열도 자동으로 처리할 수 있다는 장점이 있습니다.