Open In App

Products of Vectors and Matrices in NumPy

Last Updated : 16 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Working with vector and matrix operations is a fundamental part of scientific computing and data analysis. NumPy is a Python library that computes various types of vector and matrix products. Let’s discuss how to find the inner, outer and cross products of matrices and vectors using NumPy in Python.

1. Inner Product of Vectors and Matrices

Inner product or dot product is the sum you get by multiplying matching elements of two arrays and then adding them. First we define two vectors a and b and use np.inner() function to compute their inner product. Then we define two 2D matrices x and y and use np.inner() to get the inner product matrix.

Syntax of inner method is:

numpy.inner(arr1, arr2)

Python
import numpy as np

a = np.array([2, 6])
b = np.array([3, 10])
print("Vectors :")
print("a = ", a)
print("\nb = ", b)

print("\nInner product of vectors a and b =")
print(np.inner(a, b))

print("---------------------------------------")

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)

print("\nInner product of matrices x and y =")
print(np.inner(x, y))

Output : 

2. Outer Product of Vectors and Matrices

Outer product of two vectors creates a matrix where each element is the product of elements from both vectors. In NumPy you can find this using np.outer(a, b). For matrices you can flatten them into 1D arrays and compute the outer product between each element.

Syntax: numpy.outer(arr1, arr2, out = None)

Python
import numpy as np

a = np.array([2, 6])
b = np.array([3, 10])
print("Vectors :")
print("a = ", a)
print("\nb = ", b)

print("\nOuter product of vectors a and b =")
print(np.outer(a, b))

print("------------------------------------")

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)

print("\nOuter product of matrices x and y =")
print(np.outer(x, y))

Output : 

Each value is the product of one value from a and one from b. Matrices are flattened before computing all pairwise multiplications.

3. Cross Product of Vectors and Matrices 

Cross product gives a new vector that is perpendicular to both input vectors (only for 3D vectors). For 2D vectors it gives a scalar value instead of a vector. With matrices it applies the cross product row by row and treat each row as a 3D vector.

Syntax: numpy.cross(arr1 , arr2)

Python
import numpy as np

a = np.array([3, 6])
b = np.array([9, 10])
print("Vectors :")
print("a = ", a)
print("\nb = ", b)

print("\nCross product of vectors a and b =")
print(np.cross(a, b))

print("------------------------------------")

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)

print("\nCross product of matrices x and y =")
print(np.cross(x, y))

Output : 

The result -24 is a scalar representing the 2D cross product. Matrix result shows row-wise cross products between 3D vectors in x and y. 



Next Article
Practice Tags :

Similar Reads