Get Help

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Community
- :
- PTC Mathcad
- :
- PTC Mathcad
- :
- Computing some parts of a function only the first ...

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-22-2018
03:27 AM

02-22-2018
03:27 AM

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.

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

Solved! Go to Solution.

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-22-2018
03:24 PM

02-22-2018
03:24 PM

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:

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

Success!

Luc

9 REPLIES

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-22-2018
09:12 AM

02-22-2018
09:12 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-22-2018
09:17 AM

02-22-2018
09:17 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-22-2018
09:47 AM

02-22-2018
09:47 AM

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:

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-22-2018
09:49 AM

02-22-2018
09:49 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-22-2018
03:24 PM

02-22-2018
03:24 PM

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:

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

Success!

Luc

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-23-2018
09:41 AM

02-23-2018
09:41 AM

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:

Thanks again and regards

Claudio

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-23-2018
09:49 AM

02-23-2018
09:49 AM

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?

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-23-2018
01:18 PM

02-23-2018
01:18 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-26-2018
02:54 AM

02-26-2018
02:54 AM

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

Top Tags