piecewise_linear_distribution 클래스

각 간격의 확률이 선형적으로 다양하고 폭이 다양한 간격이 있는 부분 선형 분포를 생성합니다.

구문

template<class RealType = double>
class piecewise_linear_distribution
   {
public:
   // types
   typedef RealType result_type;
   struct param_type;

   // constructor and reset functions
   piecewise_linear_distribution();
   template <class InputIteratorI, class InputIteratorW>
   piecewise_linear_distribution(
      InputIteratorI firstI, InputIteratorI lastI, InputIteratorW firstW);
   template <class UnaryOperation>
   piecewise_linear_distribution(
      initializer_list<result_type> intervals, UnaryOperation weightfunc);
   template <class UnaryOperation>
   piecewise_linear_distribution(
      size_t count, result_type xmin, result_type xmax, UnaryOperation weightfunc);
   explicit piecewise_linear_distribution(const param_type& parm);
   void reset();

   // generating functions
   template <class URNG>
   result_type operator()(URNG& gen);
   template <class URNG>
   result_type operator()(URNG& gen, const param_type& parm);

   // property functions
   vector<result_type> intervals() const;
   vector<result_type> densities() const;
   param_type param() const;
   void param(const param_type& parm);
   result_type min() const;
   result_type max() const;
   };

매개 변수

RealType
부동 소수점 결과 형식으로, 기본적으로 double로 지정되어 있습니다. 가능한 형식은 임>의 형식을 참조하세요<.

설명

이 표본 분포에는 각 간격의 확률이 선형적으로 다양하고 폭이 다양한 간격이 있습니다. 표본 분포에 대한 자세한 내용은 piecewise_linear_distributiondiscrete_distribution을 참조하세요.

다음 테이블은 개별 멤버에 대한 문서와 연결되어 있습니다.

piecewise_linear_distribution
param_type

속성 함수 intervals()는 저장된 분포 간격 집합과 함께 vector<result_type>을 반환합니다.

속성 함수 densities()는 각 간격 집합에 대해 저장된 밀도와 함께 vector<result_type>을 반환합니다. 이러한 밀도는 생성자 매개 변수에서 제공하는 가중치에 따라 계산됩니다.

속성 멤버 param()param_type으로 저장된 분포 매개 변수 패키지를 설정하거나 반환합니다.

min()max() 구성원 함수는 각각 가능한 가장 작은 결과 및 가능한 가장 큰 결과를 반환합니다.

reset() 구성원 함수는 캐시된 모든 값을 버립니다. 따라서 operator()에 대한 다음 호출의 결과는 호출 전 엔진에서 얻은 어떠한 값의 영향도 받지 않습니다.

operator() 구성원 함수는 현재 매개 변수 패키지 또는 지정된 매개 변수 패키지에서 URNG 엔진을 기반으로 하여 다음에 생성된 값을 반환합니다.

배포 클래스 및 해당 멤버에 대한 자세한 내용은 임>의 클래스를 참조<하세요.

예시

// compile with: /EHsc /W4
#include <random>
#include <iostream>
#include <iomanip>
#include <string>
#include <map>

using namespace std;

void test(const int s) {

    // uncomment to use a non-deterministic generator
    // random_device rd;
    // mt19937 gen(rd());
    mt19937 gen(1701);

    // Three intervals, non-uniform: 0 to 1, 1 to 6, and 6 to 15
    vector<double> intervals{ 0, 1, 6, 15 };
    // weights determine the densities used by the distribution
    vector<double> weights{ 1, 5, 5, 10 };

    piecewise_linear_distribution<double> distr(intervals.begin(), intervals.end(), weights.begin());

    cout << endl;
    cout << "min() == " << distr.min() << endl;
    cout << "max() == " << distr.max() << endl;
    cout << "intervals (index: interval):" << endl;
    vector<double> i = distr.intervals();
    int counter = 0;
    for (const auto& n : i) {
        cout << fixed << setw(11) << counter << ": " << setw(14) << setprecision(10) << n << endl;
        ++counter;
    }
    cout << endl;
    cout << "densities (index: density):" << endl;
    vector<double> d = distr.densities();
    counter = 0;
    for (const auto& n : d) {
        cout << fixed << setw(11) << counter << ": " << setw(14) << setprecision(10) << n << endl;
        ++counter;
    }
    cout << endl;

    // generate the distribution as a histogram
    map<int, int> histogram;
    for (int i = 0; i < s; ++i) {
        ++histogram[distr(gen)];
    }

    // print results
    cout << "Distribution for " << s << " samples:" << endl;
    for (const auto& elem : histogram) {
        cout << setw(5) << elem.first << '-' << elem.first + 1 << ' ' << string(elem.second, ':') << endl;
    }
    cout << endl;
}

int main()
{
    int samples = 100;

    cout << "Use CTRL-Z to bypass data entry and run using default values." << endl;
    cout << "Enter an integer value for the sample count: ";
    cin >> samples;

    test(samples);
}
Use CTRL-Z to bypass data entry and run using default values.
Enter an integer value for the sample count: 100
min() == 0
max() == 15
intervals (index: interval):
          0:   0.0000000000
          1:   1.0000000000
          2:   6.0000000000
          3:  15.0000000000
