# Replacement in Module[] after importing a list

I have a function defined by its series expansion \begin{align} F(\vec{x}=(x_1,\dots x_l);\vec{y}=(y_1,\dots,y_l);q)=\frac{1}{\sum_{k=1}^l y_k}\sum \limits_{j=0}^{\infty} (\prod\limits_{i=1}^{l} x_i)^{j-2}q^j \end{align} only and I want to evaluate it for a fixed but high order in $q$ and many different $\vec{x},\vec{y}$.
Therefore I want to tabulate the coefficients of the series expansion for arbitrary $\vec{x},\vec{y}$, save them in a external table and use them for specific values afterwards.
The code I've written is:

    (* SETUP *)
MyFunc2Table[myfunc[xlist_,ylist_,var_],upper_]:=Module[{term,newterm},
(* xlist and ylist are lists, var is a symbol, upper an integer *)

(* define expansion term at j-th order *)
term=Apply[Times,xlist]^(j-2)/Apply[Plus,ylist] var^(j);

(* expand to O[var]^upper *)
newterm=0;
Do[newterm=newterm+term,{j,0,upper}];

(* tabulate and export series coefficients *)
Export["myExpansionTable",CoefficientList[Series[newterm,{var,0,upper}],var],"List"];
]

MyFuncFromTable[myfunc[xlist_,ylist_,var_],upper_]:=Module[
{x,y,expansion,coeff}
,
(* xlist and ylist are lists, var is a symbol, upper an integer *)

(* get general expansion coefficents *)
coeff=ToExpression[Import["myExpansionTable","List"]];

(* replace (?!) x[i] and y[i] by it's values *)
x[i_]:=xlist[[i]];
y[i_]:=ylist[[i]];

(* expand in var *)
expansion=Normal[
Series[FromDigits[Reverse[coeff],var],{var,0,upper}]
];

expansion
]
(* Test of the procedures *)

(* Step 1: Save Coefficients for ARBITRARY x[i] and y[i] by fixed length:*)
MyFunc2Table[myfunc[{x[1],x[2]},{y[1],y[2]},Q],3]

(* Step 2: Get a series expansion for SPECIFIC x[i] and y[i] *)
MyFuncFromTable[myfunc[{1,2},{3,4},Q],3]

  Q^2/(y[1]+y[2])+1/(x[1]^2 x[2]^2 (y[1]+y[2]))+Q/(x[1] x[2] (y[1]+y[2]))+(Q^3 x[1] x[2])/(y[1]+y[2])

(* The replacement by specific values did not work *)


but it does not work. There seems to be a problem with the correct replacement in the function

MyFuncFromTable[myfunc[{1,2},{3,4},Q],3]


What do I miss? Many thanks in advance for helping me out!

Remark 1:
The function defined above is not my actual function. It is just for the sake of illustration and keeping it simple. my actual problem is the function defined in another question I already asked some time ago.

• What happens if you use Get instead of Import and ToExpression? – Mr.Wizard Sep 3 '16 at 16:06
• I get the following error: "Get::notencode: Warning: the file myExpansionTable is not encoded." – Armin Sep 3 '16 at 16:09
• Okay, it was just a shot in the dark. I'll try to come back to this later today if someone else has not answered it first. – Mr.Wizard Sep 3 '16 at 16:10
• @Mr.Wizard Thanks! are you interested in the stuff I already tried? – Armin Sep 3 '16 at 17:24
• I found my mistake. Since Module creates local variables x$something which do not match the x[i] and y[i] of the imported coefficients, I had to replace Module[] by Block[] in MyFuncFromTable. Am I supposed to delete the question or should I edit it and include the proper solution for others? Sry, I am pretty new and not so familiar with the rules of stackexchange. – Armin Sep 5 '16 at 10:46 ## 1 Answer The above mentioned problem is a special case of: 1.) compute the most general case of a mathematical expression (here: series expansion) 2.) export the result to an external file 3.) import the result at a later stage of your computations and use it for specific evaluations (replace symbolic parameters by numerical values). In short: Do the time consuming part only once and use replacements instead of actual computations afterwards. The crucial part in this whole procedure is to match the general parameters in the previously saved result. This can not be done by using an Module[] environment since Module creates a symbol with name x$nnn for the local variable x.
Therefore one has to use a Block[] structure (no local copies with different namespaces).

For the sake of completeness, the correct code for my initial question is:

(* SETUP *)
(* compute and export general expression *)
MyFunc2Table[myfunc[xlist_, ylist_, var_], upper_] :=
Block[{term, newterm},
(* xlist and ylist are lists, var is a symbol, upper an integer *)

(* define expansion term at j-th order *)
term = Apply[Times, xlist]^(j - 2)/Apply[Plus, ylist] var^(j);
(* expand to O[var]^upper *)
newterm = 0;
Do[newterm = newterm + term, {j, 0, upper}];

(* tabulate and export series coefficients in the file:
myExpansionTable*)
Export["myExpansionTable",
CoefficientList[Series[newterm, {var, 0, upper}], var], "List"];]

(* import expansion, replace general parameters by specific values *)
MyFuncFromTable[myfunc[xlist_, ylist_, var_], upper_] :=
Block[{x, y, expansion, coeff},
(* xlist and ylist are lists, var is a symbol, upper an integer *)

(* get general expansion coefficents from previous saved file*)
coeff = ToExpression[Import["myExpansionTable", "List"]];

(* replace x[i] and y[i] by its values: crucial point *)
x[i_] := xlist[[i]];
y[i_] := ylist[[i]];

(* expand in var *)
expansion =
Normal[Series[FromDigits[Reverse[coeff], var], {var, 0, upper}]];
expansion
]


The test:

(* Step 1: Save Coefficients for ARBITRARY x[i] and y[i] by fixed \
order of 100:*)
MyFunc2Table[
myfunc[{x[1], x[2]}, {y[1], y[2]}, Q], 100]
(* Step 2: Get a series expansion for SPECIFIC x[i] and y[i] *)
MyFuncFromTable[myfunc[{1, 2}, {3, 4}, Q], 3]
MyFuncFromTable[myfunc[{1, 2}, {3, 4}, Q], 5]


Output:

1/28 + Q/14 + Q^2/7 + (2 Q^3)/7
1/28 + Q/14 + Q^2/7 + (2 Q^3)/7 + (4 Q^4)/7 + (8 Q^5)/7


Remark:
I used this approach for a rather complicated expansion and had to figure out that importing a huge symbolic expansion (25M) is not that fast. Who would have guessed ;)