LOGM

Computes the matrix logarithm, which is the inverse of the matrix exponential. For a matrix A, it finds a matrix L such that \exp(L) = A.

Excel Usage

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

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

Example 1: Logarithm of a diagonal matrix

Inputs:

matrix
2.718281828459 0
0 7.389056098931

Excel formula:

=LOGM({2.718281828459,0;0,7.389056098931})

Expected output:

Result
1 0
0 2
Example 2: Logarithm of identity matrix is zero matrix

Inputs:

matrix
1 0
0 1

Excel formula:

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

Expected output:

Result
0 0
0 0
Example 3: Logarithm of scalar promoted to 1x1 matrix

Inputs:

matrix
2.718281828459045

Excel formula:

=LOGM(2.718281828459045)

Expected output:

1

Example 4: Logarithm of diagonal matrix with fractional exponents

Inputs:

matrix
1.6487212707001282 0
0 4.4816890703380645

Excel formula:

=LOGM({1.6487212707001282,0;0,4.4816890703380645})

Expected output:

Result
0.5 0
0 1.5

Python Code

import numpy as np
from scipy.linalg import logm as scipy_logm

def logm(matrix):
    """
    Compute matrix logarithm.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.logm.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 logarithm.
    """
    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_logm(a, disp=False)
        except Exception as exc:
            return f"Error: scipy.linalg.logm 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.logm 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.