如何使用RcppEigen编译C代码中的函数
how to compile function in C code using RcppEigen
我对Rcpp,或者更具体地说是RcppEigen非常陌生,并且很难理解如何使用RppEigen编译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"
出现错误,所以我猜C++中的double MatOp(const Map<MatrixXd> X, Map<MatrixXd> Y)
函数有问题。任何帮助都将不胜感激。
您的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"
这是正确的:您没有提供CCD_ 3。
提供TestInRcppEigen
(通过将"MatOp"重命名为它)或调用MatOp
。
所有这些都与RcppEigen本身无关,你只是迷失在如何从R中调用C++的杂草中。看看Rcpp属性小插曲——它确实可以帮助和简化。
相关文章:
- 如何将 c++ get 函数代码转换为 opencv 算法中使用的 python
- 修改链表主函数代码,用户将在其中输入节点的索引和数据以及正确的消息
- 是否可以像这样编写c malloc函数代码
- 函数范围的静态变量如何导致与共享库中函数代码的未来使用不兼容
- 如何将C++ lambda 函数代码转换为 C#?
- 该构造函数代码如何将指针作为参数
- 调用C++具有复杂参数和复杂返回类型的函数 C 代码
- "_FCbuild"不能用作函数 - C/C++ 代码错误
- 复制赋值和复制构造函数(代码C++的差异)
- 反向链表的递归函数(代码段说明)
- 当满足条件时,是否可以在 GLSL 着色器中回调 C/C++ 函数/代码
- 在构造函数代码之前禁用默认类成员初始化
- 以下移动构造函数代码安全吗?
- C++构造函数代码..这叫什么
- 如何更改SWIG生成的CSHARP文件中的构造函数代码
- 找不到当前函数(代码::块)C++的边界
- 使用模板进行 C++ 函数代码泛化
- 编写此指针函数代码的长格式方法是什么
- C++某些构造函数代码之间的区别
- 错误C2668从站点对重载函数代码的调用不明确http://h264bitstream.sourceforge.net/