代码之家  ›  专栏  ›  技术社区  ›  user1993

已安装Rcpp,但复杂代码段中出现编译错误

  •  0
  • user1993  · 技术社区  · 3 年前

    我已经在RStudio中安装了Rcpp和RcpEigen。我也能够成功地运行Rcpp代码(没有使用RcpEigen)。然而,下面使用两者的代码似乎都不起作用。

    这是代码-

    library(Rcpp)
    library(RcppEigen)
    sourceCpp(code = '
      #include <Rcpp.h>
      #include <RcppEigen.h>
      // [[Rcpp::depends(RcppEigen)]]
      using namespace Rcpp;
      using namespace Eigen;
      using namespace RcppEigen;
      // [[Rcpp::export]]
        List luEigen(MatrixXd M) {
        FullPivLU<MatrixXd> luE(M);
        return List::create(Named("L_matrix") = luE.matrixLU().triangularView<Upper>());
    }')
    
    A <- 0.8 + 0.2 * diag(100)
    (luEigen(A))
    

    这段代码给出了一个非常长的错误,因此以下是关键的错误行-

    /Library/Frameworks/R.framework/Versions/4.1/Resources/library/Rcpp/include/Rcpp/generated/Vector__create.h:71:10: note: in instantiation of function template specialization 'Rcpp::Vector<19, PreserveStorage>::create__dispatch<Rcpp::traits::named_object<Eigen::TriangularView<const Eigen::Matrix<double, -1, -1, 0>, 2>>>' requested here
                    return create__dispatch( typename traits::integral_constant<bool,
                           ^
    file16bbd8305f5c.cpp:11:18: note: in instantiation of function template specialization 'Rcpp::Vector<19, PreserveStorage>::create<Rcpp::traits::named_object<Eigen::TriangularView<const Eigen::Matrix<double, -1, -1, 0>, 2>>>' requested here
        return List::create(Named("L_matrix") = luE.matrixLU().triangularView<Upper>());
                     ^
    18 warnings and 1 error generated.
    make: *** [file16bbd8305f5c.o] Error 1
    clang++ -mmacosx-version-min=10.13 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Library/Frameworks/R.framework/Versions/4.1/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/4.1/Resources/library/RcppEigen/include" -I"/private/var/folders/_3/wdql3v5d4vggzffw3xdcr3p80000gn/T/RtmpQioi38/sourceCpp-x86_64-apple-darwin17.0-1.0.7" -I/usr/local/include   -fPIC  -Wall -g -O2  -c file16bbd8305f5c.cpp -o file16bbd8305f5c.o
    

    假设安装了Rcpp和RccpEigen,并且不同的Rccp代码确实有效,是什么导致了此代码中的错误?

    0 回复  |  直到 3 年前
        1
  •  1
  •   user1993    3 年前

    在@Dirk的一个非常有用的建议下,我简化了分解,这就成功了。仍然不确定为什么更复杂的结构会出错,但底线是简化就能完成任务。这是我修改后的代码-

    library(Rcpp)
    library(RcppEigen)
    sourceCpp(code = '
      #include <Rcpp.h>
      #include <RcppEigen.h>
      // [[Rcpp::depends(RcppEigen)]]
      using namespace Rcpp;
      using namespace Eigen;
      using namespace RcppEigen;
      // [[Rcpp::export]]
        List luEigen(MatrixXd M) { // here I name our function 
        FullPivLU<MatrixXd> luE(M); // here I perform the decomposition
        MatrixXd upper = luE.matrixLU().triangularView<Upper>(); // this creates the upper matrix
        MatrixXd lower = luE.matrixLU().triangularView<StrictlyLower>(); // this creates the lower matrix
        return List::create(Named("U_matrix") = upper, Named("L_matrix") = lower); // this makes the list of the 2 matrices
    }')
    
    A <- 0.8 + 0.2 * diag(100)
    (luEigen(A))
    

    您可以通过只进行一次分解并从中调用上三角矩阵和下三角矩阵来进一步加快速度,如下所示-

    FullPivLU<MatrixXd> luE(M); // here I perform the decomposition
    MatrixXd decomp = luE.matrixLU();
    MatrixXd upper = decomp.triangularView<Upper>(); // this creates the upper matrix
    MatrixXd lower = decomp.triangularView<StrictlyLower>(); // this creates the lower matrix