|
| 1 | +const AnyOp = Union{LocalReshape,ColonOrEllipsis} |
| 2 | + |
| 3 | +""" |
| 4 | + Rewrap.reshape(x, ops...) |
| 5 | + Rewrap.reshape(x, ops::Tuple) |
| 6 | +
|
| 7 | +Reshape the array `x` using the given operations, which can include |
| 8 | +`:` (Base.Colon) and `..` (EllipsisNotation.Ellipsis). |
| 9 | +
|
| 10 | +See also [`Base.reshape`](@ref). |
| 11 | +
|
| 12 | +```jldoctest |
| 13 | +julia> x = view(rand(4, 5, 2), 1:3, :, :); |
| 14 | +
|
| 15 | +julia> x′ = Rewrap.reshape(x, Keep(), :); |
| 16 | +
|
| 17 | +julia> size(x′) |
| 18 | +(3, 10) |
| 19 | +
|
| 20 | +julia> y′ = rand(2, 3) * x′; # project from 3 to 2 |
| 21 | +
|
| 22 | +julia> size(y′) |
| 23 | +(2, 10) |
| 24 | +
|
| 25 | +julia> y = Rewrap.reshape(y′, Keep(), Split(1, size(x)[2:end])); |
| 26 | +
|
| 27 | +julia> size(y) |
| 28 | +(2, 5, 2) |
| 29 | +
|
| 30 | +julia> Rewrap.reshape(view(rand(2, 3), :, 1:2), :) # Rewrap owns `Rewrap.reshape` |
| 31 | +``` |
| 32 | +""" |
| 33 | +function reshape end |
| 34 | + |
| 35 | +Rewrap.reshape(x::AbstractArray, args...) = Base.reshape(x, args...) |
| 36 | + |
| 37 | +@constprop function Rewrap.reshape(x::AbstractArray, ops::Tuple{LocalReshape,Vararg{LocalReshape}}) |
| 38 | + r = resolve(ops, Val(ndims(x))) |
| 39 | + r(x) |
| 40 | +end |
| 41 | + |
| 42 | +@constprop function Rewrap.reshape(x::AbstractArray, ops::Tuple{AnyOp,Vararg{AnyOp}}) |
| 43 | + count(op -> op isa ColonOrEllipsis, ops) > 1 && throw(ArgumentError("At most one Colon or Ellipsis is allowed")) |
| 44 | + ops′ = map(ops) do op |
| 45 | + if op isa Colon |
| 46 | + Merge(..) |
| 47 | + elseif op isa Ellipsis |
| 48 | + Keep(..) |
| 49 | + else |
| 50 | + op |
| 51 | + end |
| 52 | + end |
| 53 | + return Rewrap.reshape(x, ops′) |
| 54 | +end |
| 55 | + |
| 56 | +@constprop function Rewrap.reshape(x::AbstractArray, op1::AnyOp, ops::AnyOp...) |
| 57 | + return Rewrap.reshape(x, (op1, ops...)) |
| 58 | +end |
| 59 | + |
| 60 | +## Base.reshape |
| 61 | + |
| 62 | +""" |
| 63 | + Base.reshape(x, ops...) |
| 64 | + Base.reshape(x, ops::Tuple) |
| 65 | +
|
| 66 | +Reshape the array `x` using the given operations, which can include |
| 67 | +`:` (Base.Colon) and `..` (EllipsisNotation.Ellipsis), but there |
| 68 | +must be at least one `LocalReshape`. |
| 69 | +
|
| 70 | +```jldoctest |
| 71 | +julia> x = view(rand(4, 5, 2), 1:3, :, :); |
| 72 | +
|
| 73 | +julia> x′ = reshape(x, Keep(), :); |
| 74 | +
|
| 75 | +julia> size(x′) |
| 76 | +(3, 10) |
| 77 | +
|
| 78 | +julia> y′ = rand(2, 3) * x′; # project from 3 to 2 |
| 79 | +
|
| 80 | +julia> size(y′) |
| 81 | +(2, 10) |
| 82 | +
|
| 83 | +julia> y = reshape(y′, Keep(), Split(1, size(x)[2:end])); |
| 84 | +
|
| 85 | +julia> size(y) |
| 86 | +(2, 5, 2) |
| 87 | +
|
| 88 | +julia> reshape(view(rand(2, 3), :, 1:2), Merge(..)) # can not use a single `:` (type piracy) |
| 89 | +``` |
| 90 | +""" |
| 91 | +Base.reshape |
| 92 | + |
| 93 | +@constprop function Base.reshape(x::AbstractArray, ops::Tuple{LocalReshape,Vararg{LocalReshape}}) |
| 94 | + return Rewrap.reshape(x, ops) |
| 95 | +end |
| 96 | + |
| 97 | +@constprop function Base.reshape( |
| 98 | + x::AbstractArray, |
| 99 | + ops::Union{ |
| 100 | + Tuple{ColonOrEllipsis,LocalReshape,Vararg{LocalReshape}}, |
| 101 | + Tuple{LocalReshape,Vararg{AnyOp}} |
| 102 | + } |
| 103 | +) |
| 104 | + return Rewrap.reshape(x, ops) |
| 105 | +end |
| 106 | + |
| 107 | +@constprop function Base.reshape( |
| 108 | + x::AbstractArray, op1::LocalReshape, ops::Union{LocalReshape,ColonOrEllipsis}... |
| 109 | +) |
| 110 | + return Rewrap.reshape(x, (op1, ops...)) |
| 111 | +end |
| 112 | + |
| 113 | +@constprop function Base.reshape( |
| 114 | + x::AbstractArray, op1::ColonOrEllipsis, op2::LocalReshape, ops::LocalReshape... |
| 115 | +) |
| 116 | + return Rewrap.reshape(x, (op1, op2, ops...)) |
| 117 | +end |
| 118 | + |
| 119 | + |
0 commit comments