1
- """
2
- Dynamic()
3
-
4
- Used to signify that a dimension of an array is not known statically.
5
- """
6
- struct Dynamic end
7
-
8
- const StaticDimension = Union{Int, Dynamic}
9
1
10
2
"""
11
3
dimmatch(x::StaticDimension, y::StaticDimension)
@@ -19,85 +11,13 @@ function dimmatch end
19
11
@inline dimmatch (x:: Int , y:: Int ) = x === y
20
12
@inline dimmatch (x:: StaticDimension , y:: StaticDimension ) = true
21
13
22
- """
23
- Size(dims::Int...)
24
-
25
- `Size` is used extensively throughout the `StaticArrays` API to describe _compile-time_
26
- knowledge of the size of an array. The dimensions are stored as a type parameter and are
27
- statically propagated by the compiler, resulting in efficient, type-inferrable code. For
28
- example, to create a static matrix of zeros, use `A = zeros(SMatrix{3,3})`. The static
29
- size of `A` can be obtained by `Size(A)`. (rather than `size(zeros(3,3))`, which returns
30
- `Base.Tuple{2,Int}`).
31
-
32
- Note that if dimensions are not known statically (e.g., for standard `Array`s),
33
- [`Dynamic()`](@ref) should be used instead of an `Int`.
34
-
35
- Size(a::AbstractArray)
36
- Size(::Type{T<:AbstractArray})
37
-
38
- The `Size` constructor can be used to extract static dimension information from a given
39
- array. For example:
40
-
41
- ```julia-repl
42
- julia> Size(zeros(SMatrix{3, 4}))
43
- Size(3, 4)
44
-
45
- julia> Size(zeros(3, 4))
46
- Size(StaticArrays.Dynamic(), StaticArrays.Dynamic())
47
- ```
48
-
49
- This has multiple uses, including "trait"-based dispatch on the size of a statically-sized
50
- array. For example:
51
-
52
- ```julia
53
- det(x::StaticMatrix) = _det(Size(x), x)
54
- _det(::Size{(1,1)}, x::StaticMatrix) = x[1,1]
55
- _det(::Size{(2,2)}, x::StaticMatrix) = x[1,1]*x[2,2] - x[1,2]*x[2,1]
56
- # and other definitions as necessary
57
- ```
58
-
59
- """
60
- struct Size{S}
61
- function Size {S} () where {S}
62
- new {S::Tuple{Vararg{StaticDimension}}} ()
63
- end
64
- end
65
-
66
- @pure Size (s:: Tuple{Vararg{StaticDimension}} ) = Size {s} ()
67
- @pure Size (s:: StaticDimension... ) = Size {s} ()
68
- @pure Size (s:: Type{<:Tuple} ) = Size {tuple(s.parameters...)} ()
69
-
70
- Base. show (io:: IO , :: Size{S} ) where {S} = print (io, " Size" , S)
71
-
72
- function missing_size_error (:: Type{SA} ) where SA
73
- error ("""
74
- The size of type `$SA ` is not known.
75
-
76
- If you were trying to construct (or `convert` to) a `StaticArray` you
77
- may need to add the size explicitly as a type parameter so its size is
78
- inferrable to the Julia compiler (or performance would be terrible). For
79
- example, you might try
80
-
81
- m = zeros(3,3)
82
- SMatrix(m) # this error
83
- SMatrix{3,3}(m) # correct - size is inferrable
84
- SArray{Tuple{3,3}}(m) # correct, note Tuple{3,3}
85
- """ )
86
- end
87
-
88
- Size (a:: T ) where {T<: AbstractArray } = Size (T)
89
- Size (:: Type{SA} ) where {SA <: StaticArray } = missing_size_error (SA)
90
- Size (:: Type{SA} ) where {SA <: StaticArray{S} } where {S<: Tuple } = @isdefined (S) ? Size (S) : missing_size_error (SA)
91
-
92
14
Size (:: Type{Adjoint{T, A}} ) where {T, A <: AbstractVecOrMat{T} } = Size (Size (A)[2 ], Size (A)[1 ])
93
15
Size (:: Type{Transpose{T, A}} ) where {T, A <: AbstractVecOrMat{T} } = Size (Size (A)[2 ], Size (A)[1 ])
94
16
Size (:: Type{Symmetric{T, A}} ) where {T, A <: AbstractMatrix{T} } = Size (A)
95
17
Size (:: Type{Hermitian{T, A}} ) where {T, A <: AbstractMatrix{T} } = Size (A)
96
18
Size (:: Type{Diagonal{T, A}} ) where {T, A <: AbstractVector{T} } = Size (Size (A)[1 ], Size (A)[1 ])
97
19
Size (:: Type{<:LinearAlgebra.AbstractTriangular{T, A}} ) where {T,A} = Size (A)
98
20
99
- @pure Size (:: Type{<:AbstractArray{<:Any, N}} ) where {N} = Size (ntuple (_ -> Dynamic (), N))
100
-
101
21
struct Length{L}
102
22
function Length {L} () where L
103
23
check_length (L)
0 commit comments