Skip to content

[Rcpp-devel] Returning an arma vec

10 messages · Gabor Grothendieck, Dirk Eddelbuettel, Romain Francois +2 more

#
1. This returns a matrix rather than a vector:

--  start of file teste.cpp ---
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]

using namespace arma;
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector teste(const vec& x) {
vec v = x.subvec(1, 2); // x
return wrap(v); // x
}
/*** R
teste(1:4)
*/
--- end of file teste.cpp ---

Now run in R:
[,1]
[1,]    2
[2,]    3


2. If we replace the lines marked // x with

       return wrap(x.subvec(1, 2));

then it fails with a compiler error.

error: cannot convert 'const arma::subview_col<double>' to 'SEXP' in
initialization
#
This is known, documented / has been discussed before, and hard to change now
given that 100 CRAN packages depend on RcppArmadillo and its existing interface.

You could write your own converter to SEXP and use that.

Dirk
On 3 December 2014 at 11:59, Gabor Grothendieck wrote:
| 1. This returns a matrix rather than a vector:
| 
| --  start of file teste.cpp ---
| #include <RcppArmadillo.h>
| // [[Rcpp::depends(RcppArmadillo)]]
| 
| using namespace arma;
| using namespace Rcpp;
| 
| // [[Rcpp::export]]
| NumericVector teste(const vec& x) {
| vec v = x.subvec(1, 2); // x
| return wrap(v); // x
| }
| /*** R
| teste(1:4)
| */
| --- end of file teste.cpp ---
| 
| Now run in R:
| 
| > sourceCpp("teste.cpp", rebuild = TRUE)
| 
| > teste(1:4)
|      [,1]
| [1,]    2
| [2,]    3
| 
| 
| 2. If we replace the lines marked // x with
| 
|        return wrap(x.subvec(1, 2));
| 
| then it fails with a compiler error.
| 
| error: cannot convert 'const arma::subview_col<double>' to 'SEXP' in
| initialization
| 
| 
| -- 
| Statistics & Software Consulting
| GKX Group, GKX Associates Inc.
| tel: 1-877-GKX-GROUP
| email: ggrothendieck at gmail.com
| _______________________________________________
| 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
#
Envoy? de mon iPhone
This should be easy/trivial to fix for someone with the right skills. 

Romain
#
On 3 December 2014 at 11:14, Dirk Eddelbuettel wrote:
| 
| This is known, documented / has been discussed before, and hard to change now
| given that 100 CRAN packages depend on RcppArmadillo and its existing interface.
| 
| You could write your own converter to SEXP and use that.

That said, I am no big fan either of the current behaviour you singled out.  

Maybe we could plan for a change, add a loud warning in X months and then
phase out the current behaviour in Y months with Y >= 2 * X.  So if someone
wants to propose a plan, I am sure a few of us would review it..

Dirk
#
On 3 December 2014 at 18:30, Romain Francois wrote:
| > 2. If we replace the lines marked // x with
| > 
| >       return wrap(x.subvec(1, 2));
| > 
| > then it fails with a compiler error.
| > 
| > error: cannot convert 'const arma::subview_col<double>' to 'SEXP' in
| > initialization
| 
| This should be easy/trivial to fix for someone with the right skills. 

Sure. Maybe Gabor wants to give it a try.

Dirk
#
You just need to put a new template specialization of "wrap" for the subview_col class in RcppArmadilloWrap.h based on the existing one for the subview class. And throw in one for subview_row for good measure.

Martyn
#
Something like this: 

template <typename T>
inline wrap( const arma::subview_col<double>& x ){
    return wrap( arma::Mat<T>( x ) ) ;
}

(untested) 

This would still wrap it as a matrix though as this is what subview_col conceptually gives. 

The only downside is that this is somewhat inefficient as it would have to allocate memory for a arma::mat first and then copy that across to an Rcpp::Matrix ? 

Romain
#
Le 04/12/2014 17:57, Romain Fran?ois a ?crit :
Interesting. If I do

inline SEXP wrap( const arma::vec& x ) {
     return wrap(Rcpp::NumericVector( x.begin(), x.end()));
}

then wrap(v) returns a vector to R, not a matrix n x 1
(v is a vec here) but wrap(v+1) still returns a matrix.
Is there a more universal way to say that any expression
resulting in vec should return a vector?

Serguei.
#
I had something simpler in mind. I'll send a pull request when I've
finished testing (This package has many dependencies!)

Martyn
On Thu, 2014-12-04 at 17:57 +0100, Romain Fran?ois wrote:
1 day later
#
On 5 December 2014 at 11:38, Martyn Plummer wrote:
| I had something simpler in mind. I'll send a pull request when I've
| finished testing (This package has many dependencies!)

I am truly impressed that you test before submitting a pull request, and
greatly appreciate it.

Looking forward to what you come up with.

Dirk