Message Boards Message Boards

List layout using Grid

GROUPS:

Hello friends, I am new using Wolfram language and I have a problem. The issue is:

  • I create two lists

    pc = Dynamic [Function [g, Rationalize [g, ct * 0.1]] / @ RandomReal [{- 10, 10}, n + 1]];
    
    qc = Dynamic [Function [g, Rationalize [g, ct * 0.1]] / @ RandomReal [{- 10, 10}, m + 1]];
    
  • m & n are obtained by sliders and ct by means of a radio button

The pc and qc lists seem to be calculated correctly but when I use them in a Grid:

Grid [{pc, qc}, Frame -> All]

Does not seem to recognize them properly. The output is:

Grid [{{8, -2,5,4}, {- 3,7,1,4,5-1}}, Frame-> All]

What am I doing wrong?

POSTED BY: Jorge Manrique
Answer
13 days ago

I would move the definitions pc= and qc= inside Dynamic, so that the Dynamic wrapper does not interfere with Grid:

Slider[Dynamic[m], {1, 10, 1}]
Slider[Dynamic[n], {1, 10, 1}]
RadioButtonBar[Dynamic[ct], 10^-Range[10]]
Dynamic[pc = Rationalize[RandomReal[{-10, 10}, n + 1], ct*0.1]]
Dynamic[qc = Rationalize[RandomReal[{-10, 10}, m + 1], ct*0.1]]
Dynamic[Grid[{pc, qc}, Frame -> All]]
POSTED BY: Gianluca Gorni
Answer
12 days ago

I move pc and qc into Dynamic, as you advise, it works well. But there is another related problem, I will try to explain with the code:

DynamicModule[{pc, n = 3, qc, m = 3, g, ct = 5},
 Column[{
   Row[{
     Column[{
       Slider[Dynamic[n], {1, 8, 1}, Appearance -> "Labeled"],
       Slider[Dynamic[m], {1, 8, 1}, Appearance -> "Labeled"]
       }],
     RadioButtonBar[
      Dynamic[ct], {5 -> Z], 
       1 -> Q]}]}
    ],
   Dynamic[
    pc = Function[g, Rationalize[g, ct*0.1]] /@ 
      RandomReal[{-10, 10}, n + 1]],
   Dynamic[
    qc = Function[g, Rationalize[g, ct*0.1]] /@ 
      RandomReal[{-10, 10}, m + 1]],
   Dynamic[Grid[{pc, qc}, Frame -> All]]
   }
  ]
 ]

If I run the above code, I get:

enter image description here

As expected, but I want to show, as a final result, only the grid. To hide the lists pc and qc I replace commas by semicolons where pc and qc are defined:

Dynamic[pc = 
  Function[g, Rationalize[g, ct*0.1]] /@ RandomReal[{-10, 10}, n + 1]];
Dynamic[qc = 
  Function[g, Rationalize[g, ct*0.1]] /@ RandomReal[{-10, 10}, m + 1]];
Dynamic[Grid[{pc, qc}, Frame -> All]]

But now the result is:

enter image description here

I have previously programmed with functional languages like Haskell but this one has me confused.

Your help is and will be appreciated.

POSTED BY: Jorge Manrique
Answer
12 days ago
Grid[{Sequence @@ pc, Sequence @@ qc}, Frame -> All]
POSTED BY: l van Veen
Answer
12 days ago

Hello I Van, I did what you advised me with a small change:

DynamicModule[{pc, n = 3, qc, m = 3, g, ct = 5},
 Column[{
   Row[{
     Column[{
       Slider[Dynamic[n], {1, 8, 1}, Appearance -> "Labeled"],
       Slider[Dynamic[m], {1, 8, 1}, Appearance -> "Labeled"]
       }],
     RadioButtonBar[
      Dynamic[ct], {5 -> \[DoubleStruckCapitalZ], 
       1 -> \[DoubleStruckCapitalQ]}]}
    ],
   pc = Dynamic[
     Function[g, Rationalize[g, ct*0.1]] /@ 
      RandomReal[{-10, 10}, n + 1]],
   qc = Dynamic[
     Function[g, Rationalize[g, ct*0.1]] /@ 
      RandomReal[{-10, 10}, m + 1]],
   Dynamic[Grid[{Sequence @@ pc, Sequence @@ qc}, Frame -> All]]
   }
  ]
 ]

As you noticed, I wrapped the Grid inside a Dynamic. However I still have a problem, the partial results and the final result do not match:

enter image description here

I guess it's due to the call to RandomReal. But I do not know how to make them match, any use of pc or qc will invoke RandomReal again. I appreciate your help

POSTED BY: Jorge Manrique
Answer
12 days ago

Hi Jorge, I don't understand exactly what you need. Your module will evaluate indeed different numbers after you change the values of m or n. If you want these values to remain the same I suggest you generate random numbers first ( a list of 9 numbers ) and then select these numbers with Part with the n and m. Or is this not what you mean because then you only select from a list. Could you show me what you expect when selecting m and n back and forth?

