API

Enhanced Base

Base.reshapeFunction
Base.reshape(x, ops...)
Base.reshape(x, ops::Tuple)

Reshape the array x using the given operations, which can include : (Base.Colon), .. (EllipsisNotation.Ellipsis), and integers, but there must be at least one LocalReshape.

See also Rewrap.reshape.

julia> x = view(rand(4, 5, 2), 1:3, :, :);

julia> x′ = reshape(x, Keep(), :);

julia> size(x′)
(3, 10)

julia> y′ = rand(2, 3) * x′; # project from 3 to 2

julia> size(y′)
(2, 10)

julia> y = reshape(y′, Keep(), Split(1, size(x)[2:end]));

julia> size(y)
(2, 5, 2)

julia> reshape(view(rand(2, 3), :, 1:2), Merge(..)) |> summary # can not use a single `:` (type piracy)
"4-element view(::Vector{Float64}, 1:4) with eltype Float64"
source
Rewrap.reshapeFunction
Rewrap.reshape(x, ops...)
Rewrap.reshape(x, ops::Tuple)

Reshape the array x using the given operations, which can include : (Base.Colon), .. (EllipsisNotation.Ellipsis), and integers.

Integers and colons can form a single contiguous sequence that becomes a Split(.., sizes).

See also Base.reshape.

julia> x = view(rand(4, 5, 2), 1:3, :, :);

julia> x′ = Rewrap.reshape(x, Keep(), :);

julia> size(x′)
(3, 10)

julia> y′ = rand(2, 3) * x′; # project from 3 to 2

julia> size(y′)
(2, 10)

julia> y = Rewrap.reshape(y′, Keep(), Split(1, size(x)[2:end]));

julia> size(y)
(2, 5, 2)

julia> Rewrap.reshape(view(rand(2, 3), :, 1:2), :) |> summary # Rewrap owns `Rewrap.reshape`
"4-element view(::Vector{Float64}, 1:4) with eltype Float64"
source
Rewrap.dropdimsFunction
dropdims(x; dims)

Drop the specified dimensions from the array x.

Note

This method may not be type stable if dims cannot be constant-propagated.

julia> x = [1 3 5; 2 4 6;;;]
2×3×1 Array{Int64, 3}:
[:, :, 1] =
 1  3  5
 2  4  6

julia> y = view(x, :, 1:2, :)
2×2×1 view(::Array{Int64, 3}, :, 1:2, :) with eltype Int64:
[:, :, 1] =
 1  3
 2  4

julia> Rewrap.dropdims(y; dims=3)
2×2 view(::Matrix{Int64}, :, 1:2) with eltype Int64:
 1  3
 2  4
source
Rewrap.vecFunction
vec(x)

Flatten the array x into a vector.

julia> x = [1 3 5; 2 4 6];

julia> Rewrap.vec(view(x, :, 1:2))
4-element view(::Vector{Int64}, 1:4) with eltype Int64:
 1
 2
 3
 4

julia> Rewrap.vec(view(x, 1:2, :)) # not contiguous!
6-element reshape(view(::Matrix{Int64}, 1:2, :), 6) with eltype Int64:
 1
 2
 3
 4
 5
 6
source

LocalReshape

Rewrap.KeepType
Keep(N = 1)

Keep the first N dimensions. N can be an integer or ellipsis (..). .. is semantically equivalent, and gets converted to Keep(..) when passed to reshape.

julia> x = reshape(collect(1:24), 3, 4, 2);

julia> y = view(x, 2:3, :, :);

julia> reshape(y, Keep(), :)
2×8 view(::Matrix{Int64}, 2:3, :) with eltype Int64:
 2  5  8  11  14  17  20  23
 3  6  9  12  15  18  21  24
source
Rewrap.MergeType
Merge(N)

Merge the first N dimensions into one. N can be an integer or ellipsis (..). : is semantically equivalent, and gets converted to Merge(..) when passed to reshape.

