Commit e31a6971 authored by Miroslav Kratochvil's avatar Miroslav Kratochvil
Browse files

Improve symbolics conversion

Fixes #100

- standalone literals return Num type
- constants are now converted properly
parent f2410e40
......@@ -66,12 +66,22 @@ allowed_sym(x, allowed_funs) =
haskey(allowed_funs, x) ? allowed_funs[x] :
throw(DomainError(x, "Unknown SBML function"))
"""
const default_symbolics_constants::Dict{String, Any}
A dictionary of default constants filled in place of SBML Math constants in the
symbolics conversion.
"""
const default_symbolics_constants =
Dict{String,Any}("true" => true, "false" => false, "pi" => pi, "e" => exp(1))
"""
Base.convert(
::Type{Num},
x::SBML.Math;
mapping = default_symbolics_mapping,
convert_time = (x::SBML.MathTime) -> Num(Variable(Symbol(x.id))).val,
convert_const = (x::SBML.MathConst) -> Num(default_symbolics_constants[x.id]),
)
Convert SBML.[`Math`](@ref) to `Num` type from Symbolics package. The
......@@ -91,11 +101,13 @@ function Base.convert(
x::SBML.Math;
mapping = default_symbolics_mapping,
convert_time = (x::SBML.MathTime) -> Num(Variable(Symbol(x.id))).val,
convert_const = (x::SBML.MathConst) -> Num(default_symbolics_constants[x.id]),
)
conv(x::SBML.MathApply) = eval(allowed_sym(x.fn, mapping))(conv.(x.args)...)
conv(x::SBML.MathTime) = convert_time(x)
conv(x::SBML.MathConst) = convert_const(x)
conv(x::SBML.MathIdent) = Num(Variable(Symbol(x.id))).val
conv(x::SBML.MathVal) = x.val
conv(x::SBML.MathVal) = Num(x.val)
conv(x::SBML.MathLambda) = throw(DomainError(x, "can't translate lambdas to symbolics"))
conv(x)
end
Markdown is supported
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