如何解决Rcpp具有默认null参数的函数:与请求的类型不兼容:[type = NULL; target = double]
此处显示了我的Rcpp代码,我已经使用sourceCpp来编译cpp文件。没有错误的报告,只是一些警告。当我尝试使用 softImpute(m1,NULL,mr1,mr2,0,-1)实现功能 softImpute 时,报告的错误是与请求的不兼容类型:[type = NULL; target = double] 。
注意: m1 是一个数字矩阵, mr1 和 mr2 是两个逻辑矩阵。例如,我们可以如下生成m1,mr1,mr2:
m1 <- matrix(rnorm(30),5,6)
mr1 <- matrix(runif(30) > 0.5,6)
mr2 <- !mr1
您是否有解决此错误的想法?预先谢谢你!
Rcpp代码:
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
Rcpp::List dcSVD(arma::mat X) {
arma::mat u,v;
arma::vec d;
arma::svd(u,d,v,X,"dc");
return Rcpp::List::create(Rcpp::Named("u") = u,Rcpp::Named("d") = d,Rcpp::Named("v") = v);
}
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
Rcpp::List svd_st(arma::mat X,double lambda){
Rcpp::List mysvd = dcSVD(X);
arma::mat U = mysvd["u"];
arma::mat V = mysvd["v"];
arma::mat VT = V.t();
arma::vec d = mysvd["d"];
arma::mat D = arma::diagmat(d);
arma::uvec index_list = arma::find(d >= lambda);
arma::vec w;
arma::mat W;
arma::mat L;
if(index_list.n_elem > 1){
w = d.elem(index_list) - lambda;
W = arma::diagmat(w);
arma::mat U1 = U.cols(index_list);
arma::mat VT1 = VT.rows(index_list);
L = U1 * W * VT1;
}else if(index_list.n_elem == 1){
w = d.elem(index_list) - lambda;
W = arma::diagmat(w);
L = U.cols(index_list) * W * VT.rows(index_list);
}else if(index_list.n_elem == 0){
W = arma::zeros(1,1);
L = arma::zeros(X.n_rows,X.n_cols);
}
return Rcpp::List::create(Rcpp::Named("L") = L,Rcpp::Named("W") = W);
}
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
Rcpp::List soft(arma::mat X,Rcpp::Nullable<Rcpp::NumericMatrix> Z_ = R_NilValue,Rcpp::Nullable<Rcpp::LogicalMatrix> Ome_ = R_NilValue,Rcpp::Nullable<Rcpp::LogicalMatrix> Ome1_ = R_NilValue,Rcpp::Nullable<Rcpp::LogicalMatrix> Ome2_ = R_NilValue,Rcpp::Nullable<Rcpp::NumericVector> alpha0_ = R_NilValue,Rcpp::Nullable<Rcpp::NumericVector> maxRank_ = R_NilValue){
Rcpp::NumericMatrix ZH = R_NilValue;
double alpha = 0;
int maxRank = -1;
arma::mat Z;
if (Ome_.isNotNull() && Ome1_.isNotNull() && Ome2_.isNotNull()){
Rcpp::LogicalMatrix Ome(Ome_);
arma::umat Omega = Rcpp::as<arma::umat>(Ome);
Rcpp::LogicalMatrix Ome1(Ome1_);
arma::umat Omega1 = Rcpp::as<arma::umat>(Ome1);
Rcpp::LogicalMatrix Ome2(Ome2_);
arma::umat Omega2 = Rcpp::as<arma::umat>(Ome2);
arma::mat X_0 = X % Omega;
if (!Z_.isNotNull()){
Rcpp::NumericMatrix ZH = Rcpp::wrap(X_0);
}else{
Rcpp::NumericMatrix ZH = Rcpp::as<Rcpp::NumericMatrix>(Z_);
}
if (!alpha0_.isNotNull()){
Rcpp::List my_svd = dcSVD(X_0);
arma::vec d = my_svd["d"];
double alpha = arma::as_scalar(d(1));
}else{
Rcpp::NumericVector aa(alpha0_);
double alpha = arma::as_scalar(aa(0));
}
if (!maxRank_.isNotNull()){
int maxRank = -1;
}else{
Rcpp::NumericVector bb(maxRank_);
int maxRank = arma::as_scalar(bb(0));
}
Z = Rcpp::as<arma::mat>(ZH);
}
return Rcpp::List::create(Rcpp::Named("Z") = Z,Rcpp::Named("alpha") = alpha,Rcpp::Named("maxRank") = maxRank);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。