Vector outer product with Einstein summation convention using NumPy in Python
Last Updated :
03 Jun, 2022
In this article, we will find vector outer product with Einstein summation convention in Python.
numpy.einsum() methodÂ
The numpy.einsum() method from the NumPy library is used to find the vector outer product with the Einstein summation convention in Python. Many common multi-dimensional, linear algebraic array operations can be described in a simple way using the Einstein summation method which computes these values in implicit mode. By suppressing or forcing summation over a defined subscript label in explicit mode, it allows us additional freedom to compute alternative array operations that may not be considered standard Einstein summation operations.
Syntax: numpy.einsum(subscripts, *operands, out=None)
Parameters:
- subscripts: str type. As a comma separated list of subscript labels, specifies the subscripts for summation. Unless the explicit sign '->' and subscript labels of the precise output form are given, an implicit (classical Einstein summation) computation is done.
- operands: list of array_like. The arrays for the operation are as follows.
- out: ndarray, optional parameter. The calculation is done into this array if it is provided.
Returns: The calculation based on the Einstein summation convention is the output.
Example 1
Here, we will create a NumPy array using np.arrange() method. After that numpy.einsum() method is used to find the vector outer product with the Einstein summation convention in Python. The string 'i', 'j' passed in the np.einsum() method represents the vector outer product, the vector outer product is carried out on the array formed and [1,2,3] array in the np.einsum() method.
Python3
# import packages
import numpy as np
# Creating an array
array = np.arange(10)
print(array)
# shape of the array is
print("Shape of the array is : ", array.shape)
# dimension of the array
print("The dimension of the array is : ", array.ndim)
# Datatype of the array
print("Datatype of our Array is : ", array.dtype)
# vector outer product using np.einsum()
# method
print(np.einsum('i,j', [1, 2, 3], array))
Output:
[0 1 2 3 4 5 6 7 8 9]
Shape of the array is : (10,)
The dimension of the array is : 1
Datatype of our Array is : int64
[[ 0 1 2 3 4 5 6 7 8 9]
[ 0 2 4 6 8 10 12 14 16 18]
[ 0 3 6 9 12 15 18 21 24 27]]
Example 2
In this example, we will check the vector outer product with another method, and we will see that we will get the same output using the np.outer() method.
Python3
# import packages
import numpy as np
# vector outer product
print(np.outer([1, 2, 3], array))
Output:
[[ 0 1 2 3 4 5 6 7 8 9]
[ 0 2 4 6 8 10 12 14 16 18]
[ 0 3 6 9 12 15 18 21 24 27]]
Similar Reads
Tensor contraction with Einstein summation convention using NumPy in Python In this article let's find the Tensor contraction with the Einstein summation convention in Python. numpy.einsum() method Einstein summation is a notational convention for simplifying formulas, such as vector, matrix, and general tensor summations. The numpy.einsum() method from the NumPy library is
3 min read
Get the Outer Product of an array with vector of letters using NumPy in Python In this article let's see how to get the outer product of an array with a vector of letters in Python. numpy.outer() method The numpy.outer() method is used to get the outer product of an array with a vector of elements in Python. A matrix is the outer product of two coordinate vectors in linear alg
4 min read
Make grid for computing a Mandelbrot set with outer product using NumPy in Python In this article let's learn how to make a grid for computing a Mandelbrot set with an outer product in Numpy using Python. numpy.outer() method: In Python, the numpy.outer() method is used to acquire the outer product of an array and a vector of elements. In linear algebra, a matrix is the outer pr
4 min read
Python Program to Get dot product of multidimensional Vectors using NumPy Given two multidimensional Vectors, the task is to write a Python program to get the dot product of two multidimensional Vectors using NumPy.Example: Lets take 2 vectors a = [2,5,3] and b = [6,3,1]Dot Product(ab) = (a[0] * b[0])+ (a[1] * b[1]) + (a[2] * b[2]) = (2*6)+ (5*3) + (3*1) = 30Dot Product i
3 min read
Compute the inner product of vectors for 1-D arrays using NumPy in Python Python has a popular package called NumPy which used to perform complex calculations on 1-D and multi-dimensional arrays. To find the inner product of two arrays, we can use the inner() function of the NumPy package. Syntax: numpy.inner(array1, array2) Parameters:Â array1, array2: arrays to be evalu
2 min read