在Mathematica中写入文件时格式化数字 [英] Formatting numbers when writing to files in Mathematica

查看:240
本文介绍了在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.

我经常必须在Mathematica中使用高精度来生成数据,但出于可视化目的只需要相对较低的精度.我还想存储数据,以便以后完整使用所有符号名称和数组结构.为此,我一直在使用Save[],但是有两个相关的问题.

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. 高精度用多余的数字污染"了我的结果,这些数字很难摆脱:

  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.

我真的只需要0.448799.

where I really only need 0.448799.

这两种方法都会给文件大小带来很大的开销,并且尽管硬盘存储很便宜,但是以后将文件加载回Mathematica时,文件大小会产生巨大的差异.

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.

因此,例如从aa开始,该文件在不规则数组中包含50位任意精度数字,我是否有一种内置的方式来获取将读取类似内容的文本文件

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.

使用NNumberFormOutputFormInputForm*Form等,均不能在Save下正常工作.而且Save是我可以找到的唯一导出选项,可以导出符号和数组结构. ExportPut*可以用来更好地控制格式,但是它们不包含符号(在Export的情况下,数组结构也会丢失).

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).

推荐答案

您是否真的需要2.0437`4之类的东西,还是该机器加倍2.0437就足够了?如果是后者,那么您可以做类似的事情

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.

可能的优点是回读它.您的阵列现在将是机器的双打,因此可打包.我不确定获取"或导入"是否会自动打包,但是Developer`ToPackedArray会做到这一点.

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.

---编辑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}

首先转换为字符串.为了保存到文件,这实际上可能就是您真正想要的.我使用NumberForm,但是具有自定义格式设置功能(在文档页面中大体上被抄袭了.)

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}

---结束编辑---

丹尼尔·里奇布劳 Wolfram研究

Daniel Lichtblau Wolfram Research

这篇关于在Mathematica中写入文件时格式化数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