[Rcpp-devel] Use of SubMatrix function

Dirk Eddelbuettel edd at debian.org
Sat Apr 21 16:17:57 CEST 2012


Hi Søren,

On 21 April 2012 at 13:46, Søren Højsgaard wrote:
| Dear list
| 
| I've tried to write a function for extracting a submatrix i.e. for mimicing A[c(1,3,4),c(2,3,5)]. So I did:
| 
| submat <- cxxfunction(signature(r_SS='int', r_aa='int', r_bb='int'), body = '
| NumericMatrix SS (r_SS);
| IntegerVector aa (r_aa);
| IntegerVector bb (r_bb);
| SubMatrix<INTSXP> yy = SS( aa, bb ) ;
| ', plugin='Rcpp')
| 
| submat(matrix(1:9,nr=3), 0:1, 1:2)
| 
| This fails with the error message:
| ....
| 31: NumericMatrix SS (r_SS);
|  32: IntegerVector aa (r_aa);
|  33: IntegerVector bb (r_bb);
|  34: SubMatrix<INTSXP> yy = SS( aa, bb ) ;
|  35: 
|  36: END_RCPP
|  37: }
|  38: 
|  39: 
| Error in compileCode(f, code, language = language, verbose = verbose) : 
|   Compilation ERROR, function(s)/method(s) not created! file1e00bc456c.cpp: In function 'SEXPREC* file1e00bc456c(SEXP, SEXP, SEXP)':
| file1e00bc456c.cpp:34:35: error: invalid conversion from 'SEXP' to 'size_t {aka unsigned int}' [-fpermissive]
| C:/programs/R/devel/library/Rcpp/include/Rcpp/vector/Matrix.h:126:18: error:   initializing argument 1 of 'Rcpp::Matrix<RTYPE>::Proxy Rcpp::Matrix<RTYPE>::operator()(const size_t&, const size_t&) [with int RTYPE = 14, Rcpp::Matrix<RTYPE>::Proxy = double&, size_t = unsigned int]' [-fpermissive]
| file1e00bc456c.cpp:34:35: error: invalid conversion from 'SEXP' to 'size_t {aka unsigned int}' [-fpermissive]
| C:/programs/R/devel/library/Rcpp/include/Rcpp/vector/Matrix.h:126:18: error:   initializing argument 2 of 'Rcpp::Matrix<RTYPE>::Proxy Rcpp::Matrix<RTYPE>::operator()(const size_t&, const size_t&) [with int RTYPE = 14, Rcpp::Matrix<RTYPE>::Proxy = double&, size_t = unsigned int]' [-fpermissive]
| file1e00bc456c.cpp:34:35: error: conversion from 
| In addition: Warning message:
| running command 'C:/programs/R/devel/bin/i386/R CMD SHLIB file1e00bc456c.cpp 2> file1e00bc456c.cpp.err.txt' had status 1 
| 
| I've seen examples where SubMatrix has been used in connection with e.g. SS(Range(0,1), Range(2,4)) and that works. From this one can not conclude that it should work with arbitrary index vectors (aa and bb in my example) - I know that. But what puzzles me is the error message above:
|      invalid conversion from 'SEXP' to 'size_t {aka unsigned int}' [-fpermissive]
| I read this such that aa is really an SEXP. However, my understanding was that 
|      IntegerVector aa (r_aa);
| would coerce an SEXP (here r_aa) to an integer vector. 
| 
| Can anyone spare a moment to clarify this for me?

Whenever I need to look up "working examples" I go to our unitTests
directory. For SubMatrix I only see

                 NumericMatrix xx(4, 5);
                 xx(0,0) = 3;
                 xx(0,1) = 4;
                 xx(0,2) = 5;
                 xx(1,_) = xx(0,_);
                 xx(_,3) = xx(_,2);
                 SubMatrix<REALSXP> yy = xx( Range(0,2), Range(0,3) ) ;
                 NumericMatrix res = yy ;
                 return res;

which is notable in that it copies the SubMatrix once more to a NumericMatrix
(for which wrap() works, of course).  Also note that we use Range rather than
arbitrary vectors.

So in sum this part of the API may well be a little underdeveloped.

Because of that, I would take a good hard look at RcppArmadillo.  Whenever I
want 'more' operations from matrices, I usually go there.  You still get all
the Rcpp goodies of easy access to/from R data structures, tests via inline,
etc pp but you also get a more fleshed out API for math.

Armadillo docs are here:   http://arma.sourceforge.net/docs.html

RcppArmadillo shadows Armadillo very closely.

Hope this helps,  Dirk


| Best regards
| Søren
| 
| 
| 
| _______________________________________________
| 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

-- 
R/Finance 2012 Conference on May 11 and 12, 2012 at UIC in Chicago, IL
See agenda, registration details and more at http://www.RinFinance.com


More information about the Rcpp-devel mailing list