Skip to content

Commit 1c72f5c

Browse files
authored
Fix reshaping of a mutable StaticArray (#933)
* Fix reshaping of a mutable StaticArray * more reshape changes
1 parent 7dc6499 commit 1c72f5c

File tree

4 files changed

+40
-20
lines changed

4 files changed

+40
-20
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "StaticArrays"
22
uuid = "90137ffa-7385-5640-81b9-e52037218182"
3-
version = "1.2.5"
3+
version = "1.2.6"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/abstractarray.jl

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -183,26 +183,20 @@ homogenize_shape(shape::Tuple{Vararg{SOneTo}}) = Size(map(last, shape))
183183
homogenize_shape(shape::Tuple{Vararg{HeterogeneousShape}}) = map(last, shape)
184184

185185

186-
@inline reshape(a::StaticArray, s::Size) = similar_type(a, s)(Tuple(a))
187-
@inline reshape(a::AbstractArray, s::Size) = _reshape(a, IndexStyle(a), s)
188-
@inline reshape(a::StaticArray, s::Tuple{SOneTo,Vararg{SOneTo}}) = reshape(a, homogenize_shape(s))
189-
@generated function _reshape(a::AbstractArray, indexstyle, s::Size{S}) where {S}
190-
if indexstyle == IndexLinear
191-
exprs = [:(a[$i]) for i = 1:prod(S)]
192-
else
193-
exprs = [:(a[$(inds)]) for inds CartesianIndices(S)]
194-
end
195-
196-
return quote
197-
@_inline_meta
198-
if length(a) != prod(s)
199-
throw(DimensionMismatch("Tried to resize dynamic object of size $(size(a)) to $s"))
200-
end
201-
return similar_type(a, s)(tuple($(exprs...)))
202-
end
186+
@inline reshape(a::SArray, s::Size) = similar_type(a, s)(Tuple(a))
187+
@inline reshape(a::AbstractArray, s::Size) = __reshape(a, ((typeof(s).parameters...)...,), s)
188+
@inline reshape(a::SArray, s::Tuple{SOneTo,Vararg{SOneTo}}) = reshape(a, homogenize_shape(s))
189+
@inline function reshape(a::StaticArray, s::Tuple{SOneTo,Vararg{SOneTo}})
190+
return __reshape(a, map(u -> last(u), s), homogenize_shape(s))
191+
end
192+
@inline function __reshape(a, shape, ::Size{S}) where {S}
193+
return SizedArray{Tuple{S...}}(Base._reshape(a, shape))
194+
end
195+
@inline function __reshape(a::SizedArray, shape, ::Size{S}) where {S}
196+
return SizedArray{Tuple{S...}}(Base._reshape(a.data, shape))
203197
end
204198

205-
reshape(a::Array, ::Size{S}) where {S} = SizedArray{Tuple{S...}}(a)
199+
reshape(a::Vector, ::Size{S}) where {S} = SizedArray{Tuple{S...}}(a)
206200

207201
Base.rdims(out::Val{N}, inds::Tuple{SOneTo, Vararg{SOneTo}}) where {N} = Base.rdims(ntuple(i -> SOneTo(1), Val(N)), inds)
208202
Base.rdims(out::Tuple{Any}, inds::Tuple{SOneTo, Vararg{SOneTo}}) = (SOneTo(Base.rdims_trailing(inds...)),)

test/abstractarray.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ using StaticArrays, Test, LinearAlgebra
109109
@test @inferred(reshape(SVector(1,2,3,4), axes(SMatrix{2,2}(1,2,3,4)))) === SMatrix{2,2}(1,2,3,4)
110110
@test @inferred(reshape(SVector(1,2,3,4), Size(2,2))) === SMatrix{2,2}(1,2,3,4)
111111
@test @inferred(reshape([1,2,3,4], Size(2,2)))::SizedArray{Tuple{2,2},Int,2,1} == [1 3; 2 4]
112-
@test_throws DimensionMismatch reshape([1 2; 3 4], Size(2,1,2))
112+
@test_throws DimensionMismatch reshape([1 2; 3 4], Size(2,2,2))
113113

114114
@test @inferred(vec(SMatrix{2, 2}([1 2; 3 4])))::SVector{4,Int} == [1, 3, 2, 4]
115115

@@ -119,6 +119,21 @@ using StaticArrays, Test, LinearAlgebra
119119
# IndexLinear
120120
@test reshape(view(ones(4, 4), 1, 1:4), Size(4, 1)) == SMatrix{4,1}(ones(4, 1))
121121
@test_throws DimensionMismatch reshape(view(ones(4,4), 1:4, 1:2), Size(5, 2))
122+
123+
# mutation
124+
m = @MMatrix [1 2; 3 4]
125+
mr = reshape(m, SOneTo(4))
126+
mr[2] = 10
127+
@test m == SA[1 2; 10 4]
128+
129+
mrs = reshape(m, Size(4))
130+
mrs[2] = 10
131+
@test m == SA[1 2; 10 4]
132+
133+
ms = SizedMatrix{2,2}([1 2; 3 4])
134+
msr = reshape(ms, SOneTo(4))
135+
msr[2] = 10
136+
@test ms == SA[1 2; 10 4]
122137
end
123138

124139
@testset "copy" begin

test/broadcast.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,15 @@ end
270270
@test DS2 == DS .* DS
271271
end
272272
end
273+
274+
@testset "broadcast! with reshaping" begin
275+
m = @MMatrix [1 2; 3 4]
276+
m[1:2] .= 10
277+
@test m == SA[10 2; 10 4]
278+
279+
ms = SizedMatrix{2,2}([1 2; 3 4])
280+
ms[1:2] .= 10
281+
@test ms == SA[10 2; 10 4]
282+
end
283+
273284
end

0 commit comments

Comments
 (0)