Skip to content

Split jth_hess_residual into coord and structure functions #502

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 2 commits into from
Jul 27, 2025
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
2 changes: 1 addition & 1 deletion docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ and its derivatives. Namely,
|---------------------|---|
| ``F(x)`` | [`residual`](@ref), [`residual!`](@ref) |
| ``J_F(x)`` | [`jac_residual`](@ref), [`jac_coord_residual`](@ref), [`jac_coord_residual!`](@ref), [`jac_structure_residual`](@ref), [`jac_structure_residual!`](@ref), [`jprod_residual`](@ref), [`jprod_residual!`](@ref), [`jtprod_residual`](@ref), [`jtprod_residual!`](@ref), [`jac_op_residual`](@ref), [`jac_op_residual!`](@ref) |
| ``\nabla^2 F_i(x)`` | [`hess_residual`](@ref), [`hess_coord_residual`](@ref), [`hess_coord_residual!`](@ref), [`hess_structure_residual`](@ref), [`hess_structure_residual!`](@ref), [`jth_hess_residual`](@ref), [`hprod_residual`](@ref), [`hprod_residual!`](@ref), [`hess_op_residual`](@ref), [`hess_op_residual!`](@ref) |
| ``\nabla^2 F_i(x)`` | [`hess_residual`](@ref), [`hess_coord_residual`](@ref), [`hess_coord_residual!`](@ref), [`hess_structure_residual`](@ref), [`hess_structure_residual!`](@ref), [`jth_hess_residual`](@ref), [`jth_hess_residual_coord`](@ref), [`jth_hess_residual_coord!`](@ref), [`hprod_residual`](@ref), [`hprod_residual!`](@ref), [`hess_op_residual`](@ref), [`hess_op_residual!`](@ref) |
40 changes: 36 additions & 4 deletions src/nls/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export residual, residual!, jac_residual, jac_structure_residual, jac_structure_
export jac_coord_residual!, jac_coord_residual, jprod_residual, jprod_residual!
export jtprod_residual, jtprod_residual!, jac_op_residual, jac_op_residual!
export hess_residual, hess_structure_residual, hess_structure_residual!
export hess_coord_residual!, hess_coord_residual, jth_hess_residual
export hess_coord_residual!, hess_coord_residual, jth_hess_residual, jth_hess_residual_coord, jth_hess_residual_coord!
export hprod_residual, hprod_residual!, hess_op_residual, hess_op_residual!

"""
Expand Down Expand Up @@ -325,10 +325,39 @@ Computes the Hessian of the j-th residual at x.
"""
function jth_hess_residual(nls::AbstractNLSModel{T, S}, x::AbstractVector, j::Int) where {T, S}
@lencheck nls.meta.nvar x
@rangecheck 1 nls.nls_meta.nequ j
rows, cols = hess_structure_residual(nls)
vals = jth_hess_residual_coord(nls, x, j)
return Symmetric(sparse(rows, cols, vals, nls.meta.nvar, nls.meta.nvar), :L)
end

"""
vals = jth_hess_residual_coord(nls, x, j)

Evaluate the Hessian of j-th residual at `x` in sparse coordinate format.
Only the lower triangle is returned.
"""
function jth_hess_residual_coord(nls::AbstractNLSModel{T, S}, x::AbstractVector, j::Int) where {T, S}
@lencheck nls.meta.nvar x
@rangecheck 1 nls.nls_meta.nequ j
vals = S(undef, nls.nls_meta.nnzh)
return jth_hess_residual_coord!(nls, x, j, vals)
end

"""
vals = jth_hess_residual_coord!(nls, x, j, vals)

Evaluate the Hessian of j-th residual at `x` in sparse coordinate format, with `vals` of
length `nls.nls_meta.nnzh`, in place. Only the lower triangle is returned.
"""
function jth_hess_residual_coord!(nls::AbstractNLSModel{T, S}, x::AbstractVector, j::Int, vals::AbstractVector) where {T, S}
@lencheck nls.meta.nvar x
@rangecheck 1 nls.nls_meta.nequ j
@lencheck nls.nls_meta.nnzh vals
increment!(nls, :neval_jhess_residual)
decrement!(nls, :neval_hess_residual)
v = [i == j ? one(T) : zero(T) for i = 1:(nls.nls_meta.nequ)]
return hess_residual(nls, x, v)
return hess_coord_residual!(nls, x, v, vals)
end

"""
Expand All @@ -342,8 +371,9 @@ function hprod_residual(
i::Int,
v::AbstractVector,
) where {T, S}
@lencheck nls.meta.nvar x
Hv = S(undef, nls_meta(nls).nvar)
@lencheck nls.meta.nvar x v
@rangecheck 1 nls.nls_meta.nequ i
Hv = S(undef, nls.meta.nvar)
hprod_residual!(nls, x, i, v, Hv)
end

Expand All @@ -361,6 +391,7 @@ Computes the Hessian of the i-th residual at x, in linear operator form.
"""
function hess_op_residual(nls::AbstractNLSModel{T, S}, x::AbstractVector, i::Int) where {T, S}
@lencheck nls.meta.nvar x
@rangecheck 1 nls.nls_meta.nequ i
Hiv = S(undef, nls.meta.nvar)
return hess_op_residual!(nls, x, i, Hiv)
end
Expand All @@ -377,6 +408,7 @@ function hess_op_residual!(
Hiv::AbstractVector,
) where {T, S}
@lencheck nls.meta.nvar x Hiv
@rangecheck 1 nls.nls_meta.nequ i
prod! = @closure (res, v, α, β) -> begin
hprod_residual!(nls, x, i, v, Hiv)
if β == 0
Expand Down
Loading