densities (index: density):
          0:   0.0645161290
          1:   0.3225806452
          2:   0.3225806452
          3:   0.6451612903
Distribution for 100 samples:
    0-1 :::::::::::::::::::::
    1-2 ::::::
    2-3 :::
    3-4 :::::::
    4-5 ::::::
    5-6 ::::::
    6-7 :::::
    7-8 ::::::::::
    8-9 ::::::::::
    9-10 ::::::
   10-11 ::::
   11-12 :::
   12-13 :::
   13-14 :::::
   14-15 :::::

요구 사항

헤더:<random>

네임스페이스: std

piecewise_linear_distribution::piecewise_linear_distribution

분포를 생성합니다.

// default constructor
piecewise_linear_distribution();

// constructs using a range of intervals, [firstI, lastI), with
// matching weights starting at firstW
template <class InputIteratorI, class InputIteratorW>
piecewise_linear_distribution(InputIteratorI firstI, InputIteratorI lastI, InputIteratorW firstW);

// constructs using an initializer list for range of intervals,
// with weights generated by function weightfunc
template <class UnaryOperation>
piecewise_linear_distribution(initializer_list<RealType>
intervals, UnaryOperation weightfunc);

// constructs using an initializer list for range of count intervals,
// distributed uniformly over [xmin,xmax] with weights generated by function weightfunc
template <class UnaryOperation>
piecewise_linear_distribution(size_t count, RealType xmin, RealType xmax, UnaryOperation weightfunc);

// constructs from an existing param_type structure
explicit piecewise_linear_distribution(const param_type& parm);

매개 변수

firstI
대상 범위에 있는 첫 번째 요소의 입력 반복기입니다.

lastI
대상 범위에 있는 마지막 요소의 입력 반복기입니다.

firstW
가중치 범위에 있는 첫 번째 요소의 입력 반복기입니다.

intervals
분포의 간격이 있는 initializer_list입니다.

count
분포 범위의 요소 수입니다.

xmin
분포 범위의 가장 작은 값입니다.

xmax
분포 범위의 가장 큰 값입니다. xmin보다 커야 합니다.

weightfunc
분포의 확률 함수를 나타내는 개체입니다. 매개 변수와 반환 값은 둘 다 double로 변환할 수 있어야 합니다.

parm
분포를 생성하는 데 사용되는 매개 변수 구조입니다.

설명

기본 생성자는 저장된 매개 변수를 설정합니다. 따라서 확률 밀도가 1인 0~1 간격이 하나 있습니다.

반복기 범위 생성자

template <class InputIteratorI, class InputIteratorW>
piecewise_linear_distribution(
    InputIteratorI firstI,
    InputIteratorI lastI,
    InputIteratorW firstW);

는 시퀀스 [ firstI, lastI)를 통해 반복기에서 반복기를 사용하여 배포 개체를 생성하고 첫 번째W부터 일치하는 가중치 시퀀스를 생성합니다.

다음 이니셜라이저 목록 생성자는

template <class UnaryOperation>
piecewise_linear_distribution(
    initializer_list<result_type> intervals,
    UnaryOperation weightfunc);

는 이니셜라이저 목록 간격의 간격 과 함수 가중치에서 생성된 가중치를 사용하여 배포 개체를 생성합니다.

다음과 같이 정의된 생성자는

template <class UnaryOperation>
piecewise_linear_distribution(
    size_t count,
    result_type xmin,
    result_type xmax,
    UnaryOperation weightfunc);

는 [xmin,xmax]에 대해 균일하게 분산된 개수 간격이 있는 분포 개체를 생성하고, 함수 가중치에 따라 각 간격 가중치를 할당하고, 가중치는 하나의 매개 변수를 수락하고 반환 값을 가져야 하며, 둘 다 변환할 수 double있습니다. 사전 조건:xmin < xmax.

다음과 같이 정의된 생성자는

explicit piecewise_linear_distribution(const param_type& parm);

는 parm을 저장된 매개 변수 구조로 사용하여 배포 개체를 생성합니다.

piecewise_linear_distribution::param_type

분포의 모든 매개 변수를 저장합니다.

struct param_type {
   typedef piecewise_linear_distribution<result_type> distribution_type;
   param_type();
   template <class IterI, class IterW>
   param_type(
      IterI firstI, IterI lastI, IterW firstW);
   template <class UnaryOperation>
   param_type(
      size_t count, result_type xmin, result_type xmax, UnaryOperation weightfunc);
   std::vector<result_type> densities() const;
   std::vector<result_type> intervals() const;

   bool operator==(const param_type& right) const;
   bool operator!=(const param_type& right) const;
   };

매개 변수

piecewise_linear_distribution에 대한 생성자 매개 변수를 참조하세요.

설명

사전 조건:xmin < xmax

이 구조를 인스턴스화 시에는 분포의 클래스 생성자로, 기존 분포의 저장된 매개 변수를 설정하기 위해서는 param() 멤버 함수로, 저장된 매개 변수 대신 사용하기 위해서는 operator()로 전달할 수 있습니다.

참고 항목

<random>