重新插入Eigen库的稀疏矩阵

Re-insertion of sparse matrix of Eigen library

我在我的 C++ 代码中使用了 Eigen 库的稀疏矩阵。我的代码中有一个主迭代。在本次迭代开始之前,我初始化了一个已定义的稀疏矩阵(S,即线性方程 (S*x=b) 的系数矩阵)。该矩阵的某些元素必须在每次迭代中更改。但是当我使用 "S.insert(ii,ii)=new_value" 执行此操作时 returns 这个错误:

...断言`(p<=startId || m_data.index(p-1)!=inner) && "you cannot insert an element that already exist, you must call coeffRef to this end"' 失败...

我怎样才能正确地重新插入?

我在 Eigen 库快速参考页面中找到了正确的方法。我必须使用 "S.coeffRef(ii,ii)=new_value" 而不是 "S.insert(ii,ii)=new_value"。 它说:"insert() assumes that the element does not already exist; otherwise, use coeffRef()".