返回Python中一维向量的点积
要返回一维向量的点积,请在Python中使用numpy.vdot()方法。vdot(a, b)函数与dot(a, b)在处理复数时有所不同。如果第一个参数是复数,则会使用第一个参数的共轭进行点积的计算。vdot与dot在处理多维数组时有所不同:它不执行矩阵乘法,而是先将输入参数展平为1维向量。因此,它只能用于向量。
该方法返回a和b的点积。可以是整数、浮点数或复数,具体取决于a和b的类型。第一个参数是a。如果a是复数,则在计算点积之前会取共轭。b是点积的第二个参数。
步骤
首先,导入所需的库。
import numpy as np
使用array()方法创建两个numpy一维数组 –
arr1 = np.array([2+3j,5+6j])
arr2 = np.array([9+10j,11+12j])
显示数组 –
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)
要返回一维向量的点积,请使用numpy.vdot()方法 –
print("\nResult...\n",np.vdot(arr1, arr2))
示例
import numpy as np
# Creating two numpy One-Dimensional array using the array() method
arr1 = np.array([2+3j,5+6j])
arr2 = np.array([9+10j,11+12j])
# 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 return the dot product of One-Dimensional vectors, use the numpy.vdot() method in Python.
print("\nResult...\n",np.vdot(arr1, arr2))
输出
Array1...
[2.+3.j 5.+6.j]
Array2...
[ 9.+10.j 11.+12.j]
Dimensions of Array1...
1
Dimensions of Array2...
1
Shape of Array1...
(2,)
Shape of Array2...
(2,)
Result...
(175-13j)