我有两个使用稀疏矩阵以坐标存储格式运行的c ++函数(foo,goo),
也就是说,矩阵以3个数组的形式给出:row_index [nnz],column_index [nnz],value [nnz]
其中nnz是非零元素的数量。

foo以行优先顺序返回稀疏矩阵,例如:


1 1 4.000000
1 2 4.000000
2 1 6.000000
2 3 8.000000
3 3 10.000000


Goo则需要将向量按“按列的主要顺序”排序,即:


1 1 4.000000
2 1 6.000000 //已更改
1 2 4.000000 //已更改
2 3 8.000000
3 3 10.000000


我如何以最有效的方式进行转换?

附加信息:
goo还支持压缩列格式。

最佳答案

如果您对执行过程中的数据结构有控制权,那么一种干净有效的方法是将格式存储为结构数组,然后将w.r.t排序到相关列,例如

typedef std::tuple<size_t,size_t,double> elem_t;
// std::get<0>(storage[i]) is the row index of the i-th non-zero
// std::get<1>(storage[i]) is the col index of the i-th non-zero
// std::get<2>(storage[i]) is the value of the i-th non-zero
std::vector<elem_t> storage;
// this sort can be parallel
std::sort(storage.begin(),storage.end(),[](const elem_t& L, const elem_t& R){return std::get<1>(L)<std::get<1>(R);});


如果不是,则可以编写一个函子来根据该列进行索引排序,然后进行置换。当然,这更加混乱,并且会导致内存开销。

关于c++ - 稀疏矩阵坐标存储格式:从行优先转换为列优先,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27177664/

10-17 00:18