diff --git a/stdlib/SuiteSparse/src/spqr.jl b/stdlib/SuiteSparse/src/spqr.jl index 2678ce09a4839..0450bde1c07bc 100644 --- a/stdlib/SuiteSparse/src/spqr.jl +++ b/stdlib/SuiteSparse/src/spqr.jl @@ -23,7 +23,7 @@ const ORDERING_BESTAMD = Int32(9) # try COLAMD and AMD; pick best# # tried. If there is a high fill-in with AMD then try METIS(A'A) and take # the best of AMD and METIS. METIS is not tried if it isn't installed. -using SparseArrays: SparseMatrixCSC +using SparseArrays: SparseMatrixCSC, nnz using ..SuiteSparse.CHOLMOD using ..SuiteSparse.CHOLMOD: change_stype!, free! @@ -342,7 +342,7 @@ end _ret_size(F::QRSparse, b::AbstractVector) = (size(F, 2),) _ret_size(F::QRSparse, B::AbstractMatrix) = (size(F, 2), size(B, 2)) -LinearAlgebra.rank(F::QRSparse) = maximum(F.R.rowval) +LinearAlgebra.rank(F::QRSparse) = reduce(max, view(F.R.rowval, 1:nnz(F.R)), init = eltype(F.R.rowval)(0)) function (\)(F::QRSparse{T}, B::VecOrMat{Complex{T}}) where T<:LinearAlgebra.BlasReal # |z1|z3| reinterpret |x1|x2|x3|x4| transpose |x1|y1| reshape |x1|y1|x3|y3| diff --git a/stdlib/SuiteSparse/test/spqr.jl b/stdlib/SuiteSparse/test/spqr.jl index 382bd8dfe1351..96ee478937297 100644 --- a/stdlib/SuiteSparse/test/spqr.jl +++ b/stdlib/SuiteSparse/test/spqr.jl @@ -93,4 +93,10 @@ end @test propertynames(F) == (:R, :Q, :prow, :pcol) @test propertynames(F, true) == (:R, :Q, :prow, :pcol, :factors, :τ, :cpiv, :rpivinv) end + +@testset "rank" begin + @test rank(qr(sprandn(10, 5, 1.0)*sprandn(5, 10, 1.0))) == 5 + @test all(iszero, (rank(qr(spzeros(10, i))) for i in 1:10)) +end + end