From a14c06bd734b2c51425b7cf5d6ae2f02d27bda91 Mon Sep 17 00:00:00 2001 From: dpo Date: Tue, 8 Oct 2024 02:45:21 +0000 Subject: [PATCH] :robot: Format .jl files --- src/lbfgs.jl | 6 +++--- src/utilities.jl | 25 ++++++++++++++----------- test/runtests.jl | 2 +- test/test_solve_shifted_system.jl | 6 +++--- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/lbfgs.jl b/src/lbfgs.jl index 067f76e0..1b3177dc 100644 --- a/src/lbfgs.jl +++ b/src/lbfgs.jl @@ -46,9 +46,9 @@ function LBFGSData( inverse ? Vector{T}(undef, 0) : [zeros(T, n) for _ = 1:mem], 1, Vector{T}(undef, n), - Array{T}(undef, (n, 2*mem)), - Vector{T}(undef, 2*mem), - Vector{T}(undef, n) + Array{T}(undef, (n, 2 * mem)), + Vector{T}(undef, 2 * mem), + Vector{T}(undef, n), ) end diff --git a/src/utilities.jl b/src/utilities.jl index 0f53ea64..70b0dedd 100644 --- a/src/utilities.jl +++ b/src/utilities.jl @@ -1,4 +1,5 @@ -export check_ctranspose, check_hermitian, check_positive_definite, normest, solve_shifted_system!, ldiv! +export check_ctranspose, + check_hermitian, check_positive_definite, normest, solve_shifted_system!, ldiv! import LinearAlgebra.ldiv! """ @@ -147,8 +148,7 @@ end check_positive_definite(M::AbstractMatrix; kwargs...) = check_positive_definite(LinearOperator(M); kwargs...) - - """ +""" solve_shifted_system!(x, B, b, σ) Solve linear system (B + σI) x = b, where B is a forward L-BFGS operator and σ ≥ 0. @@ -209,14 +209,13 @@ function solve_shifted_system!( B::LBFGSOperator{T, I, F1, F2, F3}, b::AbstractVector{T}, σ::T, - ) where {T, I, F1, F2, F3} - +) where {T, I, F1, F2, F3} if σ < 0 throw(ArgumentError("σ must be nonnegative")) end data = B.data insert = data.insert - + γ_inv = 1 / data.scaling_factor x_0 = 1 / (γ_inv + σ) @. x = x_0 * b @@ -234,20 +233,20 @@ function solve_shifted_system!( sign_t = 1 for t = 1:(i - 1) c0 = dot(view(data.shifted_p, :, t), data.shifted_u) - c1= sign_t .*data.shifted_v[t] + c1 = sign_t .* data.shifted_v[t] c2 = c1 * c0 view(data.shifted_p, :, i) .+= c2 .* view(data.shifted_p, :, t) sign_t = -sign_t end data.shifted_v[i] = 1 / (1 - sign_i * dot(data.shifted_u, view(data.shifted_p, :, i))) - x .+= sign_i *data.shifted_v[i] * (view(data.shifted_p, :, i)' * b) .* view(data.shifted_p, :, i) + x .+= + sign_i * data.shifted_v[i] * (view(data.shifted_p, :, i)' * b) .* view(data.shifted_p, :, i) sign_i = -sign_i end return x end - """ ldiv!(x, B, b) @@ -279,8 +278,12 @@ ldiv!(x, B, b) # The vector `x` now contains the solution """ -function ldiv!(x::AbstractVector{T}, B::LBFGSOperator{T, I, F1, F2, F3}, b::AbstractVector{T}) where {T, I, F1, F2, F3} +function ldiv!( + x::AbstractVector{T}, + B::LBFGSOperator{T, I, F1, F2, F3}, + b::AbstractVector{T}, +) where {T, I, F1, F2, F3} # Call solve_shifted_system! with σ = 0 solve_shifted_system!(x, B, b, T(0.0)) return x -end \ No newline at end of file +end diff --git a/test/runtests.jl b/test/runtests.jl index eea61206..53f05beb 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -15,4 +15,4 @@ include("test_deprecated.jl") include("test_normest.jl") include("test_diag.jl") include("test_chainrules.jl") -include("test_solve_shifted_system.jl") \ No newline at end of file +include("test_solve_shifted_system.jl") diff --git a/test/test_solve_shifted_system.jl b/test/test_solve_shifted_system.jl index caafea53..3d41e0ed 100644 --- a/test/test_solve_shifted_system.jl +++ b/test/test_solve_shifted_system.jl @@ -16,13 +16,13 @@ function setup_test_val(; M = 5, n = 100, scaling = false, σ = 0.1) x = randn(n) b = B * x + σ .* x # so we know the true answer is x - return B, H , b, σ, zeros(n), x + return B, H, b, σ, zeros(n), 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,_, b, σ, x_sol, x_true = setup_test_val(n = 100, M = 5) + B, _, b, σ, x_sol, x_true = setup_test_val(n = 100, M = 5) result = solve_shifted_system!(x_sol, B, b, σ) @@ -40,7 +40,7 @@ function test_solve_shifted_system() end @testset "solve_shifted_system! Negative σ test" begin # Setup Test Case 2: Negative σ - B,_, b, _, x_sol, _ = setup_test_val(n = 100, M = 5) + B, _, b, _, x_sol, _ = setup_test_val(n = 100, M = 5) σ = -0.1 # Expect an ArgumentError to be thrown