Skip to content

Commit

Permalink
Fix bug in nullspace when the matrix is empty (#29725)
Browse files Browse the repository at this point in the history
Make nullspace work for Adjoint

Fixes #28846

(cherry picked from commit 2e2b14c)
  • Loading branch information
andreasnoack authored and KristofferC committed Feb 11, 2019
1 parent cfaa70a commit db716e0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
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

0 comments on commit db716e0

Please sign in to comment.