Concatenate sparse matrix Eigen

eigen/sparse matrix
eigen::map sparse matrix
eigen/sparse matrix inverse
eigen/sparse identity matrix
eigen csr format
eigen sparse
sparse matrix manipulation
c++ sparse matrix

I have two sparse matrices in Eigen, and I would like to join them vertically into one. As an example the target of the code would be:

SparseMatrix<double> matrix1;
matrix1.resize(10, 10);
SparseMatrix<double> matrix2;
matrix2.resize(5, 10);

SparseMatrix<double> MATRIX_JOIN;
MATRIX_JOIN.resize(15, 10);
MATRIX_JOIN << matrix1, matrix2;

I found some solutions on a forum however, I wasn't able to implement it.

What's the proper way to join the matrices vertically?


My implementation:

SparseMatrix<double> L;
SparseMatrix<double> C;
// ... (Operations with the matrices)
SparseMatrix<double> EMATRIX;
EMATRIX.resize(L.rows() + C.rows(), L.cols());
EMATRIX.middleRows(0, L.rows()) = L;
EMATRIX.middleRows(L.rows(), C.rows()) = C;

I get an error of types, acording to the compiler the right hand side is an Eigen::Block and the left side is Eigen::SparseMatrix

As far as I know, there is currently no built-in solution. You can be way more efficient than your solution by using the internal insertBack function:

SparseMatrix<double> M(L.rows() + C.rows(), L.cols());
M.reserve(L.nonZeros() + C.nonZeros());
for(Index c=0; c<L.cols(); ++c)
    M.startVec(c); // Important: Must be called once for each column before inserting!
    for(SparseMatrix<double>::InnerIterator itL(L, c); itL; ++itL)
         M.insertBack(itL.row(), c) = itL.value();
    for(SparseMatrix<double>::InnerIterator itC(C, c); itC; ++itC)
         M.insertBack(itC.row()+L.rows(), c) = itC.value();

Quick reference guide for sparse matrices, Sparse Matrix Initialization. Category, Operations, Notes. Constructor. SparseMatrix<double> sm1(1000,1000);. SparseMatrix<  The class SparseMatrix is the main sparse matrix representation of Eigen's sparse module; it offers high performance and low memory usage. It implements a more versatile variant of the widely-used Compressed Column (or Row) Storage scheme.

I ended up doing the following:

MATRIX_JOIN.resize(matrix1.rows() + matrix2.rows(), matrix1.cols() + matrix2.cols());

// Fill MATRIX_JOIN with triples from the other matrices
std::vector<Triplet<double> > tripletList;
for (int k = 0; k < matrix1.outerSize(); ++k)
    for (SparseMatrix<double>::InnerIterator it(matrix1, k); it; ++it)
        tripletList.push_back(Triplet<double>(it.row(), it.col(), it.value()));
for (int k = 0; k < matrix2.outerSize(); ++k)
    for (SparseMatrix<double>::InnerIterator it(matrix2, k); it; ++it)
        tripletList.push_back(Triplet<double>(it.row(), it.col(), it.value()));
FINALMATRIX.setFromTriplets(tripletList.begin(), tripletList.end());

There can be a speedup by calling tripleList.reserve(X) with X being the expected amount of triplets to insert.

concatenate two sparse matrices • KDE Community Forums, Re: concatenate two sparse matrices. Fri Jul 20, 2012 1:51 pm. Thanks dzenanz, However, unfortunately, Eigen sparse seems not to support yet  r,matlab,matrix,sparse-matrix. When converting from a full representation of a symmetric matrix to a sparse representation, you will need to scan all the elements on the main diagonal and above (or symmetrically on the main diagonal and below). For an (n x n) matrix matrix this is (n^2+n)/2 entries that need to

Sparse Matrix Manipulation in Libigl, #include <Eigen/Sparse> Perform concatenation of a two matrices along a single dimension Mat matrix type for all matrices (e.g. MatrixXd, SparseMatrix). In this page, we give a quick summary of the main operations available for sparse matrices in the class SparseMatrix. First, it is recommended to read the introductory tutorial at Sparse matrix manipulations. The important point to have in mind when working on sparse matrices is how they are stored : i.e either row major or column major.

libigl/cat.h at master · libigl/libigl · GitHub, Reordering, factoring, and computing with sparse matrices. Matrix concatenation using either the cat function or square brackets produces sparse A second matrix, B , can be included for the generalized eigenvalue problem: Aυ = λBυ. If S is a sparse matrix, then chol (S) is also a sparse matrix, and diag (S) is a sparse vector. Columnwise functions such as max and sum also return sparse vectors, even though these vectors can be entirely nonzero. Important exceptions to this rule are the sparse and full functions.

Sparse Matrix Operations - MATLAB & Simulink, Three steps are necessary to create a block sparse matrix: bogus::​SparseBlockMatrix< Eigen::MatrixXd > sbm ; CompoundBlockMatrix is a utility class representing the concatenation of two objects, which may have different block types. Eigen contains many more features than I have listed here. In particular, it supports multiple data structures for efficient matrix storage, depending on structural sparsity of values via the Sparse namespace. Further, Eigen has support for LR, Cholesky, SVD and QR decomposition. Eigenvalues can also be calculated in an optimised manner.

So-Bogus: Block, The Eigen C++ matrix library ( uses a specialised sparse matrix format which has deliberate redundancy and overprovisioned storage. While  A block is a rectangular part of a matrix or array. Blocks expressions can be used both as rvalues and as lvalues. As usual with Eigen expressions, this abstraction has zero runtime cost provided that you let your compiler optimize. Using block operations. The most general block operation in Eigen is called .block() . There are two versions, whose syntax is as follows:

  • What does "I wasn't able to implement it." mean?
  • @Code-Apprentice I wasn't able to implement the solution that I found in the forum. It was also meant to join (the solution in the forum) a sparse and a dense matrix
  • Repeating the same words doesn't explain what the words mean. Please show exactly what you did and what happens when you compile and run the code you wrote.
  • Is this more efficient than using triplets?
  • @Javier it should be in all but very weird cases. It may not matter much in many cases.
  • @Pippo Thanks for fixing that mistake. I noticed another one (I actually never tested this code)
  • I think you still have a bug here: When you create result, upper.rows() + upper.rows() should be upper.rows() + lower.rows() I think.
  • There is no block of any kind in my solution. Did you mean the attempt by @Javier in his question? That does indeed not work.