You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Memoization of SymPy functions in a Dict does not work as expected. When integrate() is called with equal, but not identical arguments it computes integrate() again and grows the cache.
See the following MWE
using SymPy
using Memoization
functionintegrate_wrapper(args...; kwargs...)
# memoize the call in Dict, not IdDict
out =@memoize Dict integrate(args...; kwargs...)
return out
end
t, a =symbols("t, a")
f =sin(a * t)
integrate_wrapper(f, (t, 0, t))
g =sin(a * t)
integrate_wrapper(g, (t, 0, t))
f == g # true
f === g # false
Memoization.get_caches()[SymPy.integrate]
# output
Dict{Any, Any} with 2 entries:
((sin(a*t), (t, 0, t)), NamedTuple()) =>Piecewise((-cos(a*t)/a +1/a, (a >-…
((sin(a*t), (t, 0, t)), NamedTuple()) =>Piecewise((-cos(a*t)/a +1/a, (a >-…
If I call integrate_wrapper() again with (f, (t, 0, t) the cache does not grow. But if I create f = sin(a * t) again and then call integrate_wrapper(f, (t, 0, t)) again the cache does grow. Exactly the same behavious is observed when using @memoize integrate(args...; kwargs...) or @memoize LRU(maxcache=5) integrate(args...; kwargs...).
The same is true for many other SymPy functions.
The text was updated successfully, but these errors were encountered:
jlbosse
changed the title
Memoization of SymPy function calls does recognize identical arguments
Memoization of SymPy function calls does not recognize identical arguments
Jul 25, 2023
I have done some more digging and it appears that Tuples containing SymPy.Syms don't work properly as keys in julia's standard Dicts. See the following MWE:
Memoization of SymPy functions in a
Dict
does not work as expected. Whenintegrate()
is called with equal, but not identical arguments it computesintegrate()
again and grows the cache.See the following MWE
If I call
integrate_wrapper()
again with(f, (t, 0, t)
the cache does not grow. But if I createf = sin(a * t)
again and then callintegrate_wrapper(f, (t, 0, t))
again the cache does grow. Exactly the same behavious is observed when using@memoize integrate(args...; kwargs...)
or@memoize LRU(maxcache=5) integrate(args...; kwargs...)
.The same is true for many other SymPy functions.
The text was updated successfully, but these errors were encountered: