[Rcpp-devel] How to figure out the signature of cxxfunction is a matrix or vector?
Dirk Eddelbuettel
edd at debian.org
Wed Aug 22 04:57:09 CEST 2012
On 21 August 2012 at 21:43, Peng Yu wrote:
| Hi,
|
| "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
the documentation.
| 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:
R> library(inline)
R>
R> src='
+ Rcpp::NumericVector mat =
+ Rcpp::clone<Rcpp::NumericMatrix>(mx);
+ std::transform(mat.begin(), mat.end(), mat.begin(), ::sqrt);
+ return mat;
+ '
R>
R> fun <- cxxfunction(signature(mx="numeric"), src, plugin="Rcpp")
R>
R> orig <- matrix(1:9, 3, 3)
R> fun(orig)
[,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>
R> orig2 <- 1:9
R> fun(orig2)
Error in fun(orig2) : not a matrix
R>
which is not much different from passing in other inadmissible objects:
R> fun(c("A","B"))
Error in fun(c("A", "B")) : not a matrix
R> fun(NULL)
Error in fun(NULL) : not a matrix
R>
If you want a test at the cxxfunction level, you need to write a different
cxxfunction that implements tests.
Dirk
| Or the check has to be before "fun" is called?
|
| Thanks!
|
| library(inline)
| src='
| Rcpp::NumericVector mat =
| Rcpp::clone<Rcpp::NumericMatrix>(mx);
| std::transform(mat.begin(), mat.end(),
| mat.begin(), ::sqrt);
| return mat;
| '
| fun=cxxfunction(signature(mx="numeric"), src,
| plugin="Rcpp")
| orig=matrix(1:9, 3, 3)
| fun(orig)
|
|
| --
| Regards,
| Peng
| _______________________________________________
| 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
--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
More information about the Rcpp-devel
mailing list