@@ -36,10 +36,80 @@ start(R::ReshapedArrayIterator) = start(R.iter)
36
36
end
37
37
length (R:: ReshapedArrayIterator ) = length (R. iter)
38
38
39
+ """
40
+ reshape(A, dims...)
41
+ reshape(A, dims)
42
+
43
+ Return an array with the same data as the given array, but with different dimensions.
44
+
45
+ The new dimensions may be specified either as a list of arguments or as a shape
46
+ tuple. At most one dimension may be specified with a `:`, in which case its
47
+ length is computed such that its product with all the specified dimensions is
48
+ equal to the length of the original array A.
49
+
50
+ ```jldoctest
51
+ julia> A = collect(1:16)
52
+ 16-element Array{Int64,1}:
53
+ 1
54
+ 2
55
+ 3
56
+ 4
57
+ 5
58
+ 6
59
+ 7
60
+ 8
61
+ 9
62
+ 10
63
+ 11
64
+ 12
65
+ 13
66
+ 14
67
+ 15
68
+ 16
69
+
70
+ julia> reshape(A, (4, 4))
71
+ 4×4 Array{Int64,2}:
72
+ 1 5 9 13
73
+ 2 6 10 14
74
+ 3 7 11 15
75
+ 4 8 12 16
76
+
77
+ julia> reshape(A, 2, :)
78
+ 2×8 Array{Int64,2}:
79
+ 1 3 5 7 9 11 13 15
80
+ 2 4 6 8 10 12 14 16
81
+ ```
82
+
83
+ """
84
+ reshape
85
+
39
86
reshape (parent:: AbstractArray , dims:: IntOrInd... ) = reshape (parent, dims)
40
87
reshape (parent:: AbstractArray , shp:: NeedsShaping ) = reshape (parent, to_shape (shp))
41
88
reshape (parent:: AbstractArray , dims:: Dims ) = _reshape (parent, dims)
42
89
90
+ # Allow missing dimensions with Colon():
91
+ reshape (parent:: AbstractArray , dims:: Int... ) = _reshape (parent, dims)
92
+ reshape (parent:: AbstractArray , dims:: Union{Int,Colon} ...) = reshape (parent, dims)
93
+ reshape (parent:: AbstractArray , dims:: Tuple{Vararg{Int}} ) = _reshape (parent, dims)
94
+ reshape (parent:: AbstractArray , dims:: Tuple{Vararg{Union{Int,Colon}}} ) = _reshape (parent, _reshape_uncolon (parent, dims))
95
+ # Recursively move dimensions to pre and post tuples, splitting on the Colon
96
+ @inline _reshape_uncolon (A, dims) = _reshape_uncolon (A, (), nothing , (), dims)
97
+ @inline _reshape_uncolon (A, pre, c:: Void , post, dims:: Tuple{Any, Vararg{Any}} ) =
98
+ _reshape_uncolon (A, (pre... , dims[1 ]), c, post, tail (dims))
99
+ @inline _reshape_uncolon (A, pre, c:: Void , post, dims:: Tuple{Colon, Vararg{Any}} ) =
100
+ _reshape_uncolon (A, pre, dims[1 ], post, tail (dims))
101
+ @inline _reshape_uncolon (A, pre, c:: Colon , post, dims:: Tuple{Any, Vararg{Any}} ) =
102
+ _reshape_uncolon (A, pre, c, (post... , dims[1 ]), tail (dims))
103
+ _reshape_uncolon (A, pre, c:: Colon , post, dims:: Tuple{Colon, Vararg{Any}} ) =
104
+ throw (DimensionMismatch (" new dimensions $((pre... , c, post... , dims... )) may only have at most one omitted dimension specified by Colon()" ))
105
+ @inline function _reshape_uncolon (A, pre, c:: Colon , post, dims:: Tuple{} )
106
+ sz, remainder = divrem (length (A), prod (pre)* prod (post))
107
+ remainder == 0 || _throw_reshape_colon_dimmismatch (A, pre, post)
108
+ (pre... , sz, post... )
109
+ end
110
+ _throw_reshape_colon_dimmismatch (A, pre, post) =
111
+ throw (DimensionMismatch (" array size $(length (A)) must be divisible by the product of the new dimensions $((pre... , :, post... )) " ))
112
+
43
113
reshape {T,N} (parent:: AbstractArray{T,N} , ndims:: Type{Val{N}} ) = parent
44
114
function reshape {T,AN,N} (parent:: AbstractArray{T,AN} , ndims:: Type{Val{N}} )
45
115
reshape (parent, rdims ((), indices (parent), Val{N}))
0 commit comments