Skip to content

Commit b313d25

Browse files
mateuszbaranc42f
authored andcommitted
Conversion of SizedArray to Array reshapes (#666)
* Deprecate reshaping of Array when wrapping in a SizedArray * Array(::SizedArray) now makes a copy for consistency with Base
1 parent 973a709 commit b313d25

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

src/SizedArray.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ struct SizedArray{S <: Tuple, T, N, M} <: StaticArray{S, T, N}
1818
if length(a) != tuple_prod(S)
1919
error("Dimensions $(size(a)) don't match static size $S")
2020
end
21+
if size(a) != size_to_tuple(S)
22+
Base.depwarn("Construction of `SizedArray` with an `Array` of a different
23+
size is deprecated. If you need this functionality report it at
24+
https://github.com/JuliaArrays/StaticArrays.jl/pull/666 .
25+
Calling `sa = reshape(a::Array, s::Size)` will actually reshape
26+
array `a` in the future and converting `sa` back to `Array` will
27+
return an `Array` of shape `s`.", :SizedArray)
28+
end
2129
new{S,T,N,M}(a)
2230
end
2331

@@ -55,9 +63,9 @@ end
5563
@inline convert(::Type{SA}, sa::SA) where {SA<:SizedArray} = sa
5664

5765
# Back to Array (unfortunately need both convert and construct to overide other methods)
58-
@inline Array(sa::SizedArray) = sa.data
59-
@inline Array{T}(sa::SizedArray{S,T}) where {T,S} = sa.data
60-
@inline Array{T,N}(sa::SizedArray{S,T,N}) where {T,S,N} = sa.data
66+
@inline Array(sa::SizedArray) = Array(sa.data)
67+
@inline Array{T}(sa::SizedArray{S,T}) where {T,S} = Array{T}(sa.data)
68+
@inline Array{T,N}(sa::SizedArray{S,T,N}) where {T,S,N} = Array{T,N}(sa.data)
6169

6270
@inline convert(::Type{Array}, sa::SizedArray) = sa.data
6371
@inline convert(::Type{Array{T}}, sa::SizedArray{S,T}) where {T,S} = sa.data

test/SizedArray.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@testset "Inner Constructors" begin
33
@test SizedArray{Tuple{2}, Int, 1}((3, 4)).data == [3, 4]
44
@test SizedArray{Tuple{2}, Int, 1}([3, 4]).data == [3, 4]
5-
@test SizedArray{Tuple{2, 2}, Int, 2}(collect(3:6)).data == collect(3:6)
5+
@test_deprecated SizedArray{Tuple{2, 2}, Int, 2}(collect(3:6)).data == collect(3:6)
66
@test size(SizedArray{Tuple{4, 5}, Int, 2}(undef).data) == (4, 5)
77
@test size(SizedArray{Tuple{4, 5}, Int}(undef).data) == (4, 5)
88

@@ -25,7 +25,7 @@
2525
@test @inferred(SizedArray{Tuple{2}}([1,2]))::SizedArray{Tuple{2},Int,1,1} == [1,2]
2626
@test @inferred(SizedArray{Tuple{2,2}}([1 2;3 4]))::SizedArray{Tuple{2,2},Int,2,2} == [1 2; 3 4]
2727
# From Array, reshaped
28-
@test @inferred(SizedArray{Tuple{2,2}}([1,2,3,4]))::SizedArray{Tuple{2,2},Int,2,1} == [1 3; 2 4]
28+
@test_deprecated @inferred(SizedArray{Tuple{2,2}}([1,2,3,4]))::SizedArray{Tuple{2,2},Int,2,1} == [1 3; 2 4]
2929
# Uninitialized
3030
@test @inferred(SizedArray{Tuple{2,2},Int,2}(undef)) isa SizedArray{Tuple{2,2},Int,2,2}
3131
@test @inferred(SizedArray{Tuple{2,2},Int}(undef)) isa SizedArray{Tuple{2,2},Int,2,2}
@@ -41,14 +41,14 @@
4141
@test @inferred(SizedVector{2}([1,2]))::SizedArray{Tuple{2},Int,1,1} == [1,2]
4242
@test @inferred(SizedVector{2}((1,2)))::SizedArray{Tuple{2},Int,1,1} == [1,2]
4343
# Reshaping
44-
@test @inferred(SizedVector{2}([1 2]))::SizedArray{Tuple{2},Int,1,2} == [1,2]
44+
@test_deprecated @inferred(SizedVector{2}([1 2]))::SizedArray{Tuple{2},Int,1,2} == [1,2]
4545
# Back to Vector
4646
@test Vector(SizedVector{2}((1,2))) == [1,2]
4747
@test convert(Vector, SizedVector{2}((1,2))) == [1,2]
4848

4949
@test @inferred(SizedMatrix{2,2}([1 2; 3 4]))::SizedArray{Tuple{2,2},Int,2,2} == [1 2; 3 4]
5050
# Reshaping
51-
@test @inferred(SizedMatrix{2,2}([1,2,3,4]))::SizedArray{Tuple{2,2},Int,2,1} == [1 3; 2 4]
51+
@test_deprecated @inferred(SizedMatrix{2,2}([1,2,3,4]))::SizedArray{Tuple{2,2},Int,2,1} == [1 3; 2 4]
5252
@test @inferred(SizedMatrix{2,2}((1,2,3,4)))::SizedArray{Tuple{2,2},Int,2,2} == [1 3; 2 4]
5353
# Back to Matrix
5454
@test Matrix(SizedMatrix{2,2}([1 2;3 4])) == [1 2; 3 4]
@@ -84,7 +84,14 @@
8484
@test convert(Matrix, SMatrix{2,2}((1,2,3,4))) == [1 3; 2 4]
8585
@test convert(Array, SizedArray{Tuple{2,2,2,2}, Int}(ones(2,2,2,2))) == ones(2,2,2,2)
8686
# Conversion after reshaping
87-
@test_broken Array(SizedMatrix{2,2}([1,2,3,4])) == [1 3; 2 4]
87+
@test_deprecated Array(SizedMatrix{2,2}([1,2,3,4])) == [1 3; 2 4]
88+
# Array(a::Array) makes a copy so this should work similarly
89+
a = [1 2; 3 4]
90+
@test Array(SizedMatrix{2,2}(a)) !== a
91+
@test @inferred(convert(Array, SizedMatrix{2,2}(a))) === a
92+
@test @inferred(convert(Array{Int}, SizedMatrix{2,2}(a))) === a
93+
@test @inferred(convert(Matrix{Int}, SizedMatrix{2,2}(a))) === a
94+
@test @inferred(convert(Matrix{Float64}, SizedMatrix{2,2}(a))) == a
8895
end
8996

9097
@testset "promotion" begin

test/abstractarray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ using StaticArrays, Test, LinearAlgebra
101101
@testset "reshape" begin
102102
@test @inferred(reshape(SVector(1,2,3,4), axes(SMatrix{2,2}(1,2,3,4)))) === SMatrix{2,2}(1,2,3,4)
103103
@test @inferred(reshape(SVector(1,2,3,4), Size(2,2))) === SMatrix{2,2}(1,2,3,4)
104-
@test @inferred(reshape([1,2,3,4], Size(2,2)))::SizedArray{Tuple{2,2},Int,2,1} == [1 3; 2 4]
104+
@test_deprecated @inferred(reshape([1,2,3,4], Size(2,2)))::SizedArray{Tuple{2,2},Int,2,1} == [1 3; 2 4]
105105

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

0 commit comments

Comments
 (0)