简体   繁体   中英

Block operations on Sparse Matrices- Eigen Toolbox- C++

Block operations for sparse matrices - Eigen Toolbox - C++ 


#include "Eigen/Dense"
#include "Eigen/Sparse"
#include <iostream>
using namespace std;
using namespace Eigen;

    int main()
    {
    MatrixXd silly(6, 3);
    silly << 0, 1, 2,
            0, 3, 0,
            2, 0, 0,
            3, 2, 1,
            0, 1, 0,
            2, 0, 0;

        SparseMatrix<double> sparse_silly,temp;
        sparse_silly= Eigen::SparseMatrix<double>(6, 3);
        temp = Eigen::SparseMatrix<double>(6, 3);
        sparse_silly = silly.sparseView();

        std::cout << "Whole Matrix" << std::endl;
        std::cout << sparse_silly << std::endl;



         temp.block(0, 0, 2, 2)=sparse_silly.block(0, 0, 2, 2);

        std::cout << "block of matrix" << std::endl;
        std::cout << temp.block(0, 0, 2, 2) << std::endl;

        getchar();
        return 0;
    }

In the above code, the block operations for sparse matrices are not working using Eigen toolbox. I want a assign a block from a sparse_silly to a block in temp matrix. The output printed is zero for the temp matrix.Can anyone help me if i missed something conceptually. The recent documentation says block operations are availble for sparse matrices.

Blocks of sparse matrices in Eigen are not all writable. Certain ones are (eg .col(Index) in a column major matrix) but the generic .block(Index, Index, Index, Index) is not. The documentation is quite confusing on that matter, but if you look closely, all the examples are with dense matrices and not sparse ones. The col() documentation uses a dense matrix example as well, but if you try it, you'll see that it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM