SymEngine.jl icon indicating copy to clipboard operation
SymEngine.jl copied to clipboard

subst takes dict as 2nd arg?

Open unhandyandy opened this issue 7 years ago • 4 comments

It would be nice if we could pass a dict as a second argument to subst. As it is now, something weird happens.

julia> subs((a + b)^2,a=>2,b=>3)
25

julia> d = Dict(a=>2,b=>3)
Dict{Basic,Int64} with 2 entries:
  a => 2
  b => 3

julia> subs((a + b)^2,d)
(a + b)^a

Huh? It seems to have substituted a for 2. Is that the intended behavior?

unhandyandy avatar Aug 24 '18 15:08 unhandyandy

Yes, this defn seems like it isn't doing what is expected (https://github.com/symengine/SymEngine.jl/blob/master/src/subs.jl#L30)

Maybe, splatting will get you there until it is addressed:

subs(ex, d...)

jverzani avatar Aug 24 '18 19:08 jverzani

Looks like k, v are in wrong order https://github.com/symengine/SymEngine.jl/blob/29c86a3f9d4c7b6c44c5c6bff10cfc96c5e8bbd3/src/ctypes.jl#L123

isuruf avatar Aug 24 '18 19:08 isuruf

Splatting is very inefficient.

julia> @time subs((a + b)^2,d)
  0.000040 seconds (18 allocations: 328 bytes)
(a + b)^a

julia> @time subs((a + b)^2,d...)
  0.115859 seconds (448.00 k allocations: 22.925 MiB, 2.44% gc time)
25

unhandyandy avatar Aug 25 '18 16:08 unhandyandy

That is compilation time. Try it again, and you will see times are similar. You are right for large dictionary substitutions though. I'm only suggesting a work around, @isuruf has identified the underlying issue, and I'm sure will address with his usual speed attention.

jverzani avatar Aug 25 '18 16:08 jverzani