mathematica manipulate with macros AND variable number of controls (persistence desired)
===

[this post][1] about avoiding code duplication in mathematica/manipulate helped a lot getting as far as I did. The method of "macros" outlined there works excellent, but when the number of controls is again controlled by yet another manipulate control, things get difficult.

I got 2 closely related problems in that area:

**Problem 1:** The following works like a charm:

    With[{rows = 3, cols = 3, fieldSize = 5},
      Manipulate[
        Column[{
          TableForm@mat,
          Row[{"det = ", Det[mat]}]
        }]
        , {mat, None}, {initialized, None}
        , Evaluate@With[{makeRow = Function[{rowIndex},
            Map[Function[{colIndex}
                ,InputField[Dynamic[mat[[rowIndex, colIndex]]], Number, FieldSize -> fieldSize]]
			  , Range[cols]]
            , HoldAll]}
        , Grid[Map[makeRow, Range[rows]]]]
        , Initialization :> (
          If[Not[initialized === True], mat = Table[0, {rows}, {cols}]];
          initialized = True
        )
        , SaveDefinitions -> True
	]]

I'm a new user, can't post images, here it is http://mathheadinclouds.com/images/detWorking.jpg

I can enter a matrix, and Manipulate will calculate the determinant.  
If I start a new session, the values from the last session will still be there (important).  
But now suppose, I want to specify the matrix size with a setter bar.  
This can be done by wrapping a manipulate around it.

    Manipulate[
      With[{rows = size, cols = size, fieldSize = 5},
        Manipulate[
          Column[{TableForm@mat, Row[{"det = ", Det[mat]}]}]
          , {mat, None}, {initialized, None}
          , Evaluate@With[{makeRow = Function[{rowIndex},
              Map[Function[{colIndex}, InputField[Dynamic[mat[[rowIndex, colIndex]]], Number, FieldSize -> fieldSize]], Range[cols]]
              , HoldAll]}
            , Grid[Map[makeRow, Range[rows]]]]
          , Initialization :> (
            If[Not[initialized === True], mat = Table[0, {rows}, {cols}]];
            initialized = True
          ), SaveDefinitions -> True]]
        , {{size, 3}, Range[7], ControlType -> SetterBar}
    ]

But this breaks the persistence over sessions.  
I tried to put the variable mat into the outer Manipulate, but that broke the whole thing.



I'm a new user, can't post images, here it is see above / detNotWorking.jpg


**Problem 2:** Kind of the same thing, with sliders instead of input fields.  
I want to use a combination of setter bars and sliders to input a discrete probability distribution, and then do stuff with it within a manipulate.  
IMPORTANT: The values should be persistent over sessions.


I'm a new user, can't post images, here it is mathheadinclouds.com/images/probDist.jpg

Via the setter bar, I indicate that I want, for example, a prob dist for the integers from 2 through 5.  
That should then bring up 4 sliders that I can use to define the prob dist, one for each number.  
So the number of sliders should be variable.  
As you can see, I solved the problem of writing a macro for the slider generation, but
I failed to make the number of sliders dependent on the inputs from the setter bars.  
It's clear from the code how I tried to solve the problem: The code, as it is, does everything I want, except that
the number of sliders is always the same; and if you change one of the "lifeIsEasy" variables, it will seize to work.


    With[{slidersPerRow = 3, lifeIsEasy1 = False, lifeIsEasy2 = False, 
      length = 8
      , normalize = Function[{pd, from, to}
        , With[{total = Total[pd[[Range[from, to]]]], len = to - from + 1}
         , If[total == 0, Table[1/len, {len}], 
          pd[[Range[from, to]]]/total]]]},
     Manipulate[
      If[max < min, max = min];
      Column[{
        Row[{"min ", min, "   max ", max}]
        , Row[{"=== slider values ==="}]
        , TableForm[{sliderVals}, TableHeadings -> {None, Range[length]}]
        , Row[{"=== prob dist ==="}]
        , TableForm[{normalize[sliderVals, min, max]}, 
         TableHeadings -> {None, Range[min, max]}]
        , Row[{"expectation: ", 
          normalize[sliderVals, min, max] . Range[min, max]}]
        }]
      , {initialized, None}
      , {sliderVals, None}
      , Row[{
        Control[{{min, 1}, Range[4], ControlType -> SetterBar}]
        , Spacer[25]
        , Control[{{max, min + 3}, Range[min, min + 4], 
          ControlType -> SetterBar}]
        }]
      , Row[{"I want ", Dynamic[max - min + 1], " sliders in ", 
        Dynamic@Ceiling[(max - min + 1)/slidersPerRow], " rows, "
        , slidersPerRow, " per Row, and ", 
        Dynamic@Mod[max - min + 1, slidersPerRow, 1], " in the last" }]
      , Evaluate@With[{makeSl = Function[{index}
           , Dynamic@
            Row[{ToString[index] <> " ", 
              Manipulator[Dynamic[sliderVals[[index]]]]}]
           , HoldAll]}
        , With[{
          makeSliderRow = Function[{startIndex, endIndex}
            , Row[Map[makeSl, Range[startIndex, endIndex]], Spacer[10]]]
          }
         , Sequence @@ Table[
           makeSliderRow[
            min + slidersPerRow*sliderRow(*=startIndex*)
            , Min[Join[{min + slidersPerRow - 1 + slidersPerRow*sliderRow}, If[lifeIsEasy1, max, {}]]]](*=endIndex*)  
            , {sliderRow, 0, If[lifeIsEasy2, Ceiling[(max - min + 1)/slidersPerRow] - 1, 1]}
            ](*seqtab*)
         ](*with*)
        ](*evalwith*)
      , Initialization :> (
        If[Not[initialized === True], sliderVals = Table[0, {length}]];
        initialized = True
        )
      , SaveDefinitions -> True
      ](*manip*)
     ](*with*)


Another approach I had for the problem was to use an inner Manipulate, but that didn't work either;  
I didn't get that even to the point where everything except persistence works, as in problem 1.

Thanks.

  [1]: http://stackoverflow.com/questions/7551647/how-to-define-part-of-a-manipulate-control-variable-definition-to-reduce-code-du
[2]: http://mathheadinclouds.com/images/detWorking.jpg
[3]: http://mathheadinclouds.com/images/detNotWorking.jpg
[4]: http://mathheadinclouds.com/images/probDist.jpg

