Skip to content

Commit d6883bc

Browse files
committed
fix: LU potentially gives 0 pivots
1 parent c66fe0d commit d6883bc

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/matrixlu.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ function _optimizerrlu!(
136136
k = lu.npivot + 1
137137
newpivot = submatrixargmax(abs2, A, k)
138138
lu.error = abs(A[newpivot...])
139-
if abs(lu.error) < reltol * maxerror || abs(lu.error) < abstol
139+
# Add at least 1 pivot to get a well-defined L * U
140+
if (abs(lu.error) < reltol * maxerror || abs(lu.error) < abstol) && lu.npivot > 0
140141
break
141142
end
142143
maxerror = max(maxerror, lu.error)

test/test_matrixlu.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,20 @@ using LinearAlgebra
192192
lu3 = TCI.rrlu(A, abstol=0.0)
193193
@test TCI.lastpivoterror(lu3) == 0.0
194194
end
195+
196+
@testset "LU for matrices with very small absolute values" begin
197+
A = 1e-13 * [
198+
0.585383 0.124568 0.352426 0.573507
199+
0.865875 0.600153 0.727443 0.902388
200+
0.913477 0.954081 0.116965 0.817
201+
0.985918 0.516114 0.600366 0.0200085
202+
]
203+
204+
lu = TCI.rrlu(A, abstol=1e-3)
205+
@test TCI.npivots(lu) == 1
206+
@test length(TCI.pivoterrors(lu)) > 0
207+
@test TCI.lastpivoterror(lu) > 0
208+
@test size(lu) == size(A)
209+
@test maximum(abs.(TCI.left(lu) * TCI.right(lu) .- A)) < 1e-3
210+
end
195211
end

0 commit comments

Comments
 (0)