[Rcpp-devel] Compilation error using Rcpp::as<Eigen::MatrixXd>

Douglas Bates bates at stat.wisc.edu
Tue May 8 18:33:36 CEST 2012


Thanks for the report.  It is a problem in the 'as' methods defined
for Eigen objects.

I will fix that but I think what you are trying to do is more easily
accomplished by using a copy constructor in C++, as in the enclosed

On Tue, May 8, 2012 at 10:45 AM, Jelmer Ypma <jelmerypma at gmail.com> wrote:
> Hi all,
>
> I've been happily using the RcppEigen package for a couple of
> projects, but now I'm running into problems converting a matrix from R
> into an Eigen::MatrixXd. Converting an R matrix to an
> Eigen::Map<Eigen::MatrixXd> works fine, but I would like to (deep)copy
> the values from R to a C++ Eigen::MatrixXd object. From the definition
> for the Exporter for Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
> in RcppEigen/inst/include/RcppEigenWrap.h, it looks like
> Rcpp::as<Eigen::MatrixXd> could be used, but when compiling some
> example code (see below) I get an error (see below). Compiling the
> same code with an arma::mat or with Eigen::Map<Eigen::MatrixXd> does
> not result in any errors.
>
> Does anyone have an idea what is causing this?
>
> Many thanks in advance and best wishes,
> Jelmer
>
>
>> sessionInfo()
> R version 2.15.0 (2012-03-30)
> Platform: i386-pc-mingw32/i386 (32-bit)
>
> locale:
> [1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United
> Kingdom.1252    LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
> [5] LC_TIME=English_United Kingdom.1252
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
> [1] inline_0.3.8          RcppArmadillo_0.3.0.3 RcppEigen_0.2.0
> Rcpp_0.9.10
>
> loaded via a namespace (and not attached):
> [1] grid_2.15.0    lattice_0.20-6 Matrix_1.0-6   tools_2.15.0
>
>
>
> The error I get, is:
>
> In file included from c:/tools/R/library/RcppEigen/include/Eigen/Core:282:0,
>                 from c:/tools/R/library/RcppEigen/include/Eigen/Dense:1,
>                 from
> c:/tools/R/library/RcppEigen/include/RcppEigenForward.h:29,
>                 from c:/tools/R/library/RcppEigen/include/RcppEigen.h:25,
>                 from file160c52054c0.cpp:3:
> c:/tools/R/library/RcppEigen/include/Eigen/src/Core/DenseCoeffsBase.h:
> In member function 'Eigen::DenseCoeffsBase<Derived, 1>::Scalar&
> Eigen::DenseCoeffsBase<Derived,
> 1>::operator[](Eigen::DenseCoeffsBase<Derived, 1>::Index) [with
> Derived = Eigen::Matrix<double, -0x00000000000000001,
> -0x00000000000000001>, Eigen::DenseCoeffsBase<Derived, 1>::Scalar =
> double, Eigen::DenseCoeffsBase<Derived, 1>::Index = int]':
> c:/tools/R/library/Rcpp/include/Rcpp/internal/export.h:89:3:
> instantiated from 'void Rcpp::internal::export_indexing__impl(SEXP,
> T&, Rcpp::traits::false_type) [with T = Eigen::Matrix<double,
> -0x00000000000000001, -0x00000000000000001>, value_type = double, SEXP
> = SEXPREC*, Rcpp::traits::false_type =
> Rcpp::traits::integral_constant<bool, false>]'
> c:/tools/R/library/Rcpp/include/Rcpp/internal/export.h:109:6:
> instantiated from 'void
> Rcpp::internal::export_indexing__dispatch(SEXP, T&,
> Rcpp::traits::r_type_primitive_tag) [with T = Eigen::Matrix<double,
> -0x00000000000000001, -0x00000000000000001>, value_type = double, SEXP
> = SEXPREC*]'
> c:/tools/R/library/Rcpp/include/Rcpp/internal/export.h:127:6:
> instantiated from 'void Rcpp::internal::export_indexing(SEXP, T&)
> [with T = Eigen::Matrix<double, -0x00000000000000001,
> -0x00000000000000001>, value_type = double, SEXP = SEXPREC*]'
> c:/tools/R/library/Rcpp/include/Rcpp/traits/Exporter.h:86:17:
> instantiated from 'T Rcpp::traits::MatrixExporter<T,
> value_type>::get() [with T = Eigen::Matrix<double,
> -0x00000000000000001, -0x00000000000000001>, value_type = double]'
> c:/tools/R/library/Rcpp/include/Rcpp/as.h:53:33:   instantiated from
> 'T Rcpp::internal::as(SEXP, Rcpp::traits::r_type_generic_tag) [with T
> = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001>,
> SEXP = SEXPREC*]'
> c:/tools/R/library/Rcpp/include/Rcpp/as.h:75:89:   instantiated from
> 'T Rcpp::as(SEXP) [with T = Eigen::Matrix<double,
> -0x00000000000000001, -0x00000000000000001>, SEXP = SEXPREC*]'
> file160c52054c0.cpp:31:40:   instantiated from here
> c:/tools/R/library/RcppEigen/include/Eigen/src/Core/DenseCoeffsBase.h:388:7:
> error: 'THE_BRACKET_OPERATOR_IS_ONLY_FOR_VECTORS__USE_THE_PARENTHESIS_OPERATOR_INSTEAD'
> is not a member of 'Eigen::internal::static_assertion<false>'
> make: *** [file160c52054c0.o] Error 1
>
> ERROR(s) during compilation: source code errors or compiler
> configuration errors!
>
>
> The code I used for the example is:
>
> library('RcppEigen')
> library('RcppArmadillo')
> library('inline')
>
> # Define C++ some function that uses a matrix as input.
> # MatrixType will be defined using a typedef in includes,
> # when compiling the function below.
> # MatrixType will take on values
> #   * arma::mat
> #   * Eigen::MatrixXd
> #   * Eigen::Map<Eigen::MatrixXd>
> printMatrix_code <- '
> MatrixType _A( Rcpp::as<MatrixType>( A ) );
>
> Rcpp::Rcout << "_A: " << std::endl << _A << std::endl;
>
> return R_NilValue;
> '
>
> # Define some matrix A
> set.seed( 3141 )
> A <- matrix( rnorm( 10 ), nrow=5, ncol=2 )
> A
>
> # Compile using MatrixType = arma::mat
> # (WORKS)
> try( {
> printArmaMatrix <- cxxfunction(
>    signature(A="matrix"),
>    printMatrix_code,
>    plugin="RcppArmadillo",
>    includes = 'typedef arma::mat MatrixType;',
>    verbose=TRUE )
>
> printArmaMatrix( A )
> } )
>
> # Compile using MatrixType = Eigen::MatrixXd
> # (FAILS)
> try( {
> printEigenMatrix <- cxxfunction(
>    signature(A="matrix"),
>    printMatrix_code,
>    plugin="RcppEigen",
>    includes = 'typedef Eigen::MatrixXd MatrixType;',
>    verbose=TRUE )
>
> printEigenMatrix( A )
> } )
>
> # Compile using MatrixType = Eigen::Map<Eigen::MatrixXd>
> # (WORKS)
> try( {
> printEigenMappedMatrix <- cxxfunction(
>    signature(A="matrix"),
>    printMatrix_code,
>    plugin="RcppEigen",
>    includes = 'typedef Eigen::Map<Eigen::MatrixXd> MatrixType;',
>    verbose=TRUE )
>
> printEigenMappedMatrix( A )
> } )
> _______________________________________________
> 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
-------------- next part --------------

R version 2.15.0 (2012-03-30)
Copyright (C) 2012 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(inline)
> src <- '
+ typedef Eigen::MatrixXd  Mat;
+ typedef Eigen::Map<Mat> MMat;
+ Mat A(as<MMat>(A_));
+ Rcpp::Rcout << "A:" << std::endl << A << std::endl;
+ A(0,0) = 0;
+ Rcpp::Rcout << "A:" << std::endl << A << std::endl;
+ '
> ff <- cxxfunction(signature(A_ = "matrix"), src, "RcppEigen")
Loading required package: lattice
> 
> set.seed(1234321)
> aa <- matrix(rnorm(4), nrow=2L, ncol=2L)
> ff(aa)
A:
 1.23691  1.83962
 1.56162 0.317325
A:
       0  1.83962
 1.56162 0.317325
NULL
> aa
         [,1]      [,2]
[1,] 1.236908 1.8396185
[2,] 1.561623 0.3173245
> 
> proc.time()
   user  system elapsed 
  6.388   0.276   6.685 


More information about the Rcpp-devel mailing list