使用C++的Octave API构造稀疏矩阵

Sparse matrix construction using Octave API for C++

本文关键字:C++ Octave API 使用      更新时间:2023-10-16

我正在尝试使用Octave C++API来解决稀疏线性系统。我遇到的问题是,我找不到一种有效的方法来构造稀疏线性系统。

SparseMatrix A;
int dim,nnz;
// dim = dimension
// nnz = number of non-zero entry in the matrix A
/*
    somehow assign the values for dim and nnz
*/
A = SparseMatrix ( dim, dim, nnz );
// row index array
int *pidx_r = new int[nnz];
// col index array
int *pidx_c = new int[nnz];
// value array
double *pval = new double[nnz];
// total number of nonzero elements
int tot; 
/*
    somehow assign values for pidx_r, pidx_c, pval and tot
*/
for ( int i = 0; i < tot; i++ )
    A ( pidx_r[i], pidx_c[i] ) = pval[i];

上面的代码描述了我的天真实现,它效率极低,我希望应该有一些成员函数来将值大规模插入稀疏矩阵。

例如,A=SparseMatrix(pidx_r,pidx_c,pval);

然而,我找不到任何成员函数可以这样做。至少,天真的实现似乎是唯一的方法。

假设我已经准备了某种格式的矩阵,我想问是否有任何方法可以使用C++的Octave API有效地构造稀疏矩阵?

我刚刚找到了这个教程,第一段中的方法看起来像你想要的。

链接

@wirew0rm链接中提到的方法琐碎且效率低下。

事实上,我已经找到了一个解决方案,它基本上修改了继承成员SparseMatrixRep的内容。

尽管文件中指出它是不安全的,不推荐使用,但性能得到了显著改善。