Skip to content

Commit 4cfeb58

Browse files
authored
Deprecate calling of Size to get SizedArray (JuliaArrays#669)
This feature makes it hard to find code which actually uses SizedArray, and it blesses SizedArray in an odd fashion by providing a special syntax for constructing it from the trait which is completely different from the way that other StaticArray subtypes are constructed.
1 parent cab2f07 commit 4cfeb58

13 files changed

+31
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ similar_type(m3) == SArray{Tuple{3,3},Int64,2,9}
124124
Size(m3) === Size(3,3)
125125

126126
# A standard Array can be wrapped into a SizedArray
127-
m4 = Size(3,3)(rand(3,3))
127+
m4 = SizedMatrix{3,3}(rand(3,3))
128128
inv(m4) # Take advantage of specialized fast methods
129129

130130
# reshape() uses Size() or types to specify size:

docs/src/pages/api.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ _det(::Size{(2,2)}, x::StaticMatrix) = x[1,1]*x[2,2] - x[1,2]*x[2,1]
8282
Examples of using `Size` as a compile-time constant include
8383
```julia
8484
reshape(svector, Size(2,2)) # Convert SVector{4} to SMatrix{2,2}
85-
Size(3,3)(rand(3,3)) # Construct a random 3×3 SizedArray (see below)
85+
SizedMatrix{3,3}(rand(3,3)) # Construct a random 3×3 SizedArray (see below)
8686
```
8787

8888
### Indexing
@@ -152,8 +152,9 @@ Another convenient mutable type is the `SizedArray`, which is just a wrapper-typ
152152
about a standard Julia `Array` which declares its known size. For example, if
153153
we knew that `a` was a 2×2 `Matrix`, then we can type `sa = SizedArray{Tuple{2,2}}(a)`
154154
to construct a new object which knows the type (the size will be verified
155-
automatically). A more convenient syntax for obtaining a `SizedArray` is by calling
156-
a `Size` object, e.g. `sa = Size(2,2)(a)`.
155+
automatically). For one and two dimensions, a more convenient syntax for
156+
obtaining a `SizedArray` is by using the `SizedMatrix` and `SizedVector`
157+
aliases, e.g. `sa = SizedMatrix{2,2}(a)`.
157158

