NumPy Einsum Function

NumPyBeginner
Practice Now

Introduction

This challenge introduces you to numpy.einsum, a powerful and versatile function for tensor operations. Instead of calling specific functions like dot, trace, or multiply, einsum allows you to define these operations and more using a simple string-based notation. By completing this challenge, you will gain a solid understanding of how to use einsum for common and complex array manipulations.

This is a Challenge, which differs from a Guided Lab. You need to complete the tasks independently. If you encounter difficulties, feel free to ask Labby for hints or check the provided solution.

Task 1: Matrix Multiplication

Matrix multiplication is a fundamental operation in linear algebra. With einsum, you can express this operation by defining how the indices of the input arrays should be combined. For two matrices A (shape m \times n) and B (shape n \times p), the product C (shape m \times p) is defined. In einsum notation, this is ij,jk->ik. The repeated index j is summed over.

Your Task

Complete the matmul function in the file matmul.py. This function should use numpy.einsum to multiply two matrices, A and B, and return the result.

File to Edit

  • /home/labex/project/matmul.py

The file has been created for you with the following content:

import numpy as np

def matmul(A: np.ndarray, B: np.ndarray) -> np.ndarray:
    ## TODO: Perform matrix multiplication using Numpy's einsum function.
    pass
✨ Check Solution and Practice

Task 2: Trace of a Matrix

The trace of a square matrix is the sum of the elements on its main diagonal. einsum provides a very concise way to specify this summation. For a matrix A, the diagonal elements are those where the row index equals the column index (A_{ii}). The einsum string for this operation is ii->. The repeated index i indicates summation, and the empty output means the result is a scalar.

Your Task

Complete the trace function in trace_of_matrix.py. Use numpy.einsum to calculate the trace of a given square matrix A.

File to Edit

  • /home/labex/project/trace_of_matrix.py

The file has been created for you with the following content:

import numpy as np

def trace(A: np.ndarray) -> float:
    ## TODO: Compute the trace of a matrix using Numpy's einsum function.
    pass
✨ Check Solution and Practice

Task 3: Hadamard Product

The Hadamard product, or element-wise product, creates a new matrix where each element is the product of the corresponding elements from two input matrices of the same shape. For two matrices A and B of shape (m \times n), the Hadamard product C is also of shape (m \times n), where C_{ij} = A_{ij} \times B_{ij}. The einsum string for this is ij,ij->ij.

Your Task

Complete the hadamard_product function in hadamard_product.py. This function should compute the element-wise product of two matrices, A and B, using numpy.einsum.

File to Edit

  • /home/labex/project/hadamard_product.py

The file has been created for you with the following content:

import numpy as np

def hadamard_product(A: np.ndarray, B: np.ndarray) -> np.ndarray:
    ## TODO: Compute the Hadamard product of two matrices using Numpy's einsum function.
    pass
✨ Check Solution and Practice

Task 4: Tensor Contraction

einsum truly shines when dealing with higher-dimensional arrays, or tensors. Tensor contraction is a generalization of matrix multiplication. In this task, you will contract a 3D tensor A of shape (m \times n \times p) with a 2D matrix B of shape (p \times q). The goal is to sum over the shared dimension p, resulting in a new tensor of shape (m \times n \times q). The einsum string for this operation is ijk,kl->ijl.

Your Task

Complete the tensor_contract function in tensor_contract.py. Use numpy.einsum to perform a contraction between a 3D tensor A and a 2D matrix B.

File to Edit

  • /home/labex/project/tensor_contract.py

The file has been created for you with the following content:

import numpy as np

def tensor_contract(A: np.ndarray, B: np.ndarray) -> np.ndarray:
    ## TODO: Perform tensor contraction between two tensors using Numpy's einsum function.
    pass
✨ Check Solution and Practice

Summary

Congratulations on completing the NumPy Einsum Challenge! You have successfully used einsum to perform matrix multiplication, calculate a matrix trace, compute the Hadamard product, and perform tensor contraction. This demonstrates your ability to use Einstein summation notation to write concise, efficient, and readable code for complex array operations.