julia> x = reshape(collect(1:8), 2, 4);

julia> y = view(x, :, 2:3);

julia> reshape(y, Merge(..))
4-element view(::Vector{Int64}, 3:6) with eltype Int64:
 3
 4  
 5
 6
source
Rewrap.SplitType
Split(N, sizes)
Split{N}(sizes...)
Split(sizes...)
Split(sizes)

Split the first N (1, if not provided) dimensions into M dimensions, with sizes given by a tuple of integers and at most one colon (:). This can be interpreted as a local reshape operation on the N dimensions, and doesn't have many of the compile time guarantees of the other operations.

julia> x = reshape(collect(1:16), 8, 2);

julia> y = view(x, 1:4, :);

julia> reshape(y, Split(1, (2, :)), :)
2×2×2 view(::Array{Int64, 3}, :, 1:2, :) with eltype Int64:
[:, :, 1] =
 1  3
 2  4

[:, :, 2] =
  9  11
 10  12
source
Rewrap.Split1Type
Split1(sizes...)

Split the first dimension into length(sizes) dimensions, with sizes given by a tuple of integers and at most one colon (:). This is a shortcut for Split(1, sizes) or Split{1}(sizes...).

source
Rewrap.ResqueezeType
Resqueeze(N => M)

Turn N singleton dimensions into M singleton dimensions.

See also Squeeze and Unsqueeze.

julia> x = reshape(collect(1:6), 3, 1, 2);

julia> y = view(x, 1:2, :, :);

julia> reshape(y, Keep(), Resqueeze(1 => 2), Keep())
2×1×1×2 reshape(view(::Array{Int64, 3}, 1:2, :, :), 2, 1, 1, 2) with eltype Int64:
[:, :, 1, 1] =
 1
 2

[:, :, 1, 2] =
 4
 5
source
Rewrap.SqueezeType
Squeeze(N = 1)

Remove N singleton dimensions.

See also Resqueeze and Unsqueeze.

julia> x = reshape(collect(1:6), 3, 1, 2);

julia> y = view(x, 1:2, :, :);

julia> reshape(y, Keep(), Squeeze(..), Keep())
2×2 view(::Matrix{Int64}, 1:2, :) with eltype Int64:
 1  4
 2  5
source
Rewrap.UnsqueezeType
Unsqueeze(M = 1)

Add M singleton dimensions.

See also Resqueeze and Squeeze.

julia> x = reshape(collect(1:6), 3, 2);

julia> y = view(x, 1:2, :);

julia> reshape(y, Keep(), Unsqueeze(1), Keep())
2×1×2 view(::Array{Int64, 3}, 1:2, :, :) with eltype Int64:
[:, :, 1] =
 1
 2

[:, :, 2] =
 4
 5
source

Global Axis Operations

Rewrap.ReshapeType
Reshape(ops, ::Val{N})
Reshape(ops, ::AbstractArray{<:Any,N})

Given a tuple of operations and the input array dimensionality, construct a Reshape object that can be used to reshape arrays.

See also Rewrap.reshape and Base.reshape.

source
Rewrap.PermuteType
Permute(perm::NTuple{N,Int})

Permute the axes of an array according to the given permutation. The resulting array may be a copy, or the same array if the permutation is the identity.

Can be applied to arrays with N dimensions.

source
Rewrap.ReduceType
Reduce(f::Function; dims::NTuple{K,Int})

Reduce an array over dimensions dims, using e.g. sum, prod, minimum, maximum, any, all, or Statistics.mean.

f must accept a dims::Tuple{Vararg{Int}} keyword argument, allowing for reduction over specific dimensions. This should reduce the specified dimensions to singletons, but not drop them.

source
Rewrap.RepeatType
Repeat(repeats::NTuple{K,Int})

Repeats an array along multiple dimensions.

Can be applied to arrays with at most K dimensions.

source