Open In App

Vector outer product with Einstein summation convention using NumPy in Python

Improve
Improve
Like Article
Like
Save
Share
Report

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]]


Last Updated : 03 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads