-
-
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
Feature request: Reshape shorthand for squashing first dimensions instead of last #27474
Comments
I think the best way to express this is with the
This |
I would propose deleting reshape with |
Yes I would agree. I initially was the one who introduced this |
I agree the
which feels way more cluttered than
If the
|
I should also point out that using negative numbers for special behaviors is something we've tried to avoid. |
I'm looking at deprecating this, and it's true that replacing this is not so easy. For example when growing the number of dimensions you need to pass |
We had talked about using |
At most, I would use |
|
It occurs to me that this operation is closely related to |
I don't think we have time to design and implement a replacement for this for 0.7. Hopefully we can add a non-breaking new feature for this any time, then eventually deprecate the existing function. |
Sorry, this is probably not the outcome you were hoping for when you requested a new feature here, @tbole — a better API will be forthcoming in a future Julia version! |
I'm mostly using |
struct Die end
const ☠️ = Die()
struct Recycle{N} end
const ♻️ = Recycle{1}()
Base.:*(::Recycle{M}, ::Recycle{N}) where {M,N} = Recycle{M+N}()
@inline Base.literal_pow(::typeof(^), ::Recycle{1}, ::Type{Val{N}}) where N = Recycle{N}()
export ☠️, ♻️ |
Seriously, expressing this as an indexing is very clever and actually works without much consternation: julia> Base.to_indices(A, inds, I::Tuple{Tuple{Vararg{Colon,N}}, Vararg{Any}}) where N = (vec(CartesianIndices(inds[1:N])), Base.to_indices(A, inds[N+1:end], Base.tail(I))...)
julia> A = rand(3,4,5);
julia> A[:, (:, :)]
3×20 Array{Float64,2}:
0.232048 0.113046 0.0152497 0.279078 0.385592 0.835976 0.148665 0.266044 0.870706 0.800947 0.0904307 0.750843 0.341379 0.989266 0.786485 0.812475 0.556002 0.6635 0.50563 0.509057
0.172248 0.511823 0.438507 0.511153 0.0461281 0.562187 0.629468 0.849421 0.453065 0.086566 0.174001 0.737693 0.655136 0.417266 0.712693 0.843132 0.177883 0.166547 0.123498 0.167775
0.136909 0.147522 0.915731 0.624489 0.146087 0.993909 0.25996 0.227684 0.317993 0.285055 0.249758 0.519837 0.579373 0.874992 0.739405 0.45147 0.849409 0.410294 0.433313 0.904717
julia> A[(:, :), :]
12×5 Array{Float64,2}:
0.232048 0.385592 0.870706 0.341379 0.556002
0.172248 0.0461281 0.453065 0.655136 0.177883
0.136909 0.146087 0.317993 0.579373 0.849409
0.113046 0.835976 0.800947 0.989266 0.6635
0.511823 0.562187 0.086566 0.417266 0.166547
0.147522 0.993909 0.285055 0.874992 0.410294
0.0152497 0.148665 0.0904307 0.786485 0.50563
0.438507 0.629468 0.174001 0.712693 0.123498
0.915731 0.25996 0.249758 0.739405 0.433313
0.279078 0.266044 0.750843 0.812475 0.509057
0.511153 0.849421 0.737693 0.843132 0.167775
0.624489 0.227684 0.519837 0.45147 0.904717 A little bit more is required here to do the |
What about these syntaxes: |
@StefanKarpinski @mbauman This seems to work pretty well and feels a lot more simple than using |
Although |
While the above was definitely tongue-in-cheek, one other point I want to make sure isn't being lost: for programming for generic dimensionality, |
We have that! It's a We had talked about using ellipses for "filling out" the rest of an indexing expression with Whether we use a |
You are truly the array master, @mbauman. |
Indexing with |
To a certain extent, yes. |
In fact the better performance of reshaped arrays over cartesian indexing one of the main reason I picked this approach, especially when combined with |
This now seems like just a feature addition so doesn't need to block the release. |
Currently
reshape(a, Val(N))
withN>0
is a useful shorthand returning an N-dimensional array by squashing the trailing dimensions ofa
. However, there is no such shorthand for the first dimensions. For that I propose to make reshape accept negative values ofN
.The text was updated successfully, but these errors were encountered: