SQRTM

Computes the matrix square root X such that X^2 = A.

Excel Usage

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

Returns (list[list]): 2D array representing the matrix square root.

Example 1: Square root of a 2x2 identity matrix

Inputs:

matrix
1 0
0 1

Excel formula:

=SQRTM({1,0;0,1})

Expected output:

Result
1 0
0 1
Example 2: Square root of a 2x2 matrix

Inputs:

matrix
4 0
0 9

Excel formula:

=SQRTM({4,0;0,9})

Expected output:

Result
2 0
0 3
Example 3: Square root of scalar promoted to 1x1 matrix

Inputs:

matrix
16

Excel formula:

=SQRTM(16)

Expected output:

4

Example 4: Square root of diagonal matrix with mixed magnitudes

Inputs:

matrix
25 0
0 0.25

Excel formula:

=SQRTM({25,0;0,0.25})

Expected output:

Result
5 0
0 0.5

Python Code

import numpy as np
from scipy.linalg import sqrtm as scipy_sqrtm

def sqrtm(matrix):
    """
    Compute the matrix square root.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.sqrtm.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]: 2D array representing the matrix square root.
    """
    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."

        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_sqrtm(a, disp=False)
        except Exception as exc:
            return f"Error: scipy.linalg.sqrtm 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.sqrtm 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.