EIGVALS

Compute the eigenvalues of a square matrix. For a general matrix, eigenvalues can be complex. Complex eigenvalues are returned as Excel Data Types, allowing extraction of Real, Imaginary, Magnitude, and Phase properties.

Excel Usage

=EIGVALS(matrix)
  • matrix (list[list], required): Square 2D array of numeric values.

Returns (list[list]): Row vector (1xN) containing the eigenvalues. Complex values are returned as Data Types.

Example 1: Eigenvalues of 2x2 matrix

Inputs:

matrix
1 2
3 4

Excel formula:

=EIGVALS({1,2;3,4})

Expected output:

Result
-0.372281 5.37228
Example 2: Complex eigenvalues of a rotation matrix

Inputs:

matrix
0 -1
1 0

Excel formula:

=EIGVALS({0,-1;1,0})

Expected output:

Result
[object Object] [object Object]

Python Code

import numpy as np
from scipy.linalg import eigvals as scipy_eigvals

def eigvals(matrix):
    """
    Compute eigenvalues of a general square matrix.

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

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

    Args:
        matrix (list[list]): Square 2D array of numeric values.

    Returns:
        list[list]: Row vector (1xN) containing the eigenvalues. Complex values are returned as Data Types.
    """
    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_eigvals(a)
        except Exception as e:
            return f"Error: {str(e)}"

        def format_complex(res):
            if not np.iscomplexobj(res):
                return (res.reshape(1, -1) if res.ndim == 1 else res).tolist()

            out = []
            for row in (res.reshape(1, -1) if res.ndim == 1 else res):
                out_row = []
                for val in row:
                    if val.imag == 0.0:
                        out_row.append(float(val.real))
                    else:
                        out_row.append({
                            "type": "Double",
                            "basicValue": float(val.real),
                            "properties": {
                                "Real": {"type": "Double", "basicValue": float(val.real)},
                                "Imaginary": {"type": "Double", "basicValue": float(val.imag)},
                                "Magnitude": {"type": "Double", "basicValue": float(np.abs(val))},
                                "Phase": {"type": "Double", "basicValue": float(np.angle(val))}
                            }
                        })
                out.append(out_row)
            return out

        return format_complex(ev)

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

Online Calculator

Square 2D array of numeric values.