Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting numbers when writing to files in Mathematica

This is a continuation of this question regarding number formatting, and related to my earlier question about obtaining very specific Mathematica output to text files.

I frequently have to use high precision in Mathematica for data generation but only need relatively low precision for visualization purposes. I also want to store the data for later use with all Symbol names and Array structures intact. For this I have been using Save[], but there are two related problems.

  1. The high precision "pollutes" my results with superfluous digits which are very hard to get rid of:

    In[1]  := b = SetPrecision[7, 50]; a = Pi/b
    Out[1] := 0.44879895051282760549466334046850041202816705705358654585356351318683091518373`50.
    In[2]  := InputForm @ N[a, 6]
    Out[2] := 0.44879895051282760549466334046850041203`6.
    

    where I really only need 0.448799.

  2. Sometimes even the number indicating precision is corrupted and I get values like 4.72642364528438598726943'5.9999999999999999999999 where I don't generally need the precision and 4.72642 would suffice.

Both of these introduce significant overhead to the file size, and while hard disk storage is cheap, file size makes a huge difference when later loading the files back into Mathematica.

So, starting with, e.g., aa that contains 50 digit arbitrary precision numbers in an irregular array, is there a built in way for me to get a text file that would read something like this

aa = {{2.0437`4, 4.7276`4, ...}, ...}

EDIT: To clarify, I am not having problems with the display of numbers or with tracking the precision of numbers or with changing the precision of numbers. What I am having trouble with is controlling how a number is written to a file.

Using N, NumberForm, OutputForm, InputForm, *Form, etc, all do not work properly with Save. And Save is the only exporting option I can find that exports the symbol and array structure. Export and Put* can be used to control the formatting better but they don't include the symbol (and in the case of Export the array structure is lost as well).

like image 319
Timo Avatar asked Feb 10 '11 09:02

Timo


People also ask

How do you write normal Text in Mathematica?

You can select a cell and do Format > Style > Text (keyboard shortcut Alt+7). Also, in the current version, you should see a popup prompting you to convert your cell into a text cell when you start typing something that looks like a sentence.

How do you change Text in Mathematica?

(1) Open a notebook, in the menu choose "Format" -> "Edit Stylesheet". (2) Choose a style to change. For example, "Text", in the menu at the top left. (3) Modify fonts, etc.

How do you write notes in Mathematica?

Move the cursor to the bottom of the cells. A horizontal line will appear below the previous Input-Output cells. Select Style from the Format menu. Choose Text and then type in a comment.

How do you write a heading in Mathematica?

Click below the section header and type. By default, the Wolfram Language interprets this as an Input cell: Click the cell bracket to the right of the Input cell and change the style from Input to Text by using the Format ▶ Style ▶ Text menu item.


1 Answers

Do you really require things like 2.0437`4, or would the machine double 2.0437 suffice? If the latter then you could do something like

N[SetPrecision[values,6]]

to coerce to machine doubles that will (mostly) show six decimal digits.

An possible advantage is in reading back it. Your array will now be machine doubles, hence packable. I'm not sure if Get or Import automatically pack, but Developer`ToPackedArray will do that.

---edit 2011-02-11---

Now that I've seen what can go wrong...

Here is an example, using your later input and a few others that I hope will be representative.

aa = {7.469702041097916467293771347613073888816285869`15.\
  954589770191005*^-51, 5555.22222222222222222223,
  .00000000002222222222222222222222222227777777777777, N[E, 22]^33}

First convert to a string. This may actually be all you really want, for purposes of saving to a file. I use NumberForm, but with a custom formatting function (cribbed by and large from documentation pages).

In[39]:= 
    InputForm[ToString[
      NumberForm[N[aa], 6, 
       NumberFormat :> (If[#3 != "", Row[{#1, "*^", #3}], #1] &)]]]

Out[39]//InputForm=
"{7.4697*^-51, 5555.22, 2.22222*^-11, 2.14644*^14}"

Notice that the expression conversion works fine on this.

In[40]:=
    InputForm[ToExpression[
      ToString[NumberForm[N[aa], 6, 
       NumberFormat :> (If[#3 != "", Row[{#1, "*^", #3}], #1] &)]]]]

Out[40]//InputForm=
{7.4697*^-51, 5555.22, 2.22222*^-11, 2.14644*^14}

---end edit---

Daniel Lichtblau Wolfram Research

like image 98
Daniel Lichtblau Avatar answered Sep 17 '22 11:09

Daniel Lichtblau