Skip to content

Commit c2e78d9

Browse files
adds binary and integer options to variable metadata
1 parent db7df72 commit c2e78d9

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/variables.jl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,37 @@ end
286286
function hasdescription(x)
287287
getdescription(x) != ""
288288
end
289+
290+
## binary variables =================================================================
291+
struct VariableBinary end
292+
Symbolics.option_to_metadata_type(::Val{:binary}) = VariableBinary
293+
294+
isbinary(x::Num) = isbinary(Symbolics.unwrap(x))
295+
296+
"""
297+
isbinary(x)
298+
299+
Determine if a variable is binary.
300+
"""
301+
function isbinary(x)
302+
p = Symbolics.getparent(x, nothing)
303+
p === nothing || (x = p)
304+
return Symbolics.getmetadata(x, VariableBinary, false)
305+
end
306+
307+
## integer variables =================================================================
308+
struct VariableInteger end
309+
Symbolics.option_to_metadata_type(::Val{:integer}) = VariableInteger
310+
311+
isinteger(x::Num) = isinteger(Symbolics.unwrap(x))
312+
313+
"""
314+
isinteger(x)
315+
316+
Determine if a variable is integer.
317+
"""
318+
function isinteger(x)
319+
p = Symbolics.getparent(x, nothing)
320+
p === nothing || (x = p)
321+
return Symbolics.getmetadata(x, VariableInteger, false)
322+
end

test/test_variable_metadata.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,19 @@ sp = Set(p)
8282
@named sys = ODESystem([u ~ p], t)
8383

8484
@test_nowarn show(stdout, "text/plain", sys)
85+
86+
@testset "binary" begin
87+
@parameters t
88+
@variables u(t) [binary = true]
89+
@parameters p [binary = true]
90+
@test isbinary(u)
91+
@test isbinary(p)
92+
end
93+
94+
@testset "integer" begin
95+
@parameters t
96+
@variables u(t) [integer = true]
97+
@parameters p [integer = true]
98+
@test isinteger(u)
99+
@test isinteger(p)
100+
end

0 commit comments

Comments
 (0)