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

compute_As_slope_qs! Function #204

Merged
merged 1 commit into from
Sep 27, 2023
Merged
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
19 changes: 18 additions & 1 deletion src/auxiliary/bounds.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Auxiliary function related to bound-constrainted problems

export active, active!, breakpoints, compute_Hs_slope_qs!, project!, project_step!
export active, active!, breakpoints, compute_Hs_slope_qs!, compute_As_slope_qs!, project!, project_step!

"""
active(x, ℓ, u; rtol = 1e-8, atol = 1e-8)
Expand Down Expand Up @@ -110,6 +110,23 @@ function compute_Hs_slope_qs!(
return slope, qs
end

"""
slope, qs = compute_As_slope_qs!(As, A, s, Fx)

Compute `slope = dot(As, Fx)` and `qs = dot(As, As) / 2 + slope`. Use `As` to store `A * s`.
"""
function compute_As_slope_qs!(
As::AbstractVector{T},
A::Union{AbstractMatrix, AbstractLinearOperator},
s::AbstractVector{T},
Fx::AbstractVector{T},
) where {T <: Real}
mul!(As, A, s)
slope = dot(As, Fx)
qs = dot(As, As) / 2 + slope
return slope, qs
end

"""
project!(y, x, ℓ, u)

Expand Down
10 changes: 10 additions & 0 deletions test/test_auxiliary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ function test_auxiliary()
a = @allocated compute_Hs_slope_qs!(Hx, H, x, g)
@test a == 0

x1 = [10.0; 11.0; 12.0; 13.0; 14.0; 15.0; 16.0]
A = diagm(0 => 2 * ones(7), -1 => -ones(6), 1 => -ones(6))
As = zeros(7)
Fx = rand(7)
slope, qx = compute_As_slope_qs!(As, A, x1, Fx)
@test slope == dot(As, Fx)
@test qx == dot(As, As) / 2 + slope
b = @allocated compute_As_slope_qs!(As, A, x1, Fx)
@test b == 0

z = [ℓ[1:2] - rand(2); x[3]; u[4:5] + rand(2)]
y = rand(5)
project!(y, z, ℓ, u)
Expand Down
Loading