question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`linalg.hankel` returns unexpected results

See original GitHub issue

A Hankel matrix is supposed to be a square matrix, but the scipy implementation can sometimes return non-square matrices, which I think is misleading/unexpected:

In [1]: from scipy import linalg

In [2]: linalg.hankel([1, 2, 3], [4, 5, 6, 7, 8, 9])
Out[2]:
array([[1, 2, 3, 5, 6, 7],
       [2, 3, 5, 6, 7, 8],
       [3, 5, 6, 7, 8, 9]])

Note that 4 is not included in the output, which may be unexpected (I know that the documentation says that r[0] is ignored, but the initial description of r is “the last row” of the matrix, so at the very least the documentation could do a better job clarifying). This output seems much more reasonable as it captures all elements from both arrays and returns a square matrix:

array([[1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6],
       [3, 4, 5, 6, 7],
       [4, 5, 6, 7, 8],
       [5, 6, 7, 8, 9]])

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ilayncommented, Oct 7, 2021

There are a couple of things to unpack here. First is that when given two arguments hence working in the context of nonsymmetric Hankel matrix the result is exactly like as required. It has to be a rectangle matrix otherwise the result is not a Hankel matrix.

The data problem here is that the user supplied arrays are not compatible for forming a Hankel matrix, that is to say, the last element of the c must match the first element of r. Because that is the property of being a Hankel matrix; constant anti-diagonals.

Matlab handles the violation with a warning in scipy historically it is chosen to ignore the first element since it is supposed to be the last element of the column anyways otherwise Hankelness is not preserved.

The point can be emphasized better and I’d appreciate a PR for that but this property of the Hankel matrix can be expected also from the user since the result should be familiar.

0reactions
ilayncommented, Oct 7, 2021

No problem at all. The difference is the way the function is called. Suppose you are trying to form the array you got from the current implementation

array([[1, 2, 3, 5, 6, 7],
       [2, 3, 5, 6, 7, 8],
       [3, 5, 6, 7, 8, 9]])

what should be the input in your proposed syntax?

Read more comments on GitHub >

github_iconTop Results From Across the Web

scipy.linalg.hankel — SciPy v1.9.3 Manual
Construct a Hankel matrix. The Hankel matrix has constant anti-diagonals, with c as its first column and r as its last row. If...
Read more >
scipy/linalg.py at main - GitHub
scipy/benchmarks/benchmarks/linalg.py ... return rand(*size) ... Retain old benchmark results (remove this if changing the benchmark).
Read more >
arXiv:2002.06621v3 [math.NA] 4 Sep 2020
Experimental results show that the proposed algorithm usually achieves good accuracy and shows a higher robustness with respect to the initial ...
Read more >
Unexpected result using linalg.matrix_rank in numpy.linalg ...
This was based on the definition of rank as being the number of rows of the largest determinant so formed from the matrix....
Read more >
Foundations of System Theory: The Hankel Matrix* - CORE
for nonlinear state-space description, we define the Hankel matrix for an arbitrary adjoint system. This returns the usual definition for linear systems,.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found