Skip to content

Commit 22a05ef

Browse files
authored
permutedims for vectors and matrices (#917)
* permutedims, easy cases * fix * add sizedarray methods * tests * bump version
1 parent 1d5524c commit 22a05ef

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "StaticArrays"
22
uuid = "90137ffa-7385-5640-81b9-e52037218182"
3-
version = "1.2.8"
3+
version = "1.2.9"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/abstractarray.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,21 @@ for rot in [:rotl90, :rotr90]
224224
end
225225
end
226226

227-
# TODO permutedims?
227+
# TODO permutedims? So far just the cases without perm:
228+
229+
Base.permutedims(A::SVector{N}) where {N} = SMatrix{1,N}(A.data...)
230+
Base.permutedims(A::MVector{N}) where {N} = MMatrix{1,N}(A.data...)
231+
Base.permutedims(A::SizedVector{N}) where {N} = SizedMatrix{1,N}(permutedims(A.data))
232+
233+
@generated function Base.permutedims(A::SMatrix{M,N}) where {M,N}
234+
exs = permutedims([:(getindex(A,$i,$j)) for i in 1:M, j in 1:N])
235+
return :(SMatrix{N,M}($(exs...)))
236+
end
237+
@generated function Base.permutedims(A::MMatrix{M,N}) where {M,N}
238+
exs = permutedims([:(getindex(A,$i,$j)) for i in 1:M, j in 1:N])
239+
return :(MMatrix{N,M}($(exs...)))
240+
end
241+
Base.permutedims(A::SizedMatrix{M,N}) where {M,N} = SizedMatrix{N,M}(permutedims(A.data))
228242

229243
#--------------------------------------------------
230244
# Concatenation

test/abstractarray.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,24 @@ using StaticArrays, Test, LinearAlgebra
218218
end
219219
end
220220

221+
@testset "permutedims" begin
222+
# vector -> one-row matrix
223+
@test @inferred(permutedims(SVector(1,2,3))) === SMatrix{1,3}(1,2,3)
224+
@test @inferred(permutedims(MVector(1,2,3))) isa MMatrix{1,3}
225+
@test @inferred(permutedims(MVector(1,2,3))) == [1 2 3]
226+
@test @inferred(permutedims(SizedVector{3}([1,2,3]))) isa SizedMatrix{1,3}
227+
@test @inferred(permutedims(SizedVector{3}([1,2,3]))) == [1 2 3]
228+
229+
# matrix
230+
@test @inferred(permutedims(SMatrix{2,2}(1,2,3,4))) === SMatrix{2,2}(1,3,2,4)
231+
A = rand(2,3)
232+
@test @inferred(permutedims(SMatrix{2,3}(A))) === SMatrix{3,2}(A')
233+
@test @inferred(permutedims(MMatrix{2,3}(A))) isa MMatrix{3,2}
234+
@test @inferred(permutedims(MMatrix{2,3}(A))) == A'
235+
@test @inferred(permutedims(SizedMatrix{2,3}(A))) isa SizedMatrix{3,2}
236+
@test @inferred(permutedims(SizedMatrix{2,3}(A))) == A'
237+
end
238+
221239
@testset "vcat() and hcat()" begin
222240
@test @inferred(vcat(SVector(1,2,3))) === SVector(1,2,3)
223241
@test @inferred(hcat(SVector(1,2,3))) === SMatrix{3,1}(1,2,3)

0 commit comments

Comments
 (0)