【问题标题】:sparse matrix Coordinate Storage format: convert from row-major to column-major稀疏矩阵坐标存储格式:从行优先转换为列优先
【发布时间】:2015-01-26 11:08:14
【问题描述】:

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

【问题讨论】:

    标签: c++ matrix


    【解决方案1】:

    如果您可以通过执行来控制数据结构,那么一种干净、有效的方法是将格式存储为结构数组,然后将 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);});
    

    如果没有,可以编写一个仿函数来根据列进行索引排序,然后进行置换。这当然会更加混乱,并且会产生内存开销。

    【讨论】:

    • 这两个函数由两个独立的库提供;所以我无法控制数据结构。
    猜你喜欢
    • 2012-12-10
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多