繁体   English   中英

稀疏矩阵坐标存储格式:从行优先转换为列优先

[英]sparse matrix Coordinate Storage format: convert from row-major to column-major

我有两个使用坐标存储格式的稀疏矩阵进行操作的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还支持压缩列格式。

如果您可以控制数据结构的执行,那么一种干净有效的方法是将格式存储为结构数组,然后将wrt排序到相关列,例如

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);});

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

暂无
暂无

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

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