67 dimension ==
std::numeric_limits<unsigned int>::max() ? -1 : static_cast<int>(dimension))),
68 number_of_functions_for_vectorization_(intervals_.
size()),
69 number_of_functions_for_projections_to_reals_(1)
79 : intervals_(intervals),
80 number_of_functions_for_vectorization_(intervals.
size()),
81 number_of_functions_for_projections_to_reals_(1)
89 double min = std::numeric_limits<int>::max();
90 double max = -std::numeric_limits<int>::max();
91 for (std::size_t i = 0; i != this->intervals_.size(); ++i) {
92 if (this->intervals_[i].first < min) min = this->intervals_[i].first;
93 if (this->intervals_[i].second > max) max = this->intervals_[i].second;
95 return std::make_pair(min, max);
103 double min = std::numeric_limits<int>::max();
104 double max = -std::numeric_limits<int>::max();
105 for (std::size_t i = 0; i != this->intervals_.size(); ++i) {
106 if (this->intervals_[i].second < min) min = this->intervals_[i].second;
107 if (this->intervals_[i].second > max) max = this->intervals_[i].second;
109 return std::make_pair(min, max);
122 std::vector<std::pair<double, double> >
dominant_intervals(std::size_t where_to_cut = 100)
const;
149 std::size_t number_of_bins = 10)
const;
156 std::size_t number_of_bins = 10)
const;
171 std::vector<double>
k_n_n(std::size_t k, std::size_t where_to_cut = 10)
const;
178 for (std::size_t i = 0; i != intervals.intervals_.size(); ++i) {
179 out << intervals.intervals_[i].first <<
" " << intervals.intervals_[i].second << std::endl;
187 void plot(
const char* filename,
188 double min_x = std::numeric_limits<double>::max(),
189 double max_x = std::numeric_limits<double>::max(),
190 double min_y = std::numeric_limits<double>::max(),
191 double max_y = std::numeric_limits<double>::max())
const
196 std::stringstream gnuplot_script;
197 gnuplot_script << filename <<
"_GnuplotScript";
199 out.open(gnuplot_script.str().c_str());
201 std::pair<double, double> min_max_values = this->
get_x_range();
202 if (min_x == max_x) {
203 out <<
"set xrange [" << min_max_values.first - 0.1 * (min_max_values.second - min_max_values.first) <<
" : "
204 << min_max_values.second + 0.1 * (min_max_values.second - min_max_values.first) <<
" ]" << std::endl;
205 out <<
"set yrange [" << min_max_values.first - 0.1 * (min_max_values.second - min_max_values.first) <<
" : "
206 << min_max_values.second + 0.1 * (min_max_values.second - min_max_values.first) <<
" ]" << std::endl;
208 out <<
"set xrange [" << min_x <<
" : " << max_x <<
" ]" << std::endl;
209 out <<
"set yrange [" << min_y <<
" : " << max_y <<
" ]" << std::endl;
211 out <<
"plot '-' using 1:2 notitle \"" << filename <<
"\", \\" << std::endl;
212 out <<
" '-' using 1:2 notitle with lp" << std::endl;
213 for (std::size_t i = 0; i != this->intervals_.size(); ++i) {
214 out << this->intervals_[i].first <<
" " << this->intervals_[i].second << std::endl;
216 out <<
"EOF" << std::endl;
217 out << min_max_values.first - 0.1 * (min_max_values.second - min_max_values.first) <<
" "
218 << min_max_values.first - 0.1 * (min_max_values.second - min_max_values.first) << std::endl;
219 out << min_max_values.second + 0.1 * (min_max_values.second - min_max_values.first) <<
" "
220 << min_max_values.second + 0.1 * (min_max_values.second - min_max_values.first) << std::endl;
225 std::clog <<
"To visualize, install gnuplot and type the command: gnuplot -persist -e \"load \'"
226 << gnuplot_script.str().c_str() <<
"\'\"" << std::endl;
233 std::size_t
size()
const {
return this->intervals_.size(); }
239 const std::pair<double, double>&
operator[](std::size_t i)
const
241 if (i >= this->intervals_.size())
throw(
"Index out of range! Operator [], one_d_gaussians class\n");
242 return this->intervals_[i];
266 std::vector<double>
vectorize(
int number_of_function)
const
280 const std::vector<std::pair<double, double> >& output_for_visualization() {
return this->intervals_; }
283 std::vector<std::pair<double, double> > intervals_;
286 std::size_t number_of_functions_for_vectorization_;
287 std::size_t number_of_functions_for_projections_to_reals_;
304 std::vector<std::pair<std::size_t, double> > position_length_vector(this->intervals_.size());
305 for (std::size_t i = 0; i != this->intervals_.size(); ++i) {
306 position_length_vector[i] = std::make_pair(i, this->intervals_[i].second - this->intervals_[i].first);
309 std::sort(position_length_vector.begin(),
310 position_length_vector.end(),
311 [](
const std::pair<std::size_t, double>& first,
const std::pair<std::size_t, double>& second) ->
bool {
312 return first.second > second.second;
315 std::vector<std::pair<double, double> > result;
316 result.reserve(std::min(where_to_cut, position_length_vector.size()));
318 for (std::size_t i = 0; i != std::min(where_to_cut, position_length_vector.size()); ++i) {
319 result.push_back(this->intervals_[position_length_vector[i].first]);
321 std::clog <<
"Position : " << position_length_vector[i].first <<
" length : " << position_length_vector[i].second
332 std::clog <<
"this->intervals.size() : " << this->intervals_.size() << std::endl;
336 double lengthOfLongest = 0;
337 for (std::size_t i = 0; i != this->intervals_.size(); ++i) {
338 if ((this->intervals_[i].second - this->intervals_[i].first) > lengthOfLongest) {
339 lengthOfLongest = this->intervals_[i].second - this->intervals_[i].first;
344 std::clog <<
"lengthOfLongest : " << lengthOfLongest << std::endl;
348 std::vector<std::size_t> result(number_of_bins + 1, 0);
351 for (std::size_t i = 0; i != this->intervals_.size(); ++i) {
353 double relative_length_of_this_interval =
354 (this->intervals_[i].second - this->intervals_[i].first) / lengthOfLongest;
357 std::size_t position = (std::size_t)(relative_length_of_this_interval * number_of_bins);
362 std::clog <<
"i : " << i << std::endl;
363 std::clog <<
"Interval : [" << this->intervals_[i].first <<
" , " << this->intervals_[i].second <<
" ] \n";
364 std::clog <<
"relative_length_of_this_interval : " << relative_length_of_this_interval << std::endl;
365 std::clog <<
"position : " << position << std::endl;
369 result[number_of_bins - 1] += result[number_of_bins];
370 result.resize(number_of_bins);
373 for (std::size_t i = 0; i != result.size(); ++i) std::clog << result[i] << std::endl;
393 std::size_t number_of_bins)
const
395 std::vector<double> result(number_of_bins);
396 std::fill(result.begin(), result.end(), 0);
398 for (std::size_t i = 0; i != this->intervals_.size(); ++i) {
400 std::clog <<
"Interval : " << this->intervals_[i].first <<
" , " << this->intervals_[i].second << std::endl;
403 std::size_t beginIt = 0;
404 if (this->intervals_[i].first < x_min) beginIt = 0;
405 if (this->intervals_[i].first >= x_max) beginIt = result.size();
406 if ((this->intervals_[i].first > x_min) && (this->intervals_[i].first < x_max)) {
407 beginIt = number_of_bins * (this->intervals_[i].first - x_min) / (x_max - x_min);
410 std::size_t endIt = 0;
411 if (this->intervals_[i].second < x_min) endIt = 0;
412 if (this->intervals_[i].second >= x_max) endIt = result.size();
413 if ((this->intervals_[i].second > x_min) && (this->intervals_[i].second < x_max)) {
414 endIt = number_of_bins * (this->intervals_[i].second - x_min) / (x_max - x_min);
417 if (beginIt > endIt) {
422 std::clog <<
"beginIt : " << beginIt << std::endl;
423 std::clog <<
"endIt : " << endIt << std::endl;
426 for (std::size_t pos = beginIt; pos != endIt; ++pos) {
427 result[pos] += ((x_max - x_min) /
static_cast<double>(number_of_bins)) *
428 (this->intervals_[i].second - this->intervals_[i].first);
431 std::clog <<
"Result at this stage \n";
432 for (std::size_t aa = 0; aa != result.size(); ++aa) {
433 std::clog << result[aa] <<
" ";
435 std::clog << std::endl;
458 std::vector<std::pair<double, bool> > places_where_pbs_change(2 * this->intervals_.size());
460 for (std::size_t i = 0; i != this->intervals_.size(); ++i) {
461 places_where_pbs_change[2 * i] = std::make_pair(this->intervals_[i].first,
true);
462 places_where_pbs_change[2 * i + 1] = std::make_pair(this->intervals_[i].second,
false);
466 places_where_pbs_change.begin(),
467 places_where_pbs_change.end(),
468 [](
const std::pair<double, bool>& f,
const std::pair<double, bool>& s) ->
bool { return f.first < s.first; });
471 std::vector<std::pair<double, std::size_t> > pbns(places_where_pbs_change.size());
472 for (std::size_t i = 0; i != places_where_pbs_change.size(); ++i) {
473 if (places_where_pbs_change[i].second ==
true) {
478 pbns[i] = std::make_pair(places_where_pbs_change[i].first, pbn);
486 std::clog <<
"Here are the intervals : \n";
487 for (std::size_t i = 0; i != this->intervals_.size(); ++i) {
488 std::clog <<
"[ " << this->intervals_[i].first <<
" , " << this->intervals_[i].second <<
"] \n";
492 auto compute_euclidean_distance = [](
const std::pair<double, double>& f,
493 const std::pair<double, double>& s) ->
double {
494 return std::sqrt((f.first - s.first) * (f.first - s.first) + (f.second - s.second) * (f.second - s.second));
497 std::vector<double> result;
500 std::vector<std::vector<double> > distances(this->intervals_.size());
501 for (std::size_t i = 0; i != this->intervals_.size(); ++i) {
502 std::vector<double> aa(this->intervals_.size());
503 std::fill(aa.begin(), aa.end(), 0);
506 std::vector<double> distances_from_diagonal(this->intervals_.size());
507 std::fill(distances_from_diagonal.begin(), distances_from_diagonal.end(), 0);
509 for (std::size_t i = 0; i != this->intervals_.size(); ++i) {
510 std::vector<double> distancesFromI;
511 for (std::size_t j = i + 1; j != this->intervals_.size(); ++j) {
512 distancesFromI.push_back(compute_euclidean_distance(this->intervals_[i], this->intervals_[j]));
515 double distanceToDiagonal =
516 compute_euclidean_distance(this->intervals_[i],
517 std::make_pair(0.5 * (this->intervals_[i].first + this->intervals_[i].second),
518 0.5 * (this->intervals_[i].first + this->intervals_[i].second)));
519 distances_from_diagonal[i] = distanceToDiagonal;
522 std::clog <<
"Here are the distances form the point : [" << this->intervals_[i].first <<
" , "
523 << this->intervals_[i].second <<
"] in the diagram \n";
524 for (std::size_t aa = 0; aa != distancesFromI.size(); ++aa) {
525 std::clog <<
"To : " << i + aa <<
" : " << distancesFromI[aa] <<
" ";
527 std::clog << std::endl;
531 for (std::size_t j = i + 1; j != this->intervals_.size(); ++j) {
532 distances[i][j] = distancesFromI[j - i - 1];
533 distances[j][i] = distancesFromI[j - i - 1];
538 std::clog <<
"Here is the distance matrix : \n";
539 for (std::size_t i = 0; i != distances.size(); ++i) {
540 for (std::size_t j = 0; j != distances.size(); ++j) {
541 std::clog << distances[i][j] <<
" ";
543 std::clog << std::endl;
545 std::clog << std::endl << std::endl <<
"And here are the distances to the diagonal : " << std::endl;
546 for (std::size_t i = 0; i != distances_from_diagonal.size(); ++i) {
547 std::clog << distances_from_diagonal[i] <<
" ";
549 std::clog << std::endl << std::endl;
552 for (std::size_t i = 0; i != this->intervals_.size(); ++i) {
553 std::vector<double> distancesFromI = distances[i];
554 distancesFromI.push_back(distances_from_diagonal[i]);
557 std::sort(distancesFromI.begin(), distancesFromI.end(), std::greater<double>());
559 if (k > distancesFromI.size()) {
561 std::clog <<
"There are not enough neighbors in your set. We set the result to plus infty \n";
563 result.push_back(std::numeric_limits<double>::max());
565 if (distances_from_diagonal[i] > distancesFromI[k]) {
567 std::clog <<
"The k-th n.n. is on a diagonal. Therefore we set up a distance to diagonal \n";
569 result.push_back(distances_from_diagonal[i]);
571 result.push_back(distancesFromI[k]);
575 std::sort(result.begin(), result.end(), std::greater<double>());
576 result.resize(std::min(result.size(), where_to_cut));