Skip to content

Commit

Permalink
feat: add all_solvable_symbols and all_symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSabharwal committed Dec 17, 2023
1 parent 0e67d45 commit 218d311
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ is_observed
observed
is_time_dependent
constant_structure
all_solvable_symbols
all_symbols
parameter_values
getp
setp
Expand Down
21 changes: 19 additions & 2 deletions docs/src/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ struct ExampleSolution
state_index::Dict{Symbol,Int}
parameter_index::Dict{Symbol,Int}
independent_variable::Union{Symbol,Nothing}
# mapping from observed variable to Expr to calculate its value
observed::Dict{Symbol,Expr}
u::Vector{Vector{Float64}}
p::Vector{Float64}
t::Vector{Float64}
Expand Down Expand Up @@ -86,9 +88,9 @@ function SymbolicIndexingInterface.independent_variable_symbols(sys::ExampleSolu
sys.independent_variable === nothing ? [] : [sys.independent_variable]
end

# this types accepts `Expr` for observed expressions involving state/parameter
# this type accepts `Expr` for observed expressions involving state/parameter/observed
# variables
SymbolicIndexingInterface.is_observed(sys::ExampleSolution, sym) = sym isa Expr
SymbolicIndexingInterface.is_observed(sys::ExampleSolution, sym) = sym isa Expr || sym isa Symbol && haskey(sys.observed, sym)

function SymbolicIndexingInterface.observed(sys::ExampleSolution, sym::Expr)
if is_time_dependent(sys)
Expand All @@ -109,6 +111,21 @@ function SymbolicIndexingInterface.is_time_dependent(sys::ExampleSolution)
end

SymbolicIndexingInterface.constant_structure(::ExampleSolution) = true

function SymbolicIndexingInterface.all_solvable_symbols(sys::ExampleSolution)
return vcat(
collect(keys(sys.state_index)),
collect(keys(sys.observed)),
)
end

function SymbolicIndexingInterface.all_symbols(sys::ExampleSolution)
return vcat(
all_solvable_symbols(sys),
collect(keys(sys.parameter_index)),
sys.independent_variable === nothing ? Symbol[] : sys.independent_variable
)
end
```

Note that the method definitions are all assuming `constant_structure(p) == true`.
Expand Down
16 changes: 16 additions & 0 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,19 @@ Check if `sys` has a constant structure. Constant structure systems do not chang
number of variables or parameters over time.
"""
constant_structure(sys) = constant_structure(symbolic_container(sys))

"""
all_solvable_symbols(sys)
Return an array of all symbols in the system that can be solved for. This includes
observed variables, but not parameters or independent variables.
"""
all_solvable_symbols(sys) = all_solvable_symbols(symbolic_container(sys))

Check warning on line 120 in src/interface.jl

View check run for this annotation

Codecov / codecov/patch

src/interface.jl#L120

Added line #L120 was not covered by tests

"""
all_symbols(sys)
Return an array of all symbols in the system. This includes parameters and independent
variables.
"""
all_symbols(sys) = all_symbols(symbolic_container(sys))

Check warning on line 128 in src/interface.jl

View check run for this annotation

Codecov / codecov/patch

src/interface.jl#L128

Added line #L128 was not covered by tests

0 comments on commit 218d311

Please sign in to comment.