Skip to content

Commit b5562d3

Browse files
authored
Merge pull request #7 from xtalax/observed
Add observed variable defaults
2 parents 9d0979b + 5c4cac0 commit b5562d3

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

src/SymbolicIndexingInterface.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ include("interface.jl")
66
include("symbolcache.jl")
77

88
export independent_variables, is_indep_sym, states, state_sym_to_index, is_state_sym,
9-
parameters, param_sym_to_index, is_param_sym, SymbolCache
9+
parameters, param_sym_to_index, is_param_sym, observed, observed_sym_to_index,
10+
is_observed_sym, get_state_dependencies, get_observed_dependencies,
11+
get_deps_of_observed, SymbolCache, unknown_states
1012

1113
end

src/interface.jl

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ states(::Any) = []
3131
"""
3232
$(TYPEDSIGNATURES)
3333
34+
Get an iterable over the unknown states for the given system. Default to an empty vector.
35+
"""
36+
function unknown_states end
37+
38+
unknown_states(::Any) = []
39+
40+
"""
41+
$(TYPEDSIGNATURES)
42+
3443
Find the index of the given sym in the given system. Default to the index of the first
3544
symbol in the iterable returned by `states` which matches the given `sym`. Return
3645
`nothing` if the given `sym` does not match.
@@ -81,3 +90,73 @@ to checking if the value returned by `param_sym_to_index` is not `nothing`.
8190
function is_param_sym end
8291

8392
is_param_sym(store, sym) = !isnothing(param_sym_to_index(store, sym))
93+
94+
"""
95+
$(TYPEDSIGNATURES)
96+
97+
Get an iterable over the observed variable expressions for the given system.
98+
Default to an empty vector.
99+
"""
100+
function observed end
101+
102+
observed(::Any) = []
103+
104+
"""
105+
$(TYPEDSIGNATURES)
106+
107+
Check if the given sym is an observed variable in the given system. Default
108+
to checking if the value returned by `observed_sym_to_index` is not `nothing`.
109+
"""
110+
function is_observed_sym end
111+
112+
is_observed_sym(store, sym) = !isnothing(observed_sym_to_index(store, sym))
113+
114+
"""
115+
$(TYPEDSIGNATURES)
116+
117+
Find the index of the given sym in the given system. Default to the index of the first
118+
symbol in the iterable returned by `states` which matches the given `sym`. Return
119+
`nothing` if the given `sym` does not match.
120+
"""
121+
function observed_sym_to_index end
122+
123+
function observed_sym_to_index(store, sym)
124+
findfirst(o -> isequal(sym, o.lhs), observed(store))
125+
end
126+
127+
"""
128+
$(TYPEDSIGNATURES)
129+
130+
Return a list of the dependent state variables of an observed variable. Default to returning
131+
an empty list.
132+
"""
133+
function get_state_dependencies end
134+
135+
get_state_dependencies(store, sym) = []
136+
137+
"""
138+
$(TYPEDSIGNATURES)
139+
140+
Return a list of the dependent observed variables of an observed variable. Default to returning
141+
an empty list.
142+
"""
143+
function get_observed_dependencies end
144+
145+
get_observed_dependencies(store, sym) = []
146+
147+
"""
148+
$(TYPEDSIGNATURES)
149+
150+
Return a list of the dependent state variables of all observed equations of the system.
151+
Default to returning an empty list.
152+
"""
153+
function get_deps_of_observed end
154+
155+
function get_deps_of_observed(store)
156+
obs = observed(store)
157+
deps = mapreduce(vcat, obs, init = []) do eq
158+
get_state_dependencies(store, eq.lhs)
159+
end |> unique
160+
161+
return deps
162+
end

test/default_function_test.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ using SymbolicIndexingInterface, Test
33
@test independent_variables(nothing) == []
44
@test states(nothing) == []
55
@test parameters(nothing) == []
6+
@test observed(nothing) == []
67
@test !is_indep_sym(nothing, :a)
78
@test !is_state_sym(nothing, :a)
89
@test !is_param_sym(nothing, :a)
10+
@test !is_observed_sym(nothing, :a)
911
@test isnothing(state_sym_to_index(nothing, :a))
1012
@test isnothing(param_sym_to_index(nothing, :a))
13+
@test get_state_dependencies(nothing, :a) == []
14+
@test get_observed_dependencies(nothing, :a) == []
15+
@test get_deps_of_observed(nothing) == []

0 commit comments

Comments
 (0)