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

Python에서 4차원 계수 배열로 x, y, z의 데카르트 곱에 대한 3차원 에르미트 급수 평가하기

Python에서 x, y, z의 데카르트 곱(Cartesian product) 위에서 3차원 에르미트 급수(Hermite series)를 평가하려면 numpy.polynomial.hermite 모듈의 hermite.hermgrid3d(x, y, z, c) 메서드를 사용합니다. 이 메서드는 x, y, z의 데카르트 곱에 있는 각 점에서 다항식의 값을 계산하여 반환합니다.

hermgrid3d() 메서드의 매개변수 이해하기

x, y, z — 세 개의 입력 축을 나타냅니다. 3차원 급수는 x, y, z의 데카르트 곱에 있는 점들에서 평가됩니다. 만약 x, y 또는 z가 리스트(list)나 튜플(tuple)이면 먼저 ndarray로 변환되고, ndarray가 아니면 스칼라(scalar)로 취급됩니다.

c — 계수(coefficient) 배열입니다. 차수 i, j에 해당하는 항의 계수들이 c[i, j]에 포함되도록 정렬되어 있습니다. c의 차원이 3보다 크면 나머지 인덱스들은 여러 개의 계수 집합을 나타냅니다. 반대로 c의 차원이 3보다 작으면 형태(shape)를 3차원으로 맞추기 위해 뒤쪽에 1(ones)이 암묵적으로 추가됩니다.

결과의 형태는 c.shape[3:] + x.shape + y.shape + z.shape가 됩니다.

단계별 구현 방법

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

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

2단계: 4차원 계수 배열 생성

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

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)

4단계: hermgrid3d()로 3차원 에르미트 급수 평가

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

전체 예제 코드

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

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

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

# hermgrid3d() 메서드로 x, y, z의 데카르트 곱에서 3차원 에르미트 급수 평가
print("\nResult...\n",H.hermgrid3d([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]]]


    [[[24 25]
    [26 27]
    [28 29]
    [30 31]
    [32 33]
    [34 35]]

    [[36 37]
    [38 39]
    [40 41]
    [42 43]
    [44 45]
    [46 47]]]]

Dimensions of our Array...
4

Datatype of our Array object...
int64

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

Result...
    [[[[ -8100.  32472.]
    [-14148.  56976.]]

    [[-14796.  59832.]
    [-25740.  104480.]]]


    [[[ -8343.  33543.]
    [-14553.  58761.]]

    [[-15201.  61617.]
    [-26415.  107455.]]]]

정리

hermite.hermgrid3d() 메서드를 사용하면 여러 점 조합에 대해 3차원 에르미트 급수를 한 번에 효율적으로 평가할 수 있습니다. 특히 계수 배열의 차원이 3보다 클 경우, 마지막 인덱스들을 통해 여러 개의 계수 집합을 동시에 처리할 수 있어 과학 계산 및 수치 해석 분야에서 유용하게 활용됩니다.