Skip to content
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

fix LSR1Operator when s and y are collinear #235

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/lsr1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ function push!(op::LSR1Operator, s::AbstractVector, y::AbstractVector)
ϵ = eps(eltype(op))
well_defined = abs(dot(ymBs, s)) ≥ ϵ + ϵ * norm(ymBs) * sNorm

# check if y and s are collinear: make a componentwise division and check if the resulting components get approximately the same factor.
and(bool1, bool2) = bool1 && bool2
s_y = (s ./ y)
collinear = (length(s) == 1) || mapreduce(i-> i ≈ s_y[1], and, s_y) # ≈ to avoid numerical issues
collinear && (tmp_scaling = data.scaling) # save data.scaling
collinear && (data.scaling = false) # if the scaling is applied then all the components of Matrix(op_updated) are NaN

sufficient_curvature = true
scaling_condition = true
if data.scaling
Expand Down Expand Up @@ -179,6 +186,8 @@ function push!(op::LSR1Operator, s::AbstractVector, y::AbstractVector)
end
end

collinear && (data.scaling = tmp_scaling) # set data.scaling to its original value

return op
end

Expand Down
10 changes: 10 additions & 0 deletions test/test_lsr1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ function test_lsr1()
resid = vals[end]
@test norm(resid) ≤ sqrt(eps(eltype(B)))
end

@testset "LSR1 (s,y) collinear" begin
n = 10
lsr1 = LSR1Operator(n)
s = rand(n)
alpha = rand(1) * 1000
y = s .* alpha
push!(lsr1, s, y)
Matrix(lsr1) != I
end
end

test_lsr1()