Created
May 4, 2020 14:30
-
-
Save cossio/786cb0295eb866d23e7579f1dd70dcab to your computer and use it in GitHub Desktop.
undropdims: type stable inverse of dropdims
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
| function mix_dims(dims1, dims2, t) | |
| if in(t[1], dims2) | |
| return (1, mix_dims(dims1, Base.tail(dims2), Base.tail(t))...) | |
| else | |
| return (dims1[1], mix_dims(Base.tail(dims1), dims2, Base.tail(t))...) | |
| end | |
| end | |
| function mix_dims(dims1, dims2, t::Tuple{}) | |
| return () | |
| end | |
| @generated function ntup(::Val{N}) where N | |
| return ntuple(i -> i, Val(N)) | |
| end | |
| function undropdims(A::AbstractArray{T,N}; dims::NTuple{M,Int}=()) where {T,N,M} | |
| newdims = mix_dims(size(A), dims, ntup(Val(N+M))) | |
| return reshape(A, newdims) | |
| end | |
| a = [1 2 3; 4 5 6] | |
| undropdims(a; dims= (1, 3)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source: https://julialang.slack.com/archives/C6GHSTN4T/p1588602233081400?thread_ts=1588598751.078000&cid=C6GHSTN4T