Multiply Two Matrices

problem-solving
Published

December 8, 2024

Matrix multiplication is a fundamental operation in linear algebra with wide-ranging applications in computer science, data science, and machine learning. This post will guide you through the process of multiplying two matrices in Python, exploring different approaches and highlighting their efficiency.

Understanding Matrix Multiplication

Before diving into the code, let’s briefly review the rules of matrix multiplication. Two matrices can be multiplied only if the number of columns in the first matrix equals the number of rows in the second matrix. The resulting matrix will have the same number of rows as the first matrix and the same number of columns as the second matrix.

The element at position (i, j) in the resulting matrix is calculated as the dot product of the i-th row of the first matrix and the j-th column of the second matrix. This involves multiplying corresponding elements and summing the results.

Method 1: Using Nested Loops (Basic Approach)

This method directly implements the matrix multiplication algorithm using nested loops. It’s easy to understand but can be less efficient for large matrices.

def matrix_multiply_nested_loops(A, B):
    """Multiplies two matrices using nested loops.

    Args:
      A: The first matrix (list of lists).
      B: The second matrix (list of lists).

    Returns:
      The resulting matrix (list of lists), or None if multiplication is not possible.
    """
    rows_A = len(A)
    cols_A = len(A[0])
    rows_B = len(B)
    cols_B = len(B[0])

    if cols_A != rows_B:
        return None  # Matrices cannot be multiplied

    C = [[0 for row in range(cols_B)] for col in range(rows_A)]  # Initialize result matrix

    for i in range(rows_A):
        for j in range(cols_B):
            for k in range(cols_A):
                C[i][j] += A[i][k] * B[k][j]

    return C


# Example usage:
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
result = matrix_multiply_nested_loops(A, B)
print(f"Result using nested loops: {result}")

Method 2: Using NumPy (Efficient Approach)

NumPy is a powerful Python library for numerical computing. Its dot() function provides a highly optimized way to perform matrix multiplication. This method is faster and more efficient for larger matrices.

import numpy as np

def matrix_multiply_numpy(A, B):
    """Multiplies two matrices using NumPy.

    Args:
      A: The first matrix (list of lists or NumPy array).
      B: The second matrix (list of lists or NumPy array).

    Returns:
      The resulting matrix (NumPy array), or None if multiplication is not possible.

    """
    try:
        A_np = np.array(A)
        B_np = np.array(B)
        return np.dot(A_np, B_np)
    except ValueError:
        return None


#Example Usage
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
result = matrix_multiply_numpy(A,B)
print(f"Result using NumPy: {result}")

This NumPy approach uses optimized C code under the hood, making it far superior in performance for larger datasets. For most practical applications, using NumPy is strongly recommended.

Handling Errors and Invalid Inputs

Both methods include basic error handling to check if the matrix dimensions allow for multiplication. More error handling could be added to validate input types and handle potential exceptions more comprehensively. For instance, you might want to check if the input matrices are rectangular or contain only numeric values.