3
$\begingroup$

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?

$\endgroup$
14
  • 3
    $\begingroup$ mathematica.stackexchange.com/a/157198/9490 $\endgroup$
    – Jason B.
    Commented Dec 31, 2021 at 12:25
  • $\begingroup$ @JasonB. Thank you, I completely forgot about that question (and did not find it with a search). $\endgroup$ Commented Dec 31, 2021 at 14:41
  • 1
    $\begingroup$ 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. $\endgroup$
    – Jason B.
    Commented Dec 31, 2021 at 16:35
  • $\begingroup$ I forgot that I can close duplicates with a single vote. Hope the solution there is fine for you. $\endgroup$
    – Szabolcs
    Commented Dec 31, 2021 at 17:31
  • 1
    $\begingroup$ 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) $\endgroup$
    – Szabolcs
    Commented Jan 2, 2022 at 9:58

0