Eigenvalue
Eigenvalue

Reputation: 1133

julia : setting a variable in anonymous function so that it is set forever and you can evaluate in other variables only

So for demonstration, lets say I have a function f(x,y), where y is going to be fixed for all evaluations of f.

my_func = x -> f(x,y) 

My question is I believe that when you evaluate my_func(x) it will rerun f(x,y) again and again for every x you evaluate it at. I don't want to have to reconstruct f every time I have a new x, because y is fixed. Is there any way that I can make one evaluation of f(x,y) happen for a fixed y and x is not determined yet, i.e make a g(x)= f(x,y) (because y is fixed)?

So then I can just have

my_func2 = x-> g(x) 

so that I am just evaluating it for some given x.

Thank you.

Upvotes: 2

Views: 214

Answers (1)

Jakob Nissen
Jakob Nissen

Reputation: 1991

I'm not 100% sure I understand what you mean. But if you do g(x) = f(x, y), and you make sure y is a const, then you will not pay any penalty relative to just evaluating f(x, y), and indeed g(x) may be even simpler since the compiler knows y is a constant. Julia will spend no extra time "reconstructing f".

If you're asking "how do I prevent spending time calculating f(x, y) for repeated values of x and y", the answer is to use memoization, i.e. caching previous computations.

Upvotes: 1

Related Questions