-
-
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
Scalars and array allocation convenience function #27675
Conversation
Alias for zero dimensional array, for edge cases in squeezes and reduces. `array` (a possible poor choice of name) aliases Array, mostly to avoid the braces.
Sorry... is this a reference to a discussion somewhere? |
Sorry, no, its just bad grammar :/ |
OK, thanks! I edited your opening post to reflect this. A few thoughts: This PR introduces two new concepts, an Regarding Regarding In my mind the real issue here is that we have no syntax for creating a 0-d array. If e.g. |
Oh I should point out that |
Welcome, and thank you for the suggestion! It's difficult to introduce an AbstractScalar type, since the idea of "scalar" should also include numbers, but most people feel numbers and arrays are disjoint. It's also difficult to define precisely; e.g. is a string a scalar? I feel that having all of |
I agree, Scalar isnt exactly the best name, but I see them often enough as a consequence (not as a purposeful creation) that I thought it needed a name to save key strokes.
Unfortunately, this PR does conflate two issues, but Id like to push back against this and fight for easy syntax for creating (safely) allocated uninitialized arrays. |
I think the distinction between arrays and scalars issue nearly as clean as many people seem to want it to be. consider the following 4 types: |
I'm curious what APIs are returning these zero-dimensional arrays back to you so frequently. Our reductions preserve dimensions — and if you reduce over an entire array, then you get an unwrapped scalar. Is it entirely from Would it make sense to return an unwrapped scalar from |
I use squeeze after marginalizing factors/conditional probability distributions, so that is what can return the 0d array. I think having |
Also, as a follow up to @JeffBezanson, why don't we shift more functionality to the Array([T, ]dims::Dims) = Array{T, length(dims)}(dims)
Array([T, ]dims::Dims, el::T) = fill(convert(T, el), inds) Wouldn't this simplify and unify everything, give a "general syntax that's easier to guess once you know the pattern", and avoid having to type out the braces ? |
We used to have Some of those could maybe be unified, but at least they accept different arguments and do different things, while |
Also one has to be careful adding too many behaviors to the same function. For example replacing |
So I guess my question boils down to this: |
The answer is that we should avoid spurious syntax variations. If |
And you also prefer to not alias zero dimensional array? |
Alright, here's a bit of dirty laundry: I have needed this myself in the past in developing Julia's core array infrastructure — or, at least, I thought I did! I actually introduced a non-exported alias a few years ago: Line 171 in d555a9a
But embarrassingly, that is purely a relic of a previous refactoring. It wasn't even used in the commit that introduced it! It was simply missed and forgotten, and nobody picked it up and used it since. Not even a package has discovered it. If I remember correctly, I introduced that before we had the convenient shorthand There's a tradeoff with names — there's definitely a power behind giving something a name, but if you give too many things names, you have too much to learn and can't generalize your knowledge. |
FYI there has been discussions along this line, where we use the constructor a lot more and more directly. There are various issues/PRs (open or closed) around |
I often see zero-dim arrays after reducing along and squeezing all dims in an array
Frequent
Array{T}(dims)
is awkward to type, and usingzeros
/ones
feels inappropriate since array contents will be overwritten.