Skip to content

Commit

Permalink
pinv now works for hermitian matrices. (JuliaLang#41006)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Karrasch <[email protected]>
  • Loading branch information
ArunS-tack and dkarrasch committed May 31, 2021
1 parent b46df09 commit ce8f660
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,7 @@ function pinv(A::AbstractMatrix{T}; atol::Real = 0.0, rtol::Real = (eps(real(flo
B[ind] .= (x -> abs(x) > tol ? pinv(x) : zero(x)).(dA)
return B
end
SVD = svd(A, full = false)
SVD = svd(A)
tol = max(rtol*maximum(SVD.S), atol)
Stype = eltype(SVD.S)
Sinv = fill!(similar(A, Stype, length(SVD.S)), 0)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ end
inv(A::Hermitian{<:Any,<:StridedMatrix}) = Hermitian(_inv(A), sym_uplo(A.uplo))
inv(A::Symmetric{<:Any,<:StridedMatrix}) = Symmetric(_inv(A), sym_uplo(A.uplo))

function svd(A::RealHermSymComplexHerm, full::Bool=false)
function svd(A::RealHermSymComplexHerm; full::Bool=false)
vals, vecs = eigen(A)
I = sortperm(vals; by=abs, rev=true)
permute!(vals, I)
Expand Down
6 changes: 6 additions & 0 deletions stdlib/LinearAlgebra/test/pinv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ end
@test a.diag[2] 0.0
end

@testset "hermitian matrices" begin
Q = ones(2,2)
C = pinv(Hermitian(Q))/0.25
@test C ones(2,2)
end

if eltya <: LinearAlgebra.BlasReal
@testset "sub-normal numbers/vectors/matrices" begin
a = pinv(floatmin(eltya)/100)
Expand Down
4 changes: 4 additions & 0 deletions stdlib/LinearAlgebra/test/svd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ using LinearAlgebra: BlasComplex, BlasFloat, BlasReal, QRPivoted
@test F.U'F.U Matrix(I, 2, 2)
@test F.Vt'*F.Vt [1]
@test @inferred(svdvals(3:4)) [5]
A = Matrix(1.0I, 2, 2)
Z = svd(Hermitian(A); full=true)
@test Z.S ones(2)
@test Z.U'Z.U I(2)

m1 = [2 0; 0 0]
m2 = [2 -2; 1 1]/sqrt(2)
Expand Down

0 comments on commit ce8f660

Please sign in to comment.