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

Python NumPy에서 3D 계수 배열을 사용해 점 (x, y)에서 2D HermiteE 급수 평가하기

Python NumPy에서 점 (x, y)에 대한 2차원 HermiteE(Hermite_e) 급수를 평가하려면 hermite.hermeval2d() 메서드를 사용합니다. 이 메서드는 x와 y의 대응하는 값들로 구성된 좌표 쌍에서 2차원 다항식의 값을 반환합니다.

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

첫 번째 매개변수 (x, y): 2차원 급수를 평가할 지점입니다. x와 y는 반드시 동일한 형태(shape)를 가져야 합니다. 만약 x 또는 y가 리스트나 튜플로 전달되면 먼저 ndarray로 변환되며, ndarray가 아닌 경우에는 스칼라 값으로 처리됩니다.

두 번째 매개변수 (C): 계수 배열입니다. 다중 차수(multidegree) i, j인 항의 계수가 c[i, j]에 포함되도록 정렬되어 있습니다. 만약 c의 차원이 2보다 크다면, 나머지 인덱스들은 여러 개의 계수 집합을 나타내는 데 사용됩니다.

단계별 구현 방법

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

import numpy as np
from numpy.polynomial import hermite_e 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)

배열의 형태(shape)를 확인합니다.

print("\nShape of our Array object...\n",c.shape)

hermeval2d() 메서드를 사용하여 점 (x, y)에서 2D HermiteE 급수를 평가합니다.

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

전체 예제 코드

import numpy as np
from numpy.polynomial import hermite_e 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)

# hermeval2d() 메서드로 2D HermiteE 급수 평가
print("\nResult...\n",H.hermeval2d([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...
    [[ 36. 108.]
    [ 40. 117.]
    [ 44. 126.]
    [ 48. 135.]
    [ 52. 144.]
    [ 56. 153.]]

위 결과에서 볼 수 있듯이, 3차원 계수 배열의 마지막 인덱스는 여러 개의 계수 집합을 나타내므로, 각 계수 집합별로 점 (x, y)에서 평가된 2차원 다항식 값들이 행렬 형태로 출력됩니다.