Skip to content

Commit 1697f78

Browse files
fix: handle some type-instability in dummy derivative hooks
1 parent 7b78bab commit 1697f78

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/structural_transformation/symbolics_tearing.jl

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,30 @@ function tearing(sys::AbstractSystem, state = TearingState(sys); mm = nothing,
2020
invalidate_cache!(reassemble_alg(state, tearing_result, mm; fully_determined))
2121
end
2222

23+
function safe_isinteger(@nospecialize(x::Number))
24+
if x isa Int64
25+
return true
26+
elseif x isa Int32
27+
return true
28+
elseif x isa BigInt
29+
return typemin(Int) <= x <= typemax(Int)
30+
elseif x isa Float64
31+
return isinteger(x) && typemin(Int) <= x <= typemax(Int)
32+
elseif x isa Float32
33+
return isinteger(x) && typemin(Int) <= x <= typemax(Int)
34+
elseif x isa BigFloat
35+
return isinteger(x) && typemin(Int) <= x <= typemax(Int)
36+
elseif x isa Rational{Int64}
37+
return isinteger(x) && typemin(Int) <= x <= typemax(Int)
38+
elseif x isa Rational{Int32}
39+
return isinteger(x) && typemin(Int) <= x <= typemax(Int)
40+
elseif x isa Rational{BigInt}
41+
return isinteger(x) && typemin(Int) <= x <= typemax(Int)
42+
else
43+
return isinteger(x)::Bool && (typemin(Int) <= x)::Bool && (x <= typemax(Int))::Bool
44+
end
45+
end
46+
2347
"""
2448
dummy_derivative(sys)
2549
@@ -38,10 +62,8 @@ function dummy_derivative(sys, state = TearingState(sys);
3862
el = _J[i]
3963
Moshi.Match.@match el begin
4064
BSImpl.Const(; val) && if val isa Number end => begin
41-
isinteger(val)::Bool || return nothing
42-
val = Int(val)
43-
typemin(Int) <= val <= typemax(Int) || return nothing
44-
J[i] = val
65+
safe_isinteger(val)|| return nothing
66+
J[i] = convert(Int, val)::Int
4567
end
4668
_ => return nothing
4769
end

0 commit comments

Comments
 (0)