COSM

Computes the matrix cosine of the square matrix A using the matrix exponential.

Excel Usage

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

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

Example 1: Matrix cosine of a zero matrix

Inputs:

matrix
0 0
0 0

Excel formula:

=COSM({0,0;0,0})

Expected output:

Result
1 0
0 1
Example 2: Matrix cosine of diagonal matrix

Inputs:

matrix
3.141592653589793 0
0 0

Excel formula:

=COSM({3.141592653589793,0;0,0})

Expected output:

Result
-1 0
0 1
Example 3: Matrix cosine of 2x2 identity matrix

Inputs:

matrix
1 0
0 1

Excel formula:

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

Expected output:

Result
0.540302 0
0 0.540302
Example 4: Matrix cosine of scalar promoted to 1x1 matrix

Inputs:

matrix
0.5

Excel formula:

=COSM(0.5)

Expected output:

0.877583

Python Code

import numpy as np
from scipy.linalg import cosm as scipy_cosm

def cosm(matrix):
    """
    Compute the matrix cosine.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.cosm.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 cosine.
    """
    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_cosm(a)
        except Exception as exc:
            return f"Error: scipy.linalg.cosm 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.cosm 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.