Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linalg.hankel returns unexpected results #14788

Open
ddejohn opened this issue Sep 30, 2021 · 5 comments
Open

linalg.hankel returns unexpected results #14788

ddejohn opened this issue Sep 30, 2021 · 5 comments
Labels
Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org scipy.linalg

Comments

@ddejohn
Copy link

ddejohn commented Sep 30, 2021

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]])
@jdkramhoft
Copy link

Why is r[0] ignored? Is there a practical reason for this?

@ilayn
Copy link
Member

ilayn commented 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.

@ilayn ilayn added Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org scipy.linalg labels Oct 7, 2021
@ilayn
Copy link
Member

ilayn commented Oct 7, 2021

For the last example the input can be entered as linalg.hankel([1, 2, 3, 4, 5], [5, 6, 7, 8, 9]) which has all the necessary information in the correct number of elements and data.

@ddejohn
Copy link
Author

ddejohn commented Oct 7, 2021

Please forgive my ignorance, I'm certainly much less experienced in this area and was not familiar with non-square Hankel matrices. However, I'm not entirely sure I understand the difference between my example, which also exhibits constant anti-diagonals, and what you are describing as the necessary property for "Hankelness".

@ilayn
Copy link
Member

ilayn commented 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org scipy.linalg
Projects
None yet
Development

No branches or pull requests

3 participants