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

Python NumPy로 점(x, y)에서 2D 르장드르 급수 평가하기

Python의 NumPy 라이브러리에서 polynomial.legendre.legval2d() 메서드를 사용하면 점(x, y)에서 2차원 르장드르(Legendre) 급수를 평가할 수 있습니다. 이 메서드는 x와 y의 대응하는 값들로 구성된 좌표 쌍에서 2차원 르장드르 급수의 값을 반환합니다.

주요 매개변수

1. x, y

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

2. c

계수 배열입니다. 다중 차수(multidegree) i, j에 해당하는 항의 계수가 c[i, j]에 저장되도록 정렬됩니다. 만약 c의 차원이 2보다 크다면, 나머지 인덱스들은 여러 개의 계수 집합을 나타내는 데 사용됩니다.

구현 단계

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

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

계수로 사용할 다차원 배열을 생성합니다.

c = np.array([[3,4],[5,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)

이제 polynomial.legendre.legval2d() 메서드를 사용하여 점(x, y)에서 2D 르장드르 급수를 평가합니다.

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

전체 예제 코드

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

# 계수로 사용할 다차원 배열 생성
c = np.array([[3,4],[5,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)

# legval2d() 메서드로 점(x, y)에서 2D 르장드르 급수 평가
print("\nResult...\n",L.legval2d([1,2],[1,2],c))

실행 결과

Our Array...
    [[3 4]
    [5 6]]

Dimensions of our Array...
2

Datatype of our Array object...
int64

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

Result...
    [18. 45.]

실행 결과를 보면, 계수 배열 [[3, 4], [5, 6]]과 평가 지점 [1, 2]가 주어졌을 때 각 지점에서 계산된 르장드르 급수의 값은 [18., 45.]임을 알 수 있습니다.