4
$\begingroup$

I'm currently coding a Finite Element Method (FEM) program in Mathematica. For the so called stiffness matrix I need a very large table preferably around the size of 10^6 x 10^6 cells that are all zero at first.

For a smaller Table 10^4 x 10^4, I used the following code:

Timing[k=Table[0, {i, 1, 10^4}, {j, 1, 10^4}];]

and got results around 0.9 - 1.0 seconds

since ((10^6)^2)/((10^4)^2) = 10^4 I would expect a computing time of roughly 10000 seconds (~2.7 hours) for a 10^6 x 10^6 table which is frankly put, too slow.

I tried ParallelTable as well, but it's only like 30% faster

Is there a faster way to generate such a big array?

$\endgroup$
  • $\begingroup$ k = ConstantArray[0, {10^6, 10^6}, SparseArray] $\endgroup$ – Coolwater Jul 3 '18 at 9:35
  • $\begingroup$ .. or k = SparseArray[{}, {10^6, 10^6}]? $\endgroup$ – kglr Jul 3 '18 at 9:43
  • $\begingroup$ @Coolwater, kglr exactly what I was looking for, thanks! $\endgroup$ – p4di Jul 3 '18 at 10:46
3
$\begingroup$

As Coolwater and kglr have commented SparseArray was exactly what I was looking for!

k = ConstantArray[0, {10^6, 10^6}, SparseArray]

k = SparseArray[{}, {10^6, 10^6}]

both do the job.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.