[Rcpp-devel] Bug report: need to wrap return of sugar functions

Dirk Eddelbuettel edd at debian.org
Mon Aug 15 14:58:31 CEST 2011


On 15 August 2011 at 11:37, Darren Cook wrote:
| (Hello, my first post; I discovered Rcpp and inline a few weeks ago, and
| I think it is a brilliant idea!)
| (Rcpp 0.9.6, R 2.13.1)
| 
| I've been having trouble getting the sugar functions to work. I have
| more minimal examples than the below, but as this is the one from
| Rcpp-sugar.pdf I thought it most convincing:
| 
|   library('inline')
|   src3='
|   using namespace Rcpp;
|   NumericVector x(x_),y(y_);
|   return ifelse( x<y, x*x, -(y*y) );
|   '
|   x=c(1:12)
|   y=c(4:15)
|   fun=cxxfunction(signature(x_="integer",y_="integer"),
|     src3, plugin="Rcpp")
| 
| [1] shows the error message.
| 
| If I change the last line to this, then it works:
|   return wrap( ifelse( x<y, x*x, -(y*y) ) );
| 
| 
| I have another, possibly related, problem:
| 
|   library('inline')
|   x=matrix(1:12,4,3);
|   print(ifelse(x<5,x*x,x))
|   src2='
|   using namespace Rcpp;
|   NumericMatrix x(x_);
|   return wrap(ifelse(x<5,x*x,x));
|   '
|   fun=cxxfunction(signature(x_ ="integer"),src2,plugin="Rcpp")
|   print(fun(x))

No bug.

We never said that you could get by an _implicit_ wrap everywhere. Sometimes
you do need an explicit wrap. This is just one of these situations. Our
examples and unit tests are full of explicit wraps.

| Again I need to use wrap() to get past the compiler error, but instead
| of returning a matrix (as R's ifelse does), rcpp-sugar returns a vector.
| I.e. my program output is:
| 
|      [,1] [,2] [,3]
| [1,]    1    5    9
| [2,]    4    6   10
| [3,]    9    7   11
| [4,]   16    8   12
| 
|  [1]  1  4  9 16  5  6  7  8  9 10 11 12
| 
| I've tried both these, but neither compiles:
|   return wrap<NumericMatrix>(ifelse(x<5,x*x,x));
| 
|   NumericMatrix ret=ifelse(x<5,x*x,x);return ret;
| 
| ([2] shows the compiler error.)

Well, then you may have to assign to a vector first and then set a dimension
argument explicitly.  *Matrix types are pretty close to *Vector types and the
template system does not seem to see a difference.  Maybe Romain can come up
with a clever idea.

Dirk
 
| Thanks,
| 
| Darren
| 
| [1]:
| file6be0c047.cpp: In function ‘SEXPREC* file6be0c047(SEXPREC*, SEXPREC*)’:
| file6be0c047.cpp:33: error: cannot convert ‘Rcpp::sugar::IfElse<14,
| true, Rcpp::sugar::Comparator<14, Rcpp::sugar::less<14>, true,
| Rcpp::Vector<14>, true, Rcpp::Vector<14> >, true,
| Rcpp::sugar::Times_Vector_Vector<14, true, Rcpp::Vector<14>, true,
| Rcpp::Vector<14> >, true, Rcpp::sugar::UnaryMinus_Vector<14, true,
| Rcpp::sugar::Times_Vector_Vector<14, true, Rcpp::Vector<14>, true,
| Rcpp::Vector<14> > > >’ to ‘SEXPREC*’ in return
| make: *** [file6be0c047.o] Error 1
| 
| ERROR(s) during compilation: source code errors or compiler
| configuration errors!
| 
| 
| 
| [2]
| file1adf689.cpp:34: error: conversion from ‘Rcpp::sugar::IfElse<14,
| true, Rcpp::sugar::Comparator_With_One_Value<14, Rcpp::sugar::less<14>,
| true, Rcpp::Vector<14> >, true, Rcpp::sugar::Times_Vector_Vector<14,
| true, Rcpp::Vector<14>, true, Rcpp::Vector<14> >, true, Rcpp::Vector<14>
| >’ to non-scalar type ‘Rcpp::NumericMatrix’ requested
| 
| 
| -- 
| Darren Cook, Software Researcher/Developer
| 
| http://dcook.org/work/ (About me and my work)
| http://dcook.org/blogs.html (My blogs and articles)
| _______________________________________________
| 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

-- 
Two new Rcpp master classes for R and C++ integration scheduled for 
New York (Sep 24) and San Francisco (Oct 8), more details are at
http://dirk.eddelbuettel.com/blog/2011/08/04#rcpp_classes_2011-09_and_2011-10
http://www.revolutionanalytics.com/products/training/public/rcpp-master-class.php


More information about the Rcpp-devel mailing list