Skip to content

Commit 59a953c

Browse files
author
Andy Ferris
committed
Added SizedArray wrapper for array
Can allow Array to use the (faster) algorithms from StaticArrays.jl, if the user knows the dimensions at compile-time. A convenience constructor from calling a `Size{(dims...)}()` is provided, e.g. `Size(2,2)(rand(2,2))`.
1 parent acfff71 commit 59a953c

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

src/SizedArray.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
"""
3+
SizedArray{(dims...)}(array)
4+
5+
Wraps an `Array` with a static size, so to take advantage of the (faster)
6+
methods defined by the static array package. The size is checked once upon
7+
construction.
8+
9+
(Also, `Size(dims...)(array)` acheives the same thing)
10+
"""
11+
immutable SizedArray{S,T,N} <: StaticArray{T,N}
12+
data::Array{T,N}
13+
14+
function SizedArray(a)
15+
if size(a) != S
16+
error("Dimensions $(size(a)) don't match static size $S")
17+
end
18+
new(a)
19+
end
20+
end
21+
22+
@inline (::Type{SizedArray{S,T}}){S,T,N}(a::Array{T,N}) = SizedArray{S,T,N}(a)
23+
@inline (::Type{SizedArray{S}}){S,T,N}(a::Array{T,N}) = SizedArray{S,T,N}(a)
24+
25+
@pure size{S}(::Type{SizedArray{S}}) = S
26+
@pure size{S,T}(::Type{SizedArray{S,T}}) = S
27+
@pure size{S,T,N}(::Type{SizedArray{S,T,N}}) = S
28+
29+
@propagate_inbounds getindex(a::SizedArray, i::Int) = getindex(a.data, i...)
30+
@propagate_inbounds setindex!(a::SizedArray, v, i::Int) = setindex!(a.data, v, i...)
31+
32+
typealias SizedVector{S,T} SizedArray{S,T,1}
33+
@inline (::Type{SizedVector{S}}){S,T}(a::Vector{T}) = SizedArray{S,T,1}(a)
34+
35+
typealias SizedMatrix{S,T} SizedArray{S,T,2}
36+
@inline (::Type{SizedMatrix{S}}){S,T}(a::Matrix{T}) = SizedArray{S,T,2}(a)
37+
38+
39+
"""
40+
Size(dims)(array)
41+
42+
Creates a `SizedArray` wrapping `array` with the specified statically-known
43+
`dims`, so to take advantage of the (faster) methods defined by the static array
44+
package.
45+
"""
46+
(::Size{S}){S}(a::Array) = SizedArray{S}(a)

src/StaticArrays.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export StaticScalar, StaticArray, StaticVector, StaticMatrix
1212
export Scalar, SArray, SVector, SMatrix
1313
export MArray, MVector, MMatrix
1414
export FieldVector, MutableFieldVector
15+
export SizedArray, SizedVector, SizedMatrix
1516

1617
export Size
1718

@@ -32,6 +33,7 @@ include("SArray.jl")
3233
include("MVector.jl")
3334
include("MMatrix.jl")
3435
include("MArray.jl")
36+
include("SizedArray.jl")
3537

3638
include("indexing.jl")
3739
include("abstractarray.jl")

src/det.jl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
@inline det(A::StaticMatrix) = _det(Size(A), A)
22

3-
"""
4-
det(Size(m,m), mat)
5-
6-
Calculate the matrix determinate using an algorithm specialized on the size of
7-
the `m`×`m` matrix `mat`, which is much faster for small matrices.
8-
"""
93
@inline _det(::Size{(1,1)}, A::AbstractMatrix) = @inbounds return A[1]
104

115
@inline function _det(::Size{(2,2)}, A::AbstractMatrix)

0 commit comments

Comments
 (0)