[Rcpp-devel] Use of SubMatrix function
Dirk Eddelbuettel
edd at debian.org
Sun Apr 22 02:34:23 CEST 2012
Søren,
On 21 April 2012 at 09:17, Dirk Eddelbuettel wrote:
| 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.
I was in a rush this morning to get out of the house to a little
"unconference" / workshop downtown, so I didn't immediately realize that I
had actually answered a very similar question a few days ago on
StackOverflow.
With that, the solution is pretty easy -- thanks to non-contiguous
submatrices which were added in Armadillo 3.0.0 which was released earlier
this month. Your example passes almost unaltered once I apply it
RcppArmdillo rather than plain Rcpp. I use explicit arma:: and Rcpp::
prefixes here just to make it very explicit "what comes from where".
R> library(inline)
R>
R> submat <- cxxfunction(signature(r_SS='int', r_aa='int', r_bb='int'), body = '
+ arma::mat SS = Rcpp::as<arma::mat>(r_SS);
+ arma::uvec aa = Rcpp::as<arma::uvec>(r_aa);
+ arma::uvec bb = Rcpp::as<arma::uvec>(r_bb);
+ arma::mat yy = SS.submat(aa, bb);
+ return Rcpp::wrap(yy);
+ ', plugin='RcppArmadillo')
R>
R> submat(matrix(1:9,nr=3), 0:1, 1:2)
[,1] [,2]
[1,] 4 7
[2,] 5 8
R>
For the .rows(rowindvec), .cols(colindvev) and .submat(rv, cv) member
functions, unsigned ints are used -- hence the 'uvec' (rather than vec).
Hope this helps, Dirk
--
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