3
$\begingroup$

I have a script with a long loop and very long formatted outputs like the following example code:

Do[Print[Style[Table[RandomReal[], {30}, {15}] // MatrixForm, Blue]], {10000}]

In order to display all the output in the desired format and save them into *.nb or *.pdf, at least 2GB memory is needed, which exceeds the available upper limits of x86 OS.

How can I export the formatted results immediately into a *.nb or a PDF file so that the code does not demand so much computer memory?

$\endgroup$
7
  • $\begingroup$ @m_goldberg thank you for editing it! $\endgroup$ Nov 24, 2013 at 15:52
  • 1
    $\begingroup$ As an aside, I would point out that RandomReal can produce matrices, so your example can be reduced to Do[Print[Style[RandomReal[1., {30, 15}] // MatrixForm, Blue]], {10000}]. $\endgroup$
    – m_goldberg
    Nov 25, 2013 at 1:43
  • $\begingroup$ How do you expect to use the output? The file produced, whether .nb or .pdf, will too big, to open in Mathematica on your system. $\endgroup$
    – m_goldberg
    Nov 25, 2013 at 1:54
  • $\begingroup$ I'm missing something. If you put 10000 large styled matrices into a PDF file, that file will be big. $\endgroup$
    – m_goldberg
    Nov 25, 2013 at 2:34
  • $\begingroup$ Yes, the PDF file will be big; but usually less than 50M and requires very small memory to view it. $\endgroup$ Nov 25, 2013 at 2:45

1 Answer 1

1
$\begingroup$

I could not generate a notebook with 10000 30 x15 matrices without the Mathematica front-end eating up a good deal more then 2 GB of memory. Best I have done so far is create and save a notebook file with 2500 such matrices. On my system, this used up about 800 MB of Mathematica front-end memory. The good news is that it was done with one line of code:

CreateDocument @ Table[Style[RandomReal[1., {30, 15}] // MatrixForm, Blue], {2500}]; 

After saving the newly generated matrix notebook, I quit Mathematica, and then restarted it again by double-clicking on the matrix notebook's icon. The matrix notebook loaded quickly and only used about 4 MB of front-end memory. Using the File > Print... menu items, I was able to create a PDF file with two matrices per page (trying to put more on a page made the numbers too small for comfortable reading).

You asked in chat, how this approach could be modified to handle the case where each matrix generated has a dependency on an iteration variable n. Suppose this dependency was scalar multiplication by n. In this case, n would simply be the first element in the in the iteration control list, the second argument of Table:

CreateDocument @ Table[Style[n RandomReal[1., {30, 15}] // MatrixForm, Blue], {n, 2500}]; 

Should you adopt this approach, I recommend doing it with a freshly launched front-end session.

$\endgroup$
2
  • $\begingroup$ Thank you very much. I also tried to "output.pdf"~Create~CreateDocument@ Table[Style[n RandomReal[1., {30, 15}] // MatrixForm, Blue], {n, 2500}]; but Create[] function does not make any pagebreak; unless we handle binary PDF creation operation, this should be the best way to create good looking formatted PDF output. $\endgroup$ Nov 26, 2013 at 0:21
  • 1
    $\begingroup$ @LCFactorization. To get page breaks, image scaling, and other layout controls, I had to use Mathematica's notebook printing tools. On my system, OS X, the print dialog offers "Save as PDF" as an option, so after formatting the notebook for printing, I took advantage of that option. $\endgroup$
    – m_goldberg
    Nov 26, 2013 at 5:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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