Skip to content

Commit 837be98

Browse files
Add default methods interface functions
- All interface functions have sensible defaults - All interface functions by default compare `Symbol` - Update documentation - Add `SymbolCache` docstring
1 parent 0a313d5 commit 837be98

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

docs/src/api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Interface Functions
22

3+
Default methods cast all symbols to `Symbol` before comparing.
4+
35
```@docs
46
independent_variables
57
is_indep_sym

src/interface.jl

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,83 @@
11
"""
22
$(TYPEDSIGNATURES)
33
4-
Get the set of independent variables for the given system.
4+
Get an iterable over the independent variables for the given system. Default to an empty
5+
vector.
56
"""
67
function independent_variables end
8+
independent_variables(::Any) = []
79

810
"""
911
$(TYPEDSIGNATURES)
1012
11-
Check if the given sym is an independent variable in the given system. Defaults
12-
to `false` if not implemented for the given system/container type.
13+
Check if the given sym is an independent variable in the given system. Default to checking
14+
if the given `sym` exists in the iterable returned by `independent_variables`.
1315
"""
1416
function is_indep_sym end
1517

18+
function is_indep_sym(store, sym)
19+
any(isequal(Symbol(sym)), Symbol(x) for x in independent_variables(store))
20+
end
21+
1622
"""
1723
$(TYPEDSIGNATURES)
1824
19-
Get the set of states for the given system.
25+
Get an iterable over the states for the given system. Default to an empty vector.
2026
"""
2127
function states end
2228

29+
states(::Any) = []
30+
2331
"""
2432
$(TYPEDSIGNATURES)
2533
26-
Find the index of the given sym in the given system.
34+
Find the index of the given sym in the given system. Default to the index of the first
35+
symbol in the iterable returned by `states` which matches the given `sym`. Return
36+
`nothing` if the given `sym` does not match.
2737
"""
2838
function state_sym_to_index end
2939

40+
function state_sym_to_index(store, sym)
41+
findfirst(isequal(Symbol(sym)), Symbol(x) for x in states(store))
42+
end
43+
3044
"""
3145
$(TYPEDSIGNATURES)
3246
33-
Check if the given sym is a state variable in the given system. Defaults
34-
to `false` if not implemented for the given system/container type.
47+
Check if the given sym is a state variable in the given system. Default to checking if
48+
the value returned by `state_sym_to_index` is not `nothing`.
3549
"""
3650
function is_state_sym end
3751

52+
is_state_sym(store, sym) = !isnothing(state_sym_to_index(store, sym))
53+
3854
"""
3955
$(TYPEDSIGNATURES)
4056
41-
Get the set of parameters variables for the given system.
57+
Get an iterable over the parameters variables for the given system. Default to an empty
58+
vector.
4259
"""
4360
function parameters end
4461

62+
parameters(::Any) = []
63+
4564
"""
4665
$(TYPEDSIGNATURES)
4766
48-
Find the index of the given sym in the given system.
67+
Find the index of the given sym in the given system. Default to the index of the first
68+
symbol in the iterable retruned by `parameters` which matches the given `sym`. Return
69+
`nothing` if the given `sym` does not match.
4970
"""
5071
function param_sym_to_index end
5172

73+
param_sym_to_index(store, sym) = findfirst(isequal(Symbol(sym)), Symbol.(parameters(store)))
74+
5275
"""
5376
$(TYPEDSIGNATURES)
5477
55-
Check if the given sym is a parameter variable in the given system. Defaults
56-
to `false` if not implemented for the given system/container type.
78+
Check if the given sym is a parameter variable in the given system. Default
79+
to checking if the value returned by `param_sym_to_index` is not `nothing`.
5780
"""
5881
function is_param_sym end
82+
83+
is_param_sym(store, sym) = !isnothing(param_sym_to_index(store, sym))

src/symbolcache.jl

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""
2+
SymbolCache(syms, indepsym, paramsyms)
3+
4+
A container that simply stores a vector of all syms, indepsym and paramsyms.
5+
"""
16
struct SymbolCache{S, T, U}
27
syms::S
38
indepsym::T
@@ -6,21 +11,13 @@ end
611

712
independent_variables(sc::SymbolCache) = sc.indepsym
813
independent_variables(::SymbolCache{S, Nothing}) where {S} = []
9-
is_indep_sum(::Any, _) = false
10-
is_indep_sym(sc::SymbolCache, sym) = any(isequal(sym), sc.indepsym)
1114
is_indep_sym(::SymbolCache{S, Nothing}, _) where {S} = false
1215
states(sc::SymbolCache) = sc.syms
1316
states(::SymbolCache{Nothing}) = []
14-
state_sym_to_index(sc::SymbolCache, sym) = findfirst(isequal(sym), sc.syms)
1517
state_sym_to_index(::SymbolCache{Nothing}, _) = nothing
16-
is_state_sym(::Any, _) = false
17-
is_state_sym(sc::SymbolCache, sym) = !isnothing(state_sym_to_index(sc, sym))
1818
parameters(sc::SymbolCache) = sc.paramsyms
1919
parameters(::SymbolCache{S, T, Nothing}) where {S, T} = []
20-
param_sym_to_index(sc::SymbolCache, sym) = findfirst(isequal(sym), sc.paramsyms)
2120
param_sym_to_index(::SymbolCache{S, T, Nothing}, _) where {S, T} = nothing
22-
is_param_sym(::Any, _) = false
23-
is_param_sym(sc::SymbolCache, sym) = !isnothing(param_sym_to_index(sc, sym))
2421

2522
function Base.copy(VA::SymbolCache)
2623
typeof(VA)((VA.syms === nothing) ? nothing : copy(VA.syms),

0 commit comments

Comments
 (0)