稀疏矩阵上的块操作 - 特征工具箱 - C++

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

本文关键字:操作 特征 工具箱 C++      更新时间:2023-10-16
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;
    }

在上面的代码中,稀疏矩阵的块操作无法使用特征工具箱。我想要将一个块从sparse_silly分配给临时矩阵中的块。对于临时矩阵,打印的输出为零。如果我在概念上错过了一些东西,谁能帮助我。最近的文档说块操作可用于稀疏矩阵。

特征矩阵

中的稀疏矩阵块并不都是可写的。某些是(例如 .col(Index)列主矩阵中),但通用.block(Index, Index, Index, Index)不是。文档在这个问题上非常令人困惑,但如果你仔细观察,所有的例子都是密集矩阵而不是稀疏矩阵。col()文档也使用密集矩阵示例,但如果您尝试一下,您会发现它有效。