-
-
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
Add insertdims method which is inverse to dropdims #45793
Merged
Merged
Changes from 5 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
9903e42
Add insertdims
roflmaostc 0f93207
Update abstractarraymath.jl
roflmaostc bd6e45e
Fix some whitespaces and doctest [skip ci]
roflmaostc f4087a2
Handle merge [skip ci]
roflmaostc 50ae007
Merge branch 'JuliaLang:master' into master
roflmaostc 4f53621
Fix bug in _foldoneto call
roflmaostc 1196b8f
Add test for multiple singleton dimensions at one dim
roflmaostc 6f986dc
Update base/abstractarraymath.jl
roflmaostc 16e0203
Update base/abstractarraymath.jl
roflmaostc cf23193
Update base/abstractarraymath.jl
roflmaostc 6057e22
Add docs
roflmaostc 84d0693
Update base/abstractarraymath.jl
roflmaostc aa51242
Merge branch 'JuliaLang:master' into master
roflmaostc 8648d84
Add news
roflmaostc 080c83c
Fix merge mistake
roflmaostc ec383d9
Rebase [skip ci]
roflmaostc 66d0561
Update test/arrayops.jl [skip ci]
roflmaostc bdb3258
Update base/abstractarraymath.jl
roflmaostc 0ddee55
Update comment in code [skip ci]
roflmaostc 76b31f7
Remove parts in docstring [skip ci]
roflmaostc 490b103
Adapt docstring
roflmaostc 98283db
Remove trailing whitespace
roflmaostc c321d51
Remove trailing whitespace in test [skip ci]
roflmaostc 2a9e3ac
Update base/abstractarraymath.jl [skip ci]
roflmaostc d7cb35c
Rewrite doc [skip ci]
roflmaostc 514b4fc
Update test/arrayops.jl
roflmaostc 4f07e18
Update test/arrayops.jl
roflmaostc cfa1465
Update base/abstractarraymath.jl
roflmaostc 9f174b7
Update base/abstractarraymath.jl
roflmaostc 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -93,6 +93,82 @@ function _dropdims(A::AbstractArray, dims::Dims) | |||||
end | ||||||
_dropdims(A::AbstractArray, dim::Integer) = _dropdims(A, (Int(dim),)) | ||||||
|
||||||
|
||||||
""" | ||||||
insertdims(A; dims) | ||||||
|
||||||
Return an array with the same data as `A`, but with singleton dimensions specified by | ||||||
`dims` inserted. | ||||||
The dimensions of `A` and `dims` must be contiguous. | ||||||
If dimensions occur multiple times in `dims`, several singleton dimensions are inserted. | ||||||
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
? |
||||||
|
||||||
The result shares the same underlying data as `A`, such that the | ||||||
result is mutable if and only if `A` is mutable, and setting elements of one | ||||||
alters the values of the other. | ||||||
|
||||||
See also: [`reshape`](@ref), [`dropdims`](@ref), [`vec`](@ref). | ||||||
|
||||||
# Examples | ||||||
```jldoctest | ||||||
julia> a = [1 2; 3 4] | ||||||
2×2 Matrix{Int64}: | ||||||
1 2 | ||||||
3 4 | ||||||
|
||||||
julia> b = insertdims(a, dims=(1,1)) | ||||||
1×1×2×2 Array{Int64, 4}: | ||||||
[:, :, 1, 1] = | ||||||
1 | ||||||
|
||||||
[:, :, 2, 1] = | ||||||
3 | ||||||
|
||||||
[:, :, 1, 2] = | ||||||
2 | ||||||
|
||||||
[:, :, 2, 2] = | ||||||
4 | ||||||
|
||||||
julia> b = insertdims(a, dims=(1,2)) | ||||||
1×2×1×2 Array{Int64, 4}: | ||||||
[:, :, 1, 1] = | ||||||
1 3 | ||||||
|
||||||
[:, :, 1, 2] = | ||||||
2 4 | ||||||
|
||||||
julia> b = insertdims(a, dims=(1,3)) | ||||||
1×2×2×1 Array{Int64, 4}: | ||||||
[:, :, 1, 1] = | ||||||
1 3 | ||||||
|
||||||
[:, :, 2, 1] = | ||||||
2 4 | ||||||
|
||||||
julia> b[1,1,1,1] = 5; a | ||||||
2×2 Matrix{Int64}: | ||||||
5 2 | ||||||
3 4 | ||||||
``` | ||||||
roflmaostc marked this conversation as resolved.
Show resolved
Hide resolved
roflmaostc marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
""" | ||||||
insertdims(A; dims) = _insertdims(A, dims) | ||||||
function _insertdims(A::AbstractArray{T, N}, dims::Tuple{Vararg{Int64, M}}) where {T, N, M} | ||||||
roflmaostc marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
maximum(dims) ≤ ndims(A)+1 || throw(ArgumentError("The largest entry in dims must be ≤ ndims(A) + 1.")) | ||||||
1 ≤ minimum(dims) || throw(ArgumentError("The smallest entry in dims must be ≥ 1.")) | ||||||
issorted(dims) || throw(ArgumentError("dims=$(dims) are not sorted")) | ||||||
roflmaostc marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
# n is the current index where we maybe insert | ||||||
ax_n = _foldoneto(((ds, n, dims), _) -> | ||||||
dims != Tuple(()) && n == first(dims) ? | ||||||
((ds..., Base.OneTo(1)), n, tail(dims)) : | ||||||
((), 1, dims), Val(ndims(A) + length(dims))) | ||||||
# we need only the new shape | ||||||
reshape(A, ax_n[1]) | ||||||
mbauman marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
end | ||||||
_insertdims(A::AbstractArray, dim::Integer) = _insertdims(A, (Int(dim),)) | ||||||
|
||||||
|
||||||
|
||||||
## Unary operators ## | ||||||
|
||||||
""" | ||||||
|
This file contains 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 |
---|---|---|
|
@@ -406,6 +406,7 @@ export | |
indexin, | ||
argmax, | ||
argmin, | ||
insertdims, | ||
invperm, | ||
invpermute!, | ||
isassigned, | ||
|
This file contains 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
Oops, something went wrong.
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.
What does "contiguous" mean here? Does it refer to
dims
? Butdims=(1, 3)
in the examples aren't contiguous.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.
Of
A
anddims
together.insertdims([1,2,3], dims=(3,))
would be invalid because it jumps over the second dimension.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.
I do think this could be worded slightly better and could help address the other point simultaneously. Maybe: