HANKEL

Constructs a Hankel matrix, which is a matrix where each ascending diagonal from left to right is constant.

Excel Usage

=HANKEL(column, row)
  • column (list[list], required): First column of the matrix.
  • row (list[list], optional, default: null): Last row of the matrix. The last element of ‘column’ and the first element of ‘row’ should be the same.

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

Example 1: Hankel matrix from column only

Inputs:

column
1 2 3

Excel formula:

=HANKEL({1,2,3})

Expected output:

Result
1 2 3
2 3 0
3 0 0
Example 2: Hankel matrix with column and row

Inputs:

column row
1 2 3 3 4 5

Excel formula:

=HANKEL({1,2,3}, {3,4,5})

Expected output:

Result
1 2 3
2 3 4
3 4 5
Example 3: Hankel matrix from column-style input

Inputs:

column
1
2
3
4

Excel formula:

=HANKEL({1;2;3;4})

Expected output:

Result
1 2 3 4
2 3 4 0
3 4 0 0
4 0 0 0
Example 4: Hankel matrix from scalar column value

Inputs:

column
5

Excel formula:

=HANKEL(5)

Expected output:

5

Python Code

import numpy as np
from scipy.linalg import hankel as scipy_hankel

def hankel(column, row=None):
    """
    Construct a Hankel matrix.

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

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

    Args:
        column (list[list]): First column of the matrix.
        row (list[list], optional): Last row of the matrix. The last element of 'column' and the first element of 'row' should be the same. Default is None.

    Returns:
        list[list]: 2D array representing the Hankel matrix.
    """
    try:
        def to2d(x):
            return [[x]] if not isinstance(x, list) else x

        column = to2d(column)
        if not isinstance(column, list) or not all(isinstance(row, list) for row in column):
            return "Error: column must be a 2D list"

        flat_column = []
        for row_values in column:
            for value in row_values:
                try:
                    flat_column.append(float(value))
                except (TypeError, ValueError):
                    return "Error: column must contain only numeric values"

        if not flat_column:
            return "Error: column must contain at least one numeric value"

        c = np.array(flat_column, dtype=float)

        if row is not None:
            row = to2d(row)
            if not isinstance(row, list) or not all(isinstance(row_values, list) for row_values in row):
                return "Error: row must be a 2D list"

            flat_row = []
            for row_values in row:
                for value in row_values:
                    try:
                        flat_row.append(float(value))
                    except (TypeError, ValueError):
                        return "Error: row must contain only numeric values"

            if not flat_row:
                return "Error: row must contain at least one numeric value"

            r = np.array(flat_row, dtype=float)
        else:
            r = None

        res = scipy_hankel(c, r)
        return res.tolist()

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

Online Calculator

First column of the matrix.
Last row of the matrix. The last element of 'column' and the first element of 'row' should be the same.