0
$\begingroup$

I am developing an application in one part I am checking the if the functions are constant or not. I am using a couple of lines of code (it is possible that there is another way of doing it) but the code behaves differently when I use as a package instead of in a .nb file. The notebook code is

FunCons2[var1_, var2_, var3_, var4_] := Module[{fields}
  , fields = Flatten[{var1, var2, var3, var4}]
  ; consz =  fields /. z -> {a, b}
  ; consz
]

f1 = 1;
f2 = t^3 + (z - 1) (z - 2) + E^-t Sin[z];
f3 = {-1 + E^-t Cos[1], 2 (1 + E^-t Cos[2])};
f4 = Limit[f2, t -> 0];

FunCons2[f1, f4, f3, f2]

The output as you can see t is replaced a and b

{  {1, 2 - 3 z + z^2 + Sin[z], {-1 + E^-a Cos[1], -1 + E^-b Cos[1]}, 
   {2 (1 + E^-a Cos[2]), 2 (1 + E^-b Cos[2])}, 
   {a^3 + (-2 + z) (-1 + z) + E^-a Sin[z], b^3 + (-2 + z) (-1 + z) + E^-b Sin[z]}}
}

and the package version is

Get[FileNameJoin[{NotebookDirectory[EvaluationNotebook[]],"CheckCons.wl"}]];

f1 = 1;
f2 = t^3 + (z - 1) (z - 2) + E^-t Sin[z];
f3 = {-1 + E^-t Cos[1], 2 (1 + E^-t Cos[2])};
f4 = Limit[f2, t -> 0];
CheckCons`FunCons[f1, f4, f3, f2]

The package

BeginPackage["CheckCons`"];

FunCons::usage="";

Begin["`Private`"];

  FunCons[var1_,var2_,var3_,var4_]:=Module[{fields,a,b},
    fields=Flatten[{var1,var2,var3,var4}];
    consz=fields/.z->{a,b};
    consz
  ]

End[];



EndPackage[];

And the output is

{
   {1, 2 - 3 z + z^2 + Sin[z], -1 + E^-t Cos[1], 2 (1 + E^-t Cos[2]), 
    t^3 + (-2 + z) (-1 + z) + E^-t Sin[z]}
 }

The problem is replacing z with a and b. Inside the package the code does not replace it. I don't know why or how to avoid that.

$\endgroup$
5
  • $\begingroup$ The reason I am not really happy is still I am not understanding the reason why it is happening like this. If you look at the Print from the package for the fields. {1,2-3 z+z^2+Sin[z],-1+E^-t Cos[1],2 (1+E^-t Cos[2]),t^3+(-2+z) (-1+z)+E^-t Sin[z]} t and z are still the variable no private or global why I can't replace t or z directly. Even when you look at the output of Variables[Level[fields,{-1‌​}]][[1]] it is {} or z or t. $\endgroup$
    – Erdem
    May 3, 2017 at 18:15
  • $\begingroup$ @Erdem clear the kernel, run the package and check ?? CheckCons`FunCons. $\endgroup$
    – Kuba
    May 3, 2017 at 18:31
  • $\begingroup$ @Kuba it is Attributes[FunCons]={Locked,Protected,ReadProtected} $\endgroup$
    – Erdem
    May 3, 2017 at 20:32
  • $\begingroup$ @Erdem Don't use it, there aren't any above. They are not needed. And without ReadProtected you will see that the definition uses Private symbols. $\endgroup$
    – Kuba
    May 3, 2017 at 20:34
  • $\begingroup$ @Erdem I've put some edits to the linked question. Let me know if they helped. And please learn how to create minimal working examples. $\endgroup$
    – Kuba
    May 4, 2017 at 7:37

0

Browse other questions tagged or ask your own question.