Message Boards Message Boards

Use InputField for defining a function to be applied to a list?

GROUPS:

Suppose I have a simple list, Range[4]. I apply a function to the members of this list, e.g. f[x_] = 1/x. With the following code I am getting what I expected

In[125]:= f[x_] = 1/x;
Range[4];
Map[f, Range[4]]
Out[127]= {1, 1/2, 1/3, 1/4}

But now I want to use an InputField for defining the function to be applied to the list Range[4]. I then use the following code:.

Panel[DynamicModule[{f = 1/x, f1}, 
Column[{InputField[Dynamic[f]], f1[x_] = Dynamic[f], 
Map[f1, Range[4]]}]]]

Here I started with the function f =1/x, but I can change it in the input field. With the above code I get a panel containing the input box, the definition of the new function f1 to be applied to the list Range[4], and the final result. I had expected this to be {1, 1/2, 1/3,1/4}., but I am getting {1/x,1/x,1/x,1/x}. What am I doing wrong?

POSTED BY: Laurens Wachters
Answer
10 days ago

Laurens, i would try to avoid Map[ ] and use Table[ ] instead. In the input field you can enter something like x*x or Sin[x] or 1/x, this works for example:

Column[{
  InputField[Dynamic[f], Expression]
  , Dynamic@Head[f]
  , Dynamic[f]
  , Dynamic@Table[f, {x, 4}]
  }]
POSTED BY: Raspi Rascal
Answer
10 days ago

Thank you Raspal for your quick answer. But I am afraid I have misled you by taking Range[4] as an example of an arbitrary list. By using this the use of Table works fine. But what about using an arbitrary list? For instance examplelist = {5,3,10,43,3,74,4}?

POSTED BY: Laurens Wachters
Answer
10 days ago
Column[{InputField[Dynamic[f], Expression], Dynamic@Head[f], 
  Dynamic[f], Dynamic@Table[f, {x, {5, 3, 10, 43, 3, 74, 4}}]}]

i am wondering myself why Map[ ] gives us trouble.

POSTED BY: Raspi Rascal
Answer
10 days ago

Laurens,

This does what you want:

ff = 1/x;
ff1[z_] := With[{x = z}, Evaluate[ff]]

Panel[DynamicModule[{}, 
  Column[{InputField[Dynamic[ff]], Dynamic[Map[ff1, Range[4]]]}]]]

Note that the key is to create a function that does not evaluate the right hand side until it is used. This feature is independent of Dynamic.

Regards,

Neil

POSTED BY: Neil Singer
Answer
9 days ago

Thank you very much, Raspi and Neil, for your suggestions. But now I am stuck with the further use of the resulting list. It turns out that I get for its length the value 1. Also Total, Max, and ListPlot don't give proper results. Here is the general setup of a bigger program in which I need the liitle unit that applies a function to a list::

DynamicModule[{list, newlist},
 ff = 1/x;
 ff1[z_] := With[{x = z}, Evaluate[ff]];
 list = {1, 5, 8, 4};
 Row[{Panel[
    Column[{Row[{TextCell["Function "], InputField[Dynamic[ff]],
        newlist = Dynamic[Map[ff1, list]];}],
      newlist,
      Length[newlist],
      Max[newlist]
      }]]}]]

In the bigger program the list of variables in the Dynamic mdoule will contain several dozens of variables. Everything works fine, but in some way I am not able to access the values of the arguments in newlist. You will see that newlist looks as it should be, but Length and Max give nonsense. Is there a special trick to access the values in newlist?

POSTED BY: Laurens Wachters
Answer
9 days ago

I am sorry Neil. I had forgotten a trick you once taught me. Instead of newlist I should use Map[ff1, list]] to calculate Length, etc. So the following code does what i wanted:

DynamicModule[{list, newlist},
 ff = 1/x;
 ff1[z_] := With[{x = z}, Evaluate[ff]];
 list = {1, 5, 8, 4};
 Row[{Panel[
    Column[{Row[{TextCell["Function "], InputField[Dynamic[ff]],
        newlist = Dynamic[Map[ff1, list]];}],
      newlist,
      Dynamic[Length[Map[ff1, list]]],
      Dynamic[Max[Map[ff1, list]]],
      Dynamic[Total[Map[ff1, list]]]
      }]]}]]

I am now going to build this in in my bigger program. It is a bit more awkward than I had hoped for, but I am pretty sure that I will succeed. Thanks again!

POSTED BY: Laurens Wachters
Answer
9 days ago

Group Abstract Group Abstract