gpt4 book ai didi

c++ - 如何使用 RcppEigen 在 C 代码中编译函数

转载 作者:行者123 更新时间:2023-11-28 06:37:07 31 4
gpt4 key购买 nike

我是 Rcpp 的新手,或者更具体地说是 RcppEigen,并且正在为如何使用 RcppEigen 编译 C++ 函数而苦苦挣扎。这是可能存在问题的C++代码。

#include <RcppEigen.h>
#include <string>

using namespace Eigen;
using namespace Rcpp;

double MatOp(const Map<MatrixXd> X, Map<MatrixXd> Y)
{

int n=X.rows();
int p=X.cols();
//int nY=Y.cols();
MatrixXd I(n,n);
I.setIdentity(n,n);
double SSE=(Y.transpose()*(I-X*(X.transpose()*X).inverse()*X.transpose())*Y).determinant();
return (n*log(SSE/n)+log(n)*p);
}

这是R代码,

> getwd()
[1] "C:/Users/LJH/Documents"
> RcppEigen.package.skeleton("PfCRT")

> RCppEigen_IcPf_R <- function(X,Y) {
+ .Call('TestInRcppEigen',X,Y,PACKAGE = 'PfCRT')
+ }
>
> prompt(RCppEigen_IcPf_R)

.Rcheck文件是,

* installing *source* package 'PfCRT' ...
** libs

*** arch - i386
cygwin warning:
MS-DOS style path detected: C:/R/R-31~1.1/etc/i386/Makeconf
Preferred POSIX equivalent is: /cygdrive/c/R/R-31~1.1/etc/i386/Makeconf
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
g++ -m32 -I"C:/R/R-31~1.1/include" -DNDEBUG -I"C:/Users/LJH/Documents/R/win-library/3.1/Rcpp/include" -I"C:/Users/LJH/Documents/R/win-library/3.1/RcppEigen/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include" -O2 -Wall -mtune=core2 -c TestInRcppEigen.cpp -o TestInRcppEigen.o
g++ -m32 -shared -s -static-libgcc -o PfCRT.dll tmp.def TestInRcppEigen.o -Ld:/RCompile/CRANpkg/extralibs64/local/lib/i386 -Ld:/RCompile/CRANpkg/extralibs64/local/lib -LC:/R/R-31~1.1/bin/i386 -lR
cygwin warning:
MS-DOS style path detected: C:/R/R-31~1.1/etc/i386/Makeconf
Preferred POSIX equivalent is: /cygdrive/c/R/R-31~1.1/etc/i386/Makeconf
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
installing to C:/Users/LJH/Documents/PfCRT.Rcheck/PfCRT/libs/i386

*** arch - x64
cygwin warning:
MS-DOS style path detected: C:/R/R-31~1.1/etc/x64/Makeconf
Preferred POSIX equivalent is: /cygdrive/c/R/R-31~1.1/etc/x64/Makeconf
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
g++ -m64 -I"C:/R/R-31~1.1/include" -DNDEBUG -I"C:/Users/LJH/Documents/R/win-library/3.1/Rcpp/include" -I"C:/Users/LJH/Documents/R/win-library/3.1/RcppEigen/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include" -O2 -Wall -mtune=core2 -c TestInRcppEigen.cpp -o TestInRcppEigen.o
g++ -m64 -shared -s -static-libgcc -o PfCRT.dll tmp.def TestInRcppEigen.o -Ld:/RCompile/CRANpkg/extralibs64/local/lib/x64 -Ld:/RCompile/CRANpkg/extralibs64/local/lib -LC:/R/R-31~1.1/bin/x64 -lR
cygwin warning:
MS-DOS style path detected: C:/R/R-31~1.1/etc/x64/Makeconf
Preferred POSIX equivalent is: /cygdrive/c/R/R-31~1.1/etc/x64/Makeconf
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
installing to C:/Users/LJH/Documents/PfCRT.Rcheck/PfCRT/libs/x64
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
*** arch - i386
*** arch - x64
* DONE (PfCRT)

然后我这样做,

> library(PfCRT)
> X1 <- matrix(c(1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,0,2,0,1,0,2,0,1,0,2,0,1,0,2,0,1,
+ 0,2,0,1,0,0,3,1,0,0,3,1,0,0,3,1,0,0,3,1,0,0,3,1,0,0,3,1,0,0,3,1,0,0,3,1,0,
+ 0,3,1,0,0,3.01),20,4,byrow=TRUE)
> Y <- matrix(c(50,51,52,54,53,60,59,65,67,70,70,73,74,78,82,80,87,84,88,92),20,1)
>
> RCppEigen_IcPf_R(X1,Y)
Error in .Call("TestInRcppEigen", X, Y, PACKAGE = "PfCRT") :
"TestInRcppEigen" not available for .Call() for package "PfCRT"

发生错误,所以我猜 double MatOp(const Map<MatrixXd> X, Map<MatrixXd> Y) 有问题C++中的函数。任何帮助将不胜感激。

最佳答案

您的 C++ 函数称为“MatOp”:

double MatOp(const Map<MatrixXd> X, Map<MatrixXd> Y)

而是你调用“TestInRcppEigen”:

.Call('TestInRcppEigen',X,Y,PACKAGE = 'PfCRT')

得到错误

Error in .Call("TestInRcppEigen", X, Y, PACKAGE = "PfCRT") : 
"TestInRcppEigen" not available for .Call() for package "PfCRT"

这是正确的:您没有提供 TestInRcppEigen

提供 TestInRcppEigen(通过将“MatOp”重命名为它)或调用 MatOp

这些与 RcppEigen 本身没有任何关系,您只是迷失在如何从 R 调用 C++ 的杂草中。看看 Rcpp Attributes 小插图——它确实可以帮助和简化。

关于c++ - 如何使用 RcppEigen 在 C 代码中编译函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26600395/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com