FRAC_MAT_POW
Computes A^p, the p-th power of a square matrix A, where p can be any real or complex number (though this implementation focuses on real p).
Excel Usage
=FRAC_MAT_POW(matrix, power)
matrix(list[list], required): Square 2D array of numeric values.power(float, required): The power to which to raise the matrix.
Returns (list[list]): 2D array representing the matrix raised to the fractional power.
Example 1: Matrix power 0.5 of a 2x2 identity matrix
Inputs:
| matrix | power | |
|---|---|---|
| 1 | 0 | 0.5 |
| 0 | 1 |
Excel formula:
=FRAC_MAT_POW({1,0;0,1}, 0.5)
Expected output:
| Result | |
|---|---|
| 1 | 0 |
| 0 | 1 |
Example 2: Matrix power 2 of a 2x2 matrix
Inputs:
| matrix | power | |
|---|---|---|
| 1 | 2 | 2 |
| 3 | 4 |
Excel formula:
=FRAC_MAT_POW({1,2;3,4}, 2)
Expected output:
| Result | |
|---|---|
| 7 | 10 |
| 15 | 22 |
Example 3: Matrix raised to power zero returns identity
Inputs:
| matrix | power | |
|---|---|---|
| 2 | 1 | 0 |
| 0 | 3 |
Excel formula:
=FRAC_MAT_POW({2,1;0,3}, 0)
Expected output:
| Result | |
|---|---|
| 1 | 0 |
| 0 | 1 |
Example 4: Matrix raised to negative power
Inputs:
| matrix | power | |
|---|---|---|
| 2 | 0 | -1 |
| 0 | 8 |
Excel formula:
=FRAC_MAT_POW({2,0;0,8}, -1)
Expected output:
| Result | |
|---|---|
| 0.5 | 0 |
| 0 | 0.125 |
Python Code
import numpy as np
from scipy.linalg import fractional_matrix_power as scipy_frac_pow
def frac_mat_pow(matrix, power):
"""
Compute the fractional power of a square matrix.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.fractional_matrix_power.html
This example function is provided as-is without any representation of accuracy.
Args:
matrix (list[list]): Square 2D array of numeric values.
power (float): The power to which to raise the matrix.
Returns:
list[list]: 2D array representing the matrix raised to the fractional power.
"""
try:
EPSILON = 1e-12
def to2d(x):
return [[x]] if not isinstance(x, list) else x
def format_complex_value(value):
real_part = float(value.real)
imag_part = float(value.imag)
if abs(imag_part) <= EPSILON:
return real_part
if abs(real_part) <= EPSILON:
return f"{imag_part}j"
sign = "+" if imag_part >= 0 else "-"
return f"{real_part}{sign}{abs(imag_part)}j"
matrix = to2d(matrix)
if not isinstance(matrix, list) or not matrix:
return "Error: Invalid input: matrix must be a 2D list with at least one row."
if any(not isinstance(row, list) for row in matrix):
return "Error: Invalid input: matrix must be a 2D list with at least one row."
size = len(matrix)
if any(len(row) != size for row in matrix):
return "Error: Invalid input: matrix must be square."
try:
power_value = float(power)
except Exception:
return "Error: Invalid input: power must be a finite numeric value."
if not np.isfinite(power_value):
return "Error: Invalid input: power must be a finite numeric value."
numeric_rows = []
for row in matrix:
numeric_row = []
for value in row:
try:
numeric_row.append(complex(value))
except Exception:
return "Error: Invalid input: matrix entries must be numeric values."
numeric_rows.append(numeric_row)
a = np.array(numeric_rows, dtype=np.complex128)
if not np.isfinite(a.real).all() or not np.isfinite(a.imag).all():
return "Error: Invalid input: matrix entries must be finite numbers."
try:
res = scipy_frac_pow(a, power_value)
except Exception as exc:
return f"Error: scipy.linalg.fractional_matrix_power error: {exc}"
output = []
for row in res:
output_row = []
for value in row:
real_part = float(value.real)
imag_part = float(value.imag)
if not np.isfinite(real_part) or not np.isfinite(imag_part):
return "Error: scipy.linalg.fractional_matrix_power error: non-finite result encountered."
output_row.append(format_complex_value(value))
output.append(output_row)
return output
except Exception as e:
return f"Error: {str(e)}"Online Calculator
Square 2D array of numeric values.
The power to which to raise the matrix.