Skip to content
Prev 4037 / 10988 Next

[Rcpp-devel] Question regarding Rcpp/RcppArmadillo and Rcpp/sugar

Hello,

Many thanks to Dirk for the quick and helpful answer. Rcpp is a great
project, really! Yet, I've been struggling a bit, trying to find what what
functionality is there.

If I search for NumericVector::create method in the reference site (
http://dirk.eddelbuettel.com/code/rcpp/html/classMatrix.html), I only find
create(void). That's also what my IDE suggests (I'm currently using QT
Creator on Ubuntu 12.04). However, I happen to find an example like that
one:

IntegerVector v = IntegerVector::create(0,1,NA_INTEGER,3);

Than I realize, that there is some generated source-code that is included
within the class definition for Vector. It turns out that, both the
reference-site generator and my IDE didn't understand the Vector class
definition :(. Fortunately, g++ understands everything.

I suppose, Eclipse would have understood this, because it's intellisense
engine seems to evaluate the macros in real-time, but I abandoned Eclipse,
because it apparently ignores statements like "using namespace std/Rcpp;",
so I got compilation errors on code which compiles smoothly using
command-line g++.

Also, I did the long way of searching/installing many IDE-oriented plug-ins
for VIM (the best one for autocompletion I've found is clang-complete, yet
it is far from perfect).

Do you have an IDE to recommend for Ubuntu? Although the question is not
directly related to Rcpp, knowing its answer might greatly facilitate the
work with Rcpp as well.

Now back to sugar and RcppArmadillo. I have the following code:

 using namespace Rcpp;
 using namespace sugar;
 using namespace alglib;

arma::mat momentcov_arma(const arma::vec& U, const arma::vec& V) {
        arma::vec S = moments_arma(U,V); // some c++ funtion returning a
vector...

        arma::vec Y = V - mean(V);      // If it's getting long, stop
reading and see question below:
        arma::vec X = U - mean(U);
        std::size_t len = S.n_elem;

        arma::mat M(len, len);
        M(0,0) = mean((X%X)%(Y%Y));
        M(1,1) = mean((X%X%X%X)%(Y%Y));  // Is the bold expression using *
operators defined in Armadillo or * operator defined in Sugar?
        M(2,2) = mean(X%X%(Y%Y%Y%Y));
        return M-S*trans(S);
    }


Question: according to QT Creator the line in bold-style will call
sugar::mean on an arma::vec, is that really possible, and if yes, what are
the conversions that happen. The code compiles without error using g++
compiler.

By the way, yesterday I could run the C++ version of my R-program. I got
100 times speedup! My R-code was calling constrOptim on an optimization
task with linear inequality constraints. I could replace the call to
constrOptim with a call to the minbleicoptimizer in the alglib library (
http://www.alglib.net/). So, the speedup came from:
1) faster control statement evaluation in C++ / Rcpp and RcppSugar /.
2) about 10 times fewer evaluations of the target function and gradient in
the minbleicoptimize-call compared to constrOptim call.
3) faster linear algebra expression evaluation with RcppArmadillo.

Best thanks to the authors of all the above-mentioned projects.

The Rcpp-code doesn't look too much different from the R-code. Some
programming overhead came from the need to write a small "glue" library for
conversion between alglib::*_1d/2d_array and arma::Vec/Mat. I hope this,
library can be reused, so I attached its source-code to this mail. Feel
free to use it and to send me feedback on that one. I wonder if it can
become more efficient if one uses std::copy/fill instead of for-loops...

Cheers,
Venelin


2012/7/16 Dirk Eddelbuettel <edd at debian.org>