serializing a SparseMatrix in c++

Questions and problems about using polymake go here.
jrei
Posts: 15
Joined: 31 Jan 2019, 15:37

serializing a SparseMatrix in c++

Postby jrei » 22 Apr 2019, 14:02

Hello,

I want to serialize a SparseMatrix. I noticed, that it's possible to output a SparseMatrix as

Code: Select all

cout<<"SparseMatrix: "<< someSparseMatrix
. The same is not possible for other streams:

Code: Select all

std::ofstream outputFile outputFile<<"SparseMatrix: "<< someSparseMatrix
This fails.
However i didn't find the "<<"-operator definition in the sources. Could you guide me to where the operator is defined?

User avatar
gawrilow
Main Author
Posts: 425
Joined: 25 Dec 2010, 17:40

Re: serializing a SparseMatrix in c++

Postby gawrilow » 22 Apr 2019, 16:10

There is a generic output operator for all kinds of matrices, but it deliberately does not apply to standard streams by default. You must add a small decoration:

Code: Select all

wrap(outputFile) << "SparseMatrix: "<< someSparseMatrix;
BTW, cout that you see everywhere in polymake is not std::cout but an output channel redirected to perl STDOUT, so that the entire output appears in proper order regardless of being produced in a C++ function or by a perl print statement.

If you are going to read the data later in other functions within polymake realm, I'd recommend to use save_data() instead of plain text printing. In complex cases like matrices with polynomial coefficients the latter may produce an output which can't be parsed later, while the XML files are guaranteed to be understood without any information lost even after several version upgrades. But if it's just about producing a logfile or an input to another software system, then it's surely fine to use plain text.

jrei
Posts: 15
Joined: 31 Jan 2019, 15:37

Re: serializing a SparseMatrix in c++

Postby jrei » 26 Apr 2019, 10:37

Thank you for your answer,

save_data() does the job for matrices. However it seems not to work for other data objects, like EdgeMaps. The saved EdgeMap has its data type written in the .xml file. However it doesn't contain any information on the graph it's based on. I assume that is why load_data() gives the error

Code: Select all

no matching overloaded instance of EdgeMap<Directed, Bool>->new(ARRAY)
Is there a convenient way to save and load EdgeMaps? Otherwise i think i will have to save the values as a list and assign them in a new edgeMap later.

Kind regards,
JR

User avatar
gawrilow
Main Author
Posts: 425
Joined: 25 Dec 2010, 17:40

Re: serializing a SparseMatrix in c++

Postby gawrilow » 26 Apr 2019, 13:53

EdgeMaps must be stored together with the graph, they can't live in isolation. Unfortunately, save_data does not support this special case now. What you can do is to store it as an attachment of the big object, passing the property path descending to the corresponding ADJACENCY property as an additional argument to attach(). If it's not an option for you, because you want to store it in a separate file, then you have in fact to use plain printout.

jrei
Posts: 15
Joined: 31 Jan 2019, 15:37

Re: serializing a SparseMatrix in c++

Postby jrei » 28 Apr 2019, 13:05

What you can do is to store it as an attachment of the big object, passing the property path descending to the corresponding ADJACENCY property as an additional argument to attach().
I'd like to do that. However, when using it as described here I get an error during compilation because no such function is known. That is for a G.attach where G is a Graph<Directed>.

User avatar
gawrilow
Main Author
Posts: 425
Joined: 25 Dec 2010, 17:40

Re: serializing a SparseMatrix in c++

Postby gawrilow » 28 Apr 2019, 15:12

Graph in C++ is the data structure holding just the ADJACENCY. The attach() method belongs to a `big' object holding all graph properties. On the C++ side, it's represented by the class perl::Object.

jrei
Posts: 15
Joined: 31 Jan 2019, 15:37

Re: serializing a SparseMatrix in c++

Postby jrei » 30 Apr 2019, 09:08

How do i pass the property path to attach?
I tried

Code: Select all

object.attach("name")<<graph<<edgeMap
and

Code: Select all

object.attach("name")<<"ADJACENCY"<<edgeMap
where graph holds the ADJACENCY of the object. But it seems that both are wrong.

Thank you

User avatar
gawrilow
Main Author
Posts: 425
Joined: 25 Dec 2010, 17:40

Re: serializing a SparseMatrix in c++

Postby gawrilow » 30 Apr 2019, 11:49

Argh... the C++ interface lacks a method for that. Stupid mistake. Such a misery happens when too few people are actively using the library...

The fix will appear on the master branch in the next few days, but if you are working with official releases only, you'll miss it for several months, I'm afraid, as our last release has been baked just a couple of weeks ago.

You can try this ugly workaround for the time being:

Code: Select all

object.call_method("attach", "name", edgeMap, "ADJACENCY");


Return to “Helpdesk”