Skip to content

Commit a2bd935

Browse files
authored
Added method for update (#26)
1 parent 79c62f2 commit a2bd935

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

src/BasicLU.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,19 @@ function update(F::LUFactor, pivot::Float64)
613613
getinfo(F, :pivotError)
614614
end
615615

616+
"""
617+
update(F::LUFactor, pos::Int, newcol::SparseVector) -> (lhs, piverr)
618+
619+
Update the factorization by inserting column `newcol` at index `pos`.
620+
"""
621+
function update(F::LUFactor, pos::Int, newcol::SparseVector{Float64, Int64})
622+
lhs = solve_for_update(F, newcol, getsol=true)
623+
piv = lhs[pos]
624+
solve_for_update(F, pos)
625+
piverr = update(F, piv)
626+
return lhs, piverr
627+
end
628+
616629
"""
617630
nupdate = maxvolume(F::LUFactor, A::SparseMatrixCSC{Float64, Int64}, basis::Vector{Int64}, volumetol::Float64=2.0) -> Int64
618631

test/runtests.jl

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,43 @@ test_factorize("./data/", true)
1212
test_update("./data/")
1313
test_maxvolume("./data/")
1414

15-
m = 1000
16-
obj = LUFactor(m)
17-
B = sprand(m, m, 5e-3) + I
18-
err = factorize(obj, B)
19-
rhs = randn(m)
20-
lhs = solve(obj, rhs, 'N')
21-
res = norm(B * lhs - rhs, Inf)
22-
@test res eps(Float64)
23-
col = sparsevec([1], [1.0], m)
24-
lhs = solve_for_update(obj, col, getsol=true)
25-
vmax, j = findmax(abs.(lhs))
26-
piv = lhs[j]
27-
solve_for_update(obj, j)
28-
piverr = update(obj, piv)
29-
lhs = solve(obj, rhs, 'N')
30-
B[:,j] = col
31-
res = norm(B * lhs - rhs, Inf)
32-
@test res eps(Float64)
15+
@testset "Basic test" begin
16+
m = 1000
17+
obj = LUFactor(m)
18+
B = sprand(m, m, 5e-3) + I
19+
err = factorize(obj, B)
20+
rhs = randn(m)
21+
lhs = solve(obj, rhs, 'N')
22+
res = norm(B * lhs - rhs, Inf)
23+
@test res eps(Float64)
24+
col = sparsevec([1], [1.0], m)
25+
lhs = solve_for_update(obj, col, getsol=true)
26+
vmax, j = findmax(abs.(lhs))
27+
piv = lhs[j]
28+
solve_for_update(obj, j)
29+
piverr = update(obj, piv)
30+
lhs = solve(obj, rhs, 'N')
31+
B[:,j] = col
32+
res = norm(B * lhs - rhs, Inf)
33+
@test res eps(Float64)
34+
end
35+
36+
@testset "Integrated rank-one update" begin
37+
m = 500
38+
for _ in 1:100
39+
F = LUFactor(m)
40+
B = sprand(m, m, 5e-3) + 3I
41+
err = factorize(F, B)
42+
rhs = randn(m)
43+
lhs = solve(F, rhs, 'N')
44+
res = norm(B * lhs - rhs, Inf)
45+
@test res sqrt(eps())
46+
ridx1, ridx2 = rand(1:m÷2), rand(m÷2+1:m)
47+
v1, v2 = rand(), rand()
48+
col = sparsevec([ridx1], [v1], m)
49+
_, piverr = BasicLU.update(F, ridx1, col)
50+
lhs = solve(F, rhs, 'N')
51+
B[:,ridx1] .= col
52+
@test norm(B * lhs - rhs, Inf) <= sqrt(eps())
53+
end
54+
end

0 commit comments

Comments
 (0)