Skip to content

RcppSparse - using in R project #2

@kullrich

Description

@kullrich

Hi,

I would like to integrate a cpp function using RcppSpare.

However, I run into the following issue.
RcppSparse can not be installed from CRAN and installing it locally gave some problems with using `#include <RcppSpare.h>.

Anyhow, I was able to make a function test.cpp that works, but only if I use Rcpp::sourceCpp if I use the same cpp function in my R project and build it it gives errors (see bleow).

Any suggestions of how to make it work inside my R project would be highly appreciated.

It seems that the created class is not attached to the Rcpp namespace, but by using Rcpp::sourceCpp it just works fine, so there the dgCMatrix class is valid?

Thank you in anticipation

Best regards

Kristian

errors:

no type named 'dgCMatrix' in namespace 'Rcpp'; did you mean 'RawMatrix'?
no member of 'dgCMatrix' in namespace 'Rcpp'
Rcpp::traits::input_parameter< Rcpp::dgCMatrix& >::type mat(matSEXP);

function test.cpp that works with Rcpp::sourceCpp:

> library(Rcpp)
> sourceCpp("src/test.cpp")
> library(Rcpp)
> library(Matrix)
> sourceCpp("src/test.cpp")
> set.seed(123)
> spmat <- abs(rsparsematrix(100, 100, 0.1))
> Rcpp_colSums(spmat)
  [1]  6.5400  7.9340 13.7900  6.0476  8.8450  8.1600 10.1900  7.4500  3.1820  8.5000  3.1850  1.5800  5.9100 11.9900  6.5490  9.2222  7.3350 12.1100  4.4900  8.4980 10.4180  3.9100 11.1800
 [24]  7.0100  9.4066 11.6180  1.8400  6.4510  6.5900  4.3620  8.0110 14.1690 12.7960 12.5930  8.7810  4.0210  8.8700  5.7550  7.9600  7.4310 16.5760  6.8300 10.0660  8.5400  4.4130 10.3470
 [47] 10.3422 13.2200  2.0750  8.0500  6.1800  7.7470  3.4470 11.3500  9.0030  7.1611  6.6400 11.1800  4.9200  6.6200  6.1800  9.0500  8.3900  8.3900  4.8660  3.5996  2.4230  5.4200  7.6930
 [70]  6.0900  7.6400 11.2700  4.2600  8.8600  7.5900  7.3982 10.1900  9.9800 10.8819  4.8500  3.6180  9.7180  5.3600 10.0560  2.0700  7.4500 10.0900  2.4070  9.7320  9.9880  8.6200  6.3216
 [93]  9.9680 10.3850 10.9540 10.3800 14.3600  6.0200  5.8300 16.1240
#include <Rcpp.h>

namespace Rcpp {
class dgCMatrix {
public:
    IntegerVector i, p, Dim;
    NumericVector x;
    List Dimnames;

    // constructor
    dgCMatrix(S4 mat) {
        i = mat.slot("i");
        p = mat.slot("p");
        x = mat.slot("x");
        Dim = mat.slot("Dim");
        Dimnames = mat.slot("Dimnames");
    };

    // column iterator
    class col_iterator {
        public:
            int index;
            col_iterator(dgCMatrix& g, int ind) : parent(g) { index = ind; }
            bool operator!=(col_iterator x) { return index != x.index; };
            col_iterator& operator++(int) { ++index; return (*this); };
            int row() { return parent.i[index]; };
            int col() { return column; };
            double& value() { return parent.x[index]; };
        private:
            dgCMatrix& parent;
            int column;
        };
        col_iterator begin_col(int j) { return col_iterator(*this, p[j]); };
        col_iterator end_col(int j) { return col_iterator(*this, p[j + 1]); };

    };

    template <> dgCMatrix as(SEXP mat) { return dgCMatrix(mat); }

    template <> SEXP wrap(const dgCMatrix& sm) {
        S4 s(std::string("dgCMatrix"));
        s.slot("i") = sm.i;
        s.slot("p") = sm.p;
        s.slot("x") = sm.x;
        s.slot("Dim") = sm.Dim;
        s.slot("Dimnames") = sm.Dimnames;
        return s;
    }
}
// [[Rcpp::export]]
Rcpp::NumericVector Rcpp_colSums(Rcpp::dgCMatrix& mat){
    int n_col = mat.p.size() - 1;
    Rcpp::NumericVector sums(n_col);
    for (int col = 0; col < n_col; col++)
       for (Rcpp::dgCMatrix::col_iterator it = mat.begin_col(col); it != mat.end_col(col); it++)
           sums[col] += it.value();
    return sums;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions