Skip to content

Commit 5370e25

Browse files
committed
avoid lookup, use vector
1 parent d0eea29 commit 5370e25

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

ext/SymEngineTermInterfaceExt.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module SymEngineTermInterfaceExt
22

33
import SymEngine
4-
import SymEngine: SymbolicType
54
import TermInterface
65

76

@@ -24,6 +23,16 @@ import TermInterface
2423
λ(::Val{:Acsch}) = acsch; λ(::Val{:Asech}) = asech; λ(::Val{:Acoth}) = acoth
2524
λ(::Val{:Gamma}) = gamma; λ(::Val{:Zeta}) = zeta; λ(::Val{:LambertW}) = lambertw
2625

26+
const julia_operations = Vector{Any}(missing, length(SymEngine.symengine_classes))
27+
for (i,s) enumerate(SymEngine.symengine_classes)
28+
val = try
29+
λ(Val(s))
30+
catch err
31+
missing
32+
end
33+
julia_operations[i] = val
34+
end
35+
2736
#==
2837
Check if x represents an expression tree. If returns true, it will be assumed that operation(::T) and arguments(::T) methods are defined. Definining these three should allow use of SymbolicUtils.simplify on custom types. Optionally symtype(x) can be defined to return the expected type of the symbolic expression.
2938
==#
@@ -40,7 +49,7 @@ TermInterface.isexpr(x::SymEngine.SymbolicType) = TermInterface.iscall(x)
4049

4150
function TermInterface.operation(x::SymEngine.SymbolicType)
4251
TermInterface.iscall(x) || error("$(typeof(x)) doesn't have an operation!")
43-
return λ(x)
52+
return julia_operations[SymEngine.get_type(x) + 1]
4453
end
4554

4655
function TermInterface.arguments(x::SymEngine.SymbolicType)

src/subs.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,30 @@ fn_map = Dict(
6262

6363
map_fn(key, fn_map) = haskey(fn_map, key) ? fn_map[key] : Symbol(lowercase(string(key)))
6464

65+
const julia_classes = map_fn.(symengine_classes, (fn_map,))
66+
get_julia_class(x::Basic) = julia_classes[get_type(x) + 1]
67+
Base.nameof(ex::Basic) = Symbol(toString(ex))
68+
6569
function _convert(::Type{Expr}, ex::Basic)
6670
fn = get_symengine_class(ex)
6771

6872
if fn == :Symbol
69-
return Symbol(toString(ex))
73+
return nameof(ex)
7074
elseif (fn in number_types) || (fn == :Constant)
7175
return N(ex)
7276
end
7377

7478
as = get_args(ex)
75-
76-
Expr(:call, map_fn(fn, fn_map), [_convert(Expr,a) for a in as]...)
79+
fn′ = get_julia_class(ex)
80+
Expr(:call, fn′, [_convert(Expr,a) for a in as]...)
7781
end
7882

7983

8084
function convert(::Type{Expr}, ex::Basic)
8185
fn = get_symengine_class(ex)
8286

8387
if fn == :Symbol
84-
return Expr(:call, :*, Symbol(toString(ex)), 1)
88+
return Expr(:call, :*, nameof(ex), 1)
8589
elseif (fn in number_types) || (fn == :Constant)
8690
return Expr(:call, :*, N(ex), 1)
8791
end

0 commit comments

Comments
 (0)