Operations on Eigen Map sparse objects

Operations on Eigen Map sparse objects



In my application I have to send Eigen::SparseMatrix<float> objects over the network, which means I need to serialize them and on the other end deserialize them into a Eigen::Map<Eigen::SparseMatrix<float>>, as suggested in this question.


Eigen::SparseMatrix<float>


Eigen::Map<Eigen::SparseMatrix<float>>



My issue now is that I'd like to perform some operations on the receiving end, namely sum all matrices that arrive.



However when I try to add together two Eigen::Map objects it produces a compiler error. This is true for any operation I try to perform on an Eigen::Map:


// This leads to a compiler error
Eigen::Map<Eigen::SparseMatrix<float>> recon = SerializedVectorToMappedSparseMatrix(serialized_vec);
Eigen::Map<Eigen::SparseMatrix<float>> recondouble = recon * 2;



error: conversion from ‘const type aka const
Eigen::SparseMatrix’ to non-scalar type
‘Eigen::Map >’ requested

Eigen::Map> recondouble = (recon *
2).eval();



However this compiles and works as expected:


Eigen::SparseMatrix<float> recondouble = recon * 2;



In my network case I'd like to perform a sum of the existing and incoming Map:


existing_matrix += incoming_matrix;



This causes the compiler error:



error: no match for ‘operator=’ (operand types are
‘Eigen::Map >’ and ‘const
Eigen::CwiseBinaryOp,
const Eigen::Map >, const
Eigen::Map > >’) return
derived() = derived() + other.derived();



So my question is: Are we supposed to convert Eigen::Map objects to Eigen:Matrix if we are to perform operations between them? The example in the docs seems to suggest otherwise.



Am I doing something wrong in my use of the Eigen::Map container?




1 Answer
1



A Map object is a wrapper to existing memory, allowing you to treat it as if it's actually an Eigen object with a few exceptions. Specifically, you can't change the memory, so resizing, allocating, etc. aren't allowed. In your example,


Map


Eigen::Map<Eigen::SparseMatrix<float>> recondouble = recon * 2;



should be changed to


Eigen::SparseMatrix<float> recondouble = recon * 2;



(as you noted in your question) as the operator=/c'tor needs to allocate memory for recondouble. The sparse operator+= often needs to reallocate memory unless the rhs elements are a subset of the lhs ones. I don't know if the mapped sparse matrix disallows this operation for this reason or not (and I don't have time to look into it right now).


operator=


recondouble


operator+=



P.S. the example in the docs only talks about dense matrices, and therefore reallocation in operator+= is not relevant.


operator+=





Right, the += operator between sparse matrices could lead to re-allocation because of new indexes being added, hence the operation is not possible. Thanks for the explanation!
– Bar
Aug 23 at 17:41



+=






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

Node.js puppeteer - Use values from array in a loop to cycle through pages