-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Simplify dropdims(::Transpose) and insertdims(::PermutedDimsArray) etc.
#55381
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
Open
mcabbott
wants to merge
1
commit into
JuliaLang:master
Choose a base branch
from
mcabbott:dropdims_perm
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -363,6 +363,61 @@ function Base.mapreducedim!(f::typeof(identity), op::Union{typeof(Base.mul_prod) | |||||
| B | ||||||
| end | ||||||
|
|
||||||
| function Base._dropdims(A::PermutedDimsArray{T,N,perm}, dims::Base.Dims) where {T,N,perm} | ||||||
| for d in dims | ||||||
| 1 <= d <= ndims(A) || throw(ArgumentError("dropped dims must be in range 1:ndims(A)")) | ||||||
| # dropdims also demands size(A,d)==1 and allunique(dims), checked by Base._dropdims below. | ||||||
| end | ||||||
| # Drop the appropriate dims of the parent array: | ||||||
| innerdims = map(d -> perm[d], dims) | ||||||
| inner = Base._dropdims(parent(A), innerdims) | ||||||
| # Change the permutation two ways: first account for dropdims(parent(A)), then skip entries at locations in dims. | ||||||
| innerperm = map(perm) do p | ||||||
| p - count(<=(p), innerdims) | ||||||
| end | ||||||
| newperm = ntuple(length(perm) - length(dims)) do d | ||||||
| i = d + count(<=(d), dims) | ||||||
| innerperm[i] | ||||||
| end | ||||||
| PermutedDimsArray(inner, newperm) | ||||||
| end | ||||||
| # Drop 1 dim of a matrix and you must get a vector, no need to wrap it: | ||||||
| function Base._dropdims(A::PermutedDimsArray{T,2,perm}, dims::Tuple{Int}) where {T,perm} | ||||||
| 1 <= only(dims) <= ndims(A) || throw(ArgumentError("dropped dims must be in range 1:ndims(A)")) | ||||||
| innerdim = perm[only(dims)] | ||||||
| Base._dropdims(parent(A), (innerdim,)) | ||||||
| end | ||||||
| # Drop all dims | ||||||
| function Base._dropdims(A::PermutedDimsArray{T,N,perm}, dims::NTuple{N,Int}) where {T,N,perm} | ||||||
| for d in dims | ||||||
| 1 <= d <= ndims(A) || throw(ArgumentError("dropped dims must be in range 1:ndims(A)")) | ||||||
| end | ||||||
| Base._dropdims(parent(A), dims) | ||||||
| end | ||||||
|
|
||||||
| function Base._insertdims(A::PermutedDimsArray{T,N,perm}, dims::NTuple{M,Int}) where {T,N,perm,M} | ||||||
| for i in eachindex(dims) | ||||||
| 1 ≤ dims[i] || throw(ArgumentError("the smallest entry in dims must be ≥ 1.")) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
not to be too picky but just for consistency I'd use |
||||||
| dims[i] ≤ N+M || throw(ArgumentError("the largest entry in dims must be not larger than the dimension of the array and the length of dims added")) | ||||||
| for j = 1:i-1 | ||||||
| dims[j] == dims[i] && throw(ArgumentError("inserted dims must be unique")) | ||||||
| end | ||||||
| end | ||||||
| # We can choose where to insert dims into parent array, choose the end? | ||||||
| innerdims = ntuple(d -> ndims(A) + d, length(dims)) | ||||||
| inner = Base._insertdims(parent(A), innerdims) | ||||||
| # With that choice, the new permutation just needs to insert higher numbers into sequence | ||||||
| newperm = ntuple(length(perm) + length(dims)) do d | ||||||
| c = count(<=(d), dims) | ||||||
| if d in dims | ||||||
| ndims(A) + c | ||||||
| else | ||||||
| perm[d - c] | ||||||
| end | ||||||
| end | ||||||
| PermutedDimsArray(inner, newperm) | ||||||
| end | ||||||
|
|
||||||
| function Base.showarg(io::IO, A::PermutedDimsArray{T,N,perm}, toplevel) where {T,N,perm} | ||||||
| print(io, "PermutedDimsArray(") | ||||||
| Base.showarg(io, parent(A), false) | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider
so we want to keep outer dims
(3, 4), which will be keeping inner dims(1, 3)but this would give
an invalid permutation, manifesting in examples like this
this could work instead I think
where I've defined
_sortedtuple_range_setdifflike so as I guess we can avoid the collect intoVectorfromBase.setdiffThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking! I need another coffee to understand where my logic went wrong, but good catch.