RcppMatrixExample: Example of using Rcpp NumericMatrix

Description Details Author(s) References Examples

Description

The NumericMatrix class represents numeric matrices

Details

The C++ code presented in the MatrixExample.cpp file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
        #include <Rcpp.h>
        #include <cmath>
        
        // suncc needs help to disambiguate between sqrt( float ) and sqrt(double) 
        inline static double sqrt_double(double x) { return ::sqrt(x); }
        
        using namespace Rcpp; 
        
        // [[Rcpp::export]]
        List MatrixExample(const NumericMatrix & orig) {
            NumericMatrix mat(orig.nrow(), orig.ncol());	
        
            // we could query size via
            //   int n = mat.nrow(), k=mat.ncol();
            // and loop over the elements, but using the STL is so much nicer
            // so we use a STL transform() algorithm on each element
            std::transform(orig.begin(), orig.end(), mat.begin(), sqrt_double );

            return List::create(Named("result") = mat, 
                                Named("original") = orig);
        }
    
    

Author(s)

Dominick Samperi wrote the initial versions of Rcpp (and RcppTemplate) during 2005 and 2006. Dirk Eddelbuettel made some additions, and became maintainer in 2008. Dirk Eddelbuettel and Romain Francois have been extending Rcpp since 2009.

References

Writing R Extensions, available at https://www.r-project.org.

Examples

1
2
M <- matrix((1:16)^2, 4)
RcppMatrixExample(M)


Search within the RcppExamples package
Search all R packages, documentation and source code

Questions? Problems? Suggestions? or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.