Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
LilithHafner merged 29 commits intoJuliaLang:masterfromroflmaostc:master
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from15 commits
Commits
Show all changes
29 commits
Select commitHold shift + click to select a range
9903e42
Add insertdims
roflmaostcJun 23, 2022
0f93207
Update abstractarraymath.jl
roflmaostcJun 23, 2022
bd6e45e
Fix some whitespaces and doctest [skip ci]
roflmaostcJun 24, 2022
f4087a2
Handle merge [skip ci]
roflmaostcJun 24, 2022
50ae007
Merge branch 'JuliaLang:master' into master
roflmaostcMay 30, 2024
4f53621
Fix bug in _foldoneto call
roflmaostcMay 30, 2024
1196b8f
Add test for multiple singleton dimensions at one dim
roflmaostcMay 31, 2024
6f986dc
Update base/abstractarraymath.jl
roflmaostcJul 25, 2024
16e0203
Update base/abstractarraymath.jl
roflmaostcJul 25, 2024
cf23193
Update base/abstractarraymath.jl
roflmaostcJul 25, 2024
6057e22
Add docs
roflmaostcJul 25, 2024
84d0693
Update base/abstractarraymath.jl
roflmaostcJul 25, 2024
aa51242
Merge branch 'JuliaLang:master' into master
roflmaostcJul 25, 2024
8648d84
Add news
roflmaostcJul 25, 2024
080c83c
Fix merge mistake
roflmaostcJul 26, 2024
ec383d9
Rebase [skip ci]
roflmaostcJul 27, 2024
66d0561
Update test/arrayops.jl [skip ci]
roflmaostcJul 27, 2024
bdb3258
Update base/abstractarraymath.jl
roflmaostcJul 27, 2024
0ddee55
Update comment in code [skip ci]
roflmaostcJul 27, 2024
76b31f7
Remove parts in docstring [skip ci]
roflmaostcJul 27, 2024
490b103
Adapt docstring
roflmaostcJul 27, 2024
98283db
Remove trailing whitespace
roflmaostcJul 27, 2024
c321d51
Remove trailing whitespace in test [skip ci]
roflmaostcJul 27, 2024
2a9e3ac
Update base/abstractarraymath.jl [skip ci]
roflmaostcJul 27, 2024
d7cb35c
Rewrite doc [skip ci]
roflmaostcJul 27, 2024
514b4fc
Update test/arrayops.jl
roflmaostcAug 1, 2024
4f07e18
Update test/arrayops.jl
roflmaostcAug 1, 2024
cfa1465
Update base/abstractarraymath.jl
roflmaostcAug 1, 2024
9f174b7
Update base/abstractarraymath.jl
roflmaostcAug 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletionsNEWS.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -70,6 +70,7 @@ New library functions
* The new `isfull(c::Channel)` function can be used to check if `put!(c, some_value)` will block. ([#53159])
* `waitany(tasks; throw=false)` and `waitall(tasks; failfast=false, throw=false)` which wait multiple tasks at once ([#53341]).
* `uuid7()` creates an RFC 9652 compliant UUID with version 7 ([#54834]).
* `insertdims(array; dims)` allows to insert singleton dimensions into an array which is the inverse operation to `dropdims`

New library features
--------------------
Expand Down
81 changes: 81 additions & 0 deletionsbase/abstractarraymath.jl
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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.
Copy link
Contributor

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 todims? Butdims=(1, 3) in the examples aren't contiguous.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

OfA anddims together.
insertdims([1,2,3], dims=(3,)) would be invalid because it jumps over the second dimension.

Copy link
Member

@mbaumanmbaumanJul 26, 2024
edited
Loading

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:

Suggested change
The dimensions of`A` and`dims` must be contiguous.
The new singleton dimensions are inserted immediately before the specified`dims`. The`dims` may repeat, all must bein`1:ndims(A)+1`, and they must bein sorted order.

If dimensions occur multiple times in `dims`, several singleton dimensions are inserted.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
If dimensions occur multiple timesin`dims`, several singleton dimensions are inserted.
If dimensions occur multiple timesin`dims`, several singleton dimensions are inserted after the requested position.

?


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
```

!!! 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}
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"))

# 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 ##

"""
Expand Down
1 change: 1 addition & 0 deletionsbase/exports.jl
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -407,6 +407,7 @@ export
indexin,
argmax,
argmin,
insertdims,
invperm,
invpermute!,
isassigned,
Expand Down
1 change: 1 addition & 0 deletionsdoc/src/base/arrays.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -138,6 +138,7 @@ Base.parentindices
Base.selectdim
Base.reinterpret
Base.reshape
Base.insertdims
Base.dropdims
Base.vec
Base.SubArray
Expand Down
14 changes: 14 additions & 0 deletionstest/arrayops.jl
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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)

sz = (5,8,7)
A = reshape(1:prod(sz),sz...)
@test A[2:6] == [2:6;]
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp