Skip to content

Commit ecf01b3

Browse files
feat: better detect and report non-polynomial systems
1 parent f358fed commit ecf01b3

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

ext/MTKHomotopyContinuationExt.jl

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module MTKHomotopyContinuationExt
22

33
using ModelingToolkit
44
using ModelingToolkit.SciMLBase
5-
using ModelingToolkit.Symbolics: unwrap
5+
using ModelingToolkit.Symbolics: unwrap, symtype
66
using ModelingToolkit.SymbolicIndexingInterface
77
using ModelingToolkit.DocStringExtensions
88
using HomotopyContinuation
@@ -32,8 +32,25 @@ function is_polynomial(x, wrt)
3232
end
3333
if operation(x) == (^)
3434
b, p = arguments(x)
35-
return is_polynomial(b, wrt) && !contains_variable(p, wrt)
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
42+
end
43+
exponent_has_unknowns = contains_variable(p, wrt)
44+
if exponent_has_unknowns
45+
@warn "In $x: Exponent $p cannot contain unknowns of the system."
46+
end
47+
base_polynomial = is_polynomial(b, wrt)
48+
if !base_polynomial
49+
@warn "In $x: Base is not a polynomial"
50+
end
51+
return base_polynomial && !exponent_has_unknowns && is_pow_integer
3652
end
53+
@warn "In $x: Unrecognized operation $(operation(x)). Allowed polynomial operations are `*, +, -, ^`"
3754
return false
3855
end
3956

@@ -124,7 +141,7 @@ function MTK.HomotopyContinuationProblem(
124141

125142
for eq in eqs
126143
if !is_polynomial(eq.lhs, dvs) || !is_polynomial(eq.rhs, dvs)
127-
error("Equation $eq is not a polynomial in the unknowns")
144+
error("Equation $eq is not a polynomial in the unknowns. See warnings for further details.")
128145
end
129146
end
130147

test/extensions/homotopy_continuation.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,29 @@ end
6060
sol = @test_nowarn solve(prob; threading = false)
6161
@test sol.retcode == ReturnCode.ConvergenceFailure
6262
end
63+
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+
73+
@testset "Polynomial check and warnings" begin
74+
@variables x = 1.0
75+
@parameters n = 4
76+
@mtkbuild sys = NonlinearSystem([x^n + x^2 - 1 ~ 0])
77+
@test_warn ["Exponent", "not an integer", "@parameters"] @test_throws "not a polynomial" HomotopyContinuationProblem(
78+
sys, [])
79+
@mtkbuild sys = NonlinearSystem([x^1.5 + x^2 - 1 ~ 0])
80+
@test_warn ["Exponent", "not an integer"] @test_throws "not a polynomial" HomotopyContinuationProblem(
81+
sys, [])
82+
@mtkbuild sys = NonlinearSystem([x^x - x ~ 0])
83+
@test_warn ["Exponent", "unknowns"] @test_throws "not a polynomial" HomotopyContinuationProblem(
84+
sys, [])
85+
@mtkbuild sys = NonlinearSystem([((x^2) / (x + 3))^2 + x ~ 0])
86+
@test_warn ["Base", "not a polynomial", "Unrecognized operation", "/"] @test_throws "not a polynomial" HomotopyContinuationProblem(
87+
sys, [])
88+
end

0 commit comments

Comments
 (0)