简体   繁体   English

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

[英]Sparse matrix construction using Octave API for C++

I'm trying to use the Octave C++ API for solving sparse linear system. 我正在尝试使用Octave C ++ API来解决稀疏线性系统。 The problem I have encountered is that I cannot find an efficient way to construct the sparse linear system. 我遇到的问题是我找不到构建稀疏线性系统的有效方法。

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];

The above piece of code describes my naive implementation which is extremely inefficient, and I expect that there should be some member functions to massively insert values into the sparse matrix. 上面的代码描述了我的幼稚实现,效率极低,我希望应该有一些成员函数可以将值大规模插入稀疏矩阵中。

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

However, I cannot find any member functions for doing so. 但是,我找不到这样做的任何成员函数。 At least, it seems that the naive implementation is the only approach. 至少,似乎天真的实现是唯一的方法。

Given I have prepared a matrix in some format, I would like to ask if there is any method to construct a sparse matrix efficiently using Octave API for C++? 考虑到我已经准备了某种格式的矩阵,我想问一下是否存在使用Octave API for C ++有效构造稀疏矩阵的方法?

I just found this tutorial, the method in the first paragraph looks like what you were looking for. 我刚刚找到了本教程,第一段中的方法看起来像您想要的。

link 链接

@wirew0rm The method mentioned in the link is trivial and inefficient. @ wirew0rm​​链接中提到的方法微不足道且效率低下。

In fact, I have already found a solution which basically modifies the content of a inherited member SparseMatrixRep. 实际上,我已经找到了一种解决方案,该解决方案基本上可以修改继承的成员SparseMatrixRep的内容。

Though the documentation stated that it is unsafe and not recommended, the performance is significantly improved. 尽管文档指出这是不安全的并且不建议使用,但是性能还是得到了显着提高。

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

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