HESSENBERG

The Hessenberg decomposition factors a square matrix A into a unitary matrix Q and a Hessenberg matrix H.

Excel Usage

=HESSENBERG(matrix, hess_ret_type)
  • matrix (list[list], required): Square 2D array of numeric values to decompose.
  • hess_ret_type (str, optional, default: “h”): The component of the decomposition to return.

Returns (list[list]): 2D array representing the requested component (H or Q).

Example 1: Hessenberg form H of 4x4 matrix

Inputs:

matrix hess_ret_type
2 5 8 7 h
5 2 2 8
7 5 6 6
5 4 4 8

Excel formula:

=HESSENBERG({2,5,8,7;5,2,2,8;7,5,6,6;5,4,4,8}, "h")

Expected output:

Result
2 -11.6584 1.42005 0.253491
-9.94987 14.5354 -5.31022 2.43082
0 -1.83299 0.3897 -0.51527
0 0 -3.8319 1.07495
Example 2: Transformation matrix Q

Inputs:

matrix hess_ret_type
2 5 8 7 q
5 2 2 8
7 5 6 6
5 4 4 8

Excel formula:

=HESSENBERG({2,5,8,7;5,2,2,8;7,5,6,6;5,4,4,8}, "q")

Expected output:

Result
1 0 0 0
0 -0.502519 -0.475751 -0.721897
0 -0.703526 -0.260306 0.66128
0 -0.502519 0.84018 -0.203895

Python Code

import numpy as np
from scipy.linalg import hessenberg as scipy_hessenberg

def hessenberg(matrix, hess_ret_type='h'):
    """
    Compute Hessenberg form of a matrix.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.hessenberg.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        matrix (list[list]): Square 2D array of numeric values to decompose.
        hess_ret_type (str, optional): The component of the decomposition to return. Valid options: Hessenberg Form (H), Transformation Matrix (Q). Default is 'h'.

    Returns:
        list[list]: 2D array representing the requested component (H or Q).
    """
    try:
        def to2d(x):
            return [[x]] if not isinstance(x, list) else x

        matrix = to2d(matrix)

        if not isinstance(matrix, list) or not matrix or not all(isinstance(row, list) for row in matrix):
            return "Error: matrix must be a non-empty 2D list"

        n = len(matrix)
        if any(len(row) != n for row in matrix):
            return "Error: matrix must be square (n x n)"

        try:
            a = np.array(matrix, dtype=float)
        except (ValueError, TypeError):
            return "Error: matrix must contain numeric values"

        if not np.all(np.isfinite(a)):
            return "Error: matrix must contain only finite numbers"

        try:
            if hess_ret_type.lower() == "q":
                h, q = scipy_hessenberg(a, calc_q=True)
                return q.tolist()
            else:
                h = scipy_hessenberg(a, calc_q=False)
                return h.tolist()
        except Exception as e:
            return f"Error: {str(e)}"

    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

Square 2D array of numeric values to decompose.
The component of the decomposition to return.