Skip to content

Commit dd361f8

Browse files
authored
Make similar_type preserve SizedArray (#719)
1 parent b948e05 commit dd361f8

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/abstractarray.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ similar_type(::Type{SA},::Type{T},s::Size{S}) where {SA<:Union{MVector,MMatrix,M
8686

8787
mutable_similar_type(::Type{T}, s::Size{S}, ::Type{Val{D}}) where {T,S,D} = MArray{Tuple{S...},T,D,prod(s)}
8888

89-
# Should `SizedArray` stay the same, and also take over an `Array`?
90-
#similar_type{SA<:SizedArray,T,S}(::Type{SA},::Type{T},s::Size{S}) = sizedarray_similar_type(T,s,length_val(s))
91-
#similar_type{A<:Array,T,S}(::Type{A},::Type{T},s::Size{S}) = sizedarray_similar_type(T,s,length_val(s))
89+
similar_type(::Type{<:SizedArray},::Type{T},s::Size{S}) where {S,T} = sizedarray_similar_type(T,s,length_val(s))
90+
# Should SizedArray also be used for normal Array?
91+
#similar_type(::Type{<:Array},::Type{T},s::Size{S}) where {S,T} = sizedarray_similar_type(T,s,length_val(s))
9292

9393
sizedarray_similar_type(::Type{T},s::Size{S},::Type{Val{D}}) where {T,S,D} = SizedArray{Tuple{S...},T,D,length(s)}
9494

@@ -238,4 +238,3 @@ end
238238
@inbounds return similar_type(a, promote_type(eltype(a), eltype(b)), Size($Snew))(tuple($(exprs...)))
239239
end
240240
end
241-

test/SizedArray.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,12 @@
9999
@test @inferred(promote_type(SizedVector{2,Int,1}, SizedVector{2,Float64,1})) === SizedVector{2,Float64,1}
100100
@test @inferred(promote_type(SizedMatrix{2,3,Float32,2}, SizedMatrix{2,3,Complex{Float64},2})) === SizedMatrix{2,3,Complex{Float64},2}
101101
end
102+
103+
@testset "reshaping" begin
104+
y = rand(4,1,2)
105+
sy = SizedArray{Tuple{size(y)...}}(y)
106+
107+
@test vec(sy) isa SizedArray{Tuple{8}, Float64}
108+
@test reshape(sy, Size(2,4)) isa SizedArray{Tuple{2, 4}, Float64}
109+
end
102110
end

0 commit comments

Comments
 (0)