Dimensionality Functions
NDTools.expand_dims
— Functionexpand_dims(x, ::Val{N})
Expands the dimensions of an array to a given number of dimensions.
Examples The result is a 5D array with singleton dimensions at the end
julia> expand_dims(ones((1,2,3)), Val(5))
1×2×3×1×1 Array{Float64, 5}:
[:, :, 1, 1, 1] =
1.0 1.0
[:, :, 2, 1, 1] =
1.0 1.0
[:, :, 3, 1, 1] =
1.0 1.0
expand_dims(x::AbstractArray{T, N}, dims::Vararg{Int, M}) where {T, N, M}
Insert singleton dimensions at the position of dims
into x
. Based on a reshape
operation.
Examples
julia> expand_dims(zeros((2,2)), 1) |> size
(1, 2, 2)
julia> expand_dims(zeros((2,2)), 2) |> size
(2, 1, 2)
julia> expand_dims(zeros((2,2)), 3) |> size
(2, 2, 1)
julia> expand_dims(zeros((2,2)), 1,3,4) |> size
(1, 2, 1, 1, 2)
NDTools.expand_size
— Functionexpand_size(sz,sz2)
Expands a size tuple sz
with the sizes as given in the tuple sz2
for positions which do not exist in sz
. Typically one wants to
Example:
julia> expand_size((1,2,3),(4,5,6,7,8,9))
(1, 2, 3, 7, 8, 9)
NDTools.select_sizes
— Functionselect_sizes(x::AbstractArray, dim)
Additional size method to access the size at several dimensions in one call. Keep singleton dimensions.
Examples
julia> x = ones((2,4,6,8, 10));
julia> select_sizes(x, (2,3))
(1, 4, 6, 1, 1)
julia> select_sizes(x, 5)
(1, 1, 1, 1, 10)
julia> select_sizes(x, (5,))
(1, 1, 1, 1, 10)
NDTools.select_sizes_squeeze
— Functionselect_sizes_squeeze(x::AbstractArray, dim)
Additional size method to access the size at several dimensions in one call. Remove singleton dimensions.
See also select_sizes
which does not remove singleton dimensions.
Examples
julia> select_sizes_squeeze(randn((5,6,7)), (2,3))
(6, 7)
julia> select_sizes_squeeze(randn((5,6,7)), 2)
(6,)
NDTools.reorient
— Functionreorient(vec, d::Val{dim}, total_dims=d)
Reorients a 1D vector vec
along dimension dim
. The total output dimension is total_dims
.
Type stable version of reorient
!
julia> reorient([1,2,3,4], Val(2))
1×4 Matrix{Int64}:
1 2 3 4
julia> reorient([1,2,3,4], 2, Val(3))
1×4×1 Array{Int64, 3}:
[:, :, 1] =
1 2 3 4
julia> x = reshape(1:9, 3, 3);
julia> reorient([1,2,3], 2, Val(ndims(x)))
1×3 Matrix{Int64}:
1 2 3
NDTools.idx_to_dim
— Functionidx_to_dim(idx_arr)
Converts an N-dimensional array of NTuple to an N+1 dimensional array by orienting the (inner) tuple along the (outer) trailing+1 dimension. Note that this function is not type stable!
See also: idx_to_view
which reinterprets the array to an N+1 dimensional array by unrolling the (inner) tuple. Arguments:
idx_arr
. The array of NTuple to convert
Example:
julia> idx_to_dim([(x,y) for x in 1:3, y in 1:3])
3×3×2 Array{Int64, 3}:
[:, :, 1] =
1 1 1
2 2 2
3 3 3
[:, :, 2] =
1 2 3
1 2 3
1 2 3