
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Vector outer product with Einstein summation convention in Python
To compute outer product of vectors with Einstein summation convention, use the numpy.einsum() method in Python. The 1st parameter is the subscript. It specifies the subscripts for summation as comma separated list of subscript labels. The 2nd parameter is the operands. These are the arrays for the operation.
The einsum() method evaluates the Einstein summation convention on the operands. Using the Einstein summation convention, many common multi-dimensional, linear algebraic array operations can be represented in a simple fashion. In implicit mode einsum computes these values.
In explicit mode, einsum provides further flexibility to compute other array operations that might not be considered classical Einstein summation operations, by disabling, or forcing summation over specified subscript labels.
Steps
At first, import the required libraries −
import numpy as np
Creating a numpy array using the arange() and reshape() method −
arr = np.arange(5)
Display the array −
print("Our Array...\n",arr)
Check the Dimensions −
print("\nDimensions of our Array...\n",arr.ndim)
Get the Datatype −
print("\nDatatype of our Array object...\n",arr.dtype)
Get the Shape −
print("\nShape of our Array object...\n",arr.shape)
To compute outer product of vectors with Einstein summation convention, use the numpy.einsum() method. The 1st parameter is the subscript. It specifies the subscripts for summation as comma separated list of subscript labels. The 2nd parameter is the operands. These are the arrays for the operation −
print("\nResult (outer product)...\n",np.einsum('i,j', np.arange(2)+1, arr))
Example
import numpy as np # Creating a numpy array using the arange() and reshape() method arr = np.arange(5) # Display the array print("Our Array...\n",arr) # Check the Dimensions print("\nDimensions of our Array...\n",arr.ndim) # Get the Datatype print("\nDatatype of our Array object...\n",arr.dtype) # Get the Shape print("\nShape of our Array object...\n",arr.shape) # To compute outer product of vectors with Einstein summation convention, use the numpy.einsum() method in Python. print("\nResult (outer product)...\n",np.einsum('i,j', np.arange(2)+1, arr))
Output
Our Array... [0 1 2 3 4] Dimensions of our Array... 1 Datatype of our Array object... int64 Shape of our Array object... (5,) Result (outer product)... [[0 1 2 3 4] [0 2 4 6 8]]
- Related Articles
- Vector inner product with Einstein summation convention in Python
- Matrix Vector multiplication with Einstein summation convention in Python
- Scalar multiplication with Einstein summation convention in Python
- Tensor contraction with Einstein summation convention in Python
- Array axis summations with Einstein summation convention in Python
- Compute a matrix transpose with Einstein summation convention in Python
- Get the trace of a matrix with Einstein summation convention in Python
- Extract the diagonal of a matrix with Einstein summation convention in Python
- Get the Outer Product of an array with vector of letters in Python
- Make a grid for computing a Mandelbrot set with outer product in Python
- Get the Outer product of two arrays in Python
- Python – Find Product of Index Value and find the Summation
- Get the Outer product of two multidimensional arrays in Python
- Get the Outer product of two One-Dimensional arrays in Python
- Get the Outer product of an array and a scalar in Python
