I have a first module which outputs as forth output a function G I want to transfer to another module. The specific function is
G = (72 (97 + 57 s))/(55 (55 + 96 s + 36 s^2))
Everything works before encapsulating. Here's a short version of the first module:
Rat[expr_, expc_, csi_: 1] :=
(* Fq is defined outside, hopefully global variable *)
Module[{nCLroots, nphases, sct, sc, ks, kss, kp, G, Gp, WE, WEp},
nCLroots = Length[expr];
nphases = Length[expc];
ϕ =
Product[(1 + #/expc[[i]]), {i, nphases}] /
Product[(1 + #/expr[[i]]), {i, nCLroots}] &;
rhmin =
1 - Simplify[
Product[expr[[i]], {i, nCLroots}] /
Product[expc[[i]], {i, nphases}]];
Print["input: ", Subscript[ϕ, "-"], " = ", ϕ[s], " q =", q = Fq csi (1 - rhmin)];
sct = Apart[Simplify[ϕ[#]/(csi (1 - rhmin) (# - Fq))], #] &;
(* k - q *) kss = ((1/sct[#]) + csi (1 - rhmin) Fq) &;
(* symbol, without q *);
ks = Apart[kss[#], #] &;
Print["symbol k = ", ks[s]];
kp = D[ks[s], s] /. s -> Fq;
WEp = FullSimplify[1/((ks[s] /. s -> # + Fq) - q)] &;
WE = Function[s, WEp[s]];
Gp = FullSimplify[1/(# kp) - WEp[#]] &;
G = Function[s, Gp[s]];
Print["WE = ", Factor[WE[s]]];
Print["G = ", Factor[G[s]]];
{ϕ, rhmin, ks, G}];
The following code works
exr = {1/2, 3/2};
exc = {1, 2};
Fq = 1/3;
cc = Rat[exr, exc];
G = cc[[4]][x]
Print[" G=", G]
but stops working when I encapsulate the modules in a Mathematica package and I guess it's because the way I transfer G
The structure of the RatC.wl package encapsulating the modules is
BeginPackage["RatC`"]
Unprotect @@ Names["RatC`*"];
ClearAll @@ Names["RatC`*"];
Rat::usage="..."
Begin["`Private`"]
ClearAll["Global`*"]
Copy-paste of Rat above
End[]
EndPackage[]
after encapsulation, the function G contains two Private variables and sends the second module into very long computations.
G =
1/
((1/2 - 1/(6 (1 + RatC`Private`Fq)^2) -
7/(8 (2 + RatC`Private`Fq)^2)) RatC`Private`s) -
1 /
(-(2/3) + (RatC`Private`Fq + RatC`Private`s)/2 +
1/(6 (1 + RatC`Private`Fq + RatC`Private`s)) +
7/(8 (2 + RatC`Private`Fq + RatC`Private`s)))
The existence of two Private variables seemed to me to be significant, since functions with just one Private seemed to be usable just by plugging s; I wanted to make Fq a global variable by defining it outside the package, but G has still two Private variables
Any recommendations? Is it easier to transfer G as expression than as pure function?
Gis not a function but an expression. To use it elsewhere, consider returning a properFunctioninstead, such asG = Function[s, (72 (97 + 57 s))/(55 (55 + 96 s + 36 s^2))]. This way the dependent parameter is encapsulated and you will not have to deal with context issues like the one you're seeing here. – Sjoerd Smit Sep 20 '19 at 14:03