```Date: Dec 31, 2012 7:45 PM
Author: Bill Rowe
Subject: Re: Large SparseArray in Mathematica 9 and the predictive interface

On 12/30/12 at 8:50 PM, jmmv73@gmail.com wrote:>I was generating a banded sparse array with the following code in>the new Mathematica 9.0:>gmat[K_, l_, m_] :=>Module[{bandU, bandL, bandD, res},>bandU = Table[l, {K - 1}]; bandL = Table[m, {K - 1}]; bandD = Table[-l - m, {K}]; bandD[[1]] = -l; bandD[[K]] = -m;>res = DiagonalMatrix[SparseArray[bandU], 1] +>DiagonalMatrix[SparseArray[bandD]] +>DiagonalMatrix[SparseArray[bandL], -1];>Return[res];>];>when I typed (without semicolon):>gmat[10000, 0.8, 1]>...everything went not so good, a 10000x10000 sparse matrix was>created (what was ok), but the kernel went nuts with the memory use,>some gigabytes for a small sparsearray (You can try with greater>parameters, but starting with K=5000 things go weird)>I think the problem has to do with the "predictive interface" that>tries to generate the normal form of the matrix, so it uses a lot of>memory (and even produces a kernel crash).A few comments:First, I cannot reproduce your results. That using your codeslightly cleaned up, I don't see any problems. By slightlycleaned up I mean replacing K (which as a built-in meaning) withk and eliminating the local variable res which isn't needed to get:gmat[k_, l_, m_] := Module[{bandU, bandL, bandD},   bandU = Table[l, {k - 1}];   bandL = Table[m, {k - 1}];   bandD = Table[-l - m, {k}];   bandD[[1]] = -l;   bandD[[k]] = -m;   DiagonalMatrix[SparseArray[bandU], 1] +    DiagonalMatrix[SparseArray[bandD]] +    DiagonalMatrix[SparseArray[bandL], -1]]And this code can be further simplified to:gMat[k_, l_, m_] :=  SparseArray[{Band[{1, 1}] -> -m - l, Band[{1, 2}] -> l,     Band[{2, 1}] -> m}, {k, k}] +   SparseArray[{{1, 1} -> m, {k, k} -> l}]But I do see a difference between version 8 and 9 with respectto the amount of memory used. That is:In[1]:= gMat[k_, l_, m_] :=  SparseArray[{Band[{1, 1}] -> -m - l, Band[{1, 2}] -> l,     Band[{2, 1}] -> m}, {k, k}] +   SparseArray[{{1, 1} -> m, {k, k} -> l}]In[2]:= a = MemoryInUse[];gMat[10000, .8, 1]; MemoryInUse[] - aOut[3]= 1003920In[4]:= \$VersionOut[4]= 9.0 for Mac OS X x86 (64-bit) (November 20, 2012)andIn[1]:= gMat[k_, l_, m_] :=  SparseArray[{Band[{1, 1}] -> -m - l, Band[{1, 2}] -> l,     Band[{2, 1}] -> m}, {k, k}] +   SparseArray[{{1, 1} -> m, {k, k} -> l}]In[2]:= a = MemoryInUse[];gMat[10000, .8, 1]; MemoryInUse[] - aOut[3]= 493024In[4]:= \$VersionOut[4]= 8.0 for Mac OS X x86 (64-bit) (October 5, 2011)In version 9 on my system, the array requires a bit more thantwice the memory that version 8 requires. So, there may besomething to your speculation about the predictive interface.One other thing. I should point out I have 16GB of RAM installedwhich may be why I am not seeing problems with your code.
```