RcppParallel:RMatrix 和 RVector 算术运算
RcppParallel: RMatrix and RVector arithmetic operations
我正在尝试使用 RcppArmadillo
并行化双精度 for 循环,但我在 RMatrix
和 RVector
可用的算术运算方面遇到了问题。我查看了 github 上可用的头文件,但我在那里没有看到任何东西,所以我想我找错了地方。这是我的工作线程,我评论了我尝试在两个RMatrix
对象之间进行算术运算的地方。
#include <RcppParallel.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <Rmath.h>
#include <RcppArmadillo.h>
using namespace RcppParallel;
struct ClosestMean : public Worker {
// Input data and means matrix
const RMatrix<double> input_data;
const RMatrix<double> means;
// Output labels
RVector<int> predicted_labels;
// constructor
ClosestMean(const Rcpp::NumericMatrix input_data, const Rcpp::NumericMatrix means, Rcpp::IntegerVector predicted_labels)
: input_data(input_data), means(means), predicted_labels(predicted_labels) {}
// function call operator for the specified range (begin/end)
void operator () (std::size_t begin, std::size_t end){
for (unsigned int i = begin; i < end; i++){
// Check for User Interrupts
Rcpp::checkUserInterrupt();
// Get the label corresponding to the cluster mean
// for which the point is closest to
RMatrix<double>::Row point = input_data.row(i);
int label_min = -1;
double dist;
double min_dist = INFINITY;
for (unsigned int j = 0; j < means.nrow(); j++){
RMatrix<double>::Row mean = means.row(j);
dist = sqrt(Rcpp::sum((mean - point)^2)); // This is where the operation is failing
if (dist < min_dist){
min_dist = dist;
label_min = j;
}
}
predicted_labels[i] = label_min;
}
}
};
感谢您的任何建议。
基本上
,你不能像使用常规 Rcpp 向量那样减去两个Row
对象(即,利用所谓的 Rcpp 糖)——它只是没有为RcppParallel
包装器实现。您必须自己编写迭代。
相关文章:
- 算术运算的结果类似于:C浮点变量中的1/3
- 更快的C++算术运算
- 为什么循环体中的一个基本算术运算执行得比两个算术运算慢
- 为什么对无符号字符进行算术运算会将它们提升为有符号整数
- 如何在 C++ 中计算字符串的算术运算
- 复数的算术运算
- 具有非常非常大数字的算术运算
- 如何使用C++中的处理器指令来实现快速算术运算
- boost::spirit::x3 phrase_parse 在进入 Vector 之前进行算术运算
- C++中混合数据类型的算术运算
- C++:编译器是否优化整数 + 浮点算术运算?
- 如何在特征中对二维数组的列执行简单的算术运算
- 使用数字列表和算术运算获取目标数字
- C++:“enable_if”用于限制支持特定算术运算的类型
- 如何正确避免 SIGFPE 和算术运算溢出
- 编译器用于编译 128 位整数的基本算术运算的技巧
- 将临时值存储为某种数据类型时,算术运算的标准规则是什么
- 相同的算术运算在 C++ 和 Python 中给出不同的结果
- 带有空的算术运算
- RcppParallel:RMatrix 和 RVector 算术运算