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
[Bug] sparse.linalg.LinearOperator.matmat cannot fallback properly to matvec #8444
Comments
matmat falls back to matvec in
and that is where the given example breaks. After a bit of investigation, it is not a bug, but a (probably undocumented?) feature. The trouble is that matvec(x) is designed to flexibly operate on a variety of different x: {matrix, ndarray} An array with shape (N,) or (N,1) (as well as a list? undocumented). Thus, matvec(x) is very forgiving and can be written in a sloppy way. However, when matvec needs to serve as a generator for matmat, suddenly the format of matvec must be strict in order to pass the line Probably the best fix is to add a warning message that using matvec for matmat generation should be avoided for efficiency reasons if the line I guess, one can beat it to death by trying first the original |
The LinearOperator documentation currently states: Also |
If something would be changed, But this would be enhancement request and not a bug. |
To preserve the strong backward compatibility, try first the original for (N,1): Please advise, and I can make a quick PR to close this issue. (These are my Python exercises :-) |
The current behavior is correct AFAIK, as matvec is supposed to return (N,1) output for (N,1) input. In the example in this issue above, note that the matvec implementation there returns (8,8) size result for (8,1) input, so it's not a matrix-vector product. |
yes. my proposal is for an enhancement, to allow sloppy matvec like this one |
As I see it, the enhancement would be to change |
that might break strong backward compatibility |
also would need to make rmatvec similar? |
there is a similar bug/issue for rmatvec |
Right, it would break matvec implementations that handle (N,1) inputs but fail to handle (N,) ones. As you say, this would also be true for similar changes in matmat. Trying to work around it via try/except/etc fallbacks is generally not good design, so I think the choices are either status quo or potential breakage in corner case. The iterative solvers etc. in scipy.sparse.linalg mostly feed in (N,) shape vectors, which makes that shape more probable to be OK in implementations. Re: gh-5546, the bug in that case is I think in the implementation of |
I personally would not make a PR potentially breaking tons of codes that are not strictly complaint, but currently work. But you can of course feel free to try it. If you do not like try/fail, my other proposal is: Probably the best fix is to add a warning message that using matvec for matmat generation should be avoided for efficiency reasons if the line |
I think there's not so much code that would break, but that is generally hard to find out since most code is not public. Efficiency warning here is probably quite noisy, and unclear if helpful for this issue (incorrectly implemented matvec). A safe improvement would be to make the error message clearer. The general opinion around here is that np.matrix is for legacy code only (numpy nowadays emits a PendingDeprecationWarning if you try to create a np.matrix). |
I read through this issue before creating a vaguely related GitHub issue of my own, and just wanted to make the following comment. @pv regarding changing the matvec semantics from
The bigger issue is that the matvec codepath is accessible by python's I see that the current consensus for this issue is updating an error message, so I realize that maybe the discussion about |
@rileyjmurray: note that the question here is about the internal |
Okay I see my mistake. I was worried because
and so I thought the possible change to Sorry for the bother! |
Hi,
LinearOperator
cannot properly usematvec
to implementmatmat
.Maybe somehow connected to issue #2434 because these calls seem to be a bit complicated:
matmat -> _matmat -> matvec -> _matvec -> matmat ...
Reproducing code example:
Error message:
Scipy/Numpy/Python version information:
The text was updated successfully, but these errors were encountered: