Einstein 합산 규칙을 사용하여 스칼라 곱셈을 수행하려면 Python에서 numpy.einsum() 메서드를 사용합니다. 첫 번째 매개변수는 첨자입니다. 첨자 레이블의 쉼표로 구분된 합계 합계에 대한 첨자를 지정합니다. 두 번째 매개변수는 피연산자입니다. 작업을 위한 배열입니다.
einsum() 메서드는 피연산자에 대한 Einstein 합산 규칙을 평가합니다. 아인슈타인 합산 규칙을 사용하여 많은 일반적인 다차원 선형 대수 배열 연산을 간단한 방식으로 나타낼 수 있습니다. 암시적 모드에서 einsum은 이러한 값을 계산합니다. 명시적 모드에서 einsum은 합산을 과도하게 지정된 첨자 레이블을 비활성화하거나 강제 실행하여 고전적인 Einstein 합산 연산으로 간주되지 않을 수 있는 다른 배열 연산을 계산할 수 있는 유연성을 제공합니다.
단계
먼저 필요한 라이브러리를 가져옵니다 -
import numpy as np
numpy.arange() 및 reshape() -
를 사용하여 배열을 만듭니다.arr = np.arange(6).reshape(2,3)
val은 스칼라입니다 -
val = 2
배열 표시 -
print("Our Array...\n",arr)
치수 확인 -
print("\nDimensions of our Array...\n",arr.ndim) 데이터 유형 가져오기 -
print("\nDatatype of our Array object...\n",arr.dtype)
모양 가져오기 -
print("\nShape of our Array object...\n",arr.shape) Einstein 합산 규칙을 사용하여 스칼라 곱셈을 수행하려면 numpy.einsum() 메서드를 사용하십시오 -
print("\nResult (scalar multiplication)...\n",np.einsum('..., ...', val, arr))
예시
import numpy as np
# Create an array using the numpy.arange() and reshape()
arr = np.arange(6).reshape(2,3)
# The val is the scalar
val = 2
# Display the array
print("Array...\n",arr)
# Check the datatype
print("\nDatatype of Array...\n",arr.dtype)
# Check the Dimension
print("\nDimensions of Array...\n",arr.ndim)
# Check the Shape
print("\nShape of Array...\n",arr.shape)
# To perform scalar multiplication with Einstein summation convention, use the numpy.einsum() method in Python.
print("\nResult (scalar multiplication)...\n",np.einsum('..., ...', val, arr)) 출력
Array... [[0 1 2] [3 4 5]] Datatype of Array... int64 Dimensions of Array... 2 Shape of Array... (2, 3) Result (scalar multiplication)... [[ 0 2 4] [ 6 8 10]]