1
$\begingroup$

I am creating a Manipulate that evaluates a function that takes an arbitrary number of binary control variables. For example, the Manipulate takes in three control variables {a,b,c} and returns the function f[ls_]:=DeleteDuplicates[ls]. Some minimal working examples show different ways to accomplish this:

f[ls_] := DeleteDuplicates[ls];

m1[controls_] := Manipulate[f[controls], 
    Grid[{Map[Control[{{#, 1}, {0, 1}}] &, controls]}]];

m2[controls_] := Manipulate[f[controls], 
    Grid[{Map[Function[x, Evaluate[Control[{{x, 1}, {0, 1}}]]],
    controls]}]];

m3[controls_] := Manipulate @@ {f[controls], 
    Grid[{Map[Control[{{#, 1}, {0, 1}}] &, controls]}]};

m1[{a,b,c}] // Print;
m2[{a,b,c}] // Print;
m3[{a,b,c}] // Print;

As might be expected, function m1 returns the error that the argument specifying the control variables does not have the right form. To rectify this, function m2 uses Function[x,...] instead of a pure function. But what happens here is the control variable x gets defined repeatedly. Function m3 rectifies this by using @@, but when f gets evaluated it isn't able to figure out that a=b=c, so returns the strange result that DeleteDuplicates[{a,b,c}]={1,1,1}.

Output is shown below. The desired output in this case is {1}.

Output

What's going on here? Any way around this?

$\endgroup$
0
$\begingroup$

Manipulate has attribute HoldAll, so it maintains its arguments in unevaluated form. While still unevaluated, your control definitions are not in any of the expected formats, and so do not appear correct; you need to force evaluation of your control definitions within Manipulate using Evaluate. In fact, the fact that the contents of the list were evaluated before Manipulate was applied to it was the reason that m3 worked, at least as far as generating a Manipulate object.

Here is a new definition that should achieve what you want:

Clear[f, m4]
f[ls_] := DeleteDuplicates[ls]
m4[controls_] := 
   Manipulate[f[controls], Evaluate@Row[Map[Control[{{#, 1}, {0, 1}}] &, controls]]]

m4[{a, b, c}]

Mathematica graphics

.. and after toggling the value of e.g. c:

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.