-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
ENH: broadcast lstsq #8720
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
Comments
That workaround doesn't look right to me. A truly broadcastable solution would have |
Depends on the ordering perhaps. Check A = numpy.random.rand(7, 3, 2)
b = numpy.random.rand(7, 3)
for k in range(7):
x, res, rank, sigma = numpy.linalg.lstsq(A[k], b[k])
print(x)
print
u, s, v = numpy.linalg.svd(A, full_matrices=False)
uTb = numpy.einsum('ijk,ij->ik', u, b)
xx = numpy.einsum('ijk, ij->ik', v, uTb / s)
print(xx) |
My point being that your code only broadcasts to a stack (1d) of matrices, not a grid (2d) or higher. The other linalg functions that broadcast do so for any dimension. I would guess that Either way, this would be much better to fix |
Working on this at the moment in https://github.com/eric-wieser/numpy/tree/lstsq-gufunc, which depends on both #3861 and #8649 (edit: both now merged!) What shape should |
Tricky. I fear the only sensible thing may be to let the shape depend on |
I think depending on the shapes of both A and B is reasonable. The problem is the distinction between the true rank, and the usual rank of a matrix of a given shape. So the only case in which behaviour would change, is that when passed a matrix with M > N but rank < N, it would return a (K,)-shape array of I can't think of any code that would be able to handle that Edit: |
Actually, I think
Suggested name? |
Maybe just |
This is now super close to achievable - the gufunc under the hood is fully vectorized as of #10890 - we just need to decide if we can change |
@eric-wieser - I'm actually a bit confused: if in |
The issue is these lines: Lines 2049 to 2051 in 12114c7
There's no meaninful way to broadcast this, because Arguably it wasn't ever meaningful for |
Ah, duh, of course rank can be different for every matrix. So, I guess the options are to
|
Tagging with |
I'd go for option 1, maybe relatively soon, so that we can see if downstream tests break (quite unlikely, I think). |
Option 1 is implemented in #15777. |
Uh oh!
There was an error while loading. Please reload this page.
Many
linalg
functions are already broadcastable,lstsq
isn't.A workaround is via
svd
which is already broadcasted:The text was updated successfully, but these errors were encountered: