TransWikia.com

How to define a function reminding of names of the independent variables?

Mathematica Asked by Alexei Boulbitch on January 4, 2021

I am using a function with a lot of variables. There are 9 and sometimes more of them. In this case, it is difficult to memorize, which one stays on the, say, fourth position, and which one – on the seventh. I would like to be able to include the variable name into the square brackets of the function as a reminder: what must stay here.
For example, let us define a simple function: f(x)=x^2+y^3. Here is its definition:

f[x_,y_]:=x^2+y^3;

One can, however, call it with x=5 and y=7 as follows:

f[(*x=*)5,(*y=*)7]

This way reminds me of what should stay in the first and in the second position inside the square brackets.
This works, but I do not like that it looks cumbersome. For perception, it would be much better, if the (* and *) signs are invisible.

Any idea?

3 Answers

You can get the behavior you ask for if you define your function to work with an association. The key-value paradigm provides syntax close but not exactly as you describe.

Simple approach

f[a_Association] := a[x]^2 + a[y]^3
f[<|x -> 5, y -> 7|>]

368

Argument keys are not affected by global assignments. Changing the order of the key-value pairs has no effect.

x = 42; f[<|y -> 7, x -> 5|>]

368

Behavior when given bad keys is a little messy.

f[<|a -> 5, b -> 7|>]

Missing["KeyAbsent",x]^2+Missing["KeyAbsent",y]^3

Strict approach

Clear[x,y]
g[a : <|x -> _, y -> _|>] := a[x]^2 + a[y]^3
g[<|x -> 5, y -> 7|>]

368

Order matters.

g[<|y -> 7, x -> 5|>]
g[<|y -> 7, x -> 5|>]

Bad keys simply prevent evaluation.

g[<|a -> 5, b -> 7|>]
g[<|a -> 5, b -> 7|>]

Keys are not protected from global assignments, but evaluation doesn't occur. You could fix this by making the keys strings (e.g., "x" in place of x), but typing strings is more time comsuming.

x = 42; g[<|x -> 5, y -> 7|>]
g[<|42 -> 5, y -> 7|>]

Answered by m_goldberg on January 4, 2021

If this is just to help entering values in the correct order (and not to be permanently visible), you can possibly use a Placeholder. Reevaluating phf below will produce the template again: just type phf, select it, right-click and choose Evaluate in Place.

f[x_, y_] := x^2 + y^2
phf = Defer[f[Placeholder[x], Placeholder[y]]]

enter image description here

Answered by Jean-Pierre on January 4, 2021

Here’s a couple of interesting methods:

f1[x_,y_]:=With[
{
$x=x/.Equal[$arg_,$val_]:>$val,
$y=y/.Equal[$arg_,$val_]:>$val
},
$x^2+$y^3
]
f2[X_,Y_]:=With[
{
$x=X/.Equal[$arg_,$val_]:>Rule[$arg,$val],
$y=Y/.Equal[$arg_,$val_]:>Rule[$arg,$val]
},
ClearAll[$assoc];
$a=Association[{$x,$y}];
$a[x]^2+$a[y]^3
]

Both carry the same syntax:

f1[x==5,y==3]
f2[x==5,y==3]

(*52*)
(*52*)

While neither do exactly what you might want to do, each offers a unique aspect that you might be able to pull from!

These methods do fall apart if one or more of the variables has already been assigned globally. Inspired by the answer from user @m_goldberg, find this method:

f3[X_,Y_]:=With[
{
$a=Association[{X,Y}]
},
$a[x]^2+$a[y]^3
]
x=4;
y=7;
f3[x->5,y->3]

(*52*)

Which is robust against globally assigned values of the arguments used.

This also does work:

f[__]:=x^2+y^3;
f[x=5,y=3]

(*52*)

Answered by CA Trevillian on January 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP