Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in pinv #1082

Merged
merged 3 commits into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/pinv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ end
minlen = min(m,n)
exprs = [:(zero($T)) for i in 1:n, j in 1:m]
for i in 1:minlen
exprs[i,i] = :(ifelse(A[$i,$i] > tol, inv(A[$i,$i]), zero($T)))
exprs[i,i] = :(ifelse(abs(A[$i,$i]) > tol, inv(A[$i,$i]), zero($T)))
end
return quote
Base.@_inline_meta
Expand Down
6 changes: 3 additions & 3 deletions test/pinv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ tol = 1e-13
@test N5 isa SMatrix{5,0,Float64}
@test N5 ≈ pinv(Matrix(M5))

M6 = @SMatrix [1/2 0 0;0 5/3 0;0 0 0;0 0 0]
M6 = @SMatrix [1/2 0 0;0 -5/3 0;0 0 0;0 0 0]
N6 = pinv(M6)
@test norm(M6*N6*M6 - M6) < tol
@test norm(N6*M6*N6 - N6) < tol
@test N6 isa SMatrix{3,4,Float64}
@test N6 ≈ I(3)/Matrix(M6)
# @test N6 ≈ pinv(Matrix(M6)) # Fails on Julia ≥v1.7 https://github.com/JuliaLang/julia/issues/44234
@test N6 ≈ pinv(Matrix(M6))

M7 = M6'
N7 = pinv(M7)
@test norm(M7*N7*M7 - M7) < tol
@test norm(N7*M7*N7 - N7) < tol
@test N7 isa SMatrix{4,3,Float64}
@test N7 ≈ I(4)/Matrix(M7)
# @test N7 ≈ pinv(Matrix(M7)) # Fails on Julia ≥v1.7 https://github.com/JuliaLang/julia/issues/44234
@test N7 ≈ pinv(Matrix(M7))

M8 = @MMatrix [0.5 1.1 0.0;0.0 -2.8 0.0;0.0 0.0 0.0;0.0 0.0 0.0]
N8 = pinv(M8)
Expand Down