SciPy - hankel() Function



The scipy.linalg.hankel() method generates a Hankel matrix which has equal anti-diagonals, and the first column is defined by array c and the final row by array r. For constant anti-diagonals, if the last element of c is different from the first element of r, it is substituted with the last element of c. In case r is not defined, then it is an array of zeros in the shape of c.

Hankel matrices are applied to model systems that are described by well-defined structured patterns in signal processing, control systems, and time series analysis.

Errors may arise if the input c is not a 1D array, or if r is provided but has incompatible dimensions. Using extremely big arrays may result in considerable memory consumption and performance difficulties.

The Hankel matrix can be applied with eigenvalue computations such as scipy.linalg.eig() to study the stability of systems and matrix decompositions by using SVD like scipy.linalg.svd(), where it applies dimensionality reduction in signal processing.

Syntax

The syntax for the SciPy hankel() method is as follows −

.hankel(c, r=None)

Parameters

This method accepts the following parameters −

  • c (array_like) − A 1D array specifying the first column of the Hankel matrix.

  • r (array_like, optional) − A 1D array specifying the last row of the Hankel matrix. If not provided, r defaults to c.

Return Value

H (ndarray) − A 2D Hankel matrix of size (m,n), where m is the length of c and n is the length of r. The elements are constructed such that H[i,j]=c[i+j] if i+j < m, otherwise r[i+jm+1].

Example 1

This code demonstrates how to create a Hankel matrix using the hankel() method. The input array c = [1, 2, 3, 4] is the first column of the matrix. Since the last row (r) is not defined, the process assumes that it is the reverse of c. The resulting matrix is constructed such that every increasing diagonal from top-left to bottom-right contains the same value.

from scipy.linalg import hankel

# First column of the Hankel matrix
c = [1, 2, 3, 4]

# Generate a basic Hankel matrix
matrix = hankel(c)
print("Hankel Matrix:")
print(matrix)

When we run above program, it produces following result

Hankel Matrix:
[[1 2 3 4]
 [2 3 4 0]
 [3 4 0 0]
 [4 0 0 0]]

Example 2

This code creates a Hankel matrix by using the method hankel() and first column c = [1, 2, 3], the last row is r = [4, 5, 6]. It is designed in such a way that each diagonal (from top-left to bottom-right) is identical. It begins with c filling the diagonals with components of r, and the last component of r will be reused whenever necessary.

The resulting matrix begins with c along the diagonals starting with the first column, ending in r. So for example, the first row is the concatenation of c[0], which is 1, and all elements of r, and the subsequent rows begin with the next element of c and then continue diagonally. The outcome is that this is structured similarly to the input.

from scipy.linalg import hankel

# First column and last row
c = [1, 2, 3]
r = [4, 5, 6]
matrix = hankel(c, r)
print("Hankel Matrix:")
print(matrix)

Following is an output of the above code

Hankel Matrix:
[[1 2 3]
 [2 3 5]
 [3 5 6]]

Example 3

This example demonstrates how to compute the eigenvalues of a Hankel matrix in order to analyze its spectral properties. The matrix is generated with c = [1, 2, 3, 4] as the first column and r = [5, 6, 7, 8] as the last row. The scipy.linalg.eig() function computes the eigenvalues, which contain information about the stability and spectral properties of the matrix.

from scipy.linalg import hankel, eig

c = [1, 2, 3, 4]
r = [5, 6, 7, 8]
matrix = hankel(c, r)

# Compute eigenvalues
eigenvalues, _ = eig(matrix)

print("Hankel Matrix:")
print(matrix)
print("\nEigenvalues:")
print(eigenvalues)

Output of the above code is as follows

Hankel Matrix:
[[1 2 3 4]
 [2 3 4 6]
 [3 4 6 7]
 [4 6 7 8]]

Eigenvalues:
[19.17433156+0.j -1.38699959+0.j -0.34011158+0.j  0.55277961+0.j]
scipy_special_matrices_functions.htm
Advertisements