Using reshape with UInt16 for dims


#1

I was surprised to find out that using UInt16s is not a valid way to reshape an Array. I get the following behavior on Julia 0.6.4 as well as Julia 1.0.2

julia> a = collect(1:6)
6-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6

julia> b = reshape(a, 3, 2)
3×2 Array{Int64,2}:
 1  4
 2  5
 3  6

julia> c = reshape(a, UInt16(3), UInt16(2))
ERROR: MethodError: no method matching reshape(::Array{Int64,1}, ::UInt16, ::UInt16)
Closest candidates are:
  reshape(::Array{T,N}, ::Tuple{Vararg{Int64,N}}) where {N, T} at array.jl:303
  reshape(::Array{T,N} where N, ::Tuple{Vararg{Int64,N}}) where {N, T} at array.jl:314
  reshape(::AbstractArray, ::Int64...) at reshapedarray.jl:99
  ...
Stacktrace:
 [1] top-level scope at none:0

julia>

Is this intended? I’m reading a UInt16 from a network stream as information to reshape incoming data and I now have to cast to explicitly convert my UInt16's to Int32's.

As an aside, I do enjoy the irony that reshaping with negative numbers seems invalid, but Int32 allows for that :slight_smile:


#2

It works, but you need to pass the dims as a tuple.
c = reshape(1:6, (UInt16(3), UInt16(2))).


#3

Yeah, ensuring we support all the different permutations of integer types as dimension lengths through all of our APIs is a challenge. Looks like we missed this case.


#4

Am I missing something in how this works? It looks like Int64 to me even after the reshape.

julia> a
6-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6

julia> b = reshape(a, (UInt16(3), UInt16(2)))
3×2 Array{Int64,2}:
 1  4
 2  5
 3  6

julia> b = reshape(1:6, (UInt16(3), UInt16(2)))
3×2 reshape(::UnitRange{Int64}, 3, 2) with eltype Int64:
 1  4
 2  5
 3  6

#5

Reshape doesn’t change the element type. Perhaps you want UInt16.(b) ?


#6

@oliver My original question is concerning the eltype of the supplied, new dimensions, not the eltype of the reshaped array.