Skip to content

Commit 5513b82

Browse files
Add interface, SymbolCache, tests
1 parent 0298b85 commit 5513b82

File tree

7 files changed

+124
-4
lines changed

7 files changed

+124
-4
lines changed

Project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ uuid = "2efcf032-c050-4f8e-a9bb-153293bab1f5"
33
authors = ["Aayush Sabharwal <[email protected]> and contributors"]
44
version = "0.1.0"
55

6+
[deps]
7+
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
8+
69
[compat]
10+
DocStringExtensions = "0.9"
711
julia = "1"
812

913
[extras]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# SymbolicIndexingInterface
1+
# SymbolicIndexingInterface.jl

src/SymbolicIndexingInterface.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
module SymbolicIndexingInterface
22

3-
# Write your package code here.
3+
using DocStringExtensions
4+
5+
include("interface.jl")
6+
include("symbolcache.jl")
7+
8+
export independent_variables, is_indep_sym, states, state_sym_to_index, is_state_sym,
9+
parameters, param_sym_to_index, is_param_sym, SymbolCache
410

511
end

src/interface.jl

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
$(TYPEDSIGNATURES)
3+
4+
Get the set of independent variables for the given system.
5+
"""
6+
function independent_variables end
7+
8+
"""
9+
$(TYPEDSIGNATURES)
10+
11+
Check if the given sym is an independent variable in the given system.
12+
"""
13+
function is_indep_sym end
14+
15+
"""
16+
$(TYPEDSIGNATURES)
17+
18+
Get the set of states for the given system.
19+
"""
20+
function states end
21+
22+
"""
23+
$(TYPEDSIGNATURES)
24+
25+
Find the index of the given sym in the given system.
26+
"""
27+
function state_sym_to_index end
28+
29+
"""
30+
$(TYPEDSIGNATURES)
31+
32+
Check if the given sym is a state variable in the given system.
33+
"""
34+
function is_state_sym end
35+
36+
"""
37+
$(TYPEDSIGNATURES)
38+
39+
Get the set of parameters variables for the given system.
40+
"""
41+
function parameters end
42+
43+
"""
44+
$(TYPEDSIGNATURES)
45+
46+
Find the index of the given sym in the given system.
47+
"""
48+
function param_sym_to_index end
49+
50+
"""
51+
$(TYPEDSIGNATURES)
52+
53+
Check if the given sym is a parameter variable in the given system.
54+
"""
55+
function is_param_sym end

src/symbolcache.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
struct SymbolCache{S,T,U}
2+
syms::S
3+
indepsym::T
4+
paramsyms::U
5+
end
6+
7+
8+
independent_variables(sc::SymbolCache) = sc.indepsym
9+
independent_variables(::SymbolCache{S,Nothing}) where {S} = []
10+
is_indep_sym(sc::SymbolCache, sym) = any(isequal(sym), sc.indepsym)
11+
is_indep_sym(::SymbolCache{S,Nothing}, _) where {S} = false
12+
states(sc::SymbolCache) = sc.syms
13+
states(::SymbolCache{Nothing}) = []
14+
state_sym_to_index(sc::SymbolCache, sym) = findfirst(isequal(sym), sc.syms)
15+
state_sym_to_index(::SymbolCache{Nothing}, _) = nothing
16+
is_state_sym(sc::SymbolCache, sym) = !isnothing(state_sym_to_index(sc, sym))
17+
parameters(sc::SymbolCache) = sc.paramsyms
18+
parameters(::SymbolCache{S,T,Nothing}) where {S,T} = []
19+
param_sym_to_index(sc::SymbolCache, sym) = findfirst(isequal(sym), sc.paramsyms)
20+
param_sym_to_index(::SymbolCache{S,T,Nothing}, _) where {S,T} = nothing
21+
is_param_sym(sc::SymbolCache, sym) = !isnothing(param_sym_to_index(sc, sym))
22+
23+
Base.copy(VA::SymbolCache) = typeof(VA)(
24+
(VA.syms===nothing) ? nothing : copy(VA.syms),
25+
(VA.indepsym===nothing) ? nothing : copy(VA.indepsym),
26+
(VA.paramsyms===nothing) ? nothing : copy(VA.paramsyms),
27+
)

test/runtests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using SymbolicIndexingInterface
22
using Test
33

4-
@testset "SymbolicIndexingInterface.jl" begin
5-
# Write your tests here.
4+
@time begin
5+
@time @testset begin include("symbolcache.jl") end
66
end

test/symbolcache.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using SymbolicIndexingInterface, Test
2+
3+
sc = SymbolCache(nothing, nothing, nothing)
4+
@test isempty(independent_variables(sc))
5+
@test !is_indep_sym(sc, :a)
6+
@test isempty(states(sc))
7+
@test isnothing(state_sym_to_index(sc, :a))
8+
@test !is_state_sym(sc, :a)
9+
@test isempty(parameters(sc))
10+
@test isnothing(param_sym_to_index(sc, :a))
11+
@test !is_param_sym(sc, :a)
12+
13+
sc = SymbolCache([:a, :b], [:t], [:c, :d])
14+
@test independent_variables(sc) == [:t]
15+
@test is_indep_sym(sc, :t)
16+
@test !is_indep_sym(sc, :a)
17+
@test states(sc) == [:a, :b]
18+
@test state_sym_to_index(sc, :a) == 1
19+
@test state_sym_to_index(sc, :b) == 2
20+
@test isnothing(state_sym_to_index(sc, :t))
21+
@test all(is_state_sym.((sc,), [:a, :b]))
22+
@test !is_state_sym(sc, :c)
23+
@test parameters(sc) == [:c, :d]
24+
@test param_sym_to_index(sc, :c) == 1
25+
@test param_sym_to_index(sc, :d) == 2
26+
@test isnothing(param_sym_to_index(sc, :a))
27+
@test all(is_param_sym.((sc,), [:c, :d]))
28+
@test !is_param_sym(sc, :b)

0 commit comments

Comments
 (0)