[Rcpp-devel] Default empty values for vector and matrices

2014-02-17 Thread Alessandro Mammana
Dear all,
I am trying to write an Rcpp function that accepts optionally a
vector, but I cannot figure out what default value I should give to
it.

First I tried the following:

// [[Rcpp::export]]
int nilVec(NumericVector v = R_NilValue){
int sum = 0;
for (int i = 0; i  v.length(); ++i){
sum += v[i];
}
return sum;
}

but when trying:
nilVec()
I get the error Not compatible with REALSXP, which is maybe not very
consistent with the fact that in R you can do as.numeric(NULL) and it
gives you a vector of length 0.

Then I tried:

// [[Rcpp::export]]
int nilVec(NumericVector v = NumericVector()){
int sum = 0;
for (int i = 0; i  v.length(); ++i){
sum += v[i];
}
return sum;
}

but it doesn't compile.

I found the following discussion:
http://comments.gmane.org/gmane.comp.lang.r.rcpp/5922 where I
discovered that for whatever reason the following compiles and works:

// [[Rcpp::export]]
int nilVec(NumericVector v = NumericVector::create(0)){
int sum = 0;
for (int i = 0; i  v.length(); ++i){
sum += v[i];
}
return sum;
}

But the funny thing is that this does not work:

// [[Rcpp::export]]
int nilMat(NumericMatrix v = NumericMatrix::create(0)){
int sum = 0;
for (int i = 0; i  v.length(); ++i){
sum += v[i];
}
return sum;
}

So how should I do for matrices? I would also be happy with the
solution of defining somewhere an empty vector  or an empty matrix and
assigning it as default argument, but the following does not work:

NumericVector empty(0);

// [[Rcpp::export]]
int nilVec(NumericVector v = empty){
int sum = 0;
for (int i = 0; i  v.length(); ++i){
sum += v[i];
}
return sum;
}


Suggestions? I would be super happy if in the future the special value
R_NilValue could be converted to an empty vector, it would make things
very easy and coherent with R's behaviour.

Thx in advance,
Ale


-- 
Alessandro Mammana, PhD Student
Max Planck Institute for Molecular Genetics
Ihnestraße 63-73
D-14195 Berlin, Germany
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


Re: [Rcpp-devel] Default empty values for vector and matrices

2014-02-17 Thread Romain Francois
Le 17 févr. 2014 à 12:17, Alessandro Mammana mamm...@molgen.mpg.de a écrit :

 Dear all,
 I am trying to write an Rcpp function that accepts optionally a
 vector, but I cannot figure out what default value I should give to
 it.
 
 First I tried the following:
 
 // [[Rcpp::export]]
 int nilVec(NumericVector v = R_NilValue){
int sum = 0;
for (int i = 0; i  v.length(); ++i){
sum += v[i];
}
return sum;
 }
 
 but when trying:
 nilVec()
 I get the error Not compatible with REALSXP, which is maybe not very
 consistent with the fact that in R you can do as.numeric(NULL) and it
 gives you a vector of length 0.

R’s behavior is incoherent. NULL is not a numeric vector. 


 Then I tried:
 
 // [[Rcpp::export]]
 int nilVec(NumericVector v = NumericVector()){
int sum = 0;
for (int i = 0; i  v.length(); ++i){
sum += v[i];
}
return sum;
 }
 
 but it doesn't compile.

That’s the attribute parser not making sense of it. It does compile, but the 
default is not propagated to R. 

 I found the following discussion:
 http://comments.gmane.org/gmane.comp.lang.r.rcpp/5922 where I
 discovered that for whatever reason the following compiles and works:
 
 // [[Rcpp::export]]
 int nilVec(NumericVector v = NumericVector::create(0)){
int sum = 0;
for (int i = 0; i  v.length(); ++i){
sum += v[i];
}
return sum;
 }

Well. It might appear as something that works. It is in fact a bug. 
NumericVector::create(0) is supposed to create a vector of length 1 with the 
value of the first element set to 0. not numeric vector of length zero as it is 
currently the case. bug. 
https://github.com/RcppCore/Rcpp/issues/117


 But the funny thing is that this does not work:
 
 // [[Rcpp::export]]
 int nilMat(NumericMatrix v = NumericMatrix::create(0)){
int sum = 0;
for (int i = 0; i  v.length(); ++i){
sum += v[i];
}
return sum;
 }

Matrix::create is nonsense as it does not create a matrix. check : 

 m - evalCpp( NumericMatrix::create(0) )
 str(m)
 num 0

so creates a vector, not a matrix. Besides, what do you actually expect. What 
dimensions would this matrix have ?


 So how should I do for matrices? I would also be happy with the
 solution of defining somewhere an empty vector  or an empty matrix and
 assigning it as default argument, but the following does not work:

My suggestion would be to handle this on the R side. The attribute parser is 
not a fully capable C++ parser, stick to defaults for only very simple things, 
like int or double or strings. 

For anything else, you’re better of with dealing with the parameter logic on 
the R side. 

 NumericVector empty(0);
 
 // [[Rcpp::export]]
 int nilVec(NumericVector v = empty){
int sum = 0;
for (int i = 0; i  v.length(); ++i){
sum += v[i];
}
return sum;
 }
 
 
 Suggestions? I would be super happy if in the future the special value
 R_NilValue could be converted to an empty vector, it would make things
 very easy and coherent with R's behaviour.
 
 Thx in advance,
 Ale
 
 
 -- 
 Alessandro Mammana, PhD Student
 Max Planck Institute for Molecular Genetics
 Ihnestraße 63-73
 D-14195 Berlin, Germany
 ___
 Rcpp-devel mailing list
 Rcpp-devel@lists.r-forge.r-project.org
 https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel