21
$\begingroup$

Compress[] was introduced in version 6 of Mathematica. It gives a compressed version of expressions represented as a string of ASCII characters. It preserves more information than the InputForm of expressions (for example it keeps packed arrays packed).

Are the strings returned by Compress compatible between versions? Can a compressed expression created using version 6 be uncompressed in version 8? What about the reverse (reading a version-8-compressed expression in 6 or 7)?

What happens if a version 8 specific object such as Graph is uncompressed in version 6 or 7?

$\endgroup$

2 Answers 2

14
$\begingroup$

Compress[expr] will take an expression, convert it to a string, using some form, which would allows to recover the expression later on (most likely InputForm is used) and compress the string.

If the resulting compressed expression is uncompressed in an earlier version of Mathematica, the result is going to be an expression, which has no code associated with it, so it will, most likely, just remain unevaluated, if executed.

To reiterate, yes, Compress/Uncompress is cross version compatible, yet the result obtained on uncompressing, may not evaluate in earlier versions of Mathematica

$\endgroup$
5
  • 3
    $\begingroup$ I doubt that it uses InputForm or any usual string representation of expressions, for two reasons: 1. Converting to InputForm is slower: try data = RandomReal[1, 1000000]; ToString[data, InputForm]; // Timing Compress[data]; // Timing 2. It preserves packed arrays: DeveloperPackedArrayQ[Uncompress[Compress[{1, 2, 3}]]]` vs DeveloperPackedArrayQ[ Uncompress[Compress[DeveloperToPackedArray[{1, 2, 3}]]]]. Or perhaps it is only a few objects that it treats specially? $\endgroup$
    – Szabolcs
    Commented Feb 6, 2012 at 21:46
  • $\begingroup$ @Szabolcs I am sure it is not InputForm literally, but rather some form of serialization used. Something similar to what happens with MathLink. I suspect it does not unpack either. $\endgroup$
    – Sasha
    Commented Feb 6, 2012 at 22:01
  • $\begingroup$ Would you go as far to say it's relatively safe to archive data in this format (i.e. 9 and 10 are not likely to break compatibility)? The only good alternative I know, WDX, is very very slow to read and write. (Of course for long term archiving of important things I'd just use a text table, but large and structured data is very convenient to hold in the form of Mathematica expressions) $\endgroup$
    – Szabolcs
    Commented Feb 6, 2012 at 23:18
  • $\begingroup$ As for graphs, since these are dynamic objects, they will presumably trigger the dynamic content warning and return some warning/error when being allowed to evaluate. $\endgroup$
    – Yves Klett
    Commented Feb 7, 2012 at 7:20
  • $\begingroup$ I'd guess that Compress/Uncompress will be based on the internal representation of expressions, which I think is most closely revealed by FullForm. As for Graph: Graph[{1 -> 2, 2 -> 3}] // Compress will be uncompressed to Graph[{1, 2, 3}, {{{1, 2}, {2, 3}}, Null}] in version 7, that's Global`Graph to be precise. $\endgroup$ Commented Feb 7, 2012 at 8:22
1
$\begingroup$

I can only talk about forward compatibility right now. So far I have encountered no problems with compressed/uncompressed expression from versions 6, 7 and 8.

It would be quite against the usually very thorough compatibility policy of WRI to change the behaviour of a documented function that is obviously intended to store data in a persistent fashion. But then one of the WRI people would have to comment about that (I am just assuming).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.