1
$\begingroup$

I have a mathematica script computing a list of numbers to high precision (5000 digits) which I would like to input into another program. I am running mathematica from the terminal to generate the file.

If I just ask for the output directly, the file it creates has a lot of superfluous return characters and ">" characters which would be very painful to remove.

If I ask for the output in "InputForm" it is cleaner, but it includes information about the precision which is also painful to remove.

Is there a way to generate the output in a clean way for other uses? Here I simply want to generate a list of numbers (comma separated is fine) in decimal notation. Note that I have to run this in a terminal window since it is a long computation on a remove machine, not to mention that the output is extremely long.

Here is an example file (foo) and example output (foo.out) generated by (math < foo > foo.out)

Input:

Table[N[y[n], 200], {n, 1, 3}];
InputForm[Table[N[y[n], 200], {n, 1, 3}]]

Output:

In[1]:= 
In[2]:= In[2]:= 
Out[2]= {0.585786437626904951198311275790301921430328124623051926823320262009\
 
>     26752153789296114961246567235842726498615376908770297507516394414926278\
 
>     735587850290006416858677733407249440724420004949884721793942853, 
 
>    0.4309644062711508251997185459650503202383880207705086544705533770015445\
 
>     86922982160191602077612059737877497692294847950495845860657358210464559\
 
>     31308381667736143112955567874906787403334158314120298990475, 
 
>    0.3292893218813452475599155637895150960715164062311525963411660131004633\
 
>     76076894648057480623283617921363249307688454385148753758197207463139367\
 
>     79392514500320842933886670362472036221000247494236089697143}

In[3]:= In[3]:= 
Out[3]//InputForm= 
{0.58578643762690495119831127579030192143032812462305192682332026200926752153\
78929611496124656723584272649861537690877029750751639441492627873558785029000\
6416858677733407249440724420004949884721793942852989044002839`200., 0.4309644\
06271150825199718545965050320238388020770508654470553377001544586922982160191\
60207761205973787749769229484795049584586065735821046455931308381667736143112\
955567874906787403334158314120298990475498174000474`200., 0.32928932188134524\
75599155637895150960715164062311525963411660131004633760768946480574806232836\
17921363249307688454385148753758197207463139367793925145003208429338866703624\
72036221000247494236089697142649452200142`200.}

In[4]:= 

I tried Daniel Huber's suggestion, but the output file I got was different (Full disclosure, I used Export["test.txt", d] instead. I am using Mathematica 12.3.1 Kernel for Linux x86 (64-bit).

3.14159265358979323846264338327950288419716939937510582097494459230781640628620899
86280348253421170679821480865191976`100.
2.71828182845904523536028747135266249775724709369995957496696762772407663035354759
45713821785251664274274661651602106`100.
1.14472988584940017414342735135305871164729481291531157151362307147213776988482607
97836232702754897077020098122305809`100.
$\endgroup$
2
  • $\begingroup$ Modern approach to this would probably be to use the wolframscript command and ExportString format of your choosing. For instance, wolframscript -code 'ExportString[N[Pi, 5000], "CSV"]' > foo.out or with the corresponding code in file foo with wolframscript -file foo > foo.out $\endgroup$
    – kirma
    Commented Aug 14, 2022 at 17:29
  • $\begingroup$ Some methods to process your output here: How to format a large Number $\endgroup$ Commented Aug 15, 2022 at 6:43

1 Answer 1

1
$\begingroup$

Here is a simple example. I am using a list with 3 elements and I output 100 digits to a file:

d = N[{Pi, E, Log[Pi]}, 100];
Export["d:/tmp/test.txt", ToString@d]

The file looks like:

enter image description here

To reimport it into MMA:

Import["d:/tmp/test.txt", "List"][[1]] // ToExpression

and you will get a list of the original numbers.

$\endgroup$
5
  • $\begingroup$ Thanks! Unfortunately I'm still getting extra characters which record the number of digits of accuracy (I edited my question since when I tried to write the output as a comment it was a mess) $\endgroup$
    – user297024
    Commented Aug 15, 2022 at 2:44
  • $\begingroup$ Add "ToString", that should fix it. I added it to my answer. $\endgroup$ Commented Aug 15, 2022 at 6:25
  • $\begingroup$ Thanks, that worked! $\endgroup$
    – user297024
    Commented Aug 15, 2022 at 12:24
  • $\begingroup$ Ugh, 24 hours of computation and I get I mess. These numbers are smallish (less than $1$ but bigger than $10^{-100}$ but if you type: d:=N[Pi/10^100,100]; and then ToString@d you get a mess. $\endgroup$
    – user297024
    Commented Aug 16, 2022 at 18:21
  • $\begingroup$ "ToSTring" only works o.k. for numbers without exponents. For small numbers that needs exponents we need a format that displays the exponent on the same line, that is 1D. You may e.g. use "FortranForm" or "CForm" for this purpose. Or you can define a format yourself like e.g.: NumberForm[data, NumberFormat -> (Row[{#1, "^", #3}] &)] $\endgroup$ Commented Aug 16, 2022 at 18:49

Your Answer

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

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