EIGVALSH

Compute only the eigenvalues of a symmetric (real) or Hermitian (complex) matrix. This is more efficient than the full eigenvalue solver when eigenvectors are not needed.

Excel Usage

=EIGVALSH(matrix, lower)
  • matrix (list[list], required): Square symmetric or Hermitian 2D array of numeric values.
  • lower (bool, optional, default: true): Whether to use the lower or upper triangular part of the matrix.

Returns (list[list]): Row vector containing the real eigenvalues.

Example 1: Eigenvalues only of 2x2 symmetric matrix

Inputs:

matrix
1 2
2 1

Excel formula:

=EIGVALSH({1,2;2,1})

Expected output:

Result
-1 3

Python Code

import numpy as np
from scipy.linalg import eigvalsh as scipy_eigvalsh

def eigvalsh(matrix, lower=True):
    """
    Compute eigenvalues of a real symmetric or complex Hermitian matrix.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.eigvalsh.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        matrix (list[list]): Square symmetric or Hermitian 2D array of numeric values.
        lower (bool, optional): Whether to use the lower or upper triangular part of the matrix. Default is True.

    Returns:
        list[list]: Row vector containing the real eigenvalues.
    """
    try:
        def to2d(x):
            return [[x]] if not isinstance(x, list) else x

        matrix = to2d(matrix)

        if not isinstance(matrix, list) or not matrix or not all(isinstance(row, list) for row in matrix):
            return "Error: matrix must be a non-empty 2D list"

        n = len(matrix)
        if any(len(row) != n for row in matrix):
            return "Error: matrix must be square (n x n)"

        try:
            a = np.array(matrix, dtype=float)
        except (ValueError, TypeError):
            return "Error: matrix must contain numeric values"

        if not np.all(np.isfinite(a)):
            return "Error: matrix must contain only finite numbers"

        try:
            ev = scipy_eigvalsh(a, lower=lower)
        except Exception as e:
            return f"Error: {str(e)}"

        return [ev.tolist()]

    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

Square symmetric or Hermitian 2D array of numeric values.
Whether to use the lower or upper triangular part of the matrix.