Worldscope

Cayley-Hamilton Theorem

Palavras-chave:

Publicado em: 06/08/2025

Cayley-Hamilton Theorem: A Practical Guide for Developers

The Cayley-Hamilton theorem is a fundamental result in linear algebra that states that every square matrix satisfies its own characteristic equation. This means if we substitute the matrix itself into its characteristic polynomial, the result is the zero matrix. This article will guide you through understanding and verifying this theorem with a practical code example.

Fundamental Concepts / Prerequisites

To understand the Cayley-Hamilton theorem, you should be familiar with the following concepts:

  • Matrices: Basic matrix operations (addition, subtraction, multiplication).
  • Determinants: How to calculate the determinant of a square matrix.
  • Characteristic Polynomial: The polynomial obtained by taking the determinant of (A - λI), where A is a matrix, λ is a scalar, and I is the identity matrix.
  • Identity Matrix: A square matrix with ones on the main diagonal and zeros everywhere else.
  • Zero Matrix: A matrix with all elements equal to zero.

Core Implementation: Verification in Python

This Python code verifies the Cayley-Hamilton theorem for a given matrix. It calculates the characteristic polynomial, substitutes the matrix into the polynomial, and checks if the result is approximately the zero matrix (allowing for floating-point inaccuracies).


import numpy as np

def characteristic_polynomial(matrix):
    """
    Calculates the characteristic polynomial of a matrix.
    Returns the coefficients in descending order of powers.
    """
    return np.poly(matrix)

def cayley_hamilton_verification(matrix):
    """
    Verifies the Cayley-Hamilton theorem for a given matrix.
    """
    n = matrix.shape[0]
    coeffs = characteristic_polynomial(matrix)

    result = np.zeros_like(matrix)
    for i, coeff in enumerate(coeffs):
        power = n - 1 - i
        if power == 0:
            term = coeff * np.identity(n)
        elif power == 1:
            term = coeff * matrix
        else:
            term = coeff * np.linalg.matrix_power(matrix, power)

        result += term

    # Check if the result is approximately the zero matrix
    return np.allclose(result, np.zeros_like(matrix))


# Example usage:
matrix = np.array([[1, 2], [3, 4]])
is_verified = cayley_hamilton_verification(matrix)

print(f"Matrix: \n{matrix}")
print(f"Cayley-Hamilton Theorem Verified: {is_verified}")

Code Explanation

The code is structured into two functions:

1. `characteristic_polynomial(matrix)`: This function uses the `numpy.poly()` function to compute the characteristic polynomial of the input matrix. The `numpy.poly()` function efficiently calculates the polynomial coefficients based on the matrix's eigenvalues.

2. `cayley_hamilton_verification(matrix)`: This function performs the main verification. It first computes the characteristic polynomial using the `characteristic_polynomial()` function. Then, it iterates through the coefficients of the polynomial. For each coefficient and corresponding power, it calculates the corresponding term (coefficient multiplied by the matrix raised to that power). The terms are summed up. Finally, it uses `numpy.allclose()` to check if the resulting matrix is close to the zero matrix, accounting for potential floating-point errors. The function returns `True` if the theorem is verified, and `False` otherwise.

Complexity Analysis

Time Complexity: The dominant factor in the time complexity is the computation of matrix powers within the `cayley_hamilton_verification` function. Specifically, `np.linalg.matrix_power(matrix, power)` has a time complexity of O(n3 * log(power)), where n is the dimension of the matrix. Since 'power' can go up to n-1, a simplified upper bound for the matrix power calculation in the loop is O(n3 * log(n)). Because this calculation is done 'n' times in the loop, the overall time complexity is O(n4 * log(n)). The calculation of the characteristic polynomial via numpy is not as easily expressed with standard matrix operation complexity since it is an optimized numerical algorithm.

Space Complexity: The space complexity is dominated by the storage of the matrix and intermediate results. The matrix itself requires O(n2) space. The characteristic polynomial coefficients require O(n) space. The intermediate `term` and `result` matrices also require O(n2) space. Therefore, the overall space complexity is O(n2).

Alternative Approaches

Another approach involves directly calculating the eigenvalues of the matrix and using them to determine the characteristic polynomial. Then, the substitution and verification steps would be the same as in the given implementation. However, calculating eigenvalues can be computationally expensive, especially for large matrices. While NumPy has optimized routines for eigenvalue calculation, the overall complexity is generally similar, and our current approach leverages the optimized `np.poly` function making it suitable. Another option is the Leverrier-Faddeev algorithm that calculates the characteristic polynomial in O(n^4) time, avoiding the need for eigenvalues calculation.

Conclusion

The Cayley-Hamilton theorem is a powerful tool in linear algebra. This article provided a practical understanding of the theorem through a Python implementation, demonstrating how to verify the theorem for a given matrix. Understanding the code, its complexity, and alternative approaches allows developers to apply this theorem in various numerical and scientific computing tasks.