-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
(Row)Vector equality with Matrices #21998
Comments
Yeah - a bit more consistency on whether arrays with singleton indices are the "same" or not would be awesome.
I for one hated that MATLAB made all these things equivalent and enjoyed moving to Julia where vectors and matrices were different. Broadcasting seems to cover the cases (that I need) where you want to be relaxed about this, but I see that sometimes you probably want (at least some variant of) |
RowVectors are naturally embedded into Matrices as well, so you'd consider |
Seeing as RowVector already passes the equality check, I wasn't thinking about how to include that within my proposal. :) |
EDIT: I misread Stefan's post and this is all wrong |
Also wrong |
I did NOT say that |
Oh wow, mental parsing failure. Yes, I agree with everything you said then. |
I'm in favor of this change. I know you're hoping to avoid a fundamental design discussion, but I think this decision is intrinsically coupled with the decision at #14770. Allowing vectors to equal column matrices currently matches their behavior since we allow them to be indexed with trailing singletons. Equality matches behavior. But if we disallow indexing with trailing singletons, then that means to me that we're deciding that vectors should not behave like matrices, so neither should they be equal. Last I checked, my position on #14770 was the minority. |
Let me clarify my position:
I believe this is a pretty coherent view in which the embedding of lower dimensions into higher ones is maintained but code unexpectedly "working" for the wrong dimension of argument is avoided. Yes, you can potentially pass an (n+1)-array into a loosely typed routine that expects an n-array and it might not error – but only if the last dimension is singleton, and then it will actually do exactly what you expect. What you cannot do under these rules, is pass an (n+1)-array with a non-singleton last dimension and have that second dimension effectively ignored, leading to unexpected results and hard-to-find bugs instead of errors. I believe there was quite a bit of support for this view in #14770. [Edited to generalize the example at the end since non-column matrices can be linearly indexed, so it was a bad example. Eliminating linear indexing altogether is whole other can of worms.] |
Great, we're in complete agreement. It should be a relatively small patch to get indexing behaviors the rest of the way there. |
Yes, I think what Stefan said makes the most sense - it's the right balance between scruffy and neat, seems to be what people expect, and is easy to use while catching the majority of "conceptual" size problems (not all, e.g. interaction with linear indexing when The thing which gets me is the special role of the index (not size) of |
I don't think this is hard to generalize: it all depends on what the array returns for |
I think that we have to assume something about absent dimensions, which seems like it has to be that they range from |
@StefanKarpinski, @JeffBezanson and I have been slowly mulling this one over for the past few weeks. While we often allow vectors to behave like 1-column matrices, that's not always the case. A salient example is APL indexing: I believe the general rule here is that we allow vectors to participate in linear algebra as 1-column matrices, but in other respects their dimensionality leads to behaviors that are different, observable, and distinct. Even more compelling is the fact that I really don't want to generalize a notion of equality that ignores trailing singleton dimensions. That would lead directly to a complaint that three dimensional "matrices" cannot participate in linear algebra — even though On the other hand the primary purpose of a |
Resolved: we aren't going to allow for equality between different dimension arrays. |
Right now, RowVectors are implicitly embedded within Matrices, but not so for Vectors. This leads to funky behaviors such as:
There are many ways we could try to fix this. Two take two extremal approaches, we could:
On the conservative side, we could just define
==(Matrix, Vector)
and be done with itPersonally I'd rather push for automatic "do what makes sense" equality checking for types that have natural isomorphisms. E.g. if I have an
Array{T,N}
, I think it makes sense to define equality with anArray{T,N+1}
that has a trailing singleton dimension, as the former is naturally embedded within the space of the second.I'd be happy to submit a PR implementing whatever we reach consensus on. Hopefully this is a small change to just equality and doesn't get sidetracked into anything more fundamental.
Pinging a random subset of the LinAlg posse @stevengj @jiahao @mbauman
The text was updated successfully, but these errors were encountered: