Macaulay2 » Documentation
Packages » MRDI » loadMRDI
next | previous | forward | backward | up | index | toc

loadMRDI -- deserialize a Macaulay2 object from MRDI format

Description

This method parses an MRDI-formatted JSON string (or hash table) and reconstructs the corresponding Macaulay2 object. The namespace field (_ns) in the JSON determines which set of load methods is used for deserialization.

This function handles the _refs section of the JSON to reconstruct shared references via UUIDs. For example, when an ideal and a ring element both refer to the same polynomial ring, the ring is constructed once and shared.

A polynomial ring element can be round-tripped through the format.

i1 : R = QQ[x,y,z,w];
i2 : f = x^2 + y*z

      2
o2 = x  + y*z

o2 : R
i3 : s = saveMRDI f

o3 = {"_type": {"params": "6caf806b-9118-4741-bec7-217a71096848", "name":
     "RingElement"}, "data": [[["2", "0", "0", "0"], ["1", "1"]], [["0", "1",
     "1", "0"], ["1", "1"]]], "_ns": {"Macaulay2": ["https://macaulay2.com",
     "1.26.05"]}, "_refs": {"6caf806b-9118-4741-bec7-217a71096848": {"_type":
     {"params": {"_type": "Ring", "data": "QQ"}, "name": "PolynomialRing"},
     "data": {"variables": ["x", "y", "z", "w"]}}}}
i4 : g = loadMRDI s

      2
o4 = x  + y*z

o4 : R
i5 : f === g

o5 = true

The same works for ideals.

i6 : I = monomialCurveIdeal(R, {1,2,3})

             2                    2
o6 = ideal (z  - y*w, y*z - x*w, y  - x*z)

o6 : Ideal of R
i7 : s = saveMRDI I

o7 = {"_type": {"params": "6caf806b-9118-4741-bec7-217a71096848", "name":
     "Ideal"}, "data": [[[["0", "0", "2", "0"], ["1", "1"]], [["0", "1", "0",
     "1"], ["-1", "1"]]], [[["0", "1", "1", "0"], ["1", "1"]], [["1", "0",
     "0", "1"], ["-1", "1"]]], [[["0", "2", "0", "0"], ["1", "1"]], [["1",
     "0", "1", "0"], ["-1", "1"]]]], "_ns": {"Macaulay2":
     ["https://macaulay2.com", "1.26.05"]}, "_refs":
     {"6caf806b-9118-4741-bec7-217a71096848": {"_type": {"params": {"_type":
     "Ring", "data": "QQ"}, "name": "PolynomialRing"}, "data": {"variables":
     ["x", "y", "z", "w"]}}}}
i8 : J = loadMRDI s

             2                    2
o8 = ideal (z  - y*w, y*z - x*w, y  - x*z)

o8 : Ideal of R
i9 : I === J

o9 = true

Objects can be loaded from a file as well using get.

i10 : fn = temporaryFileName() | ".mrdi"

o10 = /tmp/M2-39414-0/0.mrdi
i11 : saveMRDI(I, FileName => fn)

o11 = {"_type": {"params": "6caf806b-9118-4741-bec7-217a71096848", "name":
      "Ideal"}, "data": [[[["0", "0", "2", "0"], ["1", "1"]], [["0", "1",
      "0", "1"], ["-1", "1"]]], [[["0", "1", "1", "0"], ["1", "1"]], [["1",
      "0", "0", "1"], ["-1", "1"]]], [[["0", "2", "0", "0"], ["1", "1"]],
      [["1", "0", "1", "0"], ["-1", "1"]]]], "_ns": {"Macaulay2":
      ["https://macaulay2.com", "1.26.05"]}, "_refs":
      {"6caf806b-9118-4741-bec7-217a71096848": {"_type": {"params": {"_type":
      "Ring", "data": "QQ"}, "name": "PolynomialRing"}, "data": {"variables":
      ["x", "y", "z", "w"]}}}}
i12 : J = loadMRDI get fn

              2                    2
o12 = ideal (z  - y*w, y*z - x*w, y  - x*z)

o12 : Ideal of R
i13 : I === J

o13 = true
i14 : removeFile fn

A parsed hash table can also be passed directly, for instance when using ToString => false with saveMRDI.

i15 : h = saveMRDI(42, ToString => false)

o15 = HashTable{"_ns" => HashTable{"Macaulay2" => {https://macaulay2.com, 1.26.05}}}
                "_type" => ZZ
                "data" => 42

o15 : HashTable
i16 : loadMRDI h

o16 = 42

The MRDI format supports cross-system interoperability. Objects serialized by the OSCAR computer algebra system can also be loaded.

i17 : loadMRDI "{\"_ns\":{\"Oscar\":[\"https://github.com/oscar-system/Oscar.jl\",\"1.5.0\"]},\"_type\":\"ZZRingElem\",\"data\":\"42\"}"

o17 = 42

To see which types have built-in load methods for a given namespace, call methods as follows.

i18 : methods {"Macaulay2", loadMRDI}

o18 = {0 => ({Macaulay2, loadMRDI}, Ring)          }
      {1 => ({Macaulay2, loadMRDI}, ZZ)            }
      {2 => ({Macaulay2, loadMRDI}, Matrix)        }
      {3 => ({Macaulay2, loadMRDI}, GaloisField)   }
      {4 => ({Macaulay2, loadMRDI}, PolynomialRing)}
      {5 => ({Macaulay2, loadMRDI}, Ideal)         }
      {6 => ({Macaulay2, loadMRDI}, QuotientRing)  }
      {7 => ({Macaulay2, loadMRDI}, RingElement)   }

o18 : NumberedVerticalList
i19 : methods {"Oscar", loadMRDI}

o19 = {0 => ({Oscar, loadMRDI}, ZZRing)       }
      {1 => ({Oscar, loadMRDI}, String)       }
      {2 => ({Oscar, loadMRDI}, Base.Int)     }
      {3 => ({Oscar, loadMRDI}, MPolyRingElem)}
      {4 => ({Oscar, loadMRDI}, MPolyRing)    }
      {5 => ({Oscar, loadMRDI}, QQFieldElem)  }
      {6 => ({Oscar, loadMRDI}, QQField)      }
      {7 => ({Oscar, loadMRDI}, Float64)      }
      {8 => ({Oscar, loadMRDI}, PolyRingElem) }
      {9 => ({Oscar, loadMRDI}, FiniteField)  }
      {10 => ({Oscar, loadMRDI}, PolyRing)    }
      {11 => ({Oscar, loadMRDI}, ZZRingElem)  }

o19 : NumberedVerticalList

Additional types can be supported by calling addLoadMethod.

Caveat

If the JSON string references a namespace or type for which no load method has been registered, an error is produced. Use addLoadMethod to register handlers for custom types and addNamespace to register new namespaces.

See also

Menu

Ways to use loadMRDI:

  • loadMRDI(HashTable)
  • loadMRDI(String)

For the programmer

The object loadMRDI is a method function.


The source of this document is in /build/reproducible-path/macaulay2-1.26.05+ds/M2/Macaulay2/packages/MRDI.m2:657:0.