以文本形式保存 Mathematica 表达式
将 Mathematica 表达式无损转换为字符串(保存在内存中,而不是导出到文件中的字符串)的正确方法是什么?
我正在寻找一种
- 能够保留所有信息的文本表示形式,包括保留特殊(可能是原子)对象,例如
SparseArray
、Graph
、Dispatch
、CompiledFunction
等完好无损。例如,通过此表示循环SparseArray
应保持其稀疏(而不是将其转换为普通列表)。 - 循环相对较快(来回转换)。
ToString[expr, FullForm]
是否足以满足此目的? ToString[expr, InputForm]
怎么样?
注 1:这是在尝试解决 Graph
中内部表示偶尔会损坏的一些错误时出现的。但我对上述一般性问题的答案感兴趣。
注 2:Save
肯定会执行此操作,但它会写入文件(可能可以使用流来解决此问题),并且它只写入与符号关联的定义。
What is the proper way to convert Mathematica expressions losslessly to a string (a string kept in memory, not exported to a file)?
I am looking for a textual representation that
- will preserve all information, including keeping special (and possibly atomic) objects, such as
SparseArray
,Graph
,Dispatch
,CompiledFunction
, etc. intact. E.g. cycling aSparseArray
through this representation should keep it sparse (and not convert it to a normal list). - is relatively fast to cycle through (convert back and forth).
Is ToString[expr, FullForm]
sufficient for this? What about ToString[expr, InputForm]
?
Note 1: This came up while trying to work around some bugs in Graph
where the internal representation gets corrupted occasionally. But I'm interested in an answer to the general question above.
Note 2: Save
will surely do this, but it writes to files (probably possible to solve this using streams), and it only write definitions associated with symbols.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不打算对结果字符串执行某些字符串操作,则可以考虑使用
Compress
和Uncompress
作为ToString
的替代方案。虽然我不知道ToString[expr,InputForm]
-ToExpression
循环会中断的情况,但我可以很容易地想象它们存在。Compress
解决方案似乎更健壮,因为在Compress
-ed 字符串上调用Uncompress
可以保证重建原始表达式。Compress
的另一个优点是它非常节省内存 - 我使用它几次在笔记本中保存大量数字数据,而不是将它们保存到磁盘。If you are not going to perform some string manipulations on the resulting string, you may consider
Compress
andUncompress
as an alternative toToString
. While I don't know about cases whereToString[expr,InputForm]
-ToExpression
cycles would break, I can easily imagine that they exist. TheCompress
solution seems more robust, asUncompress
invoked onCompress
-ed string is guaranteed to reconstruct the original expression. An added advantage ofCompress
is that it is pretty memory-efficient - I used it a few times to save large amounts of numerical data in the notebook, without saving them to disk.如果
Compress
出现往返问题,ExportString
和ImportString
可能会提供有用的替代方案 - 特别是如果它们与 Mathematica 结合使用-nativeMX
格式:请注意,
MX
格式通常不能在 Mathematica 实例之间传输,但这对于所描述的内存中应用程序可能并不重要。ExpressionML
是另一种与 Mathematica 相关的导出格式,但它显然不是一种紧凑格式。Should
Compress
exhibit round-tripping problems,ExportString
andImportString
might present a useful alternative -- particularly, if they are used in conjunction with the Mathematica-nativeMX
format:Note that the
MX
format is not generally transferable between Mathematica instances, but that might not matter for the described in-memory application.ExpressionML
is another Mathematica-related export format, but it is distinctly not a compact format.