158159
Then, methods on `sa` will use the specialized code provided by the *StaticArrays*
159160
package, which in many cases will be much, much faster. For example, calling
@@ -219,17 +220,15 @@ m = [1 2;
219220

220221
sv = SVector{2}(v)
221222
sm = SMatrix{2,2}(m)
222-
sa = SArray{(2,2)}(m)
223+
sa = SArray{Tuple{2,2}}(m)
223224

224-
sized_v = Size(2)(v) # SizedArray{(2,)}(v)
225-
sized_m = Size(2,2)(m) # SizedArray{(2,2)}(m)
225+
sized_v = SizedVector{2}(v)
226+
sized_m = SizedMatrix{2,2}(m)
226227
```
227228

228229
We have avoided adding `SVector(v::AbstractVector)` as a valid constructor to
229230
help users avoid the type instability (and potential performance disaster, if
230-
used without care) of this innocuous looking expression. However, the simplest
231-
way to deal with an `Array` is to create a `SizedArray` by calling a `Size`
232-
instance, e.g. `Size(2)(v)`.
231+
used without care) of this innocuous looking expression.
233232

234233
### Arrays of static arrays
235234

docs/src/pages/quickstart.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ similar_type(m3) == SArray{Tuple{3,3},Int64,2,9}
6464
Size(m3) === Size(3,3)
6565

6666
# A standard Array can be wrapped into a SizedArray
67-
m4 = Size(3,3)(rand(3,3))
67+
m4 = SizedMatrix{3,3}(rand(3,3))
6868
inv(m4) # Take advantage of specialized fast methods
6969

7070
# reshape() uses Size() or types to specify size:

perf/benchmark2.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ A = rand(Float64,N,N)
1919
A = A*A'
2020
As = SMatrix{N,N}(A)
2121
Am = MMatrix{N,N}(A)
22-
Az = Size(N,N)(copy(A))
22+
Az = SizedMatrix{N,N}(copy(A))
2323
@static if fsa
2424
Af = Mat(ntuple(j -> ntuple(i->A[i,j], N), N)) # there is a bug in FixedSizeArrays Mat constructor (13 July 2016)
2525
end

perf/benchmark3.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ for T ∈ [Int64, Float64]
1616
println(" Vectors of length ", N, " and eltype ", T)
1717
println("=====================================================================")
1818
immutables = [rand(SVector{N,T})]
19-
mutables = [rand(T,N), rand(MVector{N,T}), Size(N)(rand(T,N))]
19+
mutables = [rand(T,N), rand(MVector{N,T}), SizedVector{N}(rand(T,N))]
2020
instances = vcat(immutables, mutables)
2121

2222
namelengths = [length(string(typeof(v).name.name)) for v instances]
@@ -45,7 +45,7 @@ for T ∈ [Int64, Float64]
4545
println(" Matrices of size ", N, "×", N, " and eltype ", T)
4646
println("=====================================================================")
4747
immutables = [rand(SMatrix{N,N,T})]
48-
mutables = [rand(T,N,N), rand(MMatrix{N,N,T}), Size(N,N)(rand(T,N,N))]
48+
mutables = [rand(T,N,N), rand(MMatrix{N,N,T}), SizedMatrix{N,N}(rand(T,N,N))]
4949
instances = vcat(immutables, mutables)
5050

5151
namelengths = [length(string(typeof(v).name.name)) for v instances]

src/SizedArray.jl

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ methods defined by the static array package. The size is checked once upon
77
construction to determine if the number of elements (`length`) match, but the
88
array may be reshaped.
99
10-
(Also, `Size(dims...)(array)` acheives the same thing)
10+
The aliases `SizedVector{N}` and `SizedMatrix{N,M}` are provided as more
11+
convenient names for one and two dimensional `SizedArray`s. For example, to
12+
wrap a 2x3 array `a` in a `SizedArray`, use `SizedMatrix{2,3}(a)`.
1113
"""
1214
struct SizedArray{S <: Tuple, T, N, M} <: StaticArray{S, T, N}
1315
data::Array{T, M}
@@ -74,14 +76,10 @@ SizedMatrix{S1,S2,T,M} = SizedArray{Tuple{S1,S2},T,2,M}
7476

7577
Base.dataids(sa::SizedArray) = Base.dataids(sa.data)
7678

77-
"""
78-
Size(dims)(array)
79-
80-
Creates a `SizedArray` wrapping `array` with the specified statically-known
81-
`dims`, so to take advantage of the (faster) methods defined by the static array
82-
package.
83-
"""
84-
(::Size{S})(a::Array) where {S} = SizedArray{Tuple{S...}}(a)
79+
function (::Size{S})(a::Array) where {S}
80+
Base.depwarn("`Size{S}(a::Array)` is deprecated, use `SizedVector{N}(a)`, `SizedMatrix{N,M}(a)` or `SizedArray{Tuple{S}}(a)` instead", :Size)
81+
SizedArray{Tuple{S...}}(a)
82+
end
8583

8684

8785
function promote_rule(::Type{<:SizedArray{S,T,N,M}}, ::Type{<:SizedArray{S,U,N,M}}) where {S,T,U,N,M}

src/abstractarray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ homogenize_shape(shape::Tuple{Vararg{HeterogeneousShape}}) = map(last, shape)
158158
end
159159
end
160160

161-
reshape(a::Array, s::Size{S}) where {S} = s(a)
161+
reshape(a::Array, ::Size{S}) where {S} = SizedArray{Tuple{S...}}(a)
162162

163163
@inline vec(a::StaticArray) = reshape(a, Size(prod(Size(typeof(a)))))
164164

src/cholesky.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,7 @@ end
5353
end
5454

5555
# Otherwise default algorithm returning wrapped SizedArray
56-
@inline _cholesky(s::Size, A::StaticArray) = s(Matrix(cholesky(Hermitian(Matrix(A))).U))
56+
@inline _cholesky(::Size{S}, A::StaticArray) where {S} =
57+
SizedArray{Tuple{S...}}(Matrix(cholesky(Hermitian(Matrix(A))).U))
58+
5759
LinearAlgebra.hermitian_type(::Type{SA}) where {T, S, SA<:SArray{S,T}} = Hermitian{T,SA}

src/lyap.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ _lyap(::Size{(1,1)}, ::Size{(1,1)}, a::StaticMatrix, c::StaticMatrix) = -c/(2a[
88
-(d*c + (a - t*I)*c*(a-t*I)')/(2*d*t) # http://www.nber.org/papers/w8956.pdf
99
end
1010

11-
@inline _lyap(sa::Size, sc::Size, a::StaticMatrix, c::StaticMatrix) = sc(lyap(Array(a),Array(c)))
11+
@inline _lyap(::Size, ::Size{SC}, a::StaticMatrix, c::StaticMatrix) where {SC} =
12+
SizedArray{Tuple{SC...}}(lyap(Array(a),Array(c)))

src/sqrtm.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ end
1717
end
1818
end
1919

20-
@inline _sqrt(s::Size, A::StaticArray) = s(sqrt(Array(A)))
20+
@inline _sqrt(::Size{S}, A::StaticArray) where {S} = SizedArray{Tuple{S...}}(sqrt(Array(A)))

test/abstractarray.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ using StaticArrays, Test, LinearAlgebra
1111

1212
@testset "strides" begin
1313
m1 = MArray{Tuple{3, 4, 5}}(rand(Int, 3, 4, 5))
14-
m2 = Size(3,4,5)(rand(Int, 3, 4, 5))
14+
m2 = SizedArray{Tuple{3,4,5}}(rand(Int, 3, 4, 5))
1515
@test strides(m1) === (1, 3, 12)
1616
@test strides(m2) === (1, 3, 12)
1717
end
@@ -117,7 +117,7 @@ using StaticArrays, Test, LinearAlgebra
117117
M = [1 2; 3 4]
118118
SM = SMatrix{2, 2}(M)
119119
MM = MMatrix{2, 2}(M)
120-
SizeM = Size(2,2)(M)
120+
SizeM = SizedMatrix{2,2}(M)
121121
@test @inferred(copy(SM)) === @SMatrix [1 2; 3 4]
122122
@test @inferred(copy(MM))::MMatrix == M
123123
@test copy(SM).data !== M

test/convert.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ using StaticArrays, Test
22

33
@testset "Copy constructors" begin
44
M = [1 2; 3 4]
5-
SizeM = Size(2,2)(M)
5+
SizeM = SizedMatrix{2,2}(M)
66
@test typeof(SizeM)(SizeM).data === M
77
end # testset
88

test/flatten.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ using StaticArrays, Test
33
@testset "Iterators.flatten" begin
44
for x in [SVector(1.0, 2.0), MVector(1.0, 2.0),
55
@SMatrix([1.0 2.0; 3.0 4.0]), @MMatrix([1.0 2.0]),
6-
Size(1,2)([1.0 2.0])
6+
SizedMatrix{1,2}([1.0 2.0])
77
]
88
X = [x,x,x]
99
@test length(Iterators.flatten(X)) == length(X)*length(x)

0 commit comments

Comments
 (0)