
- 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
Matrix Vector multiplication with Einstein summation convention in Python
For Matrix Vector multiplication 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 two numpy One-Dimensional array using the array() method −
arr1 = np.arange(25).reshape(5,5) arr2 = np.arange(5)
Display the arrays −
print("Array1...\n",arr1) print("\nArray2...\n",arr2)
Check the Dimensions of both the arrays −
print("\nDimensions of Array1...\n",arr1.ndim) print("\nDimensions of Array2...\n",arr2.ndim)
Check the Shape of both the arrays −
print("\nShape of Array1...\n",arr1.shape) print("\nShape of Array2...\n",arr2.shape)
For Matrix Vector multiplication with Einstein summation convention, use the numpy.einsum() method in Python −
print("\nResult (Matrix Vector multiplication)...\n",np.einsum('ij,j', arr1, arr2))
Example
import numpy as np # Creating two numpy One-Dimensional array using the array() method arr1 = np.arange(25).reshape(5,5) arr2 = np.arange(5) # Display the arrays print("Array1...\n",arr1) print("\nArray2...\n",arr2) # Check the Dimensions of both the arrays print("\nDimensions of Array1...\n",arr1.ndim) print("\nDimensions of Array2...\n",arr2.ndim) # Check the Shape of both the arrays print("\nShape of Array1...\n",arr1.shape) print("\nShape of Array2...\n",arr2.shape) # For Matrix Vector multiplication with Einstein summation convention, use the numpy.einsum() method in Python. print("\nResult (Matrix Vector multiplication)...\n",np.einsum('ij,j', arr1, arr2))
Output
Array1... [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14] [15 16 17 18 19] [20 21 22 23 24]] Array2... [0 1 2 3 4] Dimensions of Array1... 2 Dimensions of Array2... 1 Shape of Array1... (5, 5) Shape of Array2... (5,) Result (Matrix Vector multiplication)... [ 30 80 130 180 230]
- Related Articles
- Scalar multiplication with Einstein summation convention in Python
- Vector inner product with Einstein summation convention in Python
- Vector outer product 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
- Tensor contraction with Einstein summation convention in Python
- Array axis summations with Einstein summation convention in Python
- Python program multiplication of two matrix.
- Matrix Chain Multiplication
- Matrix multiplication algorithm
- Sparse Matrix Multiplication in C++
- How to Create a Vector or Matrix in Python?
- Algorithm for matrix multiplication in JavaScript
- Python Program to Sort Matrix Rows by summation of consecutive difference of elements
