简体   繁体   English

有效地更新特征中大型稀疏矩阵的某些块?

[英]efficiently updating inplace certain blocks of a large sparse matrix in Eigen?

Suppose that I have a large sparse matrix with the following pattern:假设我有一个具有以下模式的大型稀疏矩阵:

  • the number of nonzeros per column and their locations are fixed每列的非零数及其位置是固定的
  • only matrix block A and B will change and the rest of the matrix stays static;只有矩阵块 A 和 B 会改变,矩阵的其余部分保持不变; (blocks A and B themselves are also sparse with fixed nonzero locations) (块 A 和 B 本身也是稀疏的,具有固定的非零位置)

As instructed in the document , i've initialized the above matrix by按照文档中的说明,我已通过以下方式初始化了上述矩阵

  • reserving the exact number of nonzeros per column for the column major sparse matrix为列主要稀疏矩阵保留每列的非零的确切数量
  • inserting column by column逐列插入
  • inserting from the smallest row index per column从每列的最小行索引插入

In later part of the program, it's natural to reuse the matrix and only updates the A, B blocks inplace.在程序的后面部分,很自然地重用矩阵并且只更新 A、B 块。 Possible ways are:可能的方法是:

  1. accessing existing entries by coeffRef , would introduce binary search so not preferred here.通过coeffRef访问现有条目会引入二进制搜索,因此这里不推荐使用。
  2. iterating over the outer and inner dimensions as documented here迭代此处记录的外部和内部尺寸

However, it seems a bit unnecessary to iterate over all nonzero entries since most part of the sparse matrix stays the same.但是,似乎没有必要遍历所有非零条目,因为稀疏矩阵的大部分都保持不变。

Is it possible to update A, B inplace without iterating over all nonzeros in the matrix?是否可以在不迭代矩阵中的所有非零值的情况下就地更新 A、B?

From what I can tell, the InnerIterator can be used used for this and runs in constant time.据我所知, InnerIterator可用于此并在恒定时间内运行。

Eigen::Index col = 1;
Eigen::Index offset_in_col = 1;

using SparseMatrixD = Eigen::SparseMatrix<double>;
SparseMatrixD mat = ...;

SparseMatrixD::InnerIterator i =
      SparseMatrixD::InnerIterator(mat, col) + offset_in_col;
assert(i.row() == 1);
assert(i.col() == 1);
assert(i.value() == C);

This should access the value C. All you need to know is how many nonzero elements are per column (or inner dimension in general).这应该访问值 C。您只需要知道每列(或通常的内部维度)有多少非零元素。 You don't need to know how many nonzero columns (outer dimensions) are stored because that array ( SparseMatrix.outerIndexPtr() ) has one entry per column.您不需要知道存储了多少非零列(外部维度),因为该数组( SparseMatrix.outerIndexPtr() )每列有一个条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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