Skip to content

Commit 76ce2c5

Browse files
authored
Fix adjoint and transpose for 0×0 matrix, fixes #1066 (#1067)
1 parent def8fc2 commit 76ce2c5

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/linalg.jl

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,12 @@ end
6969
@inline transpose(a::Adjoint{<:Any,<:Union{StaticVector,StaticMatrix}}) = conj(a.parent)
7070
@inline transpose(a::Adjoint{<:Real,<:Union{StaticVector,StaticMatrix}}) = a.parent
7171

72-
@generated function _transpose(::Size{S}, m::StaticMatrix) where {S}
73-
Snew = (S[2], S[1])
74-
75-
exprs = [:(transpose(m[$(LinearIndices(S)[j1, j2])])) for j2 = 1:S[2], j1 = 1:S[1]]
76-
72+
@generated function _transpose(::Size{S}, m::StaticMatrix{n1, n2, T}) where {n1, n2, S, T}
73+
exprs = [:(transpose(m[$(LinearIndices(S)[j1, j2])])) for j2 in 1:n2, j1 in 1:n1]
7774
return quote
7875
$(Expr(:meta, :inline))
7976
elements = tuple($(exprs...))
80-
@inbounds return similar_type($m, eltype(elements), Size($Snew))(elements)
77+
@inbounds return similar_type($m, Base.promote_op(transpose, T), Size($(n2,n1)))(elements)
8178
end
8279
end
8380

@@ -86,15 +83,12 @@ end
8683
@inline adjoint(a::Transpose{<:Real,<:Union{StaticVector,StaticMatrix}}) = a.parent
8784
@inline adjoint(a::Adjoint{<:Any,<:Union{StaticVector,StaticMatrix}}) = a.parent
8885

89-
@generated function _adjoint(::Size{S}, m::StaticMatrix) where {S}
90-
Snew = (S[2], S[1])
91-
92-
exprs = [:(adjoint(m[$(LinearIndices(S)[j1, j2])])) for j2 = 1:S[2], j1 = 1:S[1]]
93-
86+
@generated function _adjoint(::Size{S}, m::StaticMatrix{n1, n2, T}) where {n1, n2, S, T}
87+
exprs = [:(adjoint(m[$(LinearIndices(S)[j1, j2])])) for j2 in 1:n2, j1 in 1:n1]
9488
return quote
9589
$(Expr(:meta, :inline))
9690
elements = tuple($(exprs...))
97-
@inbounds return similar_type($m, eltype(elements), Size($Snew))(elements)
91+
@inbounds return similar_type($m, Base.promote_op(adjoint, T), Size($(n2,n1)))(elements)
9892
end
9993
end
10094

test/linalg.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,14 @@ StaticArrays.similar_type(::Union{RotMat2,Type{RotMat2}}) = SMatrix{2,2,Float64,
225225
# Recursive adjoint/transpose correctly handles eltype (#708)
226226
@test (@inferred(adjoint(SMatrix{2,2}(fill([1,2], 2,2)))))::SMatrix == SMatrix{2,2}(fill(adjoint([1,2]), 2,2))
227227
@test (@inferred(transpose(SMatrix{2,2}(fill([1,2], 2,2)))))::SMatrix == SMatrix{2,2}(fill(transpose([1,2]), 2,2))
228+
229+
# 0×0 matrix
230+
for T in (SMatrix{0,0,Float64}, MMatrix{0,0,Float64}, SizedMatrix{0,0,Float64})
231+
m = T()
232+
@test adjoint(m)::T == transpose(m)::T == m
233+
end
234+
@test adjoint(SMatrix{0,0,Vector{Int}}()) isa SMatrix{0,0,Adjoint{Int,Vector{Int}}}
235+
@test transpose(SMatrix{0,0,Vector{Int}}()) isa SMatrix{0,0,Transpose{Int,Vector{Int}}}
228236
end
229237

230238
@testset "normalization" begin

0 commit comments

Comments
 (0)