如何在RcppEigen中使用字符串变量
how to use string variable in RcppEigen
我是Rcpp和RcppEigen的新手。如果这个问题太天真,请道歉。这是我在R,中编译的代码
require(RcppEigen)
require(inline)
body <- "
using Eigen::Map;
using Eigen::MatrixXd;
const Map<MatrixXd> X(as<Map<MatrixXd> >(X0));
const Map<MatrixXd> Y(as<Map<MatrixXd> >(Y0));
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 wrap(n*log(SSE/n)+log(n)*p);
"
IC <- cxxfunction(signature(X0 = "matrix", Y0 = "matrix"), body, plugin = "RcppEigen")
IC(X1, Y)
其中X和Y分别为
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)
我可以在R中成功运行上述代码。但现在我想添加一些string variable
和double numeric
require(RcppEigen)
require(Rcpp)
require(inline)
body <- "
//#include <string.h>
using Eigen::Map;
using Eigen::MatrixXd;
using Rcpp::as;
const Map<MatrixXd> X(as<Map<MatrixXd> >(X0));
const Map<MatrixXd> Y(as<Map<MatrixXd> >(Y0));
string Criteria = Rcpp::as<string>(Criteria0);
double sigma = Rcpp::as<double>(sigma0);
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();
if (Criteria=='A1'){
return wrap(n*log(SSE/n)+log(n)*p);
}
if (Criteria=='A2'){
return wrap(n*log(SSE/n)+(2*p*nY*n+nY*(nY+1))/n-2/n+n+2);
}
if (Criteria=='A3'){
return wrap(n*log(SSE/n)+2*(2+p)*(n*sigma/SSE)-2*(n*sigma/SSE)*(n*sigma/SSE));
}
"
IC <- cxxfunction(signature(X0 = "matrix", Y0 = "matrix",Criteria0="string",sigma0="numeric"), body, plugin = "RcppEigen")
IC(X1, Y,"A2",0)
但是发生了错误。
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! 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
filec2c6da05e50.cpp:48:14: warning: multi-character character constant [-Wmultichar]
filec2c6da05e50.cpp:51:14: warning: multi-character character constant [-Wmultichar]
filec2c6da05e50.cpp:54:14: warning: multi-character character constant [-Wmultichar]
In file included from C:/Users/LJH/Documents/R/win-library/3.1/RcppEigen/include/Eigen/Core:305:0,
from C:/Users/LJH/Documents/R/win-library/3.1/RcppEigen/include/Eigen/Dense:1,
from C:/Users/LJH/Documents/R/win-library/3.1/RcppEigen/include/RcppEigenForward.h:30,
from C:/U
In addition: Warning message:
running command 'C:/R/R-31~1.1/bin/x64/R CMD SHLIB filec2c6da05e50.cpp 2> filec2c6da05e50.cpp.err.txt' had status 1
>
> IC(X1, Y,"A2",0)
Error: could not find function "IC"
任何帮助都将不胜感激。
进行以下更改:
-
添加
using std::string;
和 -
使用
Criteria == "A1"
(即双等于和双引号,并在正文字符串周围放置单引号,以便在其中使用双引号。)
相关文章:
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 从字符串变量中逐字符读取单词
- 对函数 PlaySound 使用字符串变量
- C++中的字符串变量
- 字符串变量,比如说"字符串str",可以直接复制到数组中吗?
- 我可以用字符串变量而不是它的名字创建容器吗? C++
- 字符串变量的大小
- 为什么我的字符串变量没有打印完整的字符串?
- 字符串变量未更新 c++
- 如何使用 setfill 和 setw 在字符串变量中存储十六进制值
- 在.cpp文件之间传递全局字符串变量?
- 字符串变量顺序
- 为什么可以将整数分配给字符串变量?
- 在qt创建器中调试时如何访问字符串变量的完整值?
- 将字符串变量分配给无符号字符变量
- 在 c++ 中使用字符串变量选择静态类结构,而不带对象
- C++我应该用什么来保存格式化为字符串变量(如sprintf())的文件
- 我有一个字符串变量单词= "APA"
- 将字符串变量从 bash 脚本传递到根宏