以文本形式保存 Mathematica 表达式

发布于 2024-11-06 06:51:39 字数 584 浏览 0 评论 0原文

将 Mathematica 表达式无损转换为字符串(保存在内存中,而不是导出到文件中的字符串)的正确方法是什么?

我正在寻找一种

  1. 能够保留所有信息的文本表示形式,包括保留特殊(可能是原子)对象,例如 SparseArrayGraphDispatchCompiledFunction 等完好无损。例如,通过此表示循环 SparseArray 应保持其稀疏(而不是将其转换为普通列表)。
  2. 循环相对较快(来回转换)。

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

  1. will preserve all information, including keeping special (and possibly atomic) objects, such as SparseArray, Graph, Dispatch, CompiledFunction, etc. intact. E.g. cycling a SparseArray through this representation should keep it sparse (and not convert it to a normal list).
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

莫相离 2024-11-13 06:51:39

如果您不打算对结果字符串执行某些字符串操作,则可以考虑使用 CompressUncompress 作为 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 and Uncompress as an alternative to ToString. While I don't know about cases where ToString[expr,InputForm] - ToExpression cycles would break, I can easily imagine that they exist. The Compress solution seems more robust, as Uncompress invoked on Compress-ed string is guaranteed to reconstruct the original expression. An added advantage of Compress 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.

素年丶 2024-11-13 06:51:39

如果 Compress 出现往返问题,ExportStringImportString 可能会提供有用的替代方案 - 特别是如果它们与 Mathematica 结合使用-native MX 格式:

string = ExportString[originalExpr, "MX"]
recoveredExpr = ImportString[string, "MX"]

请注意,MX 格式通常不能在 Mathematica 实例之间传输,但这对于所描述的内存中应用程序可能并不重要。

ExpressionML 是另一种与 Mathematica 相关的导出格式,但它显然不是一种紧凑格式。

Should Compress exhibit round-tripping problems, ExportString and ImportString might present a useful alternative -- particularly, if they are used in conjunction with the Mathematica-native MX format:

string = ExportString[originalExpr, "MX"]
recoveredExpr = ImportString[string, "MX"]

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文