Skip to content

Commit bdf8912

Browse files
authored
Add rot180 etc. for SMatrix (#826)
add matrix rotations for SMatrix
1 parent 1b94778 commit bdf8912

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/abstractarray.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,17 @@ reshape(a::Array, ::Size{S}) where {S} = SizedArray{Tuple{S...}}(a)
199199
return Expr(:tuple, (:(v[$i]) for i = N:(-1):1)...)
200200
end
201201

202+
@generated function Base.rot180(A::SMatrix{M,N}) where {M,N}
203+
exs = rot180([:(getindex(A,$i,$j)) for i in 1:M, j in 1:N])
204+
return :(SMatrix{M,N}($(exs...)))
205+
end
206+
for rot in [:rotl90, :rotr90]
207+
@eval @generated function Base.$rot(A::SMatrix{M,N}) where {M,N}
208+
exs = $rot([:(getindex(A,$i,$j)) for i in 1:M, j in 1:N])
209+
return :(SMatrix{N,M}($(exs...)))
210+
end
211+
end
212+
202213
# TODO permutedims?
203214

204215
#--------------------------------------------------

test/abstractarray.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@ using StaticArrays, Test, LinearAlgebra
132132
@test @inferred(reverse(m))::typeof(m) == MVector(3, 2, 1)
133133
end
134134

135+
@testset "rotate" begin
136+
M = [1 2; 3 4]
137+
SM = SMatrix{2, 2}(M)
138+
@test @inferred(rotl90(SM)) === @SMatrix [2 4; 1 3]
139+
@test @inferred(rot180(SM)) === @SMatrix [4 3; 2 1]
140+
@test @inferred(rotr90(SM)) === @SMatrix [3 1; 4 2]
141+
M23 = rand(2, 3)
142+
SM23 = SMatrix{2, 3}(M23)
143+
@test @inferred(rotl90(SM23)) == rotl90(M23)
144+
@test @inferred(rot180(SM23)) == rot180(M23)
145+
@test @inferred(rotr90(SM23)) == rotr90(M23)
146+
end
147+
135148
@testset "Conversion to AbstractArray" begin
136149
# Issue #746
137150
# conversion to AbstractArray changes the eltype from Int to Float64

0 commit comments

Comments
 (0)