Vector inner product with Einstein summation convention in Python

To compute the inner product of vectors with Einstein summation convention, use the numpy.einsum() method in Python. The first parameter is the subscript string that specifies the summation pattern, and the second parameter contains 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 automatically.

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.

Syntax

numpy.einsum(subscripts, *operands)

Parameters:

  • subscripts ? String specifying the subscripts for summation
  • operands ? Arrays to operate on

Basic Example

Let's compute the inner product of a vector with itself ?

import numpy as np

# Create a simple vector
vector = np.array([1, 2, 3, 4])

print("Vector:", vector)
print("Shape:", vector.shape)

# Compute inner product using einsum
inner_product = np.einsum('i,i', vector, vector)
print("Inner product:", inner_product)

# Verify with dot product
dot_product = np.dot(vector, vector)
print("Verification with dot:", dot_product)
Vector: [1 2 3 4]
Shape: (4,)
Inner product: 30
Verification with dot: 30

How Einstein Summation Works

The subscript 'i,i' means we're multiplying corresponding elements and summing over index i. This is equivalent to: ??(a? × b?)

import numpy as np

# Two different vectors
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

print("Vector a:", a)
print("Vector b:", b)

# Inner product of two different vectors
result = np.einsum('i,i', a, b)
print("Inner product a·b:", result)

# Manual calculation: 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32
manual = sum(a * b)
print("Manual verification:", manual)
Vector a: [1 2 3]
Vector b: [4 5 6]
Inner product a·b: 32
Manual verification: 32

Detailed Example with Array Properties

import numpy as np

# Creating a numpy array using arange()
arr = np.arange(4)

# Display array properties
print("Our Array...\n", arr)
print("\nDimensions of our Array...\n", arr.ndim)
print("\nDatatype of our Array object...\n", arr.dtype)
print("\nShape of our Array object...\n", arr.shape)

# Compute inner product using Einstein summation
result = np.einsum('i,i', arr, arr)
print("\nResult (inner product)...\n", result)

# Show the calculation step by step
print("\nStep by step:")
print("arr * arr =", arr * arr)
print("Sum =", sum(arr * arr))
Our Array...
 [0 1 2 3]

Dimensions of our Array...
 1

Datatype of our Array object...
 int64

Shape of our Array object...
 (4,)

Result (inner product)...
 14

Step by step:
arr * arr = [0 1 4 9]
Sum = 14

Comparison of Methods

Method Syntax Best For
einsum() np.einsum('i,i', a, b) Complex tensor operations
dot() np.dot(a, b) Standard vector operations
sum() np.sum(a * b) Simple element-wise operations

Conclusion

The numpy.einsum() method provides a powerful way to compute vector inner products using Einstein summation notation. While np.dot() is simpler for basic operations, einsum() offers greater flexibility for complex multi-dimensional array operations.

Updated on: 2026-03-26T20:11:31+05:30

398 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements