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

Venelin Mitov vmitov at gmail.com
Tue Jul 17 10:43:31 CEST 2012


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

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

        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++

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...


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

> Hi Venelin,
> Thanks for posting here.
> On 16 July 2012 at 19:46, Venelin Mitov wrote:
> | Hi,
> |
> | I am trying to use Rcpp for translating an R program in C++. There seems
> to be
> | some redundancy in the functionality provided by RcppArmadillo and pure
> Rcpp,
> | for example, one can use arma::vec instead of NumericVector and arma::mat
> | instead of NumericMatrix. And it is possible to evaluate algebraic
> expressions
> | with both classes. If I understand correctly, RcppArmadillo relies on
> | overloaded operators for the arma:: classes (+,-, *...). On the other
> hand,
> | these operations are made possible with NumericMatrix with sugar, right?
> Yes and no. Sugar support is still limited for operations.
> And you do have a choice of two class libraries and interface packages for
> linear
> algebra and more: RcppArmadillo, and RcppEigen.
> | So my question is: Is there a rule of thumb to choose between one or
> another
> | implementation?
> I tend to use the Armadillo objects whenever I want to do actual linear
> algebra, and stick with the Rcpp vector and matrix classes when I just
> need,
> for lack of a better term, "storage containers" to collect data and/or pass
> it around to other libraries or functions.
> As for Rcpp and sugar: further contributions would be welcome. But this
> requires pretty solid skills with C++ templates...
> Hope this helps!
> Cheers, Dirk
> |
> | Cheers,
> | Venelin
> |
> |
> |
> | ----------------------------------------------------------------------
> | _______________________________________________
> | Rcpp-devel mailing list
> | Rcpp-devel at lists.r-forge.r-project.org
> | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
> --
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com

Venelin MITOV
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20120717/34e5e0fc/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ArmaAlglibGlue.h
Type: text/x-chdr
Size: 7082 bytes
Desc: not available
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20120717/34e5e0fc/attachment.h>

More information about the Rcpp-devel mailing list