我正在尝试使用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);
但是,我找不到任何成员函数。至少,似乎天真的实现是唯一的方法。
鉴于我已经准备了某种格式的矩阵,我想问一下使用Octave API for C ++是否有任何方法可以有效地构造稀疏矩阵?
答案 0 :(得分:0)
我刚刚发现这个教程,第一段中的方法看起来就像你要找的那样。
答案 1 :(得分:0)
@ wirew0rm 链接中提到的方法很简单,效率很低。
事实上,我已经找到了一个解决方案,它基本上修改了继承成员SparseMatrixRep的内容。
虽然文档声明它不安全且不推荐,但性能显着提高。