微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为稀疏矩阵类编写 C++ 迭代器

如何解决为稀疏矩阵类编写 C++ 迭代器

我正在尝试让一个基本的常量前向迭代器在 C++ 中工作。

namespace Rcpp {
    class SparseMatrix {
    public:
        IntegerVector i,p;
        NumericVector x;
   
        int begin_col(int j) { return p[j]; };
        int end_col(int j) { return p[j + 1]; };
        
        class iterator {
        public:
            int index;
            iterator(SparseMatrix& g) : parent(g) {}
            iterator(int ind) { index = ind; };                       // ERROR!
            bool operator!=(int x) const { return index != x; };
            iterator operator++(int) { ++index; return (*this); };
            int row() { return parent.i[index]; };
            double value() { return parent.x[index]; };
        private:
            SparseMatrix& parent;
        };
    };    
}

我的意图是在类似于以下的上下文中使用迭代器:

// sum of values in column 7
Rcpp::SparseMatrix A(nrow,ncol,fill::random);
double sum = 0;
for(Rcpp::SparseMatrix::iterator it = A.begin_col(7); it != A.end_col(7); it++)
    sum += it.value();

两个问题:

  1. 编译器在上面指示的行上抛出错误uninitialized reference member in 'class Rcpp::SparseMatrix&' [-fpermissive]。如何解决这个问题?
  2. 如何重新处理 double value() { return parent.x[index]; }; 以返回指向该值的指针而不是该值的副本?

关于 SparseMatrix 类的一些上下文:就像 R 中的 dgCMatrix,类 SparseMatrix 的这个对象由三个向量组成:

  • i 保存 x 中每个元素的行指针
  • p 给出 i 中对应于每列开头的索引
  • x 包含非零值

解决方法

感谢@Evg,这是解决方案:

namespace Rcpp {
    class SparseMatrix {
    public:
        IntegerVector i,p;
        NumericVector x;
   
        class iterator {
        public:
            int index;
            iterator(SparseMatrix& g,int ind) : parent(g) { index = ind; }
            bool operator!=(iterator x) const { return index != x.index; };
            iterator& operator++() { ++index; return (*this); };
            int row() { return parent.i[index]; };
            double& value() { return parent.x[index]; };
        private:
            SparseMatrix& parent;
        };

        iterator begin_col(int j) { return iterator(*this,p[j]); };
        iterator end_col(int j) { return iterator(*this,p[j + 1]); };
    };    
}

例如,它可以用于计算colSums:

//[[Rcpp::export]]
Rcpp::NumericVector Rcpp_colSums(Rcpp::SparseMatrix& A) {
    Rcpp::NumericVector sums(A.cols());
    for (int i = 0; i < A.cols(); ++i)
        for (Rcpp::SparseMatrix::iterator it = A.begin_col(i); it != A.end_col(i); it++)
            sums(i) += it.value();
    return sums;
}

而且,当从 R 进行微基准测试时,上述函数比 RcppArmadilloRcppEigenR::Matrix 等价函数更快!

编辑:

上述语法的灵感来自犰狳。我开始意识到稍微不同的语法(涉及较少的结构)提供了一个类似于 Eigen 的迭代器:

class col_iterator {
    public:
      col_iterator(SparseMatrix& ptr,int col) : ptr(ptr) { indx = ptr.p[col]; max_index = ptr.p[col + 1]; }
      operator bool() const { return (indx != max_index); }
      col_iterator& operator++() { ++indx; return *this; }
      const double& value() const { return ptr.x[indx]; }
      int row() const { return ptr.i[indx]; }
    private:
      SparseMatrix& ptr;
      int indx,max_index;
    };

然后可以这样使用:

int col = 0;
for (Rcpp::SparseMatrix::col_iterator it(A,col); it; ++it)
     Rprintf("row: %3d,value: %10.2e",it.row(),it.value());

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?