如何在 Mathematica 中保存与符号关联的 [] 定义而不保存辅助定义?

发布于 2024-09-30 13:22:10 字数 733 浏览 0 评论 0原文

内置 Mathematica 命令 保存[文件,符号] 使用 FullDefinition[] 查找定义 symbol 和所有辅助定义。

例如,这些命令

a:=b
c:=2a+b
Save[ToFileName[NotebookDirectory[],"test.dat"],c]

生成文件 test.dat ,其中包含

c := 2*a + b
a := b

我有一个带有大量美化内容的程序 当我保存[]许多单独的结果时,我希望保存MakeBoxes类型定义。

就上面的简单示例而言,我不希望将 a := b 定义保存到文件中。有谁知道一个巧妙的方法来实现这一点?

The built-in Mathematica command Save[file, symbol] uses FullDefinition[] to look up the definition symbol and all of the subsidiary definitions.

For example, the commands

a:=b
c:=2a+b
Save[ToFileName[NotebookDirectory[],"test.dat"],c]

produces the file test.dat containing

c := 2*a + b
a := b

I have a program with a lot of prettifying MakeBoxes type definitions that I do not want to be saved when I Save[] the many separate results.

In terms of the simple example above, I do not want the a := b definition saved to the file. Does anyone know a neat way to make this happen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

寄与心 2024-10-07 13:22:10

根据文档,Save 使用 FullDefinition,而您想要的是它使用 Definition。使用 Block 我们可以覆盖任何符号的全局定义,特别是在运行 SaveFullDefinition 替换为 Definition >:

Block[{FullDefinition},
  FullDefinition = Definition;
  Save[filename, c]
  ];
FilePrint[filename]
DeleteFile[filename]

神奇的作品:

c := 2*a + b

编辑。用正确的属性来概括事物:

SetAttributes[truncatedSave, HoldRest]
truncatedSave[filename_, args__] := Block[{FullDefinition},
   FullDefinition = Definition;
   Save[filename, args]];

According to the documentation, Save uses FullDefinition while what you want is for it to use Definition. Using a Block we can override the global definition of any symbol, and in particular replace FullDefinition with Definition while running Save:

Block[{FullDefinition},
  FullDefinition = Definition;
  Save[filename, c]
  ];
FilePrint[filename]
DeleteFile[filename]

The magic works:

c := 2*a + b

EDIT. Wrapping things up with the right attributes:

SetAttributes[truncatedSave, HoldRest]
truncatedSave[filename_, args__] := Block[{FullDefinition},
   FullDefinition = Definition;
   Save[filename, args]];
安静 2024-10-07 13:22:10

我想

DumpSave["test1", c]  

是这样吗。

示例代码:

a := b;
c := 2 a + b;
DumpSave["test1", c];
Clear[a, c];
<< test1
?a
?c

输出

_____________________
Global`a
_____________________
Global`c
c:=2 a+b

I think

DumpSave["test1", c]  

Does that.

Sample code:

a := b;
c := 2 a + b;
DumpSave["test1", c];
Clear[a, c];
<< test1
?a
?c

Out

_____________________
Global`a
_____________________
Global`c
c:=2 a+b
友欢 2024-10-07 13:22:10

警告-警告-我不知道我在做什么

刚刚随机浏览帮助系统发现了这个。

以前从未使用过 RunThrough ...无论如何似乎都可以做你想做的事。

Clear["Global`*"];  
a := b;  
c := 2 a + b;  
mathcommand =  StringReplace[First[$CommandLine], "MathKernel" -> "math"];
outputfile = "c:\\rtout";
RunThrough[mathcommand <> " -noprompt", Unevaluated[Put[Definition[c], "c:\\rtout"]]]
FilePrint[outputfile]
Clear[a, c];
<< "c:\\rtout"
DeleteFile[outputfile]
?c  

输出

c := 2*a + b
_______________________________
Global`c
c:=2 a+b

编辑.. 适用于带有一点 Hold-Fu 的列表

Clear["Global`*"];

(*Trick here *)
f[l_] := Definition @@ HoldPattern /@ Unevaluated@l;
SetAttributes[f, HoldFirst];

a := b;
c := 2 a + b;
d := 3 a + b;
mathcommand = StringReplace[First[$CommandLine], "MathKernel" -> "math"];
outputfile = "c:\\rtout";

RunThrough[mathcommand <> " -noprompt",Unevaluated[Put[Evaluate[f@{c, d}], "c:\\rtout"]]]

(* test *)

FilePrint[outputfile]
Clear[a, c, d];
<< "c:\\rtout"
DeleteFile[outputfile]  
?c  
?d   

Warning - Warning - I don't know what I am doing

Just found this browsing the help system randomly.

Never before used RunThrough ... anyway seems to do what you want.

Clear["Global`*"];  
a := b;  
c := 2 a + b;  
mathcommand =  StringReplace[First[$CommandLine], "MathKernel" -> "math"];
outputfile = "c:\\rtout";
RunThrough[mathcommand <> " -noprompt", Unevaluated[Put[Definition[c], "c:\\rtout"]]]
FilePrint[outputfile]
Clear[a, c];
<< "c:\\rtout"
DeleteFile[outputfile]
?c  

Out

c := 2*a + b
_______________________________
Global`c
c:=2 a+b

Edit.. Works on lists with a little Hold-Fu

Clear["Global`*"];

(*Trick here *)
f[l_] := Definition @@ HoldPattern /@ Unevaluated@l;
SetAttributes[f, HoldFirst];

a := b;
c := 2 a + b;
d := 3 a + b;
mathcommand = StringReplace[First[$CommandLine], "MathKernel" -> "math"];
outputfile = "c:\\rtout";

RunThrough[mathcommand <> " -noprompt",Unevaluated[Put[Evaluate[f@{c, d}], "c:\\rtout"]]]

(* test *)

FilePrint[outputfile]
Clear[a, c, d];
<< "c:\\rtout"
DeleteFile[outputfile]  
?c  
?d   

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