Skip to content

Commit 4a8def3

Browse files
docs: add docstrings to NonlinearSolveHomotopyContinuation.jl
1 parent d7972af commit 4a8def3

File tree

4 files changed

+113
-4
lines changed

4 files changed

+113
-4
lines changed

lib/NonlinearSolveHomotopyContinuation/Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
88
CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2"
99
ConcreteStructs = "2569d6c7-a4a2-43d3-a901-331e8e4be471"
1010
DifferentiationInterface = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
11+
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
1112
HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327"
1213
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1314
NonlinearSolveBase = "be0214bd-f91f-a760-ac4e-3421ce2b2da0"
@@ -16,11 +17,12 @@ SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5"
1617
TaylorSeries = "6aa5eb33-94cf-58f4-a9d0-e4b2c4fc25ea"
1718

1819
[compat]
19-
Aqua = "0.8"
2020
ADTypes = "1.11.0"
21+
Aqua = "0.8"
2122
CommonSolve = "0.2.4"
2223
ConcreteStructs = "0.2.3"
2324
DifferentiationInterface = "0.6.27"
25+
DocStringExtensions = "0.9.3"
2426
HomotopyContinuation = "2.12.0"
2527
LinearAlgebra = "1.11.0"
2628
NonlinearSolve = "4"

lib/NonlinearSolveHomotopyContinuation/src/NonlinearSolveHomotopyContinuation.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ using SymbolicIndexingInterface
77
using LinearAlgebra
88
using ADTypes
99
using TaylorSeries
10+
using DocStringExtensions
1011
import CommonSolve
1112
import HomotopyContinuation as HC
1213
import DifferentiationInterface as DI
@@ -15,6 +16,35 @@ using ConcreteStructs: @concrete
1516

1617
export HomotopyContinuationJL, HomotopyNonlinearFunction
1718

19+
"""
20+
HomotopyContinuationJL{AllRoots}(; autodiff = true, kwargs...)
21+
HomotopyContinuationJL(; kwargs...) = HomotopyContinuationJL{false}(; kwargs...)
22+
23+
This algorithm is an interface to `HomotopyContinuation.jl`. It is only valid for
24+
fully determined polynomial systems. The `AllRoots` type parameter can be `true` or
25+
`false` and controls whether the solver will find all roots of the polynomial
26+
or a single root close to the initial guess provided to the `NonlinearProblem`.
27+
The polynomial function must allow complex numbers to be provided as the state.
28+
29+
If `AllRoots` is `true`, the initial guess in the `NonlinearProblem` is ignored.
30+
The function must be traceable using HomotopyContinuation.jl's symbolic variables.
31+
Note that higher degree polynomials and systems with multiple unknowns can increase
32+
solve time significantly.
33+
34+
If `AllRoots` is `false`, a single path is traced during the homotopy. The traced path
35+
depends on the initial guess provided to the `NonlinearProblem` being solved. This method
36+
does not require that the polynomial function is traceable via HomotopyContinuation.jl's
37+
symbolic variables.
38+
39+
HomotopyContinuation.jl requires the jacobian of the system. In case a jacobian function
40+
is provided, it will be used. Otherwise, the `autodiff` keyword argument controls the
41+
autodiff method used to compute the jacobian. A value of `true` refers to
42+
`AutoForwardDiff` and `false` refers to `AutoFiniteDiff`. Alternate algorithms can be
43+
specified using ADTypes.jl.
44+
45+
HomotopyContinuation.jl requires the taylor series of the polynomial system for the single
46+
root method. This is automatically computed using TaylorSeries.jl.
47+
"""
1848
@concrete struct HomotopyContinuationJL{AllRoots} <: NonlinearSolveBase.AbstractNonlinearSolveAlgorithm
1949
autodiff
2050
kwargs

lib/NonlinearSolveHomotopyContinuation/src/interface_types.jl

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ struct Inplace <: HomotopySystemVariant end
44
struct OutOfPlace <: HomotopySystemVariant end
55
struct Scalar <: HomotopySystemVariant end
66

