Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Matrix Vector multiplication with Einstein summation convention in Python
Matrix Vector multiplication with Einstein summation convention uses the numpy.einsum() method in Python. This method evaluates the Einstein summation convention on operands, allowing many common multi-dimensional linear algebraic operations to be represented in a simple fashion.
Syntax
numpy.einsum(subscripts, operands)
Parameters:
- subscripts ? String specifying the subscripts for summation as comma separated list of subscript labels
- operands ? Arrays for the operation
Understanding Einstein Summation
For matrix-vector multiplication, the notation 'ij,j' means:
-
irepresents rows of the matrix -
jrepresents columns of the matrix and elements of the vector - The repeated index
jis summed over
Example
import numpy as np
# Create a 5x5 matrix and a vector of length 5
matrix = np.arange(25).reshape(5, 5)
vector = np.arange(5)
# Display the arrays
print("Matrix:")
print(matrix)
print("\nVector:")
print(vector)
# Check dimensions and shapes
print(f"\nMatrix shape: {matrix.shape}")
print(f"Vector shape: {vector.shape}")
# Matrix-vector multiplication using einsum
result = np.einsum('ij,j', matrix, vector)
print("\nMatrix-Vector multiplication result:")
print(result)
# Verify with traditional dot product
traditional_result = matrix.dot(vector)
print("\nVerification using dot product:")
print(traditional_result)
print(f"\nResults are equal: {np.array_equal(result, traditional_result)}")
Matrix: [[ 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]] Vector: [0 1 2 3 4] Matrix shape: (5, 5) Vector shape: (5,) Matrix-Vector multiplication result: [ 30 80 130 180 230] Verification using dot product: [ 30 80 130 180 230] Results are equal: True
How It Works
The Einstein summation 'ij,j' performs the following calculation ?
import numpy as np
matrix = np.arange(25).reshape(5, 5)
vector = np.arange(5)
print("Step-by-step calculation for first row:")
print(f"Row 0: {matrix[0]} * {vector} = {matrix[0] * vector}")
print(f"Sum: {np.sum(matrix[0] * vector)}")
print(f"\nRow 1: {matrix[1]} * {vector} = {matrix[1] * vector}")
print(f"Sum: {np.sum(matrix[1] * vector)}")
Step-by-step calculation for first row: Row 0: [0 1 2 3 4] * [0 1 2 3 4] = [ 0 1 4 9 16] Sum: 30 Row 1: [5 6 7 8 9] * [0 1 2 3 4] = [ 0 6 14 24 36] Sum: 80
Comparison with Other Methods
| Method | Syntax | Readability | Performance |
|---|---|---|---|
einsum() |
einsum('ij,j', A, B) |
Explicit notation | Optimized |
dot() |
A.dot(B) |
Simple | Fast |
@ operator |
A @ B |
Clean | Fast |
Conclusion
The numpy.einsum() method provides an elegant way to perform matrix-vector multiplication using Einstein summation convention. The notation 'ij,j' clearly shows which dimensions are multiplied and summed, making complex linear algebra operations more readable and explicit.
Advertisements
