6
$\begingroup$

After I evaluate the following dynamic expression I encounter an unexpected behavior:

DynamicModule[{c, t, main, f},

  Manipulate[

   ControlActive[{x, y}, main[x, y]],

   {{x, c/2, "n1"}, 1, y - c, 1},
   {{y, n - c/2, "n2"}, x + c, n, 1},

   SynchronousUpdating -> False,

   Initialization :> (

     n = 300;

     c = 100;

     main[x_, y_] := main[x, y] = f[x, y];

   )

  ]

]

When this expression is evaluated from a new kernel, the output without having touched any of the sliders is as follows:

enter image description here

Notice how the value of y (labeled "n2") is as expected and integer (250).

After using the second slider (which manipulates dynamic variable y) I get something like:

enter image description here

Notice how now the value of y is not an integer.

Can someone please explain to me what am I doing wrong and/or suggest a workaround?

ps. this is related to this question of mine.

$\endgroup$
  • 2
    $\begingroup$ This seems strongly related: (43889) $\endgroup$ – Lukas Lang Apr 20 '18 at 19:36
  • $\begingroup$ Yeah, it seems like a bug as Mathe172 mentioned but they didn't fix. The only work-around I came up with was to take Integer of y with IntegerPart[y] as he mentioned below. $\endgroup$ – Ron Anderson Apr 20 '18 at 19:58
  • $\begingroup$ I think it should be considered a bug, but I also wish to point out that the dependence on the global symbol n is itself bug-prone. E.g. try n = 10; ...<OP's code>. $\endgroup$ – Michael E2 Apr 20 '18 at 23:47
  • $\begingroup$ @Michael E2 it broke the code when it was local to the module; 'n' is the length of the "Times" of a TimeSeries in the actual application of the code (please, see linked question) $\endgroup$ – yosimitsu kodanuri Apr 21 '18 at 1:21
6
+50
$\begingroup$

It seems like a bug. Here's fix, via explicitly constructing the slider (Manipulator):

n =.;
DynamicModule[{c, t, main, f},
 Manipulate[
  ControlActive[{x, y}, main[x, y]],
  {{x, c/2, "n1"}, 1, y - c, 1},
  {{y, n - c/2, "n2"}, x + c, n, 1, Manipulator[#1, {x + c, n, 1}] &},
  SynchronousUpdating -> False,
  Initialization :> (
    n = 300;
    c = 100;
    main[x_, y_] := main[x, y] = f[x, y];)]]

Mathematica graphics

This isolates, or at least localizes the problem to having a Dynamic lower limit to the slider; the argument x + c automatically has Dynamic[] applied to it by Manipulate to make the slider limits dynamically dependent on x and c:

n =.;
DynamicModule[{c, t, main, f},
 Manipulate[
  ControlActive[{x, y}, main[x, y]],
  {{x, c/2, "n1"}, 1, y - c, 1},
  {{y, n - c/2, "n2"}, x + c, n, 1, Manipulator[#1, {Dynamic[x + c], n, 1}] &},
  SynchronousUpdating -> False,
  Initialization :> (
    n = 300;
    c = 100;
    main[x_, y_] := main[x, y] = f[x, y];)]]

Mathematica graphics

Further isolation:

n = 300;

DynamicModule[{y = 200, c = 100, x = 50},   (* fails with Dynamic 1st argument *)
 {Manipulator[Dynamic[y], {Dynamic[x + c], n, 1}], Dynamic@y}
 ]

Mathematica graphics

DynamicModule[{y = 200, c = 100, x = 50},   (* works with no Dynamic argument *)
 {Manipulator[Dynamic[y], {x + c, n, 1}], Dynamic@y}
 ]

Mathematica graphics

DynamicModule[{y = 200, c = 100, x = 50},   (* works with Dynamic 2nd argument *)
 {Manipulator[Dynamic[y], {x + c, Dynamic@n, 1}], Dynamic@y}
 ]

Mathematica graphics

$\endgroup$
1
$\begingroup$

You can do this:

main[x_, y_] := main[x, y] = f[IntegerPart[x], IntegerPart[y]];)]]

But it still gives real value while slider is in motion but as soon as you lift mouse button it becomes integer.

$\endgroup$
-1
$\begingroup$
DynamicModule[{c, t, main, f},
   Manipulate[
       ControlActive[{x, y}, main[x, y]],
        {{x, c/2, "n1"}, 1, y - c, 1},
        {{y, n - c/2, "n2"}, x + c, n, 1},
        SynchronousUpdating -> False,
        Initialization :> (
          n = 300;
          c = 100;
          main[x_, y_] := main[x, y] = f[x, y];)
   ]
]
$\endgroup$
  • $\begingroup$ Please, read the code formatting guidelines in the help centre and re-format your code. $\endgroup$ – Sektor Apr 25 '18 at 21:14
  • $\begingroup$ Hey @Oleg Blumenthal, thanks for the answer. I am reading your code and I fail to discern significant differences to what @Michael E2 already proposed. Please, point me to the right direction, what am I missing? $\endgroup$ – yosimitsu kodanuri Apr 26 '18 at 6:35
  • $\begingroup$ @yosimitsukodanuri Actually, it is identical to your original code. -- Oleg, maybe the wrong code was posted? $\endgroup$ – Michael E2 Apr 26 '18 at 19:58

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.