7+
"""
8+
$(TYPEDEF)
9+
10+
A simple struct that wraps a polynomial function which takes complex input and returns
11+
complex output in a form that supports automatic differentiation. If the wrapped
12+
function if ``f: \\mathbb{C}^n \\rightarrow \\mathbb{C}^n`` then it is assumed
13+
the input arrays are real-valued and have length ``2n``. They are `reinterpret`ed
14+
into complex arrays and passed into the function. This struct has an in-place signature
15+
regardless of the signature of ``f``.
16+
"""
717
@concrete struct ComplexJacobianWrapper{variant <: HomotopySystemVariant}
818
f
919
end
@@ -32,14 +42,49 @@ function (cjw::ComplexJacobianWrapper{Scalar})(u::AbstractVector{T}, x::Abstract
3242
return u
3343
end
3444

45+
"""
46+
$(TYPEDEF)
47+
48+
A struct which implements the `HomotopyContinuation.AbstractSystem` interface for
49+
polynomial systems specified using `NonlinearProblem`.
50+
51+
# Fields
52+
53+
$(FIELDS)
54+
"""
3555
@concrete struct HomotopySystemWrapper{variant <: HomotopySystemVariant} <: HC.AbstractSystem
56+
"""
57+
The wrapped polynomial function.
58+
"""
3659
f
60+
"""
61+
The jacobian function, if provided to the `NonlinearProblem` being solved. Otherwise,
62+
a `ComplexJacobianWrapper` wrapping `f` used for automatic differentiation.
63+
"""
3764
jac
65+
"""
66+
The parameter object.
67+
"""
3868
p
69+
"""
70+
The ADType for automatic differentiation.
71+
"""
3972
autodiff
73+
"""
74+
The result from `DifferentiationInterface.prepare_jacobian`.
75+
"""
4076
prep
77+
"""
78+
HomotopyContinuation.jl's symbolic variables for the system.
79+
"""
4180
vars
81+
"""
82+
The `TaylorSeries.Taylor1` objects used to compute the taylor series of `f`.
83+
"""
4284
taylorvars
85+
"""
86+
Preallocated intermediate buffers used for calculating the jacobian.
87+
"""
4388
jacobian_buffers
4489
end
4590

@@ -168,9 +213,38 @@ function HC.ModelKit.taylor!(u::AbstractVector, ::Val{N}, sys::HomotopySystemWra
168213
return u
169214
end
170215

216+
"""
217+
$(TYPEDEF)
218+
219+
A `HomotopyContinuation.AbstractHomotopy` which uses an inital guess ``x_0`` to construct
220+
the start system for the homotopy. The homotopy is
221+
222+
```math
223+
\\begin{aligned}
224+
H(x, t) &= t * (F(x) - F(x_0)) + (1 - t) * F(x)
225+
&= F(x) - t * F(x_0)
226+
\\end{aligned}
227+
```
228+
229+
Where ``F: \\mathbb{R}^n \\rightarrow \\mathbb{R}^n`` is the polynomial system and
230+
``x_0 \\in \\mathbb{R}^n`` is the guess provided to the `NonlinearProblem`.
231+
232+
# Fields
233+
234+
$(TYPEDFIELDS)
235+
"""
171236
@concrete struct GuessHomotopy <: HC.AbstractHomotopy
237+
"""
238+
The `HomotopyContinuation.AbstractSystem` to solve.
239+
"""
172240
sys <: HC.AbstractSystem
241+
"""
242+
The residual of the initial guess, i.e. ``F(x_0)``.
243+
"""
173244
fu0
245+
"""
246+
A preallocated `TaylorVector` for efficient `taylor!` implementation.
247+
"""
174248
taylorbuffer::HC.ModelKit.TaylorVector{5, ComplexF64}
175249
end
176250

@@ -180,8 +254,6 @@ end
180254

181255
Base.size(h::GuessHomotopy) = size(h.sys)
182256

183-
# H(x, t) = (1 - t) * F(x) + t * (F(x) - F(x0))
184-
# = F(x) - t * F(x0)
185257
function HC.ModelKit.evaluate!(u, h::GuessHomotopy, x, t, p = nothing)
186258
HC.ModelKit.evaluate!(u, h.sys, x, p)
187259
@inbounds for i in eachindex(u)

lib/NonlinearSolveHomotopyContinuation/src/solve.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"""
2+
$(TYPEDSIGNATURES)
3+
4+
Create and return the appropriate `HomotopySystemWrapper` to use for solving the given
5+
`prob` with `alg`.
6+
"""
17
function homotopy_continuation_preprocessing(prob::NonlinearProblem, alg::HomotopyContinuationJL)
28
# cast to a `HomotopyNonlinearFunction`
39
f = if prob.f isa HomotopyNonlinearFunction
@@ -36,7 +42,6 @@ function homotopy_continuation_preprocessing(prob::NonlinearProblem, alg::Homoto
3642
HC.variables(:x, axes(u0)...)
3743
end
3844

39-
# TODO: Is there an upper bound for the order?
4045
taylorvars = if isscalar
4146
Taylor1(zeros(ComplexF64, 5), 4)
4247
elseif iip

0 commit comments

Comments
 (0)