3

For the purposes of further development of my shortInputForm function, I wish to convert Atomic objects such as Graph into non-atomic Mathematica expressions (not the InputForm strings!) where Graph head itself would be temporarily inactivated. The following obvious method is ugly, highly inefficient, and potentially can have undesirable side-effects:

g = Graph[{UndirectedEdge[1, 2], UndirectedEdge[2, 3], UndirectedEdge[3, 1]}];

ToExpression[StringReplace[ToString[g, InputForm], "Graph" -> "Inactive[Graph]"]]

screenshot

Is there a better way to "un-evaluate" a Graph object?

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
  • 3
    https://mathematica.stackexchange.com/a/157198/9490 – Jason B. Dec 31 '21 at 12:25
  • @JasonB. Thank you, I completely forgot about that question (and did not find it with a search). – Alexey Popkov Dec 31 '21 at 14:41
  • 1
    I only found it because I remembered using the function, and it was not easy to search for. I don’t like it when the “canonical” version of a question has a horrible title. – Jason B. Dec 31 '21 at 16:35
  • I forgot that I can close duplicates with a single vote. Hope the solution there is fine for you. – Szabolcs Dec 31 '21 at 17:31
  • @Szabolcs Yes, it is fine. But that question is really deeply buried - very difficult to find. – Alexey Popkov Dec 31 '21 at 17:52
  • @Szabolcs I just found that Nucleus doesn't work with NumericArray and ByteArray. Do you have an idea how to make it work? – Alexey Popkov Jan 02 '22 at 08:41
  • Don't have time now, but Nucleus is based on my idea here: https://mathematica.stackexchange.com/a/97332/12 You can look into that perhaps, and see if the raw solution works. – Szabolcs Jan 02 '22 at 09:43
  • @Szabolcs No, it doesn't. – Alexey Popkov Jan 02 '22 at 09:57
  • 1
    It is possible that they added proper support for NumericArray to MathLink, making NumericArrays a proper first-class atomic object, obviating the need for the compound form. There needs to be a set of "real" atomic expressions in terms of which compound expressions can be written. So not everything can have a compound form. I'm really not sure. I notice that Rationals can also not be made compound this way. You can also try other serialization formats such as Compress or WXF (though I think WXF supports NumericArray directly) – Szabolcs Jan 02 '22 at 09:58
  • @Szabolcs The Uncompress[Compress[expr], HoldComplete] route also doesn't work. So the only way seems to be the good old ToExpression[ToString[expr,InputForm],InputForm,HoldComplete]? – Alexey Popkov Jan 02 '22 at 11:35
  • I guess so. Or maybe the answer is that you should not do it. It would depend on the application. – Szabolcs Jan 02 '22 at 11:36
  • You can special case these few atomic expressions, like NumericArray, Rational or Complex, and extract their parts using the normal API, e.g. Numerator / Denominator. – Szabolcs Jan 02 '22 at 11:38
  • I am wondering if a Real32/Real64 NumericArray can hold NaN or Inf values (which are not otherwise representable in Mathematica). If yes, that is an argument for not having a compound form. – Szabolcs Jan 02 '22 at 15:55
  • @Szabolcs If you are right, then probably ToExpression[ToString[expr,InputForm],InputForm,HoldComplete] won't return the original array in such cases, which should be considered as a bug. – Alexey Popkov Jan 02 '22 at 15:58

0 Answers0