加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – Eigen中的SparseMatrix

发布时间:2020-12-16 07:08:00 所属栏目:百科 来源:网络整理
导读:如果我在Eigen中设置SparseMatrix条目的值如下: sparse_matrix-coeffref(10,10) = 0; 这实际上会缩小矩阵所需的存储量,还是会尝试存储0并在那里使用4个字节(假设整数类型)? 如果答案是后者,我如何将列设置为0,以便它不使用任何额外的空间? 还有,这样的事
如果我在Eigen中设置SparseMatrix条目的值如下:

sparse_matrix->coeffref(10,10) = 0;

这实际上会缩小矩阵所需的存储量,还是会尝试存储0并在那里使用4个字节(假设整数类型)?

如果答案是后者,我如何将列设置为0,以便它不使用任何额外的空间?

还有,这样的事情:

typedef Eigen::Triplet<double> TripletType;
std::vector<TripletType> t;
for (int i = 0; i < some_value; ++i) {
    for (int j = 0; j < some_value; ++j) {
        t->push_back(TripletType(i,j,0);
    }
}
sparse_matrix->setFromTriplets(t);

这会导致稀疏矩阵中的显式零吗?

解决方法

使用coeffref插入后,您可以修剪稀疏矩阵,如:

Eigen :: SparseMatrix< double,Eigen :: ColMajor> A(5,5);
//填写A.
A.insert(0,0)= 9 .;
A.insert(1,0)= 3.0 / 2.0;
A.insert(0,1)= 3.0 / 2.0;
A.insert(2,0)= 6.0;
A.insert(0,2)= 6.0;
A.insert(3,0)= 3.0 / 4.0;
A.insert(0,3)= 3.0 / 4.0;
A.insert(4,0)= 3.0;
A.insert(0,4)= 3.0;
A.insert(1,1)= 1.0 / 2.0;
A.insert(2,2)= 12.0;
A.insert(3,3)= 5.0 / 8.0;
A.insert(4,4)= 16.0;
std :: cout<< A<<的std :: ENDL;
std :: cout<< A.data().size()<<的std :: ENDL;
A.coeffRef(3,0)= 0;
A.prune(0,0); //抑制在宽容epsilon下比参考小得多的所有非零值
std :: cout<< A<<的std :: ENDL;
std :: cout<< A.data().size()<<的std :: ENDL; 输出:

Nonzero entries:
(9,0) (1.5,1) (6,2) (0.75,3) (3,4) (_,_) (_,_) (1.5,0) (0.5,0) (12,2
) (0.75,0) (0.625,0) (16,4)

Outer pointers:
0 8 10 12 14  $
Inner non zeros:
5 2 2 2 2  $

9 1.5 6 0.75 3
1.5 0.5 0 0 0
6 0 12 0 0
0.75 0 0 0.625 0
3 0 0 0 16

16
Nonzero entries:
(9,2) (3,4) (1.5,0)
(16,4)

Outer pointers:
0 4 6 8 10  $

9 1.5 6 0.75 3
1.5 0.5 0 0 0
6 0 12 0 0
0 0 0 0.625 0
3 0 0 0 16

12

您可以看到大小已从16更改为12,同样删除了三个(_,_).

我没有检查sizeof()是否需要的内存存储真的更少.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读