Message Boards Message Boards

1
|
9356 Views
|
5 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Global vs Private in nb but CDF is working fine.

Posted 8 years ago

Hello, I am new Mathematica user and my duties require me to learn CDF development. For that I have been developing a toy application so I can learn how to create a interactive enterprise cdf. I got several helps in here but there is much I need to learn. Below is the code that I used to create a cdf file for integration. (I am also going to include the CDF file) I use an encoded package. The app is simply taking 1D definite integral.

Manipulate[
 DynamicModule[{f = Sin[x], xR = 1, xL = 0, 
   Res = 0.4596976941318603`16}, 
  Column[{Style["1D Definite Integral Calculator", "Function"] logo, 
    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 = TF1DINTPackage`D1Int[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[]], 
       "TF1DINTPackage.m"}]];)]

and the package (before I encode it , encoded version is also attached)

BeginPackage["TF1DINTPackage`"];
D1Int::usage="";
Begin["`Private`"];
D1Int[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[D1Int,{ReadProtected,Protected,Locked}];
End[];
EndPackage[];

I keep both the main file and the package in the same folder. The folder is NOT in the path of Mathematica but I think GET command take care of it. I saved this file (via menu) as enterprise CDF and kept the .cdf file with the package in the same folder (same with the .nb file) . While the CDF file is working I am getting error on .nb file. Here is the screen shots of the error

enter image description here enter image description here

How can avoid this error in the .nb file?

Thank you for the help.

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 8 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 8 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

Group Abstract Group Abstract