0
$\begingroup$

I am trying to give options to a user to enter an advance options to a calculation. Like doing NIntegrate[f,{z,1,4},WorkingPrecision->40] this works without WorkingPrecision and more options. For example

Dummyfunc[f_, g_, din_] := Module[{d,h},

If[din == {}, d = 16, d = din];

h = SetAccuracy[f g, d];

Return[h]];
f = 2/3;
g = 7/6;
test1 = Dummyfunc[f, g];
test2 = Dummyfunc[f, g, 50];

In here I want accuracy default 16 if the user doesn't enter any, but if the user enters it will use that for SetAccuracy.

How can I do that?

Thank you

Update with OptionPattern

Dummyfunc[f_, g_, OptionsPattern[{}]] := Module[{d},

If[OptionValue == {}, d = 16, d = OptionValue];

h = SetAccuracy[f g, d];

Return[h]];
f = 2/3;
g = 7/6;
test1 = Dummyfunc[f, g];
test2 = Dummyfunc[f, g, 50];
Print[test1];
Print[test2];

This also fails.

$\endgroup$
  • $\begingroup$ If you want your function to have options use OptionsPattern. More on that here reference.wolfram.com/language/ref/OptionsPattern.html $\endgroup$ – Lotus Oct 5 '17 at 9:25
  • $\begingroup$ Thank you but I could not make it work. Using OptionsPattern with no default options. $\endgroup$ – Erdem Oct 5 '17 at 9:38
  • $\begingroup$ Sorry. My bad. The simplest way seems to be to just use the optional argument pattern. Please see my answer below. $\endgroup$ – Lotus Oct 5 '17 at 9:48
  • $\begingroup$ I suggest you read through this tutorial. $\endgroup$ – Szabolcs Oct 5 '17 at 12:32
2
$\begingroup$
Dummyfunc[f_, g_, din_: 16] := Module[{d = din},
   h = SetAccuracy[f g, d];
   Return[h]];
f = 2/3;
g = 7/6;
test1 = Dummyfunc[f, g];
test2 = Dummyfunc[f, g, 50];
Print[test1];
Print[test2];

(* Outputs *)

0.777777777777778

0.77777777777777777777777777777777777777777777777778
$\endgroup$
  • $\begingroup$ Thanks it works beautifully. $\endgroup$ – Erdem Oct 5 '17 at 11:13

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.