26#include "ElementsKernel/Exception.h"
36 if (x_sampling.
size() != y_sampling.
size()) {
37 throw Elements::Exception(
"Cumulative: X and Y sampling do not have the same length.");
42 auto iter = sampling.
begin();
43 while (iter != sampling.
end()) {
53 auto iter = sampling.
begin();
54 while (iter != sampling.
end()) {
65 auto iter_pdf = pdf_sampling.
cbegin();
67 while (iter_pdf != pdf_sampling.
cend()) {
90 if (ratio > 1. || ratio < 0.) {
91 throw Elements::Exception(
"Cumulative::findValue : ratio parameter must be in range [0,1]");
98 while (iter_y !=
m_y_sampling.cend() && (*iter_y) < value) {
103 double begin_value = *iter_x;
104 double tray = *iter_y;
105 while (iter_y !=
m_y_sampling.cend() && (*iter_y) == tray) {
110 double end_value = *(--iter_x);
115 result = begin_value;
118 result = 0.5 * (begin_value + end_value);
130 if (rate > 1. || rate <= 0.) {
131 throw Elements::Exception(
"Cumulative::findMinInterval : rate parameter must be in range ]0,1]");
145 while (iter_2_x !=
m_x_sampling.cend() && (*iter_2_y - *iter_1_y + first_correction) < rate) {
152 if ((*iter_2_x - *iter_1_x) <= (*max_x - *min_x)) {
158 first_correction = 0.;
166 if (rate > 1. || rate <= 0.) {
167 throw Elements::Exception(
"Cumulative::findCenteredInterval : rate parameter must be in range ]0,1]");
170 double min_value =
m_y_sampling.back() * (0.5 - rate / 2.0);
171 double max_value =
m_y_sampling.back() * (0.5 + rate / 2.0);
175 while (iter_y !=
m_y_sampling.cend() && (*iter_y) < min_value) {
180 if ((*iter_y) < max_value) {
181 double tray = *iter_y;
182 while (iter_y !=
m_y_sampling.cend() && (*iter_y) == tray) {
186 double min_x = (iter_x !=
m_x_sampling.begin()) ? *(iter_x - 1) : *iter_x;
188 while (iter_y !=
m_y_sampling.cend() && (*iter_y) < max_value) {
192 double max_x = *iter_x;
196 double min_x = (iter_x !=
m_x_sampling.begin()) ? *(iter_x - 1) : *iter_x;
197 double max_x = *iter_x;
210 return (*function)(x_value);
Cumulative(Cumulative &&other)=default
move constructor
TrayPosition
when looking for the position having a given value, one may encounter tray where the value is constan...
std::vector< double > m_x_sampling
std::pair< double, double > findCenteredInterval(double rate) const
return the horizontal interval starting where the Cumulative has value (1-ratio)/2 and ending where t...
std::vector< double > m_y_sampling
void normalize()
Normalize the Cumulative. After calling this function the last vertical value is 1....
double findValue(double ratio, TrayPosition position=TrayPosition::middle) const
Find the first horizontal sample which vertical value is bigger or equal to the ratio value....
std::pair< double, double > findMinInterval(double rate) const
Scan the horizontal axis looking for the smallest x-interval for which the vertical interval is at le...
static Cumulative fromPdf(std::vector< double > &x_sampling, std::vector< double > &pdf_sampling)
Factory from the sampling of a PDF. The Cumulative vertical samples are build as the sum of the the p...
double eval(double x_value) const
return the value of the cumulative at a given value of the horizontal axis. If the value do not match...
This module provides an interface for accessing two dimensional datasets (pairs of (X,...
const_iterator begin() const
Returns a const iterator to the first pair of the dataset.
const_iterator end() const
Returns a const iterator to the one after last pair dataset.
ELEMENTS_API std::unique_ptr< Function > interpolate(const std::vector< double > &x, const std::vector< double > &y, InterpolationType type, bool extrapolate=false)