Skip to content

Commit de628fa

Browse files
feat: cache start system and solver in HomotopyContinuation interface
1 parent 2202f8c commit de628fa

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

ext/MTKHomotopyContinuationExt.jl

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ function is_polynomial(x, wrt)
3232
end
3333
if operation(x) == (^)
3434
b, p = arguments(x)
35-
is_pow_integer = symtype(p) <: Integer
36-
if !is_pow_integer
37-
if symbolic_type(p) == NotSymbolic()
38-
@warn "In $x: Exponent $p is not an integer"
39-
else
40-
@warn "In $x: Exponent $p is not an integer. Use `@parameters p::Integer` to declare integer parameters."
41-
end
35+
if symbolic_type(p) != NotSymbolic()
36+
@warn "In $x: Exponent $p cannot be symbolic"
37+
is_pow_integer = false
38+
elseif !(p isa Integer)
39+
@warn "In $x: Exponent $p is not an integer"
40+
is_pow_integer = false
41+
else
42+
is_pow_integer = true
4243
end
44+
4345
exponent_has_unknowns = contains_variable(p, wrt)
4446
if exponent_has_unknowns
4547
@warn "In $x: Exponent $p cannot contain unknowns of the system."
@@ -179,6 +181,8 @@ Create a `HomotopyContinuationProblem` from a `NonlinearSystem` with polynomial
179181
The problem will be solved by HomotopyContinuation.jl. The resultant `NonlinearSolution`
180182
will contain the polynomial root closest to the point specified by `u0map` (if real roots
181183
exist for the system).
184+
185+
Keyword arguments are forwarded to `HomotopyContinuation.solver_startsystems`.
182186
"""
183187
function MTK.HomotopyContinuationProblem(
184188
sys::NonlinearSystem, u0map, parammap = nothing; eval_expression = false,
@@ -223,29 +227,33 @@ function MTK.HomotopyContinuationProblem(
223227

224228
obsfn = MTK.ObservedFunctionCache(sys; eval_expression, eval_module)
225229

226-
return MTK.HomotopyContinuationProblem(u0, mtkhsys, denominator, sys, obsfn)
230+
solver_and_starts = HomotopyContinuation.solver_startsolutions(mtkhsys; kwargs...)
231+
return MTK.HomotopyContinuationProblem(
232+
u0, mtkhsys, denominator, sys, obsfn, solver_and_starts)
227233
end
228234

229235
"""
230236
$(TYPEDSIGNATURES)
231237
232238
Solve a `HomotopyContinuationProblem`. Ignores the algorithm passed to it, and always
233-
uses `HomotopyContinuation.jl`. All keyword arguments except the ones listed below are
234-
forwarded to `HomotopyContinuation.solve`. The original solution as returned by
239+
uses `HomotopyContinuation.jl`. The original solution as returned by
235240
`HomotopyContinuation.jl` will be available in the `.original` field of the returned
236241
`NonlinearSolution`.
237242
238-
All keyword arguments have their default values in HomotopyContinuation.jl, except
239-
`show_progress` which defaults to `false`.
243+
All keyword arguments except the ones listed below are forwarded to
244+
`HomotopyContinuation.solve`. Note that the solver and start solutions are precomputed,
245+
and only keyword arguments related to the solve process are valid. All keyword
246+
arguments have their default values in HomotopyContinuation.jl, except `show_progress`
247+
which defaults to `false`.
240248
241249
Extra keyword arguments:
242250
- `denominator_abstol`: In case `prob` is solving a rational function, roots which cause
243251
the denominator to be below `denominator_abstol` will be discarded.
244252
"""
245253
function CommonSolve.solve(prob::MTK.HomotopyContinuationProblem,
246254
alg = nothing; show_progress = false, denominator_abstol = 1e-7, kwargs...)
247-
sol = HomotopyContinuation.solve(
248-
prob.homotopy_continuation_system; show_progress, kwargs...)
255+
solver, starts = prob.solver_and_starts
256+
sol = HomotopyContinuation.solve(solver, starts; show_progress, kwargs...)
249257
realsols = HomotopyContinuation.results(sol; only_real = true)
250258
if isempty(realsols)
251259
u = state_values(prob)

src/systems/nonlinear/nonlinearsystem.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ A type of Nonlinear problem which specializes on polynomial systems and uses
690690
HomotopyContinuation.jl to solve the system. Requires importing HomotopyContinuation.jl to
691691
create and solve.
692692
"""
693-
struct HomotopyContinuationProblem{uType, H, D, O} <:
693+
struct HomotopyContinuationProblem{uType, H, D, O, SS} <:
694694
SciMLBase.AbstractNonlinearProblem{uType, true}
695695
"""
696696
The initial values of states in the system. If there are multiple real roots of
@@ -716,6 +716,11 @@ struct HomotopyContinuationProblem{uType, H, D, O} <:
716716
A function which generates and returns observed expressions for the given system.
717717
"""
718718
obsfn::O
719+
"""
720+
The HomotopyContinuation.jl solver and start system, obtained through
721+
`HomotopyContinuation.solver_startsystems`.
722+
"""
723+
solver_and_starts::SS
719724
end
720725

721726
function HomotopyContinuationProblem(::AbstractSystem, _u0, _p; kwargs...)

test/extensions/homotopy_continuation.jl

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,11 @@ end
6161
@test sol.retcode == ReturnCode.ConvergenceFailure
6262
end
6363

64-
@testset "Parametric exponent" begin
65-
@variables x = 1.0
66-
@parameters n::Integer = 4
67-
@mtkbuild sys = NonlinearSystem([x^n + x^2 - 1 ~ 0])
68-
prob = HomotopyContinuationProblem(sys, [])
69-
sol = solve(prob; threading = false)
70-
@test SciMLBase.successful_retcode(sol)
71-
end
72-
7364
@testset "Polynomial check and warnings" begin
7465
@variables x = 1.0
75-
@parameters n = 4
66+
@parameters n::Integer = 4
7667
@mtkbuild sys = NonlinearSystem([x^n + x^2 - 1 ~ 0])
77-
@test_warn ["Exponent", "not an integer", "@parameters"] @test_throws "not a polynomial" HomotopyContinuationProblem(
68+
@test_warn ["Exponent", "cannot be symbolic"] @test_throws "not a polynomial" HomotopyContinuationProblem(
7869
sys, [])
7970
@mtkbuild sys = NonlinearSystem([x^1.5 + x^2 - 1 ~ 0])
8071
@test_warn ["Exponent", "not an integer"] @test_throws "not a polynomial" HomotopyContinuationProblem(

0 commit comments

Comments
 (0)