Commit 6d555903 authored by Miroslav Kratochvil's avatar Miroslav Kratochvil
Browse files

Support MathTime conversion to Symbolics, document the whole thing

parent e6b27664
......@@ -19,8 +19,34 @@ allowed_sym(x, allowed_funs) =
haskey(allowed_funs, x) ? allowed_funs[x] :
throw(DomainError(x, "Unknown SBML function"))
function Base.convert(::Type{Num}, x::SBML.Math; mapping = default_symbolics_mapping)
"""
Base.convert(
::Type{Num},
x::SBML.Math;
mapping = default_symbolics_mapping,
convert_time = (x::SBML.MathTime) -> Num(Variable(Symbol(x.id))).val,
)
Convert SBML.[`Math`](@ref) to `Num` type Symbolics package. The conversion of
functions can be customized by supplying a custom mapping to symbols; if
nothing is supplied, [`default_symbolics_mapping`](@ref) that translates basic
functions to their Julia equivalents is assumed.
Translation of [`MathLambda`](@ref) is not supported by Symbolics.
[`MathTime`](@ref) is handled specially, the function from the argument
`convert_time` is called to possibly specify any desired behavior. By default,
it just creates a variable with the same name as the time variable name stored
in SBML.
"""
function Base.convert(
::Type{Num},
x::SBML.Math;
mapping = default_symbolics_mapping,
convert_time = (x::SBML.MathTime) -> Num(Variable(Symbol(x.id))).val,
)
conv(x::SBML.MathApply) = eval(allowed_sym(x.fn, mapping))(conv.(x.args)...)
conv(x::SBML.MathTime) = convert_time(x)
conv(x::SBML.MathIdent) = Num(Variable(Symbol(x.id))).val
conv(x::SBML.MathVal) = x.val
conv(x::SBML.MathLambda) = throw(DomainError(x, "can't translate lambdas to symbolics"))
......
@testset "Math to Symbolics conversions" begin
@variables A B C D E
@variables A B C D Time
test = SBML.MathApply(
"*",
......@@ -21,11 +21,11 @@
),
],
),
SBML.MathIdent("E"),
SBML.MathTime("Time"),
],
)
@test isequal(convert(Num, test), (A * B - C * D) * E)
@test isequal(convert(Num, test), (A * B - C * D) * Time)
test = SBML.MathApply(
"piecewise",
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment