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

Darren Cook darren at dcook.org
Mon Aug 15 04:37:46 CEST 2011


(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))

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

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)


More information about the Rcpp-devel mailing list