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

Python NumPy로 점 (x, y, z)에서 3차원 에르미트(Hermite) 급수 평가하기


Python NumPy에서 hermite.hermval3d() 메서드를 사용하면 점 (x, y, z)에서 3차원 에르미트(Hermite) 급수를 간편하게 평가할 수 있습니다. 이 메서드는 x, y, z에서 대응하는 값들을 묶은 삼중 좌표로 구성된 점들에 대해 다차원 다항식의 값을 반환합니다.

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

첫 번째 매개변수 (x, y, z): 3차원 급수를 평가할 점입니다. x, y, z는 반드시 동일한 형태(shape)를 가져야 하며, 리스트나 튜플이 전달되면 먼저 ndarray로 변환됩니다. ndarray가 아닌 값이 입력되면 스칼라로 취급됩니다.

두 번째 매개변수 (C): 다항식의 계수 배열입니다. 다차수(multidegree) i, j, k에 해당하는 항의 계수는 c[i, j, k]에 저장되어 있어야 합니다. 만약 c의 차원이 3보다 크다면, 남은 인덱스들은 여러 개의 계수 집합을 나타내는 데 사용됩니다.

단계별 구현 방법

1단계: 필요한 라이브러리 임포트

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

2단계: 계수용 3차원 배열 생성

reshape()를 활용해 3차원 계수 배열을 만듭니다.

c = np.arange(24).reshape(2,2,6)

3단계: 배열 출력 및 속성 확인

print("Our Array...\n",c)
print("\nDimensions of our Array...\n",c.ndim)
print("\nDatatype of our Array object...\n",c.dtype)
print("\nShape of our Array object...\n",c.shape)

위 코드는 배열의 값과 함께 차원(ndim), 데이터 타입(dtype), 형태(shape)를 순서대로 확인해 줍니다.

4단계: 3차원 에르미트 급수 평가

점 (x, y, z)에서 3차원 에르미트 급수를 평가하려면 hermite.hermval3d() 메서드를 호출합니다. 이 메서드는 x, y, z의 대응하는 값들로 구성된 점들에 대한 다차원 다항식의 값을 반환합니다.

print("\nResult...\n",H.hermval3d([1,2],[1,2],[1,2],c))

전체 예제 코드

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

# 계수용 3차원 배열 생성
c = np.arange(24).reshape(2,2,6)

# 배열 출력
print("Our Array...\n",c)

# 차원 확인
print("\nDimensions of our Array...\n",c.ndim)

# 데이터 타입 확인
print("\nDatatype of our Array object...\n",c.dtype)

# 형태 확인
print("\nShape of our Array object...\n",c.shape)

# hermite.hermval3d() 메서드로 점 (x, y, z)에서 3차원 에르미트 급수 평가
print("\nResult...\n",H.hermval3d([1,2],[1,2],[1,2],c))

실행 결과

Our Array...
    [[[ 0  1  2  3  4  5]
     [ 6  7  8  9 10 11]]

    [[12 13 14 15 16 17]
     [18 19 20 21 22 23]]]

Dimensions of our Array...
3

Datatype of our Array object...
int64

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

Result...
    [-4050.  52240.]