Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.6k
Add insertdims method which is inverse to dropdims#45793
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from15 commits
9903e42
0f93207
bd6e45e
f4087a2
50ae007
4f53621
1196b8f
6f986dc
16e0203
cf23193
6057e22
84d0693
aa51242
8648d84
080c83c
ec383d9
66d0561
bdb3258
0ddee55
76b31f7
490b103
98283db
c321d51
2a9e3ac
d7cb35c
514b4fc
4f07e18
cfa1465
9f174b7
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -93,6 +93,87 @@ 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. | ||||||||
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. What does "contiguous" mean here? Does it refer to 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. Of 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. I do think this could be worded slightly better and could help address the other point simultaneously. Maybe: Suggested change
| ||||||||
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 resolvedHide resolvedUh oh!There was an error while loading.Please reload this page.
roflmaostc marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||||||||
!!! compat "Julia 1.12" | ||||||||
Requires Julia 1.12 or later. | ||||||||
""" | ||||||||
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 resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||||||||
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 resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||||||||
# n is the amount of the dims already inserted | ||||||||
ax_n = Base._foldoneto(((ds, n, dims), _) -> | ||||||||
dims != Tuple(()) && n == first(dims) ? | ||||||||
((ds..., Base.OneTo(1)), n, Base.tail(dims)) : | ||||||||
((ds..., axes(A,n)), n+1, dims), | ||||||||
((), 1, dims), Val(ndims(A) + length(dims))) | ||||||||
# we need only the new shape and not n | ||||||||
reshape(A, ax_n[1]) | ||||||||
end | ||||||||
_insertdims(A::AbstractArray, dim::Integer) = _insertdims(A, (Int(dim),)) | ||||||||
## Unary operators ## | ||||||||
""" | ||||||||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -407,6 +407,7 @@ export | ||
indexin, | ||
argmax, | ||
argmin, | ||
insertdims, | ||
invperm, | ||
invpermute!, | ||
isassigned, | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -308,6 +308,20 @@ end | ||
@test_throws ArgumentError dropdims(a, dims=4) | ||
@test_throws ArgumentError dropdims(a, dims=6) | ||
a = rand(8, 7) | ||
@test @inferred(insertdims(a, dims=1)) == @inferred(insertdims(a, dims=(1,))) == reshape(a, (1, 8, 7)) | ||
@test @inferred(insertdims(a, dims=(1, 3))) == reshape(a, (1, 8, 7, 1)) | ||
@test @inferred(insertdims(a, dims=(1, 2, 3))) == reshape(a, (1, 8, 1, 7, 1)) | ||
@test @inferred(insertdims(a, dims=(1, 1, 2, 3))) == reshape(a, (1, 1, 8, 1, 7, 1)) | ||
@test @inferred(insertdims(a, dims=(1, 2, 2, 3))) == reshape(a, (1, 8, 1, 1, 7, 1)) | ||
@test @inferred(insertdims(a, dims=(1, 2, 3, 3))) == reshape(a, (1, 8, 1, 7, 1, 1)) | ||
@test_throws UndefKeywordError insertdims(a) | ||
@test_throws ArgumentError insertdims(a, dims=0) | ||
@test_throws ArgumentError insertdims(a, dims=(1, 2, 1)) | ||
@test_throws ArgumentError insertdims(a, dims=4) | ||
@test_throws ArgumentError insertdims(a, dims=6) | ||
roflmaostc marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
sz = (5,8,7) | ||
A = reshape(1:prod(sz),sz...) | ||
@test A[2:6] == [2:6;] | ||