Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python에서 두 개의 1차원 배열의 Kronecker 곱 얻기

<시간/>

두 1D 배열의 Kronecker 곱을 얻으려면 Python Numpy에서 numpy.kron() 메서드를 사용하십시오. 첫 번째 배열만큼 크기가 조정된 두 번째 배열의 블록으로 구성된 합성 배열인 Kronecker 곱을 계산합니다.

이 함수는 와 b의 차원 수가 같다고 가정하고 필요한 경우 가장 작은 차원 앞에 1을 붙입니다. a.shape =(r0,r1,..,rN)이고 b.shape =(s0,s1,...,sN)인 경우 Kronecker 곱의 모양은 (r0*s0, r1*s1, ..., rN*SN). 요소는 −

에 의해 명시적으로 구성된 요소와 b의 곱입니다.
kron(a,b)[k0,k1,...,kN] = a[i0,i1,...,iN] * b[j0,j1,...,jN]

단계

먼저 필요한 라이브러리를 가져옵니다 -

import numpy as np

array() 메서드를 사용하여 두 개의 numpy 1차원 배열 만들기 -

arr1 = np.array([1, 10, 100])
arr2 = np.array([5, 6, 7])

배열 표시 -

print("Array1...\n",arr1)
print("\nArray2...\n",arr2)

두 어레이의 차원을 확인하십시오 -

print("\nDimensions of Array1...\n",arr1.ndim)
print("\nDimensions of Array2...\n",arr2.ndim)

두 배열의 모양을 확인하십시오 -

print("\nShape of Array1...\n",arr1.shape)
print("\nShape of Array2...\n",arr2.shape)

두 배열의 Kronecker 곱을 얻으려면 numpy.kron() 메서드를 사용하십시오 -

print("\nResult (Kronecker product)...\n",np.kron(arr1, arr2))

import numpy as np

# Creating two numpy One-Dimensional arrays using the array() method
arr1 = np.array([1, 10, 100])
arr2 = np.array([5, 6, 7])

# Display the arrays
print("Array1...\n",arr1)
print("\nArray2...\n",arr2)

# Check the Dimensions of both the arrays
print("\nDimensions of Array1...\n",arr1.ndim)
print("\nDimensions of Array2...\n",arr2.ndim)

# Check the Shape of both the arrays
print("\nShape of Array1...\n",arr1.shape)
print("\nShape of Array2...\n",arr2.shape)

# To get the Kronecker product of two arrays, use the numpy.kron() method in Python Numpy
print("\nResult (Kronecker product)...\n",np.kron(arr1, arr2))

출력

Array1...
[ 1 10 100]

Array2...
[5 6 7]

Dimensions of Array1...
1

Dimensions of Array2...
1

Shape of Array1...
(3,)

Shape of Array2...
(3,)

Result (Kronecker product)...
[ 5 6 7 50 60 70 500 600 700]