📜  使用 NumPy 计算矩阵和向量的内积、外积和叉积

📅  最后修改于: 2022-05-13 01:54:59.066000             🧑  作者: Mango

使用 NumPy 计算矩阵和向量的内积、外积和叉积

让我们讨论如何在Python中使用 NumPy 查找矩阵和向量的内积、外积和叉积。

向量和矩阵的内积

要找到向量和矩阵的内积,我们可以使用 NumPy 的inner()方法。
句法:

numpy.inner(arr1, arr2)

代码 :

Python3
# Python Program illustrating
# numpy.inner() method
import numpy as np
  
# Vectors
a = np.array([2, 6])
b = np.array([3, 10])
print("Vectors :")
print("a = ", a)
print("\nb = ", b)
  
# Inner Product of Vectors
print("\nInner product of vectors a and b =")
print(np.inner(a, b))
  
print("---------------------------------------")
  
# Matrices
x = np.array([[2, 3, 4], [3, 2, 9]])
y = np.array([[1, 5, 0], [5, 10, 3]])
print("\nMatrices :")
print("x =", x)
print("\ny =", y)
  
# Inner product of matrices
print("\nInner product of matrices x and y =")
print(np.inner(x, y))


Python3
# Python Program illustrating 
# numpy.outer() method 
import numpy as np
  
# Vectors
a = np.array([2, 6])
b = np.array([3, 10])
print("Vectors :")
print("a = ", a)
print("\nb = ", b)
  
# Outer product of vectors 
print("\nOuter product of vectors a and b =")
print(np.outer(a, b))
  
print("------------------------------------")
  
# Matrices
x = np.array([[3, 6, 4], [9, 4, 6]])
y = np.array([[1, 15, 7], [3, 10, 8]])
print("\nMatrices :")
print("x =", x)
print("\ny =", y)
  
# Outer product of matrices
print("\nOuter product of matrices x and y =")
print(np.outer(x, y))


Python3
# Python Program illustrating 
# numpy.cross() method 
import numpy as np
  
# Vectors
a = np.array([3, 6])
b = np.array([9, 10])
print("Vectors :")
print("a = ", a)
print("\nb = ", b)
  
# Cross product of vectors 
print("\nCross product of vectors a and b =")
print(np.cross(a, b))
  
print("------------------------------------")
  
# Matrices
x = np.array([[2, 6, 9], [2, 7, 3]])
y = np.array([[7, 5, 6], [3, 12, 3]])
print("\nMatrices :")
print("x =", x)
print("\ny =", y)
  
# Cross product of matrices
print("\nCross product of matrices x and y =")
print(np.cross(x, y))


输出 :

向量和矩阵的外积

可以使用 NumPy 的outer()方法找到向量和矩阵的外积。
句法:

numpy.outer(a, b, out = None)

代码 :

Python3

# Python Program illustrating 
# numpy.outer() method 
import numpy as np
  
# Vectors
a = np.array([2, 6])
b = np.array([3, 10])
print("Vectors :")
print("a = ", a)
print("\nb = ", b)
  
# Outer product of vectors 
print("\nOuter product of vectors a and b =")
print(np.outer(a, b))
  
print("------------------------------------")
  
# Matrices
x = np.array([[3, 6, 4], [9, 4, 6]])
y = np.array([[1, 15, 7], [3, 10, 8]])
print("\nMatrices :")
print("x =", x)
print("\ny =", y)
  
# Outer product of matrices
print("\nOuter product of matrices x and y =")
print(np.outer(x, y))

输出 :

向量和矩阵的叉积

要找到向量和矩阵的叉积,我们可以使用 NumPy 的cross()方法。

句法:

numpy.cross(a, b)

代码 :

Python3

# Python Program illustrating 
# numpy.cross() method 
import numpy as np
  
# Vectors
a = np.array([3, 6])
b = np.array([9, 10])
print("Vectors :")
print("a = ", a)
print("\nb = ", b)
  
# Cross product of vectors 
print("\nCross product of vectors a and b =")
print(np.cross(a, b))
  
print("------------------------------------")
  
# Matrices
x = np.array([[2, 6, 9], [2, 7, 3]])
y = np.array([[7, 5, 6], [3, 12, 3]])
print("\nMatrices :")
print("x =", x)
print("\ny =", y)
  
# Cross product of matrices
print("\nCross product of matrices x and y =")
print(np.cross(x, y))

输出 :