Skip to content

[Rcpp-devel] How to figure out the signature of cxxfunction is a matrix or vector?

5 messages · Peng Yu, Dirk Eddelbuettel, Davor Cubranic

#
Hi,

"numeric" is specified as the signature. But it does not say whether
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?
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)
#
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
#
I can't find it in the document. Would please let me know which
document (filename) and page this is mentioned? BTW, the pdf document
(e.g. Rcpp-introduction.pdf) can not be searched correctly in Acrobat
(but it can be search correctly in skim). For example, if I search for
"matri", I will get to "some" in "assigns some values".

If I run ps2pdf on the pdf files, then the generated pdf.pdf file can
be searched correctly in Acrobat. Would you please take a look and see
if you can reproduce the problem?
#
On 12-08-21 08:30 PM, Peng Yu wrote:

            
I don't use Acrobat, but searching for "matri" in Evince works fine.

Are you on a Mac? What happens with Apple Preview? Or built-in PDF 
previewer in Safari or Chrome?

Davor
#
On Wed, Aug 22, 2012 at 12:20 AM, Davor Cubranic <cubranic at stat.ubc.ca> wrote:
I use Mac. Apple Preview works fine. PDF previewer in Safari also
works fine. It seems that the problem is specific to Acrobat. But
other pdf files do not have problem with Acrobat. So there might be
something can be changed when generating these Rcpp pdf files to make
sure they work with Acrobat.