在Python中使用NumPy计算两个给定向量的外积
在Python中,我们可以使用NumPy包的outer()函数来寻找两个矩阵的外积。
语法: numpy.outer(a, b, out = None)
参数 :
a : [array_like] 第一个输入向量。如果输入不是一维的,就会被扁平化。
b : [array_like] 第二个输入向量。如果输入不是一维的,则被扁平化。
out : [ndarray, optional] 一个存储结果的位置。
返回 : [ndarray] 返回两个向量的外积。 out[i, j] = a[i] * b[j]
例子1:一维阵列的外积
# Importing library
import numpy as np
# Creating two 1-D arrays
array1 = np.array([6,2])
array2 = np.array([2,5])
print("Original 1-D arrays:")
print(array1)
print(array2)
# Output
print("Outer Product of the two array is:")
result = np.outer(array1, array2)
print(result)
输出:
Original 1-D arrays:
[6 2]
[2 5]
Outer Product of the two array is:
[[12 30]
[ 4 10]]
例子2: 2X2矩阵的外积
# Importing library
import numpy as np
# Creating two 2-D matrix
matrix1 = np.array([[1, 3], [2, 6]])
matrix2 = np.array([[0, 1], [1, 9]])
print("Original 2-D matrix:")
print(matrix1)
print(matrix2)
# Output
print("Outer Product of the two matrix is:")
result = np.outer(matrix1, matrix2)
print(result)
输出:
Original 2-D matrix:
[[1 3]
[2 6]]
[[0 1]
[1 9]]
Outer Product of the two matrix is:
[[ 0 1 1 9]
[ 0 3 3 27]
[ 0 2 2 18]
[ 0 6 6 54]]
例子3: 3X3矩阵的外积
# Importing library
import numpy as np
# Creating two 3-D matrix
matrix1 = np.array([[2, 8, 2], [3, 4, 8], [0, 2, 1]])
matrix2 = np.array([[2, 1, 1], [0, 1, 0], [2, 3, 0]])
print("Original 3-D matrix:")
print(matrix1)
print(matrix2)
# Output
print("Outer Product of the two matrix is:")
result = np.outer(matrix1, matrix2)
print(result)
输出:
Original 3-D matrix:
[[2 8 2]
[3 4 8]
[0 2 1]]
[[2 1 1]
[0 1 0]
[2 3 0]]
Outer Product of the two matrix is:
[[ 4 2 2 0 2 0 4 6 0]
[16 8 8 0 8 0 16 24 0]
[ 4 2 2 0 2 0 4 6 0]
[ 6 3 3 0 3 0 6 9 0]
[ 8 4 4 0 4 0 8 12 0]
[16 8 8 0 8 0 16 24 0]
[ 0 0 0 0 0 0 0 0 0]
[ 4 2 2 0 2 0 4 6 0]
[ 2 1 1 0 1 0 2 3 0]]