从一前一后,Large SpMat object with RcppArmadillo,我决定用Rcpp
计算大型矩阵定义
我已经安装Rcpp
和RcppArmadillo
> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: OS X 10.11.6 (El Capitan)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] RcppArmadillo_0.7.500.0.0 Rcpp_0.12.7 cluster_2.0.4 skmeans_0.2-8
[5] ggdendro_0.1-20 ggplot2_2.1.0 lsa_0.73.1 SnowballC_0.5.1
[9] data.table_1.9.6 jsonlite_1.1 purrr_0.2.2 stringi_1.1.2
[13] dplyr_0.5.0 plyr_1.8.4
loaded via a namespace (and not attached):
[1] assertthat_0.1 slam_0.1-38 MASS_7.3-45 chron_2.3-47 grid_3.3.1 R6_2.2.0 gtable_0.2.0
[8] DBI_0.5-1 magrittr_1.5 scales_0.4.0 tools_3.3.1 munsell_0.4.3 clue_0.3-51 colorspace_1.2-7
[15] tibble_1.2
有了这样一个例子mtcars
这个完美的作品:
library(lsa)
x <- as.matrix(mtcars)
cosine(t(x))
这从lsa
cosine
功能:
cosR <- function(x) {
co <- array(0, c(ncol(x), ncol(x)))
## f <- colnames(x)
## dimnames(co) <- list(f, f)
for (i in 2:ncol(x)) {
for (j in 1:(i - 1)) {
co[i,j] <- crossprod(x[,i], x[,j])/
sqrt(crossprod(x[,i]) * crossprod(x[,j]))
}
}
co <- co + t(co)
diag(co) <- 1
return(as.matrix(co))
}
而且在Rcpp
相当于是这样的:
library(Rcpp)
library(RcppArmadillo)
cppFunction(depends='RcppArmadillo',
code="NumericMatrix cosCpp(NumericMatrix Xr) {
int n = Xr.nrow(), k = Xr.ncol();
arma::mat X(Xr.begin(), n, k, false); // reuses memory and avoids extra copy
arma::mat Y = arma::trans(X) * X; // matrix product
arma::mat res = Y/(arma::sqrt(arma::diagvec(Y)) * arma::trans(arma::sqrt(arma::diagvec(Y))));
return Rcpp::wrap(res);
}")
您可以检查这两个功能是等价的
all.equal(cosCpp(x),cosR(x))
[1] TRUE
但是,当我跑我这加载后的数据Rcpp
我获得:
x <- as.matrix(my_data)
x <- t(my_data)
y <- cosCpp(x)
error: Mat::init(): requested size is too large
Error in eval(substitute(expr), envir, enclos) :
Mat::init(): requested size is too large
更新解决方案后,@没穿外衣,的建议+ @gvegayon后+小时的阅读
我修改了功能,这一点:
sourceCpp("/myfolder/my_function.cpp")
和my_function.cpp
内容
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::export]]
arma::sp_mat cosine_rcpp(
const arma::mat & X
) {
int k = X.n_cols;
arma::sp_mat ans(k,k);
for (int i=0;i<k;i++)
for (int j=i;j<k;j++) {
// X(i) x X(j)'/sqrt(sum(X^2) * sum(Y^2))
ans.at(i,j) = arma::norm_dot(X.col(i), X.col(j));
}
return ans;
}
然后我跑
cosine_rcpp(x)
以供将来参考,*“[...]说我应该让' ARMA_64BIT_WORD',但我没有运气让它“*没有帮助;状态(并可能证明)你在你的问题中尝试了什么。 – nrussell
请自己做(和我们)一个忙,并使用'sourceCpp()'。任何两三行以外的东西都可以通过'cppFunction()'来实现。 –
您也无法理解编译器选项是什么,并且CPPFLAGS **的设置完全**在错误的地方。 –