上循环一uBLAS库稀疏矩阵的非零元素 [英] Looping over the non-zero elements of a uBlas sparse matrix

查看:209
本文介绍了上循环一uBLAS库稀疏矩阵的非零元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 O(N)以下稀疏矩阵元素

boost::numeric::ublas::compressed_matrix<int> adjacency (N, N);

我可以写一个蛮力双环走过去在 O(N ^ 2)时的所有条目类似下面,但是这将是太慢了。

I could write a brute force double loop to go over all the entries in O(N^2) time like below, but this is going to be too slow.

for(int i=0; i<N; ++i)
   for(int j=0; j<N; ++j)
       std::cout << adjacency(i,j) std::endl;

我如何遍历只在 O(N)时间非零项?对于每一个非零元素,我想有机会获得它的价值,而指数 I,J

How can I loop over only the non-zero entries in O(N) time? For each non-zero element I would like to have access to its value, and the indexes i,j.

推荐答案

您可以在此常见问题的答案:如何遍历所有非零元素?

You can find the answer in this FAQ: How to iterate over all non zero elements?

在你的情况将是:

typedef boost::numeric::ublas::compressed_matrix<int>::iterator1 it1_t;
typedef boost::numeric::ublas::compressed_matrix<int>::iterator2 it2_t;

for (it1_t it1 = adjacency.begin1(); it1 != adjacency.end1(); it1++)
{
  for (it2_t it2 = it1.begin(); it2 != it1.end(); it2++)
  {
    std::cout << "(" << it2.index1() << "," << it2.index2() << ") = ";
    std::cout << *it2 << std::endl;
  }
}

这篇关于上循环一uBLAS库稀疏矩阵的非零元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