cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Computing some parts of a function only the first time it is called? For optimization.

SOLVED
Highlighted

Computing some parts of a function only the first time it is called? For optimization.

Dear friends,

 

This could be a little academic question :-)

 

for optimization purposes, would it be possible to compute some precalculations inside a function only the first time it is called? As an example, the attached generalized interpolation function works fine, but to me it is a pity that the vector "vs" get recalculated anytime it is called.  To produce the plot, vs will be calculated hundred of times, all the times identical.  Let say that I "know" that vx and vy will remain the same.

x.JPG

 

I am perfectly aware that I could compute vs "outside" the function, making it a "global variable". But I would like to incapsulate this in the function itself.  In good old times :-) of FORTRAN programming, there was, I believe to remember, a trick based on a DATA statement that preloaded a variable at compile time.  At the first execution of a subroutine one could check if the value was the preloaded one, the subroutine knew it was being called for the first time and could perform any required precalculations and store the results in its own private memory. 

 

Thanks a lot for your insights and hints!

Regards,

Claudio

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Computing some parts of a function only the first time it is called? For optimization.

There's one way to change a worksheet variable inside a function, such that it retains its value after calling the function: it must be (a) result of the function. The function f does that exactly:

LM_20180222_FirstCall1.png

So how do we know the vector is only created once? Add some diagnostics:

LM_20180222_FirstCall2.png

Success!
Luc

 

 

9 REPLIES

Re: Computing some parts of a function only the first time it is called? For optimization.

In your example, vs is only calculated once (and only if you're asking for a spline.)  And the form and values of vs will depend on which type of spline you select.  If you compute vs "outside the function it can be used inside the function without being a global variable; the requirement is "above and/or left of" the function.

Re: Computing some parts of a function only the first time it is called? For optimization.

Hi Fred,

thanks for your answer and interest.  Are we sure it is computed only 1 time? I thought it would be computed 100 times for lspline, 100 times for pspline, 100 times for cspline.  Because of the plot and x=0,0.1 ...10

 

And it is just an example.  Imagine that I later use the function repeatedly.   So anyway you think, as I do, that the only way is to bring the evaluation out of the function.

 

Regards

Claudio

 

Re: Computing some parts of a function only the first time it is called? For optimization.

My apologies, you are correct.  The only way I know to only compute vs once is to do it separately.  There is a possible short circuit:

Capture.PNG

 

Re: Computing some parts of a function only the first time it is called? For optimization.

Elegant!

:-)

thanks!

 

by the way this is a (modern) thread about the same problem.  I do not use FORTRAN anymore, but I remember the trick was possible and used.  The

Re: Computing some parts of a function only the first time it is called? For optimization.

There's one way to change a worksheet variable inside a function, such that it retains its value after calling the function: it must be (a) result of the function. The function f does that exactly:

LM_20180222_FirstCall1.png

So how do we know the vector is only created once? Add some diagnostics:

LM_20180222_FirstCall2.png

Success!
Luc

 

 

Re: Computing some parts of a function only the first time it is called? For optimization.

Hi Luc, and thanks for the answer and interest in my "academic" :-) question.

Yes I understand, even if I never thought of that. This would mean to add an additional argument (e.g. "firstcall") to the function, then before calling it for the first time letting "firstcall=1" and then call the function. Inside the function one can do whatever processing is required and then set "firstcall=0" and get it back.

It works, but unfortunately I cannot use this for "precomputation" of anything, because what is computed inside a function "stays inside it" and is forever lost after exiting.  Or am I making a think-error?

See the attached example:

 firstcall.JPG

Thanks again and regards

Claudio

 

Re: Computing some parts of a function only the first time it is called? For optimization.

Hi Luc and thanks for the answer and the interest in my little "academic" question.

I have tried to use it in the sense that I was looking for (precomputing values to reuse in subsequent calls). I didn't succeed, though I think I implemented your idea correctly. Because variables computed in a function stay "inside it" and are lost forever when exiting. Or am I making a think-error?

firstcall.JPG

But as I wrote, it is academic :-).  One just has to "take out" the precomputation, that's it!

Best regards and thank you again

Claudio

Re: Computing some parts of a function only the first time it is called? For optimization.

I see where your implementation goes wrong. You carry a boolean  (fc) from one call to another. That does not carry the array from one call to another. So your implementation does not hold the solution to your problem.

As I wrote, the array that you calculate in the first call HAS to be output of the function. You can, as I did, abuse the array variable for the boolean function also, by presetting it as 'not an array'. After the first call it has become an array and from then on it is not further calculated, but carried from one call to the next...

 

Success!

Luc

Re: Computing some parts of a function only the first time it is called? For optimization.

Thanks, Luc.  I see the point.  I guess this trick can be considered the solution to my question.  Obviously the Mathcad programming language does not work as FORTRAN, where, if I remember well, a subroutine (or a function) has its own "private" data space, where it can store precomputed values, that remain "down there" between one call and the next ones.   With my question I am now sure the only way to "keep" values from a function is to send them "up" to the main.

Regards & again thanks a lot.

Claudio