From b243589b097c19d880b470ec33629bc99ebcb472 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 11 Dec 2023 17:08:45 +0530 Subject: [PATCH] refactor: format --- src/parameter_indexing.jl | 4 ++-- src/symbol_cache.jl | 29 ++++++++++++++++++++++------- src/trait.jl | 2 +- test/fallback_test.jl | 3 ++- test/runtests.jl | 2 +- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/parameter_indexing.jl b/src/parameter_indexing.jl index b106c26..62608d8 100644 --- a/src/parameter_indexing.jl +++ b/src/parameter_indexing.jl @@ -35,7 +35,7 @@ function _getp(sys, ::ScalarSymbolic, p) end end -function _getp(sys, ::ScalarSymbolic, p::Union{Tuple,AbstractArray}) +function _getp(sys, ::ScalarSymbolic, p::Union{Tuple, AbstractArray}) idxs = parameter_index.((sys,), p) return function getter(sol) return getindex.((parameter_values(sol),), idxs) @@ -77,7 +77,7 @@ function _setp(sys, ::ScalarSymbolic, p) end end -function _setp(sys, ::ScalarSymbolic, p::Union{Tuple,AbstractArray}) +function _setp(sys, ::ScalarSymbolic, p::Union{Tuple, AbstractArray}) idxs = parameter_index.((sys,), p) return function setter!(sol, val) setindex!.((parameter_values(sol),), val, idxs) diff --git a/src/symbol_cache.jl b/src/symbol_cache.jl index b622d81..c2d89fa 100644 --- a/src/symbol_cache.jl +++ b/src/symbol_cache.jl @@ -11,21 +11,35 @@ at least one independent variable. The independent variable may be specified as a single symbolic variable instead of an array containing a single variable if the system has only one independent variable. """ -struct SymbolCache{V<:Union{Nothing,AbstractVector}, P<:Union{Nothing,AbstractVector}, I} +struct SymbolCache{ + V <: Union{Nothing, AbstractVector}, + P <: Union{Nothing, AbstractVector}, + I, +} variables::V parameters::P independent_variables::I end function SymbolCache(vars = nothing, params = nothing, indepvars = nothing) - return SymbolCache{typeof(vars),typeof(params),typeof(indepvars)}(vars, params, indepvars) + return SymbolCache{typeof(vars), typeof(params), typeof(indepvars)}(vars, + params, + indepvars) end -is_variable(sc::SymbolCache, sym) = sc.variables !== nothing && any(isequal(sym), sc.variables) -variable_index(sc::SymbolCache, sym) = sc.variables === nothing ? nothing : findfirst(isequal(sym), sc.variables) +function is_variable(sc::SymbolCache, sym) + sc.variables !== nothing && any(isequal(sym), sc.variables) +end +function variable_index(sc::SymbolCache, sym) + sc.variables === nothing ? nothing : findfirst(isequal(sym), sc.variables) +end variable_symbols(sc::SymbolCache, i = nothing) = something(sc.variables, []) -is_parameter(sc::SymbolCache, sym) = sc.parameters !== nothing && any(isequal(sym), sc.parameters) -parameter_index(sc::SymbolCache, sym) = sc.parameters === nothing ? nothing : findfirst(isequal(sym), sc.parameters) +function is_parameter(sc::SymbolCache, sym) + sc.parameters !== nothing && any(isequal(sym), sc.parameters) +end +function parameter_index(sc::SymbolCache, sym) + sc.parameters === nothing ? nothing : findfirst(isequal(sym), sc.parameters) +end parameter_symbols(sc::SymbolCache) = something(sc.parameters, []) function is_independent_variable(sc::SymbolCache, sym) sc.independent_variables === nothing && return false @@ -61,5 +75,6 @@ constant_structure(::SymbolCache) = true function Base.copy(sc::SymbolCache) return SymbolCache(sc.variables === nothing ? nothing : copy(sc.variables), sc.parameters === nothing ? nothing : copy(sc.parameters), - sc.independent_variables isa AbstractArray ? copy(sc.independent_variables) : sc.independent_variables) + sc.independent_variables isa AbstractArray ? copy(sc.independent_variables) : + sc.independent_variables) end diff --git a/src/trait.jl b/src/trait.jl index af74ee1..6660e91 100644 --- a/src/trait.jl +++ b/src/trait.jl @@ -58,4 +58,4 @@ hasname(::Any) = false Get the name of a symbolic variable as a `Symbol` """ -function getname end \ No newline at end of file +function getname end diff --git a/test/fallback_test.jl b/test/fallback_test.jl index 172d8ee..4c354bf 100644 --- a/test/fallback_test.jl +++ b/test/fallback_test.jl @@ -14,7 +14,8 @@ all_syms = [:x, :y, :z, :a, :b, :t] @test variable_index.((sys,), all_syms) == variable_index.((sc,), all_syms) @test is_parameter.((sys,), all_syms) == is_parameter.((sc,), all_syms) @test parameter_index.((sys,), all_syms) == parameter_index.((sc,), all_syms) -@test is_independent_variable.((sys,), all_syms) == is_independent_variable.((sc,), all_syms) +@test is_independent_variable.((sys,), all_syms) == + is_independent_variable.((sc,), all_syms) @test is_observed.((sys,), all_syms) == is_observed.((sc,), all_syms) @test is_time_dependent(sys) == is_time_dependent(sc) @test constant_structure(sys) == constant_structure(sc) diff --git a/test/runtests.jl b/test/runtests.jl index b76e653..20538d7 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -15,4 +15,4 @@ end end @testset "Parameter indexing test" begin @time include("parameter_indexing_test.jl") -end \ No newline at end of file +end