Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ RcppParallel和C++;。不一致的结果_C++_Rcpp_Rcppparallel - Fatal编程技术网

C++ RcppParallel和C++;。不一致的结果

C++ RcppParallel和C++;。不一致的结果,c++,rcpp,rcppparallel,C++,Rcpp,Rcppparallel,我一直在使用RcppParallel,并编写了一个相当简单的示例来了解事情是如何工作的。代码显示在下面 函数float pdf(双x,双西格玛)计算平均值为0且标准偏差为西格玛的高斯分布的缩放版本 Struct_1是一个创建辅助程序以执行某些计算的结构。我填充了一个矩阵来找出某些东西不能正常工作的原因 void Struct\u check()执行计算 该函数似乎可以工作,但有时却不能按预期工作。我认为这与函数pdf中用于执行计算的类型有关 代码下面显示了一个运行示例 我将感谢任何帮助 #inc

我一直在使用RcppParallel,并编写了一个相当简单的示例来了解事情是如何工作的。代码显示在下面

函数float pdf(双x,双西格玛)计算平均值为0且标准偏差为西格玛的高斯分布的缩放版本

Struct_1是一个创建辅助程序以执行某些计算的结构。我填充了一个矩阵来找出某些东西不能正常工作的原因

void Struct\u check()执行计算

该函数似乎可以工作,但有时却不能按预期工作。我认为这与函数pdf中用于执行计算的类型有关

代码下面显示了一个运行示例

我将感谢任何帮助

#include <RcppParallel.h>
#include <RcppArmadillo.h>
#include <RcppArmadilloExtensions/sample.h>
#include <math.h>

#define pi           3.14159265358979323846  /* pi */
using namespace arma;
using namespace Rcpp;
using namespace R;
using namespace sugar;
using namespace std;
using namespace RcppParallel;

// Enable C++11 via this plugin (Rcpp 0.10.3 or later)
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::depends(RcppParallel)]]


// Returns the probability of x, given the distribution described by mu and sigma.
float pdf(double x,  double sigma)
{
     return exp( -1 * x * x / (2 * sigma * sigma)) / sigma;
}

struct Struct_1 : public Worker
{
     arma::vec wr;
     arma::vec sr; 
     NumericVector w2;

     // source matrix
     const RVector<double> input;

     // destination matrix
     RMatrix<double> output;

     // initialize with source and destination
     Struct_1(const NumericMatrix input, NumericMatrix output) 
          : input(input), output(output) {}

     //what is done. 
     void operator()(std::size_t begin, std::size_t end) {

          for (std::size_t i=begin; i<end; i++){ //the processor loop!

               NumericVector w2(3); 

               for (int comp_j=0; comp_j<3; ++comp_j){
                    w2(comp_j) = wr(comp_j) * pdf( input[i], sr(comp_j) ) ;
               }

               double sw1 = sum(w2);

               output(i,0) = w2(0); 
               output(i,1) = w2(1); 
               output(i,2) = w2(2); 
               output(i,3) = sw1; 

               w2 = w2/sw1;

               output(i,4) = w2(0); 
               output(i,5) = w2(1); 
               output(i,6) = w2(2); 

               double sw2 = sum(w2);

               output(i,7) = sw2;

          }//end of i loop
     }//end of operator
};


// [[Rcpp::depends("RcppArmadillo")]]
// [[Rcpp::export]]
void Struct_check(){

     //Some vecs defined
     arma::vec wr = {0.2522, 0.58523, 0.16257};
     arma::vec s2r = {1.2131, 2.9955, 7.5458}; 
     arma::vec sr = sqrt(s2r);

     //an arma mat that will be used in the struct 
     arma::mat arb_mat;
     arb_mat.randn(20);
     Rcout<<"Arb_mat=\n"<<arb_mat<<endl;

     NumericMatrix r_i_x_NM = as<NumericMatrix>(wrap( arb_mat )); //convert to NumericMatrix
     NumericMatrix output( r_i_x_NM.nrow() , 8 ); //define the output matrix

     Struct_1 struct_1( r_i_x_NM , output);
     struct_1.wr = wr;
     struct_1.sr = sr;

     Rcout<<"nrow output = "<<output.nrow()<<endl;
     Rcout<<"ncol output = "<<output.ncol()<<endl;

     parallelFor(0, r_i_x_NM.length(), struct_1);
     Rcout<<"completed Parallell calculations"<<endl;
     Rcout<<"output = \n"<<output<<endl;

}
当计算-1.4482以生成以下行0.228941 0.338113 0.0591801 0.618557 0.591026 0.872861 0.152777 1.61666

在R-检查中,我得到:

wr <- c(0.2522, 0.58523, 0.16257)
s2r <- c(1.2131, 2.9955, 7.5458)
sr <- sqrt(s2r)

w<-NULL
for (i in 1:3){
     w[i] = wr[i]*exp( -0.5*((-1.4482/sr[i])^ 2))/(sr[i])
}

w
[1] 0.09646706 0.23826346 0.05150315

sum(w)
[1] 0.3862337

w = w/sum(w)

w
[1] 0.2497635 0.6168894 0.1333471
wr
wr <- c(0.2522, 0.58523, 0.16257)
s2r <- c(1.2131, 2.9955, 7.5458)
sr <- sqrt(s2r)

w<-NULL
for (i in 1:3){
     w[i] = wr[i]*exp( -0.5*((-1.4482/sr[i])^ 2))/(sr[i])
}

w
[1] 0.09646706 0.23826346 0.05150315

sum(w)
[1] 0.3862337

w = w/sum(w)

w
[1] 0.2497635 0.6168894 0.1333471