Products of Vectors and Matrices in NumPy
Last Updated :
16 Apr, 2025
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.Â
Similar Reads
Find a matrix or vector norm using NumPy To find a matrix or vector norm we use function numpy.linalg.norm() of Python library Numpy. This function returns one of the seven matrix norms or one of the infinite vector norms depending upon the value of its parameters. Syntax: numpy.linalg.norm(x, ord=None, axis=None)Parameters: x: input ord:
2 min read
Generate a matrix product of two NumPy arrays We can multiply two matrices with the function np.matmul(a,b). When we multiply two arrays of order (m*n) and  (p*q ) in order to obtained matrix product then its output contains m rows and q columns where n is n==p is a necessary condition. Syntax: numpy.matmul(x1, x2, /, out=None, *, casting='same
2 min read
How to get the magnitude of a vector in NumPy? The fundamental feature of linear algebra are vectors, these are the objects having both direction and magnitude. In Python, NumPy arrays can be used to depict a vector. There are mainly two ways of getting the magnitude of vector: By defining an explicit function which computes the magnitude of a
3 min read
numpy matrix operations | zeros() function numpy.matlib.zeros() is another function for doing matrix operations in numpy. It returns a matrix of given shape and type, filled with zeros. Syntax : numpy.matlib.zeros(shape, dtype=None, order='C') Parameters : shape : [int, int] Number of rows and columns in the output matrix.If shape has length
2 min read
Find the sum and product of a NumPy array elements In this article, let's discuss how to find the sum and product of NumPy arrays. Sum of the NumPy array Sum of NumPy array elements can be achieved in the following ways Method #1:  Using numpy.sum() Syntax: numpy.sum(array_name, axis=None, dtype=None, out=None, keepdims=<no value>, initial=
5 min read