Here is a module which constructs essentially several functions in a chain [Phi], sct, ks, WE, G:
ClearAll["Global`"];
Rat[expr_, expc_, csi_: 1] :=
Module[{nCLroots, nphases, sct, sc, ks, kss, kp, G, Gp, WE,
WEp},
nCLroots = Length[expr]; nphases = Length[expc];
\[Phi] =
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}]];
sct = Apart[Simplify[\[Phi][#]/(csi (1 - rhmin) (# - Fq))],
#] &;(*Fq is defined outside, the intention is having it global variable*)
kss = ((1/sct[#]) + csi (1 - rhmin) Fq) &;
ks = Apart[kss[#], #] &;
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]];
{\[Phi], rhmin, ks, G}];
The test
exr = {1/2, 3/2}; exc = {1, 2};
Fq = 1/3;
cc = Rat[exr, exc];
G = cc[[4]][x];
Print[" G=", G]
reveals that G is outputted correctly:
G'=-(112/(55 x^2))+(1-7/(4 (7/3+x)^2)-3/(4+3 x)^2)/(-(47/48)+x+7/(4 (7/3+x))+1/(4+3 x))^2 G=112/(55 x)-1/(-(47/48)+x+7/(4 (7/3+x))+1/(4+3 x))
This stops working when I encapsulate the modules in a Mathematica package RatC.wl
BeginPackage["RatC`"]
Unprotect @@ Names["RatC`*"];
ClearAll @@ Names["RatC`*"];
Rat::usage="..."
Begin["`Private`"]
ClearAll["Global`*"]
Copy-paste of Rat above
End[]
EndPackage[]
The output with the same test
G = 1/ ((1/2 - 1/(6 (1 + RatCPrivateFq)^2) - 7/(8 (2 + RatCPrivateFq)^2)) RatCPrivate s) - 1 / (-(2/3) + (RatCPrivateFq + RatCPrivate s)/2 + 1/(6 (1 + RatCPrivateFq + RatCPrivates)) + 7/(8 (2 + RatCPrivateFq + RatCPrivate s)))
seems rather inconvenient ( one is forced to use substitutions RatCPrivate s ->s, etc
So, is it really possible to create packages from modules by copy paste? Is there a book on that?
BeginPackage[…]
andBegin[…]
? $\endgroup$BeginPackage[]
andBegin[]
. In short, changings
toRatC`Private`s
is exactly the significance of them. You may want to read the following posts: 1. mathematica.stackexchange.com/q/43381/1871 2. mathematica.stackexchange.com/q/197692/1871 $\endgroup$