2
$\begingroup$

I have the following list of rules:

minru = {"ksp" -> 0.3393, "pl" -> 0.3675, "qtz" -> 0.1628, 
         "hb" -> 0.0714552, "bi" -> 0.0476368, "sph" -> 0.006268, 
         "zrc" -> 0.000672, "ilm" -> 0.000168, "ap" -> 0.000348, 
         "aln" -> 0.000812, "cal" -> 0.002, "fl" -> 0.003}

I want to use this to make a Manipulate-able list as output. I want to turn the keys (rhs) into a set of variables and use the values (lhs) to define the range and initial parameters for the variables. I'm hoping to get a list of adjustable, normalised values within a manipulate frame with a slider for each input variable.

Here is the kind of thing I'm trying to achieve (minus the programatic variable definition):

Manipulate[
 Module[{sum, vars},
  sum = x + y + z;
  vars = {x/sum, y/sum, z/sum}],
    {{x, 0.33}, 0.001, 0.5, Appearance -> "Open"}, 
    {{y, 0.33}, 0.001, 0.5, Appearance -> "Open"}, 
    {{z, 0.33}, 0.001, 0.5, Appearance -> "Open"}]

What I've tried to far:

mantest[propru_] :=
 Manipulate[
  Module[{manvars, tot},
   manvars = ToExpression[propru[[All, 1]]];
   tot = Total[manvars];
   (#/tot) & /@ manvars
   ],
  Evaluate[Sequence @@ 
   ReplaceList[propru, 
    KeyValuePattern[{x_ -> y_}] :> 
             {{ToExpression[x], y}, y - 0.5 * y, y + 0.5 * y}]]
  ]
mantest[minru]

Alas, Mathematica says "no"...

This question got me some of the way but the variable parameters (from lhs) still aren't working....

Any suggestions on how to do this? Do need to use Hold somewhere?

$\endgroup$
  • 1
    $\begingroup$ does this give what you need: mantest[propru_] :=DynamicModule[{manvars=Symbol/@Keys[propru], values=Values@propru },Manipulate[Evaluate@Normalize[manvars ,Total],Evaluate[Sequence@@({{##},(1-.5)#2,(1+.5)#2}&@@@Transpose[{manvars, values}])] ]]; mantest[minru]? ` $\endgroup$ – kglr Nov 6 '18 at 1:45
  • $\begingroup$ Do you want to apply a function to all values like Normalize[vars, Totla] or do you want to be able to use them separately e.g. ksp + aln. $\endgroup$ – Kuba Nov 6 '18 at 10:21
  • $\begingroup$ @kglr yes, that works nicely! If you have time, perhaps you should make it into an answer and perhaps explain why this works where my approach failed. Many Thanks! $\endgroup$ – geordie Nov 6 '18 at 21:57
  • $\begingroup$ @Kuba I not quite sure I understand. I'm aiming to perform a simple normalisation of the form var_i / sum(var). Does this make sense? $\endgroup$ – geordie Nov 6 '18 at 21:59
2
$\begingroup$
mantest[propru_] := DynamicModule[{manvars = Symbol /@ Keys[propru], 
 values = Values @ propru }, 
 Manipulate[Evaluate @ Normalize[manvars, Total],
 Evaluate[Sequence@@({{##}, (1-.5)#2, (1+.5)#2} & @@@ Transpose[{manvars, values}])]]];

mantest[minru]

enter image description here

$\endgroup$
  • $\begingroup$ Hopefully a quick follow question: how do I define the list so that i ca n use it as a dynamic list object in other contexts? i.e. How do I use the resulting dynamic list outside of the manipulate wrapper? $\endgroup$ – geordie Nov 6 '18 at 23:19

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.