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 nullspace when the matrix is empty #29725

Merged
merged 1 commit into from
Oct 20, 2018
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
8 changes: 4 additions & 4 deletions stdlib/LinearAlgebra/src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1332,14 +1332,14 @@ julia> nullspace(M, 2)
0.0 0.0 1.0
```
"""
function nullspace(A::StridedMatrix, tol::Real = min(size(A)...)*eps(real(float(one(eltype(A))))))
function nullspace(A::AbstractMatrix, tol::Real = min(size(A)...)*eps(real(float(one(eltype(A))))))
m, n = size(A)
(m == 0 || n == 0) && return Matrix{T}(I, n, n)
(m == 0 || n == 0) && return Matrix{eltype(A)}(I, n, n)
SVD = svd(A, full=true)
indstart = sum(SVD.S .> SVD.S[1]*tol) + 1
indstart = sum(s -> s .> SVD.S[1]*tol, SVD.S) + 1
return copy(SVD.Vt[indstart:end,:]')
end
nullspace(a::StridedVector, tol::Real = min(size(a)...)*eps(real(float(one(eltype(a)))))) = nullspace(reshape(a, length(a), 1), tol)
nullspace(a::AbstractVector, tol::Real = min(size(a)...)*eps(real(float(one(eltype(a)))))) = nullspace(reshape(a, length(a), 1), tol)

"""
cond(M, p::Real=2)
Expand Down
5 changes: 4 additions & 1 deletion stdlib/LinearAlgebra/test/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,17 @@ bimg = randn(n,2)/2
end

@testset "Test nullspace" begin
a15null = nullspace(copy(a[:,1:n1]'))
a15null = nullspace(a[:,1:n1]')
@test rank([a[:,1:n1] a15null]) == 10
@test norm(a[:,1:n1]'a15null,Inf) ≈ zero(eltya) atol=300ε
@test norm(a15null'a[:,1:n1],Inf) ≈ zero(eltya) atol=400ε
@test size(nullspace(b), 2) == 0
@test size(nullspace(b, 100*εb), 2) == 0
@test nullspace(zeros(eltya,n)) == Matrix(I, 1, 1)
@test nullspace(zeros(eltya,n), 0.1) == Matrix(I, 1, 1)
# test empty cases
@test nullspace(zeros(n, 0)) == Matrix(I, 0, 0)
@test nullspace(zeros(0, n)) == Matrix(I, n, n)
end
end
end # for eltyb
Expand Down