gpt4 book ai didi

c++ - 稀疏矩阵上的 block 操作 - Eigen Toolbox - C++

转载 作者:行者123 更新时间:2023-11-30 02:33:43 25 4
gpt4 key购买 nike

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;
}

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

最佳答案

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

关于c++ - 稀疏矩阵上的 block 操作 - Eigen Toolbox - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35124470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com