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

Python NumPy hermsub() 메서드로 에르미트(Hermite) 급수 빼기 완벽 가이드

Python NumPy에서 한 에르미트(Hermite) 급수를 다른 에르미트 급수에서 빼려면 polynomial.hermite.hermsub() 메서드를 사용합니다. 이 메서드는 두 에르미트 급수의 차(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 hermite as H

2. 에르미트 급수 계수 배열 생성

에르미트 급수 계수를 담은 1차원 배열 두 개를 생성합니다.

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

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. hermsub() 메서드로 뺄셈 수행

Python NumPy의 polynomial.hermite.hermsub() 메서드를 사용해 한 에르미트 급수를 다른 급수에서 뺍니다.

print("\nResult (difference)....\n",H.hermsub(c1, c2))

전체 예제 코드

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

# 에르미트 급수 계수를 담은 1차원 배열 생성
c1 = np.array([1,2,3])
c2 = np.array([3,2,1])

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

# polynomial.hermite.hermsub() 메서드로 에르미트 급수 뺄셈 수행
# 이 메서드는 두 급수의 차를 나타내는 에르미트 급수 배열을 반환합니다.
print("\nResult (difference)....\n",H.hermsub(c1, c2))

실행 결과

Array1...
   [1 2 3]

Array2...
   [3 2 1]

Array1 datatype...
int64

Array2 datatype...
int64

Dimensions of Array1...
1

Dimensions of Array2...
1

Shape of Array1...
(3,)

Shape of Array2...
(3,)

Result (difference)....
   [-2. 0. 2.]

실행 결과를 보면 두 배열 모두 int64 타입의 1차원 배열이며, 형태는 (3,)입니다. hermsub() 메서드를 적용한 결과 [-2. 0. 2.]가 반환되었는데, 이는 c1 - c2 = [1-3, 2-2, 3-1] = [-2, 0, 2]와 일치합니다. 참고로 반환되는 결과 배열은 부동소수점(float) 타입으로 표현됩니다.