Computer >> 컴퓨터 >  >> 프로그래밍 >> Python

Python NumPy lagpow()로 라게르(Laguerre) 급수 거듭제곱 계산하기

라게르(Laguerre) 급수를 거듭제곱하려면 Python NumPy에서 제공하는 polynomial.laguerre.lagpow() 메서드를 사용하면 됩니다. 이 메서드는 라게르 급수 c를 pow만큼 거듭제곱한 결과를 반환합니다.

여기서 인자 c는 낮은 차수부터 높은 차수 순으로 정렬된 계수 시퀀스입니다. 예를 들어 [1, 2, 3]은 P_0 + 2*P_1 + 3*P_2 형태의 급수를 의미합니다.

lagpow() 메서드의 주요 매개변수

  • c : 낮은 차수부터 높은 차수 순으로 정렬된 라게르 급수 계수를 담고 있는 1차원 배열입니다.
  • pow : 급수를 거듭제곱할 지수입니다.
  • maxpower : 허용되는 최대 거듭제곱 값입니다. 주로 급수가 감당하기 어려운 크기로 커지는 것을 방지하는 용도이며, 기본값은 16입니다.

구현 단계

먼저 필요한 라이브러리를 임포트합니다.

import numpy as np
from numpy.polynomial import laguerre as L

라게르 급수 계수로 구성된 1차원 배열을 생성합니다.

c = np.array([1,2,3])

계수 배열을 화면에 출력합니다.

print("Our coefficient 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)

라게르 급수를 거듭제곱하기 위해 polynomial.laguerre.lagpow() 메서드를 사용합니다. 아래 예제에서는 급수를 3제곱합니다.

print("\nResult....\n",L.lagpow(c, 3))

전체 예제 코드

import numpy as np
from numpy.polynomial import laguerre as L

# 라게르 급수 계수로 구성된 1차원 배열 생성
c = np.array([1,2,3])

# 계수 배열 출력
print("Our coefficient 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)

# lagpow() 메서드로 라게르 급수를 3제곱
print("\nResult....\n",L.lagpow(c, 3))

실행 결과

Our coefficient Array...
   [1 2 3]

Dimensions of our Array...
1

Datatype of our Array object...
int64

Shape of our Array object...
(3,)

Result....
   [ 150. -1116. 4590. -9672. 11934. -8100. 2430.]

실행 결과를 보면 계수 배열 [1, 2, 3]을 3제곱했을 때 7개의 계수를 가진 새로운 라게르 급수가 반환되는 것을 확인할 수 있습니다. 이처럼 lagpow() 메서드를 활용하면 복잡한 다항식 연산을 간단하게 처리할 수 있습니다.