Python에서 르장드르(Legendre) 급수의 근을 계산하려면 numpy.polynomial 모듈의 legendre.legroots() 메서드를 사용합니다. 이 메서드는 급수의 근들을 담은 배열을 반환하며, 모든 근이 실수일 경우 결과 역시 실수 배열로, 그렇지 않으면 복소수 배열로 반환됩니다. 매개변수 c는 급수의 계수를 담고 있는 1차원 배열입니다.
단계별 진행
먼저 필요한 라이브러리를 임포트합니다.
from numpy.polynomial import legendre as L
이제 polynomial.legendre.legroots() 메서드를 호출하여 르장드르 급수의 근을 계산합니다.
print("Result...\n",L.legroots((0, 1, 2)))결과 배열의 데이터 타입(dtype)을 확인합니다.
print("\nType...\n",L.legroots((0, 1, 2)).dtype)결과 배열의 형태(shape)도 함께 확인할 수 있습니다.
print("\nShape...\n",L.legroots((0, 1, 2)).shape)전체 예제 코드
from numpy.polynomial import legendre as L
# legroots() 메서드로 르장드르 급수의 근을 계산합니다
print("Result...\n",L.legroots((0, 1, 2)))
# 데이터 타입 확인
print("\nType...\n",L.legroots((0, 1, 2)).dtype)
# 배열 형태(shape) 확인
print("\nShape...\n",L.legroots((0, 1, 2)).shape)
실행 결과
Result...
[-0.76759188 0.43425855]
Type...
float64
Shape...
(2,)
실행 결과를 보면 계수 (0, 1, 2)로 표현된 르장드르 급수의 근 두 개가 실수(float64) 값으로 반환되었으며, 배열의 형태는 요소 2개를 가진 1차원 배열임을 확인할 수 있습니다.