我需要合作RcppParallel::RMatrix.以前我Rcpp只和我一起工作过.但是现在因为RcppParallel我需要一个像什么Rcpp有的文件.
例如
我Rcpp::NumericMatrix可以选择带占位符" _" 的行或列,如下所示:
NumericMatrix new = OldMatrix(_,1);
Run Code Online (Sandbox Code Playgroud)
但我想知道怎么能这样做RcppParallel::RMatrix?
感谢您的帮助.
我遇到的问题是我必须计算形状之间的欧几里德距离矩阵,其范围从20,000到60,000点,产生10-20GB的数据量.我必须运行这些计算中的每一个数千次,所以20GB x 7,000(每个计算是一个不同的点云).形状可以是2D或3D.
已编辑(更新的问题)
是否有更有效的方法来计算前向和后向距离而不使用两个单独的嵌套循环?
我知道我可以保存数据矩阵并计算每个方向的最小距离,但是大点云存在巨大的内存问题.
有没有办法加快计算速度和/或清理代码以缩短时间?
具有讽刺意味的是,我只需要矩阵来计算一个非常简单的度量,但它需要整个矩阵才能找到该度量(Average Hausdorff distance).
数据示例,其中每列表示形状的尺寸,每行是形状中的一个点:
first_configuration <- matrix(1:6,2,3)
second_configuration <- matrix(6:11,2,3)
colnames(first_configuration) <- c("x","y","z")
colnames(second_configuration) <- c("x","y","z")
Run Code Online (Sandbox Code Playgroud)
此代码计算坐标之间的欧几里德距离:
m <- nrow(first_configuration)
n <- nrow(second_configuration)
D <- sqrt(pmax(matrix(rep(apply(first_configuration * first_configuration, 1, sum), n), m, n, byrow = F) + matrix(rep(apply(second_configuration * second_configuration, 1, sum), m), m, n, byrow = T) - 2 * first_configuration %*% t(second_configuration), 0))
D
Run Code Online (Sandbox Code Playgroud)
输出:
[,1] [,2]
[1,] 8.660254 10.392305
[2,] 6.928203 8.660254
Run Code Online (Sandbox Code Playgroud)
编辑:包括hausdorff平均代码
d1 <- mean(apply(D, 1, min))
d2 <- …Run Code Online (Sandbox Code Playgroud) 我试图使用RcppParallel并行(大)向量的并行.这就是我想出来的.
// [[Rcpp::depends(RcppParallel)]]
#include <RcppParallel.h>
#include <Rcpp.h>
#include <assert.h>
using namespace RcppParallel;
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector directVectorAddition(NumericVector first, NumericVector second) {
assert (first.length() == second.length());
NumericVector results(first.length());
results = first + second;
return results;
}
// [[Rcpp::export]]
NumericVector loopVectorAddition(NumericVector first, NumericVector second) {
assert (first.length() == second.length());
NumericVector results(first.length());
for(unsigned i = 0; i != first.length(); i++)
results[i] = first[i] + second[i];
return results;
}
struct VectorAddition : public Worker
{
const RVector<double> first, second;
RVector<double> results;
VectorAddition(const …Run Code Online (Sandbox Code Playgroud) 由于我对Rcpp有点陌生,因此我可能在这里错过了一个窍门。
让我们创建两个矩阵:
library(Rcpp)
library(microbenchmark)
P <- matrix(0, 200,500)
for(i in 1:500) P[,i] <- rep(rep(sample(0:1), 2), 25)
Parent_Check <- matrix(0, nrow(P), nrow(P))
Run Code Online (Sandbox Code Playgroud)
我现在要完成以下操作:
Test1 <- function(){
for (i in 1:nrow(P)) {
Parent_Check[i,] <- apply(P, 1, function(x) all(x == P[i,]))
}
}
Test1()
Run Code Online (Sandbox Code Playgroud)
然后,我为all()创建了一个Rcpp版本,希望提高速度,定义为:
Rcpp::cppFunction(
'bool all_C(LogicalVector x) {
// Note the use of is_true to return a bool type.
return is_true(all(x == TRUE));
}
'
)
Run Code Online (Sandbox Code Playgroud)
使用all_C检查速度,事实证明速度较慢:
Test2 <- function(){
for (i in 1:nrow(P)) {
Parent_Check[i,] <- apply(P, 1, function(x) all_C(x …Run Code Online (Sandbox Code Playgroud)