DynamicModule[{pc, n = 3, qc, m = 3, g, ct = 5, 
  rnd = RandomReal[{-10, 10}, 10]}, 
 Column[{Row[{Column[{Slider[Dynamic[n], {1, 8, 1}, 
        Appearance -> "Labeled"], 
       Slider[Dynamic[m], {1, 8, 1}, Appearance -> "Labeled"]}], 
     RadioButtonBar[
      Dynamic[ct], {5 -> \[DoubleStruckCapitalZ], 
       1 -> \[DoubleStruckCapitalQ]}]}], 
   pc = Dynamic[
     Function[g, Rationalize[g, ct*0.1]] /@ rnd[[;; n + 1]]], 
   qc = Dynamic[
     Function[g, Rationalize[g, ct*0.1]] /@ rnd[[;; m + 1]]], 
   Dynamic[Grid[{Sequence @@ pc, Sequence @@ qc}, Frame -> All]]}]]
POSTED BY: l van Veen
Answer
11 days ago

Hello I van,

I want to generate two queues (qc and pc) of random numbers. Each queue should be generated "just in time" when the user manipulates the corresponding slider.

The queues will be used in a simulation system and the grid is a simplification of the actual output. It is important that the simulation be carried out with the same random numbers shown in the Grid.

Generating a larger list and taking n or m numbers at the time should work but: how long should it be initially? When should it regenerate? and so.

I hope I have explained clearly, thank you for your suggestions and contributions.

POSTED BY: Jorge Manrique
Answer
11 days ago

Hi Jorge, I think you should then use the suggestion from Jorge Manrique . This is what you want. The Set (=) is used inside Dynamic and is the regular way to do it.

DynamicModule[{pc, n = 3, qc, m = 3, g, ct = 5}, 
 Column[{Row[{Column[{Slider[Dynamic[n], {1, 8, 1}, 
        Appearance -> "Labeled"], 
       Slider[Dynamic[m], {1, 8, 1}, Appearance -> "Labeled"]}], 
     RadioButtonBar[
      Dynamic[ct], {5 -> \[DoubleStruckCapitalZ], 
       1 -> \[DoubleStruckCapitalQ]}]}], 
   Dynamic[pc = 
     Function[g, Rationalize[g, ct*0.1]] /@ 
      RandomReal[{-10, 10}, n + 1]], 
   Dynamic[qc = 
     Function[g, Rationalize[g, ct*0.1]] /@ 
      RandomReal[{-10, 10}, m + 1]], 
   Dynamic@Grid[{pc, qc}, Frame -> All]}]]

enter image description here

POSTED BY: l van Veen
Answer
11 days ago

Yes, it works great.

But if I change the commas by semicolons it does not work.

Is there any way to hide the intermediate results in the notebook? I mean just show the Grid

POSTED BY: Jorge Manrique
Answer
11 days ago

How about using Manipulate:

numberList[n_, ct_] := 
  Rationalize[RandomReal[{-10, 10}, n + 1], ct*0.1];
Manipulate[With[{pc = numberList[n, ct], qc = numberList[m, ct]},
  Column[{pc, qc, Grid[{pc, qc}, Frame -> All]}]],
 {n, 1, 8, 1},
 {m, 1, 8, 1},
 {{ct, 5}, {5 -> \[DoubleStruckCapitalZ], 
   1 -> \[DoubleStruckCapitalQ]}}]
POSTED BY: Gianluca Gorni
Answer
10 days ago

Hi Gorni, Yes you are right. I believe Manipulate is the way to go also with all the other stuff Jorge wants to do around it.

POSTED BY: l van Veen
Answer
10 days ago

Hello guys, Gianluca's code does the job.
I confess that I tried to avoid using Manipulate, for me it is very opaque and esoteric.
I still wonder why if I replace commas with semicolons the former code stops working
Semicolons are used to avoid the outs, are they?

Last question, I promise, how can I place the radio button to the right of the sliders?

Thank you very much for your help

POSTED BY: Jorge Manrique
Answer
10 days ago

I think Dynamic triggers only when displayed. With

Clear[a];
Dynamic[a = 1];
Dynamic[a]

the command a=1 has no effect, because it is not displayed. Compare with

Clear[a];
Dynamic[a = 1]
Dynamic[a]

I often get confused too.

POSTED BY: Gianluca Gorni
Answer
9 days ago

The example is revealing.
That is a side effect and functional programming languages should not suffer from that. It is one of its main characteristics.

I will abuse your patience one more time, in Manipulate, can I arrange the controls in a row instead of columns?

POSTED BY: Jorge Manrique
Answer
9 days ago

you may follow this link , like

Manipulate[Plot[Sin[a x + b], {x, 0, 6}], Row[{Control@{a, 1, 4}, Control@{b, 0, 10}}]]

vs

Manipulate[Plot[Sin[a x + b], {x, 0, 6}], {a, 1, 4}, {b, 0, 10}]
POSTED BY: Shenghui Yang
Answer
9 days ago

Thanks Shenguhi,
I used Row [.] but not Control [.]
Now it works

POSTED BY: Jorge Manrique
Answer
8 days ago

Group Abstract Group Abstract