[Rcpp-devel] How to figure out the signature of cxxfunction is a matrix or vector?
edd at debian.org
Wed Aug 22 04:57:09 CEST 2012
On 21 August 2012 at 21:43, Peng Yu wrote:
| "numeric" is specified as the signature. But it does not say whether
a) Not that even "numeric" is just a placeholder. You could replace it with
"groucho_marx" and the code still runs.
The only thing really used is the _left-hand side_ (here 'mx') which gets
used for the variable name.
b) In R, vector and matrix are rather close -- almost the same. Matrices are
vectors which happen to have a dimension attribute. We make that point in
| it should be a matrix or a vector. The following example just assume
| the input is matrix. But should there be some checking to make sure
| the input is indeed a matrix before the conversion to NumericMatrix?
The test happens when the object is instantiated:
+ Rcpp::NumericVector mat =
+ std::transform(mat.begin(), mat.end(), mat.begin(), ::sqrt);
+ return mat;
R> fun <- cxxfunction(signature(mx="numeric"), src, plugin="Rcpp")
R> orig <- matrix(1:9, 3, 3)
[,1] [,2] [,3]
[1,] 1.00000 2.00000 2.64575
[2,] 1.41421 2.23607 2.82843
[3,] 1.73205 2.44949 3.00000
R> orig2 <- 1:9
Error in fun(orig2) : not a matrix
which is not much different from passing in other inadmissible objects:
Error in fun(c("A", "B")) : not a matrix
Error in fun(NULL) : not a matrix
If you want a test at the cxxfunction level, you need to write a different
cxxfunction that implements tests.
| Or the check has to be before "fun" is called?
| Rcpp::NumericVector mat =
| std::transform(mat.begin(), mat.end(),
| mat.begin(), ::sqrt);
| return mat;
| fun=cxxfunction(signature(mx="numeric"), src,
| orig=matrix(1:9, 3, 3)
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
More information about the Rcpp-devel