在 Python 中获取两个数组的克罗内克积
要获得两个数组的克罗内克积,请在 Python Numpy 中使用 numpy.kron() 方法。计算克罗内克积,即一个合成数组,由第二个数组的块组成,并按第一个数组进行缩放。
该函数假设 a 和 b 的维度相同,如果必要,则在最小维中添加 1。如果 a.shape = (r0,r1,..,rN) 和 b.shape = (s0,s1,...,sN),克罗内克积的形状为 (r0*s0, r1*s1, ..., rN*SN)。元素是 a 和 b 中元素的乘积,明确按 − 组织
kron(a,b)[k0,k1,...,kN] = a[i0,i1,...,iN] * b[j0,j1,...,jN]
步骤
首先,导入所需的库 −
import numpy as np
使用 array() 方法创建两个 numpy 数组 −
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)要获得两个数组的克罗内克积,请在 Python 中使用 numpy.kron() 方法 −
print("\nResult (Kronecker product)...\n",np.kron(arr1, arr2))
示例
import numpy as np
# Creating two numpy 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 array
print("\nDimensions of Array1...\n",arr1.ndim)
print("\nDimensions of Array2...\n",arr2.ndim)
# Check the Shape of both the array
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]
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP