EXPM
Overview
The EXPM
function computes the matrix exponential of a square matrix. The matrix exponential is a fundamental operation in linear algebra, with applications in solving systems of linear differential equations, quantum mechanics, control theory, and more. The matrix exponential of a square matrix is defined as:
This function uses the algorithm from Awad H. Al-Mohy and Nicholas J. Higham (2009), which is a scaling and squaring method with a variable-order Padé approximation, as implemented in scipy.linalg.expm . For more details, see the SciPy documentation .
This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=EXPM(matrix)
matrix
(2D list, required): A square matrix (n x n) of real or complex numbers.
The function returns a 2D list (n x n) representing the matrix exponential of the input matrix, or a 2D list of strings with an error message if the input is invalid.
Examples
Example 1: Exponential of the Zero Matrix
In Excel:
=EXPM({0,0;0,0})
Expected output:
1.0 | 0.0 |
0.0 | 1.0 |
Python Code
import numpy as np
from scipy.linalg import expm as scipy_expm
def expm(matrix):
"""
Compute the matrix exponential of a square matrix.
Args:
matrix: 2D list (n x n) of real or complex numbers.
Returns:
2D list (n x n) representing the matrix exponential, or a 2D list of strings with an error message if input is invalid.
This example function is provided as-is without any representation of accuracy.
"""
# Validate input is a 2D list
if not isinstance(matrix, list) or not matrix or not isinstance(matrix[0], list):
return [["Invalid input: matrix must be a 2D list."]]
n = len(matrix)
if any(len(row) != n for row in matrix):
return [["Invalid input: matrix must be square."]]
try:
arr = np.array(matrix, dtype=np.complex128)
except Exception:
return [["Invalid input: matrix must contain only numbers."]]
try:
result = scipy_expm(arr)
except Exception as e:
return [[f"scipy.linalg.expm error: {e}"]]
# Convert result to 2D list, round real and imag parts for display
out = []
for row in result:
out_row = []
for val in row:
if np.iscomplex(val):
if abs(val.imag) < 1e-10:
out_row.append(float(np.round(val.real, 4)))
elif abs(val.real) < 1e-10:
out_row.append(f"{np.round(val.imag, 4)}j")
else:
out_row.append(f"{np.round(val.real, 4)}+{np.round(val.imag, 4)}j")
else:
out_row.append(float(np.round(val, 4)))
out.append(out_row)
return out