Memoize functions defined in `SymPy.jl`

jlbosse
Tue, 25 Jul 2023 10:36:34 +0000
General Usage

Is there a way to memoize a function that is defined in another module?

Ideally, I would like to to something along the lines of

import OtherModule: func
using Memoization

# I want this to replace `func` with a memoized version of `func` in the current scope
@memoize func

function myfunc(x)
    a = func(x)    # this call should ideally be memoized
    y = # do some stuff with a
    return y
end

My current work-around is

import OtherModule: func
using Memoization

# I want this to replace `func` with a memoized version of `func` in the current scope
@memoize memoized_func(args...; kwargs...) = func(args...; kwargs...)

function myfunc(x)
    a = memoized_func(x)    # this call should ideally be memoized
    y = # do some stuff with a
    return y
end

but this doesn’t feel very elegant, because I use func in many places in my code and want it to be memoized everywhere.

Edit:
The OtherModule in question is SymPy.jl and one of the calls I want to memoize is sym.rewrite(sin) where sym is a SymPy symbol. So bonus points for a way to memoize calls to such member functions.

4 posts - 2 participants