簡體   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