Python中具有爱因斯坦求和约定的向量外积

阅读 57 收藏 0 点赞 0 评论 0

要使用 Einstein 求和约定计算向量的外积,请使用Python 中的方法。第一个参数是下标。它将求和的下标指定为逗号分隔的下标标签列表。第二个参数是操作数。这些是操作的数组。numpy.einsum()

该einsum()方法评估操作数上的 Einstein 求和约定。使用爱因斯坦求和约定,可以以简单的方式表示许多常见的多维线性代数数组运算。在隐式模式下,einsum 计算这些值。

在显式模式下,einsum 通过禁用或强制对指定的下标标签求和,为计算可能不被视为经典 Einstein 求和操作的其他数组操作提供了更大的灵活性。

脚步

首先,导入所需的库 -

import numpy as np

arange()使用andreshape()方法创建一个 numpy 数组-

arr = np.arange(5)

显示数组 -

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 (outer product)...\n",np.einsum('i,j', np.arange(2)+1, arr))

示例

import numpy as np

# Creating a numpy array using the arange() and reshape() method
arr = np.arange(5)

# Display the array
print("Our Array...\n",arr)

# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)

# Get the Shape
print("\nShape of our Array object...\n",arr.shape)

# To compute outer product of vectors with Einstein summation convention, use the numpy.einsum() method in Python.
print("\nResult (outer product)...\n",np.einsum('i,j', np.arange(2)+1, arr))
输出结果
Our Array...
[0 1 2 3 4]

Dimensions of our Array...
1

Datatype of our Array object...
int64

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

Result (outer product)...
[[0 1 2 3 4]
[0 2 4 6 8]]

评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号