0
$\begingroup$

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?

$\endgroup$
5
  • $\begingroup$ Do you understand why a package begins with BeginPackage[…] and Begin[…]? $\endgroup$
    – xzczd
    Commented Sep 21, 2019 at 12:46
  • $\begingroup$ Yes! I've been using Mathematica lightly without studying it in detail for many years and have created short .m packages in the past-- now that I need a more complicated package it seems I miss some basics. Can you recommend a book at least? $\endgroup$
    – florin
    Commented Sep 21, 2019 at 12:51
  • 2
    $\begingroup$ I don't think you understand the usage of BeginPackage[] and Begin[]. In short, changing s to RatC`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$
    – xzczd
    Commented Sep 21, 2019 at 13:07
  • $\begingroup$ Thanks! I seem to remember that Matlab has something called M files, and this is what I was trying to get . It seems, as mentioned in post2, "if you don't care about context, simply put your utilities in a file without BeginPackage " . Then, I can either run it at the beginning of each session, or maybe put it $\endgroup$
    – florin
    Commented Sep 21, 2019 at 16:05
  • $\begingroup$ You got it :) . $\endgroup$
    – xzczd
    Commented Sep 21, 2019 at 16:16

0

Browse other questions tagged or ask your own question.