# Simulation using Manipulate within Manipulate

Suppose there is a list of matrices (say m matrices), matC. The first element of this list is considered as the Benchmark matrix with which the other $$(m-1)$$ matrices will be compared. One may call the other matrices as Alternative choices. I have the following code (Manipulate within Manipulate) to perform the comparison.

Manipulate[
SeedRandom[s];
G = RandomGraph[{Round[n], Round[n*(n - 1)*d]},
DirectedEdges -> True];
a = RandomReal[{0, 1}, {m, n}];
t = RandomReal[{0, 1}, {m, n}];
matC = Table[(# - DiagonalMatrix[Diagonal[#]]) &[
KroneckerProduct[a[[i]], t[[i]]]], {i, 1, m}];
Manipulate[
(* Problem 1: With Mathematica version 10: this With does not work. *)
With[
{wG = AdjacencyMatrix[G]*mat},
With[{sqwG = wG.wG},
Grid[{{Text["Matrix_wG"], Text["Square_sqwG"]},
MatrixForm /@ {wG, sqwG}}]]
],
(* Problem 2: This slider is not working. *)
{mat, matC, Slider}
],
{{s, 1, "s"}, 1, 100, 1},
{{m, 20, "m"}, 1, 20, 5},
{{n, 10, "n"}, 5, 20, 5},
{{d, 0.1, "d"}, 0, 1, 0.05}
]


The logic behing the above code is that I want to fix the values of the variables within the first Manipulate, such as fix m, n, a, t, etc. To do that, I created a slider for SeedRandom[s]. Then, in the second Manipulate I perform the comparison of Benchmark with the Alternatives through a slider for the variable matC created in the first Manipulate.

Questions:

1. Does Manipulate within Manipulate actually do what I just described above? Efficiency is not relevant in this question.
2. Is Manipulate within Manipulate an efficient way of performing the comparison concerned? If not, can you guide me to find the efficient ways?
3. I have two other technical problems with the above code, which are named inside the code. Can you also help me to solve these problems?

Note: I am using Mathematica 10.

## 1 Answer

Manipulate[SeedRandom[s];
G = RandomGraph[{Round[n], Round[n*(n - 1)*d]}, DirectedEdges -> True];
a = RandomReal[{0, 1}, {m, n}];
t = RandomReal[{0, 1}, {m, n}];
matC = Table[(# - DiagonalMatrix[Diagonal[#]]) &[KroneckerProduct[a[[i]], t[[i]]]],
{i, 1, m}];
Manipulate[With[{wG = AdjacencyMatrix[G]*matC[[mat]]}, With[{sqwG = wG.wG},
Grid[{{Text["Matrix_wG"], Text["Square_sqwG"]}, MatrixForm /@ {wG, sqwG}}]]],
{mat, 1, Length @ matC, 1, Manipulator}],
{{s, 1, "s"}, 1, 100, 1}, {{m, 20, "m"}, 1, 20, 5}, {{n, 10, "n"}, 5, 20, 5},
{{d, 0.1, "d"}, 0, 1, 0.05}, {matC, None}, {G, None}, {a, None}, {t, None}] • I am amazed with this speed of work. It works very nice. I wonder what {matC,None}, {G, None} etc does because changing parameters such as n or m already force a change in G, matC and so on. Is that related to the problem of continuous updating of the variables in the first Manipulate? – Tugrul Temel Oct 4 '18 at 23:24
• @Tebernus, if current value of mat is,say, 16, and m is 20, and m is reduced to 10 then an error message is issued (because mat becomes out of range). {x, None} trick also helps keep all variables inside Manipulate (without it G, t, matC ... would be global symbols). – kglr Oct 4 '18 at 23:34
• Now I learned a new trick from you, a very useful piece of information that will help me in my current work. Many thanks. – Tugrul Temel Oct 4 '18 at 23:47
• Tebernus, you are most welcome. – kglr Oct 4 '18 at 23:48