Skip to content

Commit db7386b

Browse files
fix: use full_equations when generating HomotopyContinuationProblem
1 parent de628fa commit db7386b

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

ext/MTKHomotopyContinuationExt.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,12 @@ function MTK.HomotopyContinuationProblem(
192192
end
193193

194194
dvs = unknowns(sys)
195-
eqs = equations(sys)
195+
# we need to consider `full_equations` because observed also should be
196+
# polynomials (if used in equations) and we don't know if observed is used
197+
# in denominator.
198+
# This is not the most efficient, and would be improved significantly with
199+
# CSE/hashconsing.
200+
eqs = full_equations(sys)
196201

197202
denoms = []
198203
eqs2 = map(eqs) do eq
@@ -216,6 +221,9 @@ function MTK.HomotopyContinuationProblem(
216221
end
217222

218223
sys2 = MTK.@set sys.eqs = eqs2
224+
# remove observed equations to avoid adding them in codegen
225+
MTK.@set! sys2.observed = Equation[]
226+
MTK.@set! sys2.substitutions = nothing
219227

220228
nlfn, u0, p = MTK.process_SciMLProblem(NonlinearFunction{true}, sys2, u0map, parammap;
221229
jac = true, eval_expression, eval_module)

test/extensions/homotopy_continuation.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ end
7676
@mtkbuild sys = NonlinearSystem([((x^2) / sin(x))^2 + x ~ 0])
7777
@test_warn ["Unrecognized", "sin"] @test_throws "not a polynomial" HomotopyContinuationProblem(
7878
sys, [])
79+
80+
@variables y = 2.0
81+
@mtkbuild sys = NonlinearSystem([x^2 + y^2 + 2 ~ 0, y ~ sin(x)])
82+
@test_warn ["Unrecognized", "sin"] @test_throws "not a polynomial" HomotopyContinuationProblem(
83+
sys, [])
7984
end
8085

8186
@testset "Rational functions" begin
@@ -122,4 +127,21 @@ end
122127
end
123128
end
124129
@test prob.denominator([2.0, 4.0], p)[1] <= 1e-8
130+
131+
@testset "Rational function in observed" begin
132+
@variables x=1 y=1
133+
@mtkbuild sys = NonlinearSystem([x^2 + y^2 - 2x - 2 ~ 0, y ~ (x - 1) / (x - 2)])
134+
prob = HomotopyContinuationProblem(sys, [])
135+
@test any(prob.denominator([2.0], parameter_values(prob)) .≈ 0.0)
136+
@test_nowarn solve(prob; threading = false)
137+
end
138+
end
139+
140+
@test "Non-polynomial observed not used in equations" begin
141+
@variables x=1 y
142+
@mtkbuild sys = NonlinearSystem([x^2 - 2 ~ 0, y ~ sin(x)])
143+
prob = HomotopyContinuationProblem(sys, [])
144+
sol = @test_nowarn solve(prob; threading = false)
145+
@test sol[x] 2.0
146+
@test sol[y] sin(2.0)
125147
end

0 commit comments

Comments
 (0)