-
Notifications
You must be signed in to change notification settings - Fork 38
solve_shifted_system! method for LBFGS solving step in recursive way #338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
786bc0d
solve_shifted_system!
farhadrclass f95d210
Update src/utilities.jl
farhadrclass bac9f6f
Update src/utilities.jl
farhadrclass 62d35f6
Update src/utilities.jl
farhadrclass 186b6c5
Update src/utilities.jl
farhadrclass 0d4f952
Update src/utilities.jl
farhadrclass 4672bd3
Update src/utilities.jl
farhadrclass f3ea738
Update src/utilities.jl
farhadrclass 4bf8f55
Update src/utilities.jl
farhadrclass 025645e
Update src/utilities.jl
farhadrclass 94fb35f
changes after review
farhadrclass ded60c1
Update src/lbfgs.jl
farhadrclass e5b054a
Update src/lbfgs.jl
farhadrclass 6c40d0a
Update src/utilities.jl
farhadrclass 773f2fd
Update src/utilities.jl
farhadrclass 13162dc
Update src/utilities.jl
farhadrclass 2cf4246
updated according to the PR review
farhadrclass 5c1eeae
updated according to PR, renamed variable
farhadrclass 1323a98
added ldiv!
farhadrclass d1da6ef
Update src/utilities.jl
farhadrclass ad9567f
Update src/utilities.jl
farhadrclass d5c1cfd
Update src/utilities.jl
farhadrclass 759ab02
Update src/utilities.jl
farhadrclass 3094644
Update src/utilities.jl
farhadrclass 88479e5
Update src/utilities.jl
farhadrclass bc6ba12
Update src/utilities.jl
farhadrclass 5a173ef
Update src/utilities.jl
farhadrclass 4ad62e6
Update src/utilities.jl
farhadrclass 17ffb34
Update src/utilities.jl
farhadrclass 87bd7b6
Update src/utilities.jl
farhadrclass e039299
Update test/test_solve_shifted_system.jl
farhadrclass 89d28d6
Update test/test_solve_shifted_system.jl
farhadrclass aa4c8a3
added the example
farhadrclass 90084b9
Update utilities.jl
farhadrclass File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
using Test | ||
using LinearOperators | ||
using LinearAlgebra | ||
|
||
function setup_test_val(; M = 5, n = 100, scaling = false) | ||
σ = 0.1 | ||
B = LBFGSOperator(n, mem = 5, scaling = scaling) | ||
s = ones(n) | ||
y = ones(n) | ||
|
||
S = randn(n, M) | ||
Y = randn(n, M) | ||
|
||
# make sure it is positive | ||
for i = 1:M | ||
if dot(S[:, i], Y[:, i]) < 0 | ||
S[:, i] = -S[:, i] | ||
end | ||
end | ||
for i = 1:M | ||
s = S[:, i] | ||
y = Y[:, i] | ||
if dot(s, y) > 1.0e-20 | ||
push!(B, s, y) | ||
end | ||
end | ||
farhadrclass marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
γ_inv = 1 / B.data.scaling_factor | ||
|
||
x = randn(n) | ||
z = B * x + σ .* x # so we know the true answer is x | ||
|
||
data = B.data | ||
# Preallocate vectors for efficiency | ||
p = zeros(size(data.a[1], 1), 2 * (data.mem)) | ||
v = zeros(2 * (data.mem)) | ||
u = zeros(size(data.a[1], 1)) | ||
|
||
return B, z, σ, γ_inv, zeros(n), p, v, u, x | ||
end | ||
|
||
function test_solve_shifted_system() | ||
@testset "solve_shifted_system! Default setup test" begin | ||
# Setup Test Case 1: Default setup from setup_test_val | ||
B, z, σ, γ_inv, inv_Cz, p, v, u, x = setup_test_val(n = 100, M = 5) | ||
|
||
result = solve_shifted_system!(B, z, σ, γ_inv, inv_Cz, p, v, u) | ||
|
||
# Test 1: Check if result is a vector of the same size as z | ||
@test length(result) == length(z) | ||
|
||
# Test 2: Verify that inv_Cz (result) is modified in place | ||
@test result === inv_Cz | ||
|
||
# Test 3: Check if the function produces finite values | ||
@test all(isfinite, result) | ||
|
||
# Test 4: Check if inv_Cz is close to the known solution x | ||
# x = B \ (z ./ (1 + σ)) # Known true solution | ||
@test isapprox(inv_Cz, x, atol = 1e-6, rtol = 1e-6) | ||
end | ||
@testset "solve_shifted_system! Larger dimensional system tests" begin | ||
farhadrclass marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
# Test Case 2: Larger dimensional system | ||
dim = 10 | ||
mem_size = 5 | ||
B, z, σ, γ_inv, inv_Cz, p, v, u, x = setup_test_val(n = dim, M = mem_size) | ||
|
||
result = solve_shifted_system!(B, z, σ, γ_inv, inv_Cz, p, v, u) | ||
|
||
# Test 5: Check if result is a vector of the same size as z (larger case) | ||
@test length(result) == length(z) | ||
|
||
# Test 6: Verify that inv_Cz is modified in place (larger case) | ||
@test result === inv_Cz | ||
|
||
# Test 7: Check if the function produces finite values (larger case) | ||
@test all(isfinite, result) | ||
|
||
# Test 8: Check if inv_Cz is close to the known solution x (larger case) | ||
# x = B \ (z ./ (1 + σ)) | ||
@test isapprox(inv_Cz, x, atol = 1e-6, rtol = 1e-6) | ||
end | ||
|
||
@testset "solve_shifted_system! Minimal memory size test" begin | ||
# Test Case 3: Minimal memory size case (memory size = 1) | ||
dim = 4 | ||
mem_size = 1 | ||
B, z, σ, γ_inv, inv_Cz, p, v, u, x = setup_test_val(n = dim, M = mem_size) | ||
|
||
result = solve_shifted_system!(B, z, σ, γ_inv, inv_Cz, p, v, u) | ||
|
||
farhadrclass marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
# Test 9: Check if result is a vector of the same size as z (minimal memory) | ||
@test length(result) == length(z) | ||
|
||
# Test 10: Verify that inv_Cz is modified in place (minimal case) | ||
@test result === inv_Cz | ||
|
||
# Test 11: Check if the function produces finite values (minimal case) | ||
@test all(isfinite, result) | ||
|
||
# Test 12: Check if inv_Cz is close to the known solution x (minimal memory) | ||
# x = B \ (z ./ (1 + σ)) | ||
@test isapprox(inv_Cz, x, atol = 1e-6, rtol = 1e-6) | ||
end | ||
|
||
@testset "solve_shifted_system! Extra large memory size test" begin | ||
|
||
# Test Case 4: Even larger system with more memory (case 4) | ||
dim = 50 | ||
mem_size = 10 | ||
B, z, σ, γ_inv, inv_Cz, p, v, u, x = setup_test_val(n = dim, M = mem_size) | ||
|
||
# Call the function | ||
result = solve_shifted_system!(B, z, σ, γ_inv, inv_Cz, p, v, u) | ||
|
||
# Test 13: Check if result is a vector of the same size as z (case 4) | ||
@test length(result) == length(z) | ||
|
||
# Test 14: Verify that inv_Cz is modified in place (case 4) | ||
@test result === inv_Cz | ||
|
||
# Test 15: Check if the function produces finite values (case 4) | ||
@test all(isfinite, result) | ||
|
||
# Test 16: Check if inv_Cz is close to the known solution x (case 4) | ||
# x = B \ (z ./ (1 + σ)) | ||
@test isapprox(inv_Cz, x, atol = 1e-6, rtol = 1e-6) | ||
end | ||
end | ||
|
||
test_solve_shifted_system() | ||
dpo marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.