Вычислить внутренние, внешние и перекрестные произведения матриц и векторов с помощью NumPy

Опубликовано: 25 Марта, 2022

Давайте обсудим, как найти внутреннее, внешнее и перекрестное произведение матриц и векторов с помощью NumPy в Python.

Внутреннее произведение векторов и матриц

Чтобы найти внутреннее произведение векторов и матриц, мы можем использовать метод inner () NumPy.
Синтаксис:

 numpy.inner (arr1, arr2)

Code : 

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(" b = ", b)
  
# Inner Product of Vectors
print(" Inner 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(" Matrices :")
print("x =", x)
print(" y =", y)
  
# Inner product of matrices
print(" Inner product of matrices x and y =")
print(np.inner(x, y))

Выход :

Внешнее произведение векторов и матриц

Внешнее произведение векторов и матриц можно найти с помощью метода external () NumPy.
Синтаксис:

 numpy.outer (a, b, out = Нет)

Code : 

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(" b = ", b)
  
# Outer product of vectors 
print(" Outer 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(" Matrices :")
print("x =", x)
print(" y =", y)
  
# Outer product of matrices
print(" Outer product of matrices x and y =")
print(np.outer(x, y))

Выход :

Перекрестное произведение векторов и матриц

Чтобы найти перекрестное произведение векторов и матриц, мы можем использовать метод cross () NumPy.

Синтаксис:

 numpy.cross (а, б)

Code : 

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(" b = ", b)
  
# Cross product of vectors 
print(" Cross 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(" Matrices :")
print("x =", x)
print(" y =", y)
  
# Cross product of matrices
print(" Cross product of matrices x and y =")
print(np.cross(x, y))

Выход :

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.