Group Abstract Group Abstract

Message Boards Message Boards

1
|
10.7K Views
|
5 Replies
|
1 Total Like
View groups...
Share
Share this post:

Global vs Private in nb but CDF is working fine.

Posted 9 years ago
Attachments:
POSTED BY: Erdem Uguz
5 Replies

I think your questions are valid and important but they are not receiving enough attention due to the fact that you dump here a lot of irrelevant code. It isn't very long yet messy enough for me to stop reading (sorry, don't have unlimited time).

So the problem is that x declared in a package is different than used later by the user. Take a look at closely related topic:

http://mathematica.stackexchange.com/q/114769/5478

If you can't use the first solution go with the second:

Integrate[function, Symbol[$Context <> "x"]]
POSTED BY: Kuba Podkalicki
Posted 9 years ago

I found a weird behavior when I delete the private command from the package and when I first run a Quit command.

BeginPackage["TFPackageSourceCode`"];
(*WD1Int::usage="";
Begin["`Private`"];*)
WD1Int[f_,xR_,xL_,np_,nw_]:=Module[{xLcheck,xRcheck},xLcheck=NumberQ[xL];
xRcheck=NumberQ[xR];
If[xLcheck==True&&xRcheck==True,ResultTF=SetPrecision[NIntegrate[f,{x,xL,xR},WorkingPrecision->Re[IntegerPart[nw]]],Re[IntegerPart[np]]],"Enter numeric inputs for Lower and Upper Limits"]];
SetAttributes[WD1Int,{ReadProtected,Protected,Locked}];
(*End[];*)
EndPackage[];

and with the same main file.

Manipulate[
 DynamicModule[{f = Sin[x], xR = 1, xL = 0, 
   Res = 0.4596976941318603`16}, 
  Column[{Style["1D Definite Integral Calculator", "Function"], 
    Row[{"Lower Limit ", InputField[Dynamic[xL], Number]}, Spacer[5]],
     Row[{"Upper Limit ", InputField[Dynamic[xR], Number]}, 
     Spacer[5]], 
    Row[{"Function  ", InputField[Dynamic[f]]}, Spacer[20]], 
    Button[Style["Calculate", Green, Bold], 
     Dynamic[Res = TFPackageSourceCode`WD1Int[f, xR, xL, np, nw];]], 
    Row[{"Result", InputField[Dynamic[Res], Enabled -> False]}, 
     Spacer[51]], 
    Row[{"Plot the Function ", Checkbox[Dynamic[fp], {False, True}]}],
     If[fp == True, 
     InputField[
      Dynamic[Plot[f, {x, xL, xR}, Dynamic[PlotLabel -> f], 
        ImageSize -> Full]], FieldSize -> {30, 15}]], 
    Button[Mouseover[Style["http://www.wolfram.com", "Hyperlink"], 
      Style["http://www.wolfram.com", "HyperlinkActive"]], 
     NotebookLocate[{URL["http://www.wolfram.com"], None}], 
     Appearance -> None]}]], {{np, 16, "NumberFormat"}, 
  ControlPlacement -> Bottom}, {{nw, 16, "WorkingPrecision"}, 
  ControlPlacement -> Bottom}, 
 Initialization :> (Get[
     FileNameJoin[{NotebookDirectory[EvaluationNotebook[]], 
       "TFPackageSourceCode.wl"}]];)]
POSTED BY: Erdem Uguz

Try to change your package, using these simple definitions which circumvent the problem Kuba mentioned:

oneVarQ = 
  MatchQ[Context /@ 
     DeleteDuplicates@Cases[#, _Symbol, -1], {"Global`"}] &;
getVar = Cases[#, _Symbol, -1][[1]] &;
MyIntegrate[func_?oneVarQ] := NIntegrate[func, Evaluate@{getVar@func, 0, 1}];
POSTED BY: Rolf Mertig
Posted 9 years ago

Thank you for the reply but to be honest I still did not understand why I am the problem. From the main code Sin[x] or let me say f[x] goes as a symbolic variable to the package. There I create a variable x and it is privative because the package is private. I thought creating an x variable in the module should have solved the problem.

BeginPackage["TF1DINTPackage`"];
D1Int::usage="";
Begin["`Private`"];
D1Int[f_,xR_,xL_,np_,nw_]:=Module[{xLcheck,xRcheck,x},xLcheck=NumberQ[xL];
xRcheck=NumberQ[xR];
If[xLcheck==True&&xRcheck==True,ResultTF=SetPrecision[NIntegrate[f,{x,xL,xR},WorkingPrecision->Re[IntegerPart[nw]]],Re[IntegerPart[np]]],"Enter numeric inputs for Lower and Upper Limits"]];
SetAttributes[D1Int,{ReadProtected,Protected,Locked}];
End[];
EndPackage[];

but it did not. And I did not understand your solution idea.

POSTED BY: Erdem Uguz

All new(not found) symbols are created in current $Context so

MyIntegrate[func_]:= NIntegrate[func,{x,0,1}]

Will introduce x in Global` context while

BeginPackage["MyPackage`"];
    MyIntegrate::usage="";
Begin["`Private`"];
    MyIntegrate[func_]:=NIntegrate[func,{x,0,1}];
End[];
EndPackage[];

MyIntegrate[Sin[x]]
 NIntegrate[Sin[x], {MyPackage`Private`x, 0, 1}]

is creating definition with MyPackage`Private`x. So the function is integrated over different variable. Additionally some subtleties about symbolic preprocessing are giving NIntegrate::inumr: error, which will not occur if variables match.

Later, user is providing Sin[x] for example, but this x is created in current $Context which is probable Global` again.

This is one of reasons why all those built in functions like Plot Integrate etc etc will ask you to provide a variable too, like Integrate[x, {**x**,0,1}].

POSTED BY: Kuba Podkalicki
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard