Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 12 down vote accepted

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

share|improve this answer
3  
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? –  Szabolcs Feb 6 '12 at 21:46
    
@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. –  Sasha Feb 6 '12 at 22:01
    
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) –  Szabolcs Feb 6 '12 at 23:18
    
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. –  Yves Klett Feb 7 '12 at 7:20
    
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. –  Albert Retey Feb 7 '12 at 8:22

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).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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