[HP Prime] Function selection
09-21-2014, 01:30 PM (This post was last modified: 02-01-2015 03:16 PM by Han.)
Post: #1
 Han Senior Member Posts: 1,775 Joined: Dec 2013
[HP Prime] Function selection
This is an example of how to programmatically use functions for evaluation without altering the user's original functions. The technique can be used for apps and programs. Three commands are available to check if a function has been selected (ISCHECK), to select a function (CHECK), and to deselect a function (UNCHECK).

Goals:

1. To determine which function the user has selected and...
2. use that function for calculations.
3. Leave the user's functions list untampered.
4. Detect if a new function has actually been selected (to avoid unnecessary recalculations)

We use a variable fx and initialize it to an empty string. This string keeps track of the "current" function. Likewise we use another variable recompute to determine if recalculations are actually necessary. A user may possible have re-created the same function and placed it into a different slot in the [Symb] screen. In such a scenario, recalculations is highly likely to be unnecessary.

The first step is to loop through F0-F9 to check if any functions have been selected. We keep track of the total number of selected functions in ftot. In this example, we only want one function selected. We use fsel to keep track of the selected function; if only one function has been selected, then fsel will simply be the index (from our loop) for which ISCHECK(i) tested as true. While it may seem that fsel could be compared to its prior value to see if the selected function has changed, we cannot account for cases when the function has been copied (i.e. the function has not changed although it may have moved from F0 to, say, F3) using this technique. So instead, we compare the currently selected function with fx. To create a string from F0, F1, …, F9, we can use the STRING() command: STRING(F0) will return a string equivalent of the formula stored in F0. Since our selection is dynamic, we have to embed this within the EXPR() command and then store the string into fnew. Lastly, comparing the string fx against fnew will allow us to know if the function has truly changed since the last time we run this program. If so, recompute is set to 1 and we can use this within our calculations routine. Below is the code that will do all these steps:

Code:
fx:=""; recompute:=0; export getfunc() begin   local i, ftot=0, fnew, fsel;   for i from 0 to 9 do     if ISCHECK(i) then       fsel:=i; ftot:=ftot+1;     end;   end;   if ftot==0 then     msgbox("Please enter/select a function!");     startview(0,1);     kill;   end;   if ftot>1 then      msgbox("Please select only one function!");     startview(0,1);     kill;   end;   fnew:=expr("STRING(F"+left(STRING(fsel),1)+")");   if fnew<>fx then     fx:=fnew;     recompute:=1;   end;   return(recompute); end;

Now that our selected function is stored into fx, we can then use the builtin function names F0, F1, …, F9 to do our evaluations. It may be the case that the user already has formulas in all these built-in function names. We can copy, say, F0 into a temporary string, use F0 to do our calculations, and then restore F0.

Code:
 export evalfunc() begin   if getfunc() then     iferr func:=STRING(F0); then func:=""; end; // gracefully copy F0     F0:=fx; // replace F0 with our selected function     computefunc(); // run the computations if needed     F0:=func; // restore the function   end; end;

These two programs, along with our own computefunc() program, need to be within the same source file. Make sure that computefunc() exits gracefully so that F0 is restored to its original state! If necessary, surround computefunc() with an instance of an IFERR block.

Happy programming!

Han

Graph 3D | QPI | SolveSys
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)