Message Boards Message Boards

0
|
1125 Views
|
5 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Is there any construct similar to function pointers?

Posted 6 months ago

Hi, in my MATHEMATICA program I need to have a number of functions, say:

fun1[x_]:=function code 1;
fun2[x_]:=function code 2;
fun3[x_]:=function code 3;

and a further code that intensively uses one of these functions, but the choice which function is to be used depends on the input parameter selected by the program user. Is there any MATHEMATICA language construct, similar to "C" pointers to functions, which allows one to avoid rewriting several versions of the code (for every function separately) or frantically check the user parameter at every call of the function, to decide which function is to be used? Or, is there any other recommended way to handle the above situation? Lesław

POSTED BY: Leslaw Bieniasz
5 Replies
Posted 6 months ago

Could the following be an alternative?

Definitions:

fun[1]=Function[#+5];
fun[2]=Function[(#+6)^2];
fun[3]=Function[(#+7)^3];
fun[index_,argument_]:=fun[index][argument]

Use:

fun[1,7]
fun[2,7]
fun[3,7]

(* 12   *)
(* 169  *)
(* 2744 *)
POSTED BY: Hans Milton
Posted 6 months ago

In Mathematica, everything is an expression. So, the expression fun1 is effectively the "function pointer" that you want. It's an expression with down values that specify how to evaluate when applied to arguments. Let's make this concrete:

fun1[x_Integer] := ToString[StringForm["fun1 accepts `` and doubles: ``", x, 2 x]];
fun2[x_Integer] := ToString[StringForm["fun2 accepts `` and squares: ``", x, x^2]];
fun3[x_Integer] := ToString[StringForm["fun3 accepts `` and factors: ``", x, FactorInteger[x]]];

Let's simulate the user's choice with a random choice:

chosenFunc = RandomChoice[{fun1, fun2, fun3}];

Now let's use our choice:

chosenFunc[18]
(* "fun2 accepts 18 and squares: 324" *)
POSTED BY: Eric Rimbey
Posted 6 months ago

Thank you for answers. I have tried the following solution, and it seems to work:

fun1[x_]:=function code 1;
fun2[x_]:=function code 2;
fun3[x_]:=function code 3;

Which[param==1,fun[x_]:=fun1[x],param==2,fun[x_]:=fun2[x],param==3,fun[x_]:=fun3[x]];

and in the further code I just call fun[arg]. Is this an efficient solution? I wonder when exactly the above substitutions in Which[] are executed (before calling any fun[] or during such calls)?, and are the functions fun1[], fun2[] or fun3[] called directly, or indirectly (by indirectly I mean that first fun[] is called and then it calls any of fun1[], fun2[] or fun3[])? Lesław

POSTED BY: Leslaw Bieniasz
Posted 6 months ago

I think this is cleaner:

chosenFun =
 Switch[
  param,
  1, fun1,
  2, fun2,
  3, fun3,
  _, Null]

You would do something more appropriate than just Null in the fall-through case. If, Switch, and Which are special forms that hold their arguments unevaluated, so you don't need to worry about unexpected evaluation as a side-effect.

But, another way to view this is as a parameterization to your function, like Hans suggested. An alternate way to do that would be:

newFun[1] = fun1;
newFun[2] = fun2;
newFun[3] = fun3;

And now you can just use:

newFun[param][x]
POSTED BY: Eric Rimbey
Posted 6 months ago

Another way using Association

fun1[x_] := x;
fun2[x_] := x^2;
fun3[x_] := x^3;

functions = <|1 -> fun1, 2 -> fun2, 3 -> fun3|>;

functions[3][5]
(* 125 *)
POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract