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