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

Python NumPy로 Hermite_e 다항식의 작은 후행 계수 제거하기

Python NumPy에서 hermite_e.hermetrim() 메서드를 사용하면 Hermite_e(HermiteE) 다항식의 작은 후행 계수를 손쉽게 제거할 수 있습니다. 이 메서드는 뒤쪽에 있는 불필요한 계수를 제거한 1차원 배열을 반환하며, 만약 결과 시리즈가 완전히 비어 있게 된다면 0 하나만 포함된 시리즈를 대신 반환합니다.

hermetrim() 메서드의 핵심 개념

여기서 "작다(small)"는 "절댓값이 작다"는 의미이며, 매개변수 tol을 통해 그 기준을 조절할 수 있습니다. 또한 "후행(trailing)"은 최고 차수의 계수들을 의미합니다. 예를 들어 [0, 1, 1, 0, 0] 배열(즉, 0 + x + x² + 0·x³ + 0·x⁴를 나타내는 배열)에서는 3차와 4차 계수가 모두 잘려나갑니다(trimmed).

  • c: 낮은 차수부터 높은 차수 순으로 정렬된 계수들의 1차원 배열입니다.
  • tol: 절댓값이 tol 이하인 후행 요소들은 모두 제거됩니다.

단계별 구현 방법

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

import numpy as np
from numpy.polynomial import hermite_e as H

numpy.array() 메서드를 사용하여 계수 배열을 생성합니다. 이 배열은 계수를 담고 있는 1차원 배열입니다.

c = np.array([0,5,0, 0,9,0])

생성된 배열을 출력합니다.

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)

Hermite_e 다항식에서 작은 후행 계수를 제거하려면 Python에서 hermite_e.hermetrim() 메서드를 호출합니다.

print("\nResult...\n",H.hermetrim((c)))

전체 예제 코드

import numpy as np
from numpy.polynomial import hermite_e as H

# numpy.array() 메서드로 배열 생성
# 계수를 담고 있는 1차원 배열
c = np.array([0,5,0, 0,9,0])

# 배열 출력
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)

# hermite_e.hermetrim() 메서드로 작은 후행 계수 제거
print("\nResult...\n",H.hermetrim((c)))

실행 결과

Our Array...
    [0 5 0 0 9 0]

Dimensions of our Array...
1

Datatype of our Array object...
int64

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

Result...
    [0. 5. 0. 0. 9.]

실행 결과를 보면 입력 배열의 마지막 요소였던 0이 제거되어 [0. 5. 0. 0. 9.]라는 결과가 반환된 것을 확인할 수 있습니다. 이처럼 hermetrim() 메서드는 다항식 연산 과정에서 발생하는 불필요한 고차 계수를 정리하는 데 유용하게 활용됩니다.