How to save [] definitions associated with a symbol in Mathematica, without saving auxiliary definitions?

The built-in Mathematica Save[file, symbol] FullDefinition[] uses FullDefinition[] to find the definition of symbol and all auxiliary definitions.

For example, commands

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

creates a test.dat file containing

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

I have a program with a lot of MakeBoxes prefixes that I do not save when I save [] many separate results.

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

+4
source share
3 answers

According to the documentation, Save uses FullDefinition while you want to use Definition . Using Block , we can redefine the global definition of any character and, in particular, replace FullDefinition with Definition while Save running:

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

Magic works:

 c := 2*a + b 

EDIT. Combining things with the right attributes:

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

I think,

 DumpSave["test1", c] 

It.

Code example:

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

Output

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

Warning - warning - I don't know what I'm doing

Just found this help system look at random.

RunThrough has never been used before ... it seems to do what you want anyway.

 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 

Output

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

Edit .. Works with small Hold-Fu lists

 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 

+1
source

All Articles