I have two functions f(x) and g(x). Here f(x) is the objective function to minimize, and g(x) is the gradient function. My problem is for each trial x, the body of f(x) will compute a complicated matrix A(x), which will also be used in g(x). For the sake of efficiency, I don't want g(x) to repeat the computation of A. I am considering to make A(x) global by defining A <<- ... in the body of f(x). So g(x) can use A(x) directly. Because I don't know how optim in R iterates f(x) and g(x), I am not sure if this strategy is correct and efficient. Any suggestions and comments are welcome. Thanks.
Because you don't know how optim is going to call f and g you are going to have to make sure that any stashed A(x) is from the same x when you need it. It might call f(x1), f(x2), f(x3) and then g(x1).
One solution might be memoisation:
http://cran.r-project.org/web/packages/memoise/index.html
A memoised A(x) will store the return value for given input values and return that when given the same input values without recomputing. Obviously only works for non-stochastic functions (don't call any random number generators).
I'm not sure how you control the size of the cache, but the source code is all there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With