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

Python NumPy로 x, y, z 데카르트 곱에서 3D 르장드르 급수 평가하기

x, y, z의 데카르트 곱(Cartesian product) 위에서 3차원 르장드르(Legendre) 급수를 평가하려면 Python NumPy의 polynomial.legendre.leggrid3d() 메서드를 사용합니다. 이 메서드는 x, y, z의 데카르트 곱에 있는 점들에서 3차원 급수의 값을 반환합니다.

만약 계수 배열 c가 3차원보다 낮은 차원을 가진다면, 자동으로 형태(shape)에 1이 추가되어 3차원으로 변환됩니다. 결과 배열의 형태는 c.shape[3:] + x.shape + y.shape + z.shape가 됩니다.

leggrid3d() 메서드의 매개변수

첫 번째 매개변수(x, y, z): 3차원 급수가 평가될 x, y, z의 데카르트 곱상의 점들을 나타냅니다. x나 y가 리스트(list) 또는 튜플(tuple)이라면 먼저 ndarray로 변환되고, 그렇지 않으면 그대로 유지됩니다. 만약 ndarray가 아니라면 스칼라(scalar)로 취급됩니다.

두 번째 매개변수(c): 계수 배열입니다. 다중 차수(multidegree) i, j에 해당하는 항의 계수는 c[i, j]에 저장되도록 정렬됩니다. c의 차원이 2보다 크다면, 남은 인덱스들은 여러 개의 계수 집합을 나타냅니다.

단계별 구현 방법

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

import numpy as np
from numpy.polynomial import legendre as L

2단계: 계수용 3D 배열 생성

c = np.arange(16).reshape(2,2,4)

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단계: leggrid3d() 메서드로 3D 르장드르 급수 평가

print("\nResult...\n",L.leggrid3d([1,2],[1,2],[1,2],c))

전체 예제 코드

import numpy as np
from numpy.polynomial import legendre as L

# 계수용 3D 배열 생성
c = np.arange(16).reshape(2,2,4)

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

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

Dimensions of our Array...
3

Datatype of our Array object...
int64

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

Result...
    [[[ 120. 868.]
    [ 196. 1404.]]

    [[ 212. 1506.]
    [ 342. 2412.]]]

위 실행 결과에서 볼 수 있듯이, 생성된 계수 배열은 int64 데이터 타입을 가지며 형태는 (2, 2, 4)입니다. leggrid3d() 메서드를 통해 [1, 2] 범위의 x, y, z 값들의 데카르트 곱 각 지점에서 3차원 르장드르 급수가 성공적으로 평가된 것을 확인할 수 있습니다.