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

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

x, y, z의 데카르트 곱(Cartesian product) 위에서 3차원 Hermite_e 급수를 평가하려면 Python의 hermite_e.hermegrid3d(x, y, z, c) 메서드를 사용합니다. 이 메서드는 x, y, z의 데카르트 곱으로 구성된 점들에서 해당 다항식의 값을 반환합니다.

주요 매개변수

x, y, z — 3차원 급수는 x, y, z의 데카르트 곱에 속한 점들에서 평가됩니다. 인자가 리스트나 튜플이면 먼저 ndarray로 변환되고, 그 외의 경우에는 원본이 그대로 유지됩니다. ndarray가 아니라면 스칼라 값으로 취급됩니다.

c — 계수 배열입니다. 차수 i, j에 해당하는 계수가 c[i,j]에 담기도록 정렬되어야 합니다. c의 차원이 3보다 크면 나머지 인덱스는 여러 세트의 계수를 나타냅니다. 반대로 c의 차원이 3 미만이면 3차원이 되도록 형상(shape)에 1이 자동으로 추가됩니다. 최종 결과의 형상은 c.shape[3:] + x.shape + y.shape + z.shape가 됩니다.

단계별 구현 방법

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

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

배열의 형상을 확인합니다.

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

이제 hermegrid3d() 메서드를 호출하여 x, y, z의 데카르트 곱 위에서 3차원 Hermite_e 급수를 평가합니다.

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

전체 예제 코드

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

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

# x, y, z의 데카르트 곱 위에서 3차원 Hermite_e 급수 평가
print("\nResult...\n",H.hermegrid3d([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...
[[[[ 424. -1848.]
  [ 684. -2952.]]

[[ 732. -3132.]
 [ 1170. -4968.]]]


[[[ 440. -1908.]
  [ 708. -3042.]]

[[ 756. -3222.]
 [ 1206. -5103.]]]]