First I present the specific programming question, and then I provide further background details.
Given an expression in Mathematica, say f=3x+7y
, there are two common ways to substitute information for x
and y
. The first is to use the /.
rule, so something like
t=f/.{x->0,y->0};
would make the value of t
equal to 0
.
And the other is a more general substitution for x
and y
everywhere they appear, such as
x=0; y=0;
t=f;
This also makes the value of t
equal to 0
. The difference in memory usage, however, becomes apparent with more complicated polynomials. I have uploaded a text file with a complicated polynomial to MediaFire : http://www.mediafire.com/?0ea3mhtqmc8c3r2 (it is about 4MB, and I'm not aware of a better way of sharing it). If you download it and then load it into Mathematica as I will demonstrate, you can see for yourself the significant memory loss:
Substitution Method
In[1]:= << "/home/username/Downloads/p";
In[2]:= MaxMemoryUsed[]
Out[2]= 79833424
In[3]:= MemoryInUse[]
Out[3]= 76373112
In[4]:= t = p /. {x -> 0, y -> 0, z -> 0};
In[5]:= MaxMemoryUsed[]
Out[5]= 152124264
In[6]:= MemoryInUse[]
Out[6]= 112876080
General Assignment Method
In[1]:= << "/home/username/Downloads/p";
In[2]:= MaxMemoryUsed[]
Out[2]= 79831632
In[3]:= MemoryInUse[]
Out[3]= 75415960
In[4]:= x = 0; y = 0; z = 0;
In[5]:= MaxMemoryUsed[]
Out[5]= 79831632
In[6]:= MemoryInUse[]
Out[6]= 75420744
In[7]:= t = p;
In[8]:= MaxMemoryUsed[]
Out[8]= 112888664
In[9]:= MemoryInUse[]
Out[9]= 112875336
As you can see, during the substitution operation there was about 40MB of additional memory used to perform the substitution. The substitution method also takes much longer than the general method. The difference in the two methods becomes much higher (several GB) as the complexity of the polynomial increases, and ends up maxing out the available RAM. The problem is that for my program I cannot set x, y, z
equal to zero in general.
This seems to be an inefficiency with the way Mathematica performs substitutions. I would appreciate any suggestions for working around this problem.
Further Background
The reason I cannot simply employ the general method is that the next step requires that I take derivatives of the original polynomial with respect to the variables x, y, z
. The process would ideally be iterated as follows:
Step 1: Given p
, write the result of p/.{x->0,y->0,z->0}
to a file.
Step 2: Take derivatives of p
with respect to x, y, z
.
Step 3: Perform some algebraic operations with the derivatives, creating the more complicated polynomials p1 and p2.
Step 4: Repeat the process on each of p1 and p2.
Since the process runs out of RAM when using the substitution rule, I have been working around this by performing several iterations of the process without writing the output of p/.{x->0,y->0,z->0}
to a file, and then applying the "General Assignment Method" (setting x=0; y=0; z=0;
and writing the evaluation of each polynomial to a file).
This is an unsatisfactory workaround, however, because it is impossible to know the proper number of iterations which will be required, and so it is possible that after performing all the iterations and writing the results to a file that I will need to run it again, this time performing more iterations. If the results could be written out to a file in the intermediate steps, I would be able to check them and determine if I should let the iteration continue or if I can stop it.