0
$\begingroup$

Here's as simple an example as I can construct to represent the issue. The user provides 3 values: var1, var2, var3. I use Symbol here because, in the general case, the user can change the number of variables. I use precision of 20 here, but in the general case can require higher precision. The Manipulate collect the values of the 3 variables. If the values do not satisfy the condition that

SetPrecision[var1, precision] + SetPrecision[var2, precision] - SetPrecision[var3,precision] >= 0,

it should show "Condition violated". Otherwise, it captures those values in rules

output = {var[1] -> SetPrecision[var1, precision], var[2] -> SetPrecision[var2, precision], var[3] -> SetPrecision[var3, precision]}

and displays the result

var[1]+var[2]-var[3]>=0/.output

How is it possible that it displays "False" when var3 is set to the upper limit? Aren't the two tests identical?

With[{vars = Table[With[{i=i}, Symbol[ToString[Row[{"var",i}]]]],{i, 1, 3}],
      precision = 20},
     DynamicModule[{},
           Manipulate@@Join[{(* The output *)
                             If[(* If the values satisfy var[1]+var[2]-var[3] >= 0, ...*)
                                SetPrecision[vars[[1]], precision]+SetPrecision[vars[[2]],precision]-SetPrecision[vars[[3]], precision]>=0,
                                output = Table[With[{i=i}, Rule[var[i],SetPrecision[vars[[i]], precision]]], {i, 1, 3}]; (*... set the values *)
                                var[1]+var[2]-var[3]>=0/.output, (* And test the condition: var[1]+var[2]-var[3] >= 0 *)
                               "Condition violated"]}, (* Otherwise, report the condition is violated *)
                             (* The controls *)
                             Table[With[{i = i},                                         (* Build the condition into the controls *)
                                        {{vars[[i]], 1/100, Row[{"var[", i,"]"}]}, 1/1000, If[i==3, SetPrecision[vars[[1]],precision]+SetPrecision[vars[[2]],precision], 1], Appearance->"Labeled"}], {i, 1, 3}]]]]

False result

This seems related to SetPrecision with Manipulate using variable number of controls

$\endgroup$
3
  • $\begingroup$ Why you call SetPresition so many times? SetPrecision[var1 + var2 - var3, precision] >= 0 is not good for you? $\endgroup$ Commented Dec 17, 2024 at 5:24
  • $\begingroup$ A.Kato, thanks for your attention. I use SetPrecision on each term to ensure the condition is the same when testing and using the three variables. The rules in output capture their values and I want to ensure that the values used in subsequent computations satisfy the condition var1+ var2 - var3 >= 0. $\endgroup$ Commented Dec 18, 2024 at 15:02
  • $\begingroup$ I admit to begin very confused by your code, but I suspect that the problem comes from Manipulate@@. Manipulate, for obvious reasons, needs to very carefully control evaluation of its arguments, and in fact Manipulate has the HoldAll attribute. But you're delaying when Manipulate "sees" its arguments by using Apply (aka @@). Everything in the List after Manipulate@@ is being evaluated before the eventual Manipulate expression is evaluated. $\endgroup$ Commented Mar 21 at 18:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.