Skip to content

Commit

Permalink
Improve docs and format
Browse files Browse the repository at this point in the history
  • Loading branch information
tansongchen committed Apr 16, 2024
1 parent c5bf69f commit e398002
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/NonlinearSolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ include("default.jl")
end

# Core Algorithms
export NewtonRaphson, PseudoTransient, Klement, Broyden, LimitedMemoryBroyden, DFSane, Halley
export NewtonRaphson, PseudoTransient, Klement, Broyden, LimitedMemoryBroyden, DFSane,
Halley
export GaussNewton, LevenbergMarquardt, TrustRegion
export NonlinearSolvePolyAlgorithm, RobustMultiNewton, FastShortcutNonlinearPolyalg,
FastShortcutNLLSPolyalg
Expand All @@ -159,7 +160,8 @@ export LeastSquaresOptimJL, FastLevenbergMarquardtJL, CMINPACK, NLsolveJL, NLSol
export GeneralizedFirstOrderAlgorithm, ApproximateJacobianSolveAlgorithm, GeneralizedDFSane

# Descent Algorithms
export NewtonDescent, SteepestDescent, Dogleg, DampedNewtonDescent, GeodesicAcceleration, HalleyDescent
export NewtonDescent, SteepestDescent, Dogleg, DampedNewtonDescent, GeodesicAcceleration,
HalleyDescent

# Globalization
## Line Search Algorithms
Expand Down
5 changes: 4 additions & 1 deletion src/algorithms/halley.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
Halley(; concrete_jac = nothing, linsolve = nothing, linesearch = NoLineSearch(),
precs = DEFAULT_PRECS, autodiff = nothing)
An experimental Halley's method implementation.
An experimental Halley's method implementation. Improves the convergence rate of Newton's method by using second-order derivative information to correct the descent direction.
Currently depends on TaylorDiff.jl to handle the correction terms,
might have more general implementation in the future.
"""
function Halley(; concrete_jac = nothing, linsolve = nothing,

Check warning on line 10 in src/algorithms/halley.jl

View check run for this annotation

Codecov / codecov/patch

src/algorithms/halley.jl#L10

Added line #L10 was not covered by tests
linesearch = NoLineSearch(), precs = DEFAULT_PRECS, autodiff = nothing)
Expand Down
20 changes: 9 additions & 11 deletions src/descent/halley.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""
HalleyDescent(; linsolve = nothing, precs = DEFAULT_PRECS)
Compute the descent direction as ``J δu = -fu``. For non-square Jacobian problems, this is
commonly referred to as the Gauss-Newton Descent.
Improve the NewtonDescent with higher-order terms. First compute the descent direction as ``J a = -fu``.
Then compute the hessian-vector-vector product and solve for the second-order correction term as ``J b = H a a``.
Finally, compute the descent direction as ``δu = a * a / (b / 2 - a)``.
See also [`Dogleg`](@ref), [`SteepestDescent`](@ref), [`DampedNewtonDescent`](@ref).
See also [`NewtonDescent`](@ref).
"""
@kwdef @concrete struct HalleyDescent <: AbstractDescentAlgorithm
linsolve = nothing
Expand All @@ -22,8 +23,7 @@ end

supports_line_search(::HalleyDescent) = true

Check warning on line 24 in src/descent/halley.jl

View check run for this annotation

Codecov / codecov/patch

src/descent/halley.jl#L24

Added line #L24 was not covered by tests

@concrete mutable struct HalleyDescentCache{pre_inverted} <:
AbstractDescentCache
@concrete mutable struct HalleyDescentCache{pre_inverted} <: AbstractDescentCache
f
p
δu
Expand All @@ -50,8 +50,7 @@ function __internal_init(
return HalleyDescentCache{false}(prob.f, prob.p, δu, δus, b, lincache, timer)

Check warning on line 50 in src/descent/halley.jl

View check run for this annotation

Codecov / codecov/patch

src/descent/halley.jl#L50

Added line #L50 was not covered by tests
end

function __internal_solve!(
cache::HalleyDescentCache{INV}, J, fu, u, idx::Val = Val(1);
function __internal_solve!(cache::HalleyDescentCache{INV}, J, fu, u, idx::Val = Val(1);

Check warning on line 53 in src/descent/halley.jl

View check run for this annotation

Codecov / codecov/patch

src/descent/halley.jl#L53

Added line #L53 was not covered by tests
skip_solve::Bool = false, new_jacobian::Bool = true, kwargs...) where {INV}
δu = get_du(cache, idx)
skip_solve && return δu, true, (;)
Expand All @@ -68,15 +67,14 @@ function __internal_solve!(
end
b = cache.b

Check warning on line 68 in src/descent/halley.jl

View check run for this annotation

Codecov / codecov/patch

src/descent/halley.jl#L68

Added line #L68 was not covered by tests
# compute the hessian-vector-vector product
hvvp = derivative(x -> cache.f(x, cache.p), u, δu, 2)
hvvp = derivative(Base.Fix2(cache.f, cache.p), u, δu, 2)

Check warning on line 70 in src/descent/halley.jl

View check run for this annotation

Codecov / codecov/patch

src/descent/halley.jl#L70

Added line #L70 was not covered by tests
# second linear solve, reuse factorization if possible
if INV
@bb b = J × vec(hvvp)

Check warning on line 73 in src/descent/halley.jl

View check run for this annotation

Codecov / codecov/patch

src/descent/halley.jl#L72-L73

Added lines #L72 - L73 were not covered by tests
else
@static_timeit cache.timer "linear solve 2" begin
b = cache.lincache(;
A = J, b = _vec(hvvp), kwargs..., linu = _vec(b), du = _vec(b),
reuse_A_if_factorization = true)
b = cache.lincache(; A = J, b = _vec(hvvp), kwargs..., linu = _vec(b),

Check warning on line 76 in src/descent/halley.jl

View check run for this annotation

Codecov / codecov/patch

src/descent/halley.jl#L75-L76

Added lines #L75 - L76 were not covered by tests
du = _vec(b), reuse_A_if_factorization = true)
b = _restructure(cache.b, b)

Check warning on line 78 in src/descent/halley.jl

View check run for this annotation

Codecov / codecov/patch

src/descent/halley.jl#L78

Added line #L78 was not covered by tests
end
end
Expand Down

0 comments on commit e398002

Please sign in to comment.