2
$\begingroup$

So here is my problem. I am writing a software with mathematica, that uses an external package as a resource. They are some libraries for numerical integration, the CUBA libraries. http://www.feynarts.de/cuba/ The name of the function I use is Suave. It is essentially a better version of NIntegrate. Like NIntegrate, it has several options. Now, as I use it many times in my code, and always using the same options, I found this way to save the options and then plug them in at each use

$CUBAOptions={PrecisionGoal->$cubaintegralprecision,AccuracyGoal->$cubaintegralaccuracy,MinPoints->$MinPointsCUBA,MaxPoints->$MaxPointsCUBA,Verbose->$SOutput,PseudoRandom->$PseudoRandomC,Compiled->$CompiledC};

$SSpecOptions={NNew->$NIncreaseS/10,Flatness->$FlatnessS};

$SOptions=Join[$CUBAOptions,$SSpecOptions];

Install["Suave"];

MyFunctionS[m_]:=Suave[Integrand[r,Eu,s,t,m],{r,ereg,rmax},{Eu,1,EumaxE[muf[r],B[r],meff[r]]},{s,sminE[Eu,m,B[r],meff[r]],smaxE[Eu,m,B[r],meff[r]]},{t,tminE[s,m,meff[r],muf[r]],tmaxE},##&@@$SOptions];

where the last line is present many times in the code, this is just one of the repeated used, all similar.

Now with this approach, I get some shadowing warning messages when I load the code with definitions (not when I use it, just on load). It says that the option names (like Flatness, NNew and so on...are present both in global and in CUBA context.

I hoped I could solve the problem by just inserting e "remove" before "Install", but after I do that, I get another problem

Message[Suave::optx, Removed["Flatness"], Suave]

Any suggestion on how to deal with this issue? I would like to still be able to define the options in a central, compact way, but without incurring in shadowing problems.

EDIT: I forgot to clarify a couple of differences between CUBA/Suave and NIntegrate.

This code works with NIntegrate:

MyOptions={"AccuracyGoal"->1};
NIntegrate[1,{x,0,1}, Evaluate@MyOptions]

However the following 3 examples do NOT work with Suave

MyOptions={"AccuracyGoal"->1};
Suave[1,{x,0,1}, Evaluate@MyOptions]
Suave[1,{x,0,1},{AccuracyGoal->1}]
Suave[1,{x,0,1},"AccuracyGoal"->1]

The first two just return as output the input function, unevaluated. The third one says unknown option

$\endgroup$
6
  • $\begingroup$ You don't have to use ##&, there is Sequence. Also you can usually just pass in the options as a list and it works just fine. $\endgroup$
    – Jason B.
    Commented May 21, 2021 at 1:26
  • $\begingroup$ For the option names just use strings like "NNew", they will work just the same. But if symbols like $cubaintegralprecision are defined in the package you need to use their fully-qualified name - including context. $\endgroup$
    – Jason B.
    Commented May 21, 2021 at 2:28
  • $\begingroup$ it does not seem that easy. There are 2 problems/differences between Suave and NIntegrate. For Nintegrate I can write NIntegrate[1,{x,0,1},Evaluate@MyOptions], Where MyOptions={"option1"->value1}; For Suave this does not work. First, it seems not to accept options in a list, like Suave[1,{x,0,1},{option1->value1}]. Second, It does not recognise the option if it is inside "", like Suave[1,{x,0,1},"AccuracyGoal"->3]. Could you give an example of code that solves these problems? $\endgroup$ Commented May 21, 2021 at 9:23
  • $\begingroup$ It might be that the "Suave" package is older and does not use the OptionsPattern and OptionValue functions that make everything work like I describe above. If you evaluate Install["Suave"]; first before reading in the definitions of $SOptions, then everything works correctly is that right? $\endgroup$
    – Jason B.
    Commented May 21, 2021 at 11:53
  • $\begingroup$ Run Install (which effectively "loads" the package) before using any CUBA-provided option names. It appears to me that this is a case of trying to use a package before having loaded it, except that the concept of "loading" is a bit unusual in this case (done through Install). $\endgroup$
    – Szabolcs
    Commented May 21, 2021 at 11:59

1 Answer 1

1
$\begingroup$

I finally found the solution All this code is wrapped inside a big If. I downlaoded the source code of the package and I found some example notebook that were setting option using SetOptions. I tried to do that both before

If[$useCUBA,
$CUBAOptions={PrecisionGoal->$cubaintegralprecision,AccuracyGoal->$cubaintegralaccuracy};
$SSpecOptions={NNew->$NIncreaseS/10,Flatness->$FlatnessS};
$SOptions=Join[$CUBAOptions,$SSpecOptions];

SetOptions[Suave,$SOptions];

Install["Suave"];

MyFunctionS[m_]:=Suave[Integrand[r,Eu,s,t,m],{r,ereg,rmax},{Eu,1,EumaxE[muf[r],B[r],meff[r]]},{s,sminE[Eu,m,B[r],meff[r]],smaxE[Eu,m,B[r],meff[r]]},{t,tminE[s,m,meff[r],muf[r]],tmaxE}];

];

and after install

If[$useCUBA,
$CUBAOptions={PrecisionGoal->$cubaintegralprecision,AccuracyGoal->$cubaintegralaccuracy};
$SSpecOptions={NNew->$NIncreaseS/10,Flatness->$FlatnessS};
$SOptions=Join[$CUBAOptions,$SSpecOptions];

Install["Suave"];

SetOptions[Suave,$SOptions];

MyFunctionS[m_]:=Suave[Integrand[r,Eu,s,t,m],{r,ereg,rmax},{Eu,1,EumaxE[muf[r],B[r],meff[r]]},{s,sminE[Eu,m,B[r],meff[r]],smaxE[Eu,m,B[r],meff[r]]},{t,tminE[s,m,meff[r],muf[r]],tmaxE}];

];
  • no luck. Then I tried to place another IF - with same condition, outside the if with the installs, and put the SetOptions commands there.
If[$useCUBA,
>     $CUBAOptions={PrecisionGoal->$cubaintegralprecision,...};
>     $SSpecOptions={NNew->$NIncreaseS/10,Flatness->$FlatnessS};
$SOptions=Join[$CUBAOptions,$SSpecOptions];
>     
>     Install["Suave"];
>     
>     ];
>     If[$useCUBA,
SetOptions[Suave,$SOptions];
MyFunctionS[m_]:=Suave[...];
];

It worked. Somehow the code after install was evaluated before finishing to execute the install command. This is something related to strange behaviour with mathematica since v12.2

$\endgroup$
1
  • 1
    $\begingroup$ I think this happens because Install affects the context and contextpath. When the parser reads the big If statement, it doesn't know that Suave is supposed to go in a different context, so it's put in a different context than you're expecting. You can also fix this by writing Suave in it's full context form (just evaluate Context[Suave] to find out it's context). $\endgroup$ Commented May 21, 2021 at 15:26

Your Answer

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

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