EIG
Solve an ordinary or generalized eigenvalue problem for a square matrix. Returns eigenvalues or eigenvectors. Since eigenvalues and eigenvectors can be complex for general matrices, any complex values are returned as Excel Data Types with Real, Imaginary, Magnitude, and Phase properties.
Excel Usage
=EIG(matrix, eig_ret_type)
matrix(list[list], required): Square 2D array of numeric values.eig_ret_type(str, optional, default: “vals”): The component to return.
Returns (list[list]): 2D array containing the requested component (eigenvalues or eigenvectors). Complex values are Data Types.
Example 1: Eigenvalues of 2x2 matrix
Inputs:
| matrix | eig_ret_type | |
|---|---|---|
| 1 | 2 | vals |
| 3 | 4 |
Excel formula:
=EIG({1,2;3,4}, "vals")
Expected output:
| Result | |
|---|---|
| -0.372281 | 5.37228 |
Example 2: Eigenvectors of a rotation matrix
Inputs:
| matrix | eig_ret_type | |
|---|---|---|
| 0 | -1 | vecs |
| 1 | 0 |
Excel formula:
=EIG({0,-1;1,0}, "vecs")
Expected output:
| Result | |
|---|---|
| 0.707107 | 0.707107 |
| [object Object] | [object Object] |
Python Code
import numpy as np
from scipy.linalg import eig as scipy_eig
def eig(matrix, eig_ret_type='vals'):
"""
Compute eigenvalues and eigenvectors of a general square matrix.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.eig.html
This example function is provided as-is without any representation of accuracy.
Args:
matrix (list[list]): Square 2D array of numeric values.
eig_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). Complex values are 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:
vals, vecs = scipy_eig(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
rt = eig_ret_type.lower()
if rt == "vals":
return format_complex(vals)
else:
return format_complex(vecs)
except Exception as e:
return f"Error: {str(e)}"Online Calculator
Square 2D array of numeric values.
The component to return.