EIGH

Compute eigenvalues and optionally eigenvectors for a symmetric (real) or Hermitian (complex) matrix. These matrices are guaranteed to have real eigenvalues. This function is more efficient and numerically stable for these matrix types than the general eigenvalue solver.

Excel Usage

=EIGH(matrix, lower, eigh_ret_type)
  • 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.
  • eigh_ret_type (str, optional, default: “vals”): The component to return.

Returns (list[list]): 2D array containing the requested component (eigenvalues or eigenvectors).

Example 1: Eigenvalues of 2x2 symmetric matrix

Inputs:

matrix eigh_ret_type
1 2 vals
2 1

Excel formula:

=EIGH({1,2;2,1}, "vals")

Expected output:

Result
-1 3
Example 2: Eigenvectors of 2x2 symmetric matrix

Inputs:

matrix eigh_ret_type
1 2 vecs
2 1

Excel formula:

=EIGH({1,2;2,1}, "vecs")

Expected output:

Result
-0.707107 0.707107
0.707107 0.707107

Python Code

import numpy as np
from scipy.linalg import eigh as scipy_eigh

def eigh(matrix, lower=True, eigh_ret_type='vals'):
    """
    Solve eigenvalue problem for a real symmetric or complex Hermitian matrix.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.eigh.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.
        eigh_ret_type (str, optional): The component to return. Valid options: Eigenvalues, Eigenvectors. Default is 'vals'.

    Returns:
        list[list]: 2D array containing the requested component (eigenvalues or eigenvectors).
    """
    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:
            vals, vecs = scipy_eigh(a, lower=lower)
        except Exception as e:
            return f"Error: {str(e)}"

        rt = eigh_ret_type.lower()
        if rt == "vals":
            return [vals.tolist()]
        else:
            return vecs.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.
The component to return.