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

Python NumPy로 점(x, y, z)에서 3D 라게르 급수 평가하기 – lagval3d() 완벽 가이드

Python의 NumPy 라이브러리에서 polynomial.laguerre.lagval3d() 메서드를 사용하면 점 (x, y, z)에서 3차원 라게르(Laguerre) 급수를 손쉽게 평가할 수 있습니다. 이 메서드는 x, y, z에 대응하는 값들의 조합으로 구성된 점들에서 다차원 다항식의 값을 반환합니다.

lagval3d() 메서드의 주요 특징

계수 배열 c가 3차원보다 낮은 경우, 자동으로 형태(shape)에 1을 추가하여 3차원으로 변환합니다. 결과의 형태는 c.shape[3:] + x.shape가 됩니다.

첫 번째 매개변수는 x, y, z입니다. 3차원 급수는 점 (x, y, z)에서 평가되며, 이때 x, y, z는 반드시 동일한 형태를 가져야 합니다. 만약 x, y, z 중 하나라도 리스트(list)나 튜플(tuple)이라면 먼저 ndarray로 변환되고, 이미 ndarray인 경우 그대로 사용됩니다. ndarray가 아닌 값은 스칼라(scalar)로 취급됩니다.

두 번째 매개변수 C는 계수 배열입니다. 다중 차수(multidegree) i, j, k에 해당하는 항의 계수는 c[i, j, k]에 저장됩니다. 만약 c의 차원이 3보다 크다면, 나머지 인덱스들은 여러 개의 계수 집합을 나타냅니다.

단계별 구현 방법

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

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

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

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

3단계: 배열 출력하기

print("Our Array...\n",c)

4단계: 배열의 차원 확인

print("\nDimensions of our Array...\n",c.ndim)

5단계: 데이터 타입 확인

print("\nDatatype of our Array object...\n",c.dtype)

6단계: 배열의 형태(Shape) 확인

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

7단계: lagval3d()로 3D 라게르 급수 평가

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

전체 예제 코드

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

# 계수용 2차원 배열 생성
c = np.arange(4).reshape(2,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)

# lagval3d()를 사용해 점 (x,y,z)에서 3D 라게르 급수 평가
print("\nResult...\n",L.lagval3d([1,2],[1,2],[1,2], c))

실행 결과

Our Array...
   [[0 1]
   [2 3]]

Dimensions of our Array...
2

Datatype of our Array object...
int64

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

Result...
   [0. 0.]

위 결과에서 볼 수 있듯이, 2x2 형태의 계수 배열을 사용하여 점 (1,1,1)과 (2,2,2)에서 3차원 라게르 급수를 평가하면 [0. 0.]이라는 결과가 출력됩니다. 이처럼 lagval3d() 메서드를 활용하면 복잡한 다차원 다항식 연산도 간단하게 처리할 수 있습니다.