From cad723fe605fb23fef5e2b2796b145c04dc0952c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateus=20Ara=C3=BAjo?= Date: Sun, 29 Mar 2026 14:43:14 +0200 Subject: [PATCH] fix infinite loop in cond --- src/linalg.jl | 2 +- test/linalg_solvers.jl | 18 +++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/linalg.jl b/src/linalg.jl index 0bc9f7ce..82fc3049 100644 --- a/src/linalg.jl +++ b/src/linalg.jl @@ -1742,7 +1742,7 @@ function opnormestinv(A::AbstractSparseMatrixCSC{T}, t::Integer = min(2,maximum( repeated = true end end - if !repeated + if !repeated && n > 2 saux2 = S[1:n,j]' * S_old[1:n,1:t] if _any_abs_eq(saux2,n) repeated = true diff --git a/test/linalg_solvers.jl b/test/linalg_solvers.jl index b59659b4..ec2d4b37 100644 --- a/test/linalg_solvers.jl +++ b/test/linalg_solvers.jl @@ -129,20 +129,24 @@ end @testset "sparse matrix cond" begin local A = sparse(reshape([1.0], 1, 1)) - Ac = sprandn(20, 20,.5) + im*sprandn(20, 20,.5) - Ar = sprandn(20, 20,.5) + eps()*I @test cond(A, 1) == 1.0 - # For a discussion of the tolerance, see #14778 - @test 0.99 <= cond(Ar, 1) \ opnorm(Ar, 1) * opnorm(inv(Array(Ar)), 1) < 3 - @test 0.99 <= cond(Ac, 1) \ opnorm(Ac, 1) * opnorm(inv(Array(Ac)), 1) < 3 - @test 0.99 <= cond(Ar, Inf) \ opnorm(Ar, Inf) * opnorm(inv(Array(Ar)), Inf) < 3 - @test 0.99 <= cond(Ac, Inf) \ opnorm(Ac, Inf) * opnorm(inv(Array(Ac)), Inf) < 3 @test_throws ArgumentError cond(A,2) @test_throws ArgumentError cond(A,3) Arect = spzeros(10, 6) @test_throws DimensionMismatch cond(Arect, 1) @test_throws ArgumentError cond(Arect,2) @test_throws DimensionMismatch cond(Arect, Inf) + Ac = sprandn(20, 20,.5) + im*sprandn(20, 20,.5) + Ar = sprandn(20, 20,.5) + eps()*I + # For a discussion of the tolerance, see #14778 + @test 0.99 <= cond(Ar, 1) \ opnorm(Ar, 1) * opnorm(inv(Array(Ar)), 1) < 3 + @test 0.99 <= cond(Ac, 1) \ opnorm(Ac, 1) * opnorm(inv(Array(Ac)), 1) < 3 + @test 0.99 <= cond(Ar, Inf) \ opnorm(Ar, Inf) * opnorm(inv(Array(Ar)), Inf) < 3 + @test 0.99 <= cond(Ac, Inf) \ opnorm(Ac, Inf) * opnorm(inv(Array(Ac)), Inf) < 3 + #issue 680 + A22 = sparse(randn(2,2)) + @test 0.99 ≤ cond(Array(A22), 1) / cond(A22, 1) < 3 + @test 0.99 ≤ cond(Array(A22), Inf) / cond(A22, Inf) < 3 end @testset "sparse matrix opnormestinv" begin