diff --git a/stdlib/LinearAlgebra/src/dense.jl b/stdlib/LinearAlgebra/src/dense.jl index c15a1bd0dcfbd..6f4ca98ba1c07 100644 --- a/stdlib/LinearAlgebra/src/dense.jl +++ b/stdlib/LinearAlgebra/src/dense.jl @@ -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) diff --git a/stdlib/LinearAlgebra/test/dense.jl b/stdlib/LinearAlgebra/test/dense.jl index 42a77e2a12b8e..70bf67d201a2b 100644 --- a/stdlib/LinearAlgebra/test/dense.jl +++ b/stdlib/LinearAlgebra/test/dense.jl @@ -67,7 +67,7 @@ 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ε @@ -75,6 +75,9 @@ bimg = randn(n,2)/2 @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