如何使用 NumPy 计算两个给定向量的叉积?
让我们看看使用 NumPy 计算两个给定向量的叉积的程序。为了找到两个给定向量的叉积,我们使用 NumPy 库的numpy.cross()函数。
Syntax: numpy.cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None)[
Return: cross product of two (arrays of) vectors.
让我们看看例子:
示例 1:一维数组的叉积。
Python3
# import library
import numpy as np
# declare vectors
x = [1, 2]
y = [3, 4]
# find cross product of
# two given vectors
result = np.cross(x, y)
# show the result
print(result)
Python3
# import library
import numpy as np
# declare vectors
x = [[1, 2], [3, 4]]
y = [[5, 6], [7, 8]]
# find cross product of
# two given vectors
result = np.cross(x, y)
# show the result
print(result)
输出:
-2
示例 2:二维数组的叉积。
Python3
# import library
import numpy as np
# declare vectors
x = [[1, 2], [3, 4]]
y = [[5, 6], [7, 8]]
# find cross product of
# two given vectors
result = np.cross(x, y)
# show the result
print(result)
输出:
[-4 -4]