0
$\begingroup$

I am working on creating a CDF file for a calculation where I do high-precision evaluation of functions such as N[func,40]. I wrote a Module that takes user function and rationalize it, this way I create a function with user input accuracy. (Shown Below). When I don't use an inputfield my module works well but with inputfield I can't prevent it to be evaluated.

enter image description here

As you can see I need to avoid the evaluation so that I can evaluate the function in desired accuracy (number of digits). Any idea how I can do this?

Thank you

Erdem

Panel[DynamicModule[{fun = 2.1 Sin[Pi z/2.1]}, Column[{

Row[{Style["Function ", 12, Blue, Editable -> False], 
  InputField[HoldForm[Dynamic[fun]], FieldSize -> {55, 3}, 
   BaseStyle -> {12}]}, Spacer[1]]
, Button[Style["Calculate", 14, Green, Editable -> False], 
 Res = upAC[fun]],
Row[{Style["Result ", 12, Red, Editable -> False], 
  InputField[Dynamic[Res], FieldSize -> {52, 2}, 
   BaseStyle -> {12}]}, Spacer[5]]
}],
Initialization :> (upAC[in_] := 
  Module[{out}, 
   out = If[Accuracy[in] == \[Infinity], in, 
     ReleaseHold[Rationalize[in, 10^-Accuracy[in]]]];
   Return[out]];)
]]

Asked in http://community.wolfram.com/groups/-/m/t/978300

$\endgroup$
8
  • $\begingroup$ Please edit both this post and the one you made on Wolfram Community and cross-reference them. $\endgroup$
    – Szabolcs
    Dec 12, 2016 at 18:16
  • $\begingroup$ InputField takes a form as its second argument. One possible form is Hold[Expression] which simply holds the expression you input. Will this work for you? $\endgroup$
    – b3m2a1
    Dec 12, 2016 at 18:27
  • $\begingroup$ When I do InputField[Dynamic[Hold[fun]] I can't access to the input field. $\endgroup$
    – Erdem
    Dec 12, 2016 at 18:32
  • $\begingroup$ Thanks! This is so that people who would answer on one site will be aware of any existing discussion on the other site. $\endgroup$
    – Szabolcs
    Dec 12, 2016 at 18:35
  • $\begingroup$ If you add HoldForm to fun definition inside DynamicModule and remove HoldForm from InputField, do you get what you want? $\endgroup$
    – Stitch
    Dec 12, 2016 at 19:10

1 Answer 1

2
$\begingroup$

Please tell me if this is what you want, and if not in which way it fails:

Panel[DynamicModule[{fun = Hold[2.1 Sin[Pi z/2.1]], Res = upAC[fun]}, 
  Column[{Row[{Style["Function ", 12, Blue, Editable -> False], 
      InputField[Dynamic[fun], Hold[Expression], FieldSize -> {55, 3}, 
       BaseStyle -> {12}]}, Spacer[1]], 
    Button[Style["Calculate", 14, Green, Editable -> False], Res = upAC[fun]], 
    Row[{Style["Result ", 12, Red, Editable -> False], 
      InputField[Dynamic[Res], FieldSize -> {52, 2}, BaseStyle -> {12}]}, 
     Spacer[5]]}], 
  Initialization :> (upAC[Hold[in_]] := 
      Module[{out}, 
       out = If[Accuracy[Unevaluated[in]] == ∞, in, 
         Rationalize[Defer[in], 10^-Accuracy[in]]];
       Return[out]];)]]
$\endgroup$
4
  • 1
    $\begingroup$ Yes it does work. Thank you. $\endgroup$
    – Erdem
    Dec 12, 2016 at 20:04
  • $\begingroup$ @Mr.Wizard Great! One question. Shouldn't we have HoldForm around the RHS in the initialization of fun to make sure the very first evaluation of the Panel produces the correct expression in the input form? $\endgroup$
    – Stitch
    Dec 12, 2016 at 22:30
  • $\begingroup$ @Stitch Good point. I believe it needs to be Hold rather than HoldForm to work as desired. I included this in my answer now. $\endgroup$
    – Mr.Wizard
    Dec 13, 2016 at 5:24
  • $\begingroup$ Thank you for the update. It is a little detail but important. $\endgroup$
    – Erdem
    Dec 13, 2016 at 20:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.