Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.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
使用gsl_直方图_pdf_样本从即席分发中采样 我很难理解如何使用C++中的GSL库函数gSLIGraseGracpDFFX示例。文件在这里_C++_Pointers_Struct_Histogram_Gsl - Fatal编程技术网

使用gsl_直方图_pdf_样本从即席分发中采样 我很难理解如何使用C++中的GSL库函数gSLIGraseGracpDFFX示例。文件在这里

使用gsl_直方图_pdf_样本从即席分发中采样 我很难理解如何使用C++中的GSL库函数gSLIGraseGracpDFFX示例。文件在这里,c++,pointers,struct,histogram,gsl,C++,Pointers,Struct,Histogram,Gsl,我还不是一个真正的专家,所以,我想知道是否有人能告诉我这个代码有什么问题 #include <iostream> #include <gsl/gsl_histogram.h> #include <gsl/gsl_rng.h> using namespace std; int main() { // I am going to use 5 bins size_t Bins = 5; // These are the ranges (

我还不是一个真正的专家,所以,我想知道是否有人能告诉我这个代码有什么问题

#include <iostream>
#include <gsl/gsl_histogram.h>
#include <gsl/gsl_rng.h>

using namespace std;

int main()
{
    // I am going to use 5 bins
    size_t  Bins = 5;
    // These are the ranges (must be Bins + 1)
    double range[6] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 };
    // Array with probabilities
    double w[5]  = {0.05, 0.1, 0.3, 0.4, 1};

    // Create the histogram pdf

    gsl_histogram_pdf MyHistPdf;

    MyHistPdf.n     = Bins;
    MyHistPdf.range = range;
    MyHistPdf.sum   = w;

    const gsl_rng_type * T;
    gsl_rng * r;
    T = gsl_rng_default;
    r = gsl_rng_alloc (T);
    double u = gsl_rng_uniform(r);
    cout << u << endl;
    double a = gsl_histogram_pdf_sample(&MyHistPdf, u);

    return 0;
}

我不知道这是什么意思。

在回答您的问题之前,您需要了解有关GSL的一条基本规则:“除非您详细了解GSL源代码,否则切勿直接操作GSL结构变量”。如果你这样做,你的程序将不可避免地失败,或者你的结果将毫无意义!为什么?因为您不知道关于如何存储和操作这些变量的内部gsl约定的细节

仔细阅读文档,您将看到有API函数为您分配和操作所有结构变量。如果你是C++程序员,一个好的“经验法则”就是想象GSL结构中的所有变量都是私有的(这不是唯一的原因,因为C没有使成员变量私有化的能力)。 文档中很清楚,需要分配gsl_histogram结构,需要插入一些数据,并且在调用gsl_histogram_pdf样本之前,需要使用特定的API函数初始化您使用的gsl_histogram_pdf。我看到您试图通过破解“sum”指针来避免插入一些数据,但GSL失败了,正如我所说的,如果您不遵循基本规则,就会发生这种情况

下面是我写的一个例子来指导大家

#include <iostream>
#include <cassert>
#include <gsl/gsl_histogram.h>
#include <gsl/gsl_rng.h>

using namespace std;

int main()
{
   size_t  Bins = 5;
   double range[6] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 };

    // histogram 
    gsl_histogram* hist = gsl_histogram_alloc (Bins);
    assert( hist != NULL );
    gsl_histogram_set_ranges (hist, range, 6);

    // include some elements
    const gsl_rng_type * T = gsl_rng_default;
    gsl_rng *r = gsl_rng_alloc (T);

    for(int j = 0; j < 10000; ++j ) {
      const double u = gsl_rng_uniform(r) * 5.0; 
      gsl_histogram_increment (hist, u);            
    }

    // Create the histogram pdf
    gsl_histogram_pdf*  MyHistPdf = gsl_histogram_pdf_alloc (Bins);
    assert( MyHistPdf != NULL );
    int status = gsl_histogram_pdf_init (MyHistPdf, hist);
    assert( status != GSL_EDOM );

    std::cout << "result you want";
    std::cout << gsl_histogram_pdf_sample(MyHistPdf,gsl_rng_uniform(r)) << std::endl;           
    return 0;
  }
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
垃圾箱的尺寸=5;
双量程[6]={0.0,1.0,2.0,3.0,4.0,5.0};
//直方图
gsl_柱状图*历史=gsl_柱状图分配(箱);
断言(hist!=NULL);
gsl_直方图_集合_范围(历史,范围,6);
//包括一些元素
const gsl\u rng\u type*T=gsl\u rng\u默认值;
gsl_rng*r=gsl_rng_alloc(T);
对于(int j=0;j<10000;++j){
常数双u=gsl\u rng\u均匀(r)*5.0;
gsl_直方图_增量(hist,u);
}
//创建直方图pdf
gsl\U柱状图\U pdf*MyHistPdf=gsl\U柱状图\U pdf\U alloc(Bins);
断言(MyHistPdf!=NULL);
int status=gsl\u直方图\u pdf\u init(MyHistPdf,hist);
断言(status!=GSL_EDOM);

我明白了!所以它比我想象的要详细得多。你是对的,显然我没有正确理解文档。非常感谢!!
#include <iostream>
#include <cassert>
#include <gsl/gsl_histogram.h>
#include <gsl/gsl_rng.h>

using namespace std;

int main()
{
   size_t  Bins = 5;
   double range[6] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 };

    // histogram 
    gsl_histogram* hist = gsl_histogram_alloc (Bins);
    assert( hist != NULL );
    gsl_histogram_set_ranges (hist, range, 6);

    // include some elements
    const gsl_rng_type * T = gsl_rng_default;
    gsl_rng *r = gsl_rng_alloc (T);

    for(int j = 0; j < 10000; ++j ) {
      const double u = gsl_rng_uniform(r) * 5.0; 
      gsl_histogram_increment (hist, u);            
    }

    // Create the histogram pdf
    gsl_histogram_pdf*  MyHistPdf = gsl_histogram_pdf_alloc (Bins);
    assert( MyHistPdf != NULL );
    int status = gsl_histogram_pdf_init (MyHistPdf, hist);
    assert( status != GSL_EDOM );

    std::cout << "result you want";
    std::cout << gsl_histogram_pdf_sample(MyHistPdf,gsl_rng_uniform(r)) << std::endl;           
    return 0;
  }