Skip to content

Commit 146cc3f

Browse files
Fix pinv for Symmetric/Hermitian wrapped SMatrix (#1336)
\`pinv(Symmetric(smatrix))\` failed because the \`pinv\` codepath tried to construct a \`Symmetric\` from the SVD result, which doesn't work. Fix: unwrap \`HermOrSym\` to the underlying \`StaticMatrix\` before calling \`_pinv\`. --------- Co-authored-by: Mateusz Baran <mateuszbaran89@gmail.com>
1 parent a9e23b9 commit 146cc3f

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

src/pinv.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
return _pinv(A_S, atol, rtol)
88
end
99

10+
@inline pinv(A::LinearAlgebra.HermOrSym{T,<:StaticMatrix}; kwargs...) where T = pinv(convert(similar_type(A), A); kwargs...)
11+
1012
@inline function _pinv(A::StaticMatrix{m,n,T}, atol::Real, rtol::Real) where T where m where n
1113
if m == 0 || n == 0
1214
return similar_type(A, Size(n,m))()

test/pinv.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,11 @@ tol = 1e-13
7575
@test norm(N10*M10*N10 - N10) < tol
7676
@test N10 isa StaticMatrix
7777
@test N10 pinv(Matrix(M10))
78+
79+
for wrapper in (Symmetric, Hermitian)
80+
M_w = wrapper(@SMatrix [1.0 0.5; 0.5 2.0])
81+
N_w = pinv(M_w)
82+
@test N_w pinv(Matrix(M_w))
83+
@test N_w isa SMatrix{2,2,Float64}
84+
end
7885
end

0 commit comments

Comments
 (0)