12#ifndef PERSISTENCE_LANDSCAPE_ON_GRID_H_
13#define PERSISTENCE_LANDSCAPE_ON_GRID_H_
33#include <gudhi/read_persistence_from_file.h>
35#include <gudhi/Debug_utils.h>
38namespace Persistence_representations {
42template <
typename Operation>
71 number_of_functions_for_vectorization_(0),
72 number_of_functions_for_projections_to_reals_(0)
81 std::size_t number_of_points)
83 this->_set_up_values_of_landscapes(p, grid_min, grid_max, number_of_points);
93 std::size_t number_of_points,
94 unsigned number_of_levels_of_landscape)
96 this->_set_up_values_of_landscapes(p, grid_min, grid_max, number_of_points, number_of_levels_of_landscape);
110 std::size_t number_of_points,
111 unsigned number_of_levels_of_landscape,
112 std::uint16_t dimension = std::numeric_limits<std::uint16_t>::max())
114 std::vector<std::pair<double, double> > p;
115 if (dimension == std::numeric_limits<std::uint16_t>::max()) {
120 this->_set_up_values_of_landscapes(p, grid_min, grid_max, number_of_points, number_of_levels_of_landscape);
133 std::size_t number_of_points,
134 std::uint16_t dimension = std::numeric_limits<std::uint16_t>::max())
136 std::vector<std::pair<double, double> > p;
137 if (dimension == std::numeric_limits<std::uint16_t>::max()) {
142 this->_set_up_values_of_landscapes(p, grid_min, grid_max, number_of_points);
153 std::size_t number_of_points,
154 unsigned number_of_levels_of_landscape,
155 std::uint16_t dimension = std::numeric_limits<std::uint16_t>::max())
157 std::vector<std::pair<double, double> > p;
158 if (dimension == std::numeric_limits<std::uint16_t>::max()) {
163 double grid_min = std::numeric_limits<double>::max();
164 double grid_max = -std::numeric_limits<double>::max();
165 for (std::size_t i = 0; i != p.size(); ++i) {
166 if (p[i].first < grid_min) grid_min = p[i].first;
167 if (p[i].second > grid_max) grid_max = p[i].second;
169 this->_set_up_values_of_landscapes(p, grid_min, grid_max, number_of_points, number_of_levels_of_landscape);
181 std::size_t number_of_points,
182 std::uint16_t dimension = std::numeric_limits<std::uint16_t>::max())
184 std::vector<std::pair<double, double> > p;
185 if (dimension == std::numeric_limits<std::uint16_t>::max()) {
190 double grid_min = std::numeric_limits<double>::max();
191 double grid_max = -std::numeric_limits<double>::max();
192 for (std::size_t i = 0; i != p.size(); ++i) {
193 if (p[i].first < grid_min) grid_min = p[i].first;
194 if (p[i].second > grid_max) grid_max = p[i].second;
196 this->_set_up_values_of_landscapes(p, grid_min, grid_max, number_of_points);
217 for (std::size_t i = 0; i != maximal_level; ++i) {
229 double dx = (this->grid_max_ - this->grid_min_) /
static_cast<double>(this->values_of_landscapes_.size() - 1);
232 std::clog <<
"this->grid_max : " << this->grid_max_ << std::endl;
233 std::clog <<
"this->grid_min : " << this->grid_min_ << std::endl;
234 std::clog <<
"this->values_of_landscapes.size() : " << this->values_of_landscapes_.size() << std::endl;
237 double previous_x = this->grid_min_ - dx;
238 double previous_y = 0;
239 for (std::size_t i = 0; i != this->values_of_landscapes_.size(); ++i) {
240 double current_x = previous_x + dx;
241 double current_y = 0;
242 if (this->values_of_landscapes_[i].
size() > level) current_y = this->values_of_landscapes_[i][level];
245 std::clog <<
"this->values_of_landscapes[i].size() : " << this->values_of_landscapes_[i].size()
246 <<
" , level : " << level << std::endl;
247 if (this->values_of_landscapes_[i].
size() > level)
248 std::clog <<
"this->values_of_landscapes[i][level] : " << this->values_of_landscapes_[i][level] << std::endl;
249 std::clog <<
"previous_y : " << previous_y << std::endl;
250 std::clog <<
"current_y : " << current_y << std::endl;
251 std::clog <<
"dx : " << dx << std::endl;
252 std::clog <<
"0.5*dx*( previous_y + current_y ); " << 0.5 * dx * (previous_y + current_y) << std::endl;
255 result += 0.5 * dx * (previous_y + current_y);
256 previous_x = current_x;
257 previous_y = current_y;
270 for (std::size_t i = 0; i != maximal_level; ++i) {
283 double dx = (this->grid_max_ - this->grid_min_) /
static_cast<double>(this->values_of_landscapes_.size() - 1);
284 double previous_x = this->grid_min_;
285 double previous_y = 0;
286 if (this->values_of_landscapes_[0].
size() > level) previous_y = this->values_of_landscapes_[0][level];
289 std::clog <<
"dx : " << dx << std::endl;
290 std::clog <<
"previous_x : " << previous_x << std::endl;
291 std::clog <<
"previous_y : " << previous_y << std::endl;
292 std::clog <<
"power : " << p << std::endl;
295 for (std::size_t i = 0; i != this->values_of_landscapes_.size(); ++i) {
296 double current_x = previous_x + dx;
297 double current_y = 0;
298 if (this->values_of_landscapes_[i].
size() > level) current_y = this->values_of_landscapes_[i][level];
301 std::clog <<
"current_y : " << current_y << std::endl;
304 if (current_y == previous_y)
continue;
306 std::pair<double, double> coef =
308 double a = coef.first;
309 double b = coef.second;
312 std::clog <<
"A line passing through points : (" << previous_x <<
"," << previous_y <<
") and (" << current_x
313 <<
"," << current_y <<
") is : " << a <<
"x+" << b << std::endl;
318 double value_to_add = 0;
321 1 / (a * (p + 1)) * (std::pow((a * current_x + b), p + 1) - std::pow((a * previous_x + b), p + 1));
323 value_to_add = (current_x - previous_x) * (std::pow(b, p));
325 result += value_to_add;
327 std::clog <<
"Increasing result by : " << value_to_add << std::endl;
328 std::clog <<
"result : " << result << std::endl;
330 previous_x = current_x;
331 previous_y = current_y;
334 std::clog <<
"The total result is : " << result << std::endl;
346 double dx = (land.grid_max_ - land.grid_min_) /
static_cast<double>(land.values_of_landscapes_.size() - 1);
347 double x = land.grid_min_;
348 for (std::size_t i = 0; i != land.values_of_landscapes_.size(); ++i) {
350 for (std::size_t j = 0; j != land.values_of_landscapes_[i].size(); ++j) {
351 out << land.values_of_landscapes_[i][j] <<
" ";
366 if ((x < this->grid_min_) || (x > this->grid_max_))
return 0;
369 double dx = (this->grid_max_ - this->grid_min_) /
static_cast<double>(this->values_of_landscapes_.size() - 1);
370 std::size_t position = std::size_t((x - this->grid_min_) / dx);
373 std::clog <<
"This is a procedure compute_value_at_a_given_point \n";
374 std::clog <<
"level : " << level << std::endl;
375 std::clog <<
"x : " << x << std::endl;
376 std::clog <<
"position : " << position << std::endl;
381 if (this->values_of_landscapes_[position].
size() < level) {
382 return this->values_of_landscapes_[position][level];
388 std::pair<double, double> line;
389 if ((this->values_of_landscapes_[position].
size() > level) &&
390 (this->values_of_landscapes_[position + 1].
size() > level)) {
392 std::make_pair(position * dx + this->grid_min_, this->values_of_landscapes_[position][level]),
393 std::make_pair((position + 1) * dx + this->grid_min_, this->values_of_landscapes_[position + 1][level]));
395 if ((this->values_of_landscapes_[position].
size() > level) ||
396 (this->values_of_landscapes_[position + 1].
size() > level)) {
397 if ((this->values_of_landscapes_[position].
size() > level)) {
399 std::make_pair(position * dx + this->grid_min_, this->values_of_landscapes_[position][level]),
400 std::make_pair((position + 1) * dx + this->grid_min_, 0));
403 std::make_pair(position * dx + this->grid_min_, 0),
404 std::make_pair((position + 1) * dx + this->grid_min_, this->values_of_landscapes_[position + 1][level]));
411 return line.first * x + line.second;
417 if (land1.values_of_landscapes_.size() != land2.values_of_landscapes_.size())
return false;
418 if (land1.grid_min_ != land2.grid_min_)
return false;
419 if (land1.grid_max_ != land2.grid_max_)
return false;
426 template <
typename Operation>
433 if (!check_if_defined_on_the_same_domain(land1, land2))
throw std::invalid_argument(
"Two grids are not compatible");
437 result.values_of_landscapes_ = std::vector<std::vector<double> >(land1.values_of_landscapes_.size());
438 result.grid_min_ = land1.grid_min_;
439 result.grid_max_ = land1.grid_max_;
442 for (std::size_t grid_point = 0; grid_point != land1.values_of_landscapes_.size(); ++grid_point) {
443 result.values_of_landscapes_[grid_point] = std::vector<double>(
444 std::max(land1.values_of_landscapes_[grid_point].size(), land2.values_of_landscapes_[grid_point].size()));
445 for (std::size_t lambda = 0; lambda != std::max(land1.values_of_landscapes_[grid_point].size(),
446 land2.values_of_landscapes_[grid_point].size());
450 if (lambda < land1.values_of_landscapes_[grid_point].size())
451 value1 = land1.values_of_landscapes_[grid_point][lambda];
452 if (lambda < land2.values_of_landscapes_[grid_point].size())
453 value2 = land2.values_of_landscapes_[grid_point][lambda];
454 result.values_of_landscapes_[grid_point][lambda] = oper(value1, value2);
506 return first._multiply_landscape_by_real_number_not_overwrite(con);
514 return first._multiply_landscape_by_real_number_not_overwrite(con);
550 if (x == 0)
throw(
"In operator /=, division by 0. Program terminated.");
551 *
this = *
this * (1 / x);
560 if (this->values_of_landscapes_.size() != rhs.values_of_landscapes_.size()) {
562 std::clog <<
"values_of_landscapes of incompatible sizes\n";
568 std::clog <<
"grid_min not equal\n";
574 std::clog <<
"grid_max not equal\n";
578 for (std::size_t i = 0; i != this->values_of_landscapes_.size(); ++i) {
579 for (std::size_t aa = 0; aa != this->values_of_landscapes_[i].size(); ++aa) {
580 if (!
almost_equal(this->values_of_landscapes_[i][aa], rhs.values_of_landscapes_[i][aa])) {
582 std::clog <<
"Problem in the position : " << i <<
" of values_of_landscapes. \n";
583 std::clog << this->values_of_landscapes_[i][aa] <<
" " << rhs.values_of_landscapes_[i][aa] << std::endl;
604 double max_value = -std::numeric_limits<double>::max();
605 for (std::size_t i = 0; i != this->values_of_landscapes_.size(); ++i) {
606 if (this->values_of_landscapes_[i].
size()) {
607 if (this->values_of_landscapes_[i][0] > max_value) max_value = this->values_of_landscapes_[i][0];
608 if (this->values_of_landscapes_[i][this->values_of_landscapes_[i].
size() - 1] > max_value)
609 max_value = this->values_of_landscapes_[i][this->values_of_landscapes_[i].size() - 1];
622 double max_value = -std::numeric_limits<double>::max();
623 double min_value = 0;
624 for (std::size_t i = 0; i != this->values_of_landscapes_.size(); ++i) {
625 if (this->values_of_landscapes_[i].
size()) {
626 if (this->values_of_landscapes_[i][0] > max_value) max_value = this->values_of_landscapes_[i][0];
627 if (this->values_of_landscapes_[i][this->values_of_landscapes_[i].
size() - 1] > max_value)
628 max_value = this->values_of_landscapes_[i][this->values_of_landscapes_[i].size() - 1];
630 if (this->values_of_landscapes_[i][0] < min_value) min_value = this->values_of_landscapes_[i][0];
631 if (this->values_of_landscapes_[i][this->values_of_landscapes_[i].
size() - 1] < min_value)
632 min_value = this->values_of_landscapes_[i][this->values_of_landscapes_[i].size() - 1];
635 return std::make_pair(min_value, max_value);
642 std::pair<double, double>
get_x_range(std::size_t level = 0)
const
644 return std::make_pair(this->grid_min_, this->grid_max_);
658 std::size_t result = 0;
659 for (std::size_t i = 0; i != this->values_of_landscapes_.size(); ++i) {
660 if (this->values_of_landscapes_[i].
size() > result) result = this->values_of_landscapes_[i].size();
670 std::vector<std::pair<double, double> > p;
673 if (i < std::numeric_limits<double>::max()) {
694 if (!check_if_defined_on_the_same_domain(first, second))
throw "Two grids are not compatible";
696 for (std::size_t i = 0; i != first.values_of_landscapes_.size(); ++i) {
697 for (std::size_t j = 0;
698 j != std::min(first.values_of_landscapes_[i].size(), second.values_of_landscapes_[i].size());
700 if (result < std::abs(first.values_of_landscapes_[i][j] - second.values_of_landscapes_[i][j])) {
701 result = std::abs(first.values_of_landscapes_[i][j] - second.values_of_landscapes_[i][j]);
704 if (first.values_of_landscapes_[i].size() ==
705 std::min(first.values_of_landscapes_[i].size(), second.values_of_landscapes_[i].size())) {
706 for (std::size_t j = first.values_of_landscapes_[i].size(); j != second.values_of_landscapes_[i].size(); ++j) {
707 if (result < second.values_of_landscapes_[i][j]) result = second.values_of_landscapes_[i][j];
710 if (second.values_of_landscapes_[i].size() ==
711 std::min(first.values_of_landscapes_[i].size(), second.values_of_landscapes_[i].size())) {
712 for (std::size_t j = second.values_of_landscapes_[i].size(); j != first.values_of_landscapes_[i].size(); ++j) {
713 if (result < first.values_of_landscapes_[i][j]) result = first.values_of_landscapes_[i][j];
728 for (std::size_t i = 0; i != this->values_of_landscapes_.size(); ++i) {
729 for (std::size_t j = 0; j != this->values_of_landscapes_[i].size(); ++j) {
730 this->values_of_landscapes_[i][j] = std::abs(this->values_of_landscapes_[i][j]);
745 double max_value = -std::numeric_limits<double>::max();
746 for (std::size_t i = 0; i != this->values_of_landscapes_.size(); ++i) {
747 if (this->values_of_landscapes_[i].
size() > lambda) {
748 if (this->values_of_landscapes_[i][lambda] > max_value) max_value = this->values_of_landscapes_[i][lambda];
760 if (!check_if_defined_on_the_same_domain(l1, l2))
761 throw std::invalid_argument(
"Landscapes are not defined on the same grid, the program will now terminate");
764 for (std::size_t i = 0; i != maximal_level; ++i) {
778 if (!check_if_defined_on_the_same_domain(l1, l2))
779 throw std::invalid_argument(
"Landscapes are not defined on the same grid, the program will now terminate");
783 double dx = (l1.grid_max_ - l1.grid_min_) /
static_cast<double>(l1.values_of_landscapes_.size() - 1);
785 double previous_x = l1.grid_min_ - dx;
786 double previous_y_l1 = 0;
787 double previous_y_l2 = 0;
788 for (std::size_t i = 0; i != l1.values_of_landscapes_.size(); ++i) {
790 std::clog <<
"i : " << i << std::endl;
793 double current_x = previous_x + dx;
794 double current_y_l1 = 0;
795 if (l1.values_of_landscapes_[i].size() > level) current_y_l1 = l1.values_of_landscapes_[i][level];
797 double current_y_l2 = 0;
798 if (l2.values_of_landscapes_[i].size() > level) current_y_l2 = l2.values_of_landscapes_[i][level];
801 std::clog <<
"previous_x : " << previous_x << std::endl;
802 std::clog <<
"previous_y_l1 : " << previous_y_l1 << std::endl;
803 std::clog <<
"current_y_l1 : " << current_y_l1 << std::endl;
804 std::clog <<
"previous_y_l2 : " << previous_y_l2 << std::endl;
805 std::clog <<
"current_y_l2 : " << current_y_l2 << std::endl;
809 std::make_pair(current_x, current_y_l1));
811 std::make_pair(current_x, current_y_l2));
815 double a = l1_coords.first;
816 double b = l1_coords.second;
818 double c = l2_coords.first;
819 double d = l2_coords.second;
822 std::clog <<
"Here are the formulas for a line: \n";
823 std::clog <<
"a : " << a << std::endl;
824 std::clog <<
"b : " << b << std::endl;
825 std::clog <<
"c : " << c << std::endl;
826 std::clog <<
"d : " << d << std::endl;
833 double added_value = (a * c / 3 * current_x * current_x * current_x +
834 (a * d + b * c) / 2 * current_x * current_x + b * d * current_x) -
835 (a * c / 3 * previous_x * previous_x * previous_x +
836 (a * d + b * c) / 2 * previous_x * previous_x + b * d * previous_x);
839 std::clog <<
"Value of the integral on the left end i.e. : " << previous_x <<
" is : "
840 << a * c / 3 * previous_x * previous_x * previous_x + (a * d + b * c) / 2 * previous_x * previous_x +
843 std::clog <<
"Value of the integral on the right end i.e. : " << current_x <<
" is "
844 << a * c / 3 * current_x * current_x * current_x + (a * d + b * c) / 2 * current_x * current_x +
849 result += added_value;
852 std::clog <<
"added_value : " << added_value << std::endl;
853 std::clog <<
"result : " << result << std::endl;
856 previous_x = current_x;
857 previous_y_l1 = current_y_l1;
858 previous_y_l2 = current_y_l2;
880 std::clog <<
"first : " << first << std::endl;
881 std::clog <<
"second : " << second << std::endl;
888 std::clog <<
"Difference : " << lan << std::endl;
895 std::clog <<
"Abs : " << lan << std::endl;
898 if (p < std::numeric_limits<double>::max()) {
903 std::clog <<
"p : " << p << std::endl;
907 std::clog <<
"integral : " << result << std::endl;
912 std::clog <<
"integral, without power : " << result << std::endl;
916 return std::pow(result, 1.0 / p);
947 std::vector<double>
vectorize(
int number_of_function)
const
951 if (number_of_function < 0 ||
static_cast<std::size_t
>(number_of_function) >= this->values_of_landscapes_.size()) {
952 throw std::invalid_argument(
"Wrong number of function.");
954 std::vector<double> v(this->values_of_landscapes_.size());
955 for (std::size_t i = 0; i != this->values_of_landscapes_.size(); ++i) {
957 if (this->values_of_landscapes_[i].
size() >
static_cast<std::size_t
>(number_of_function)) {
958 v[i] = this->values_of_landscapes_[i][number_of_function];
978 this->values_of_landscapes_.clear();
979 this->grid_min_ = this->grid_max_ = 0;
982 if (to_average.size() == 0)
return;
985 for (std::size_t i = 0; i != to_average.size(); ++i) {
986 if (!check_if_defined_on_the_same_domain(*(to_average[0]), *(to_average[i])))
987 throw "Two grids are not compatible";
990 this->values_of_landscapes_ = std::vector<std::vector<double> >((to_average[0])->values_of_landscapes_.size());
991 this->grid_min_ = (to_average[0])->grid_min_;
992 this->grid_max_ = (to_average[0])->grid_max_;
995 std::clog <<
"Computations of average. The data from the current landscape have been cleared. We are ready to do "
996 "the computations. \n";
1000 for (std::size_t grid_point = 0; grid_point != (to_average[0])->values_of_landscapes_.size(); ++grid_point) {
1002 std::size_t maximal_size_of_vector = 0;
1003 for (std::size_t land_no = 0; land_no != to_average.size(); ++land_no) {
1004 if ((to_average[land_no])->values_of_landscapes_[grid_point].
size() > maximal_size_of_vector)
1005 maximal_size_of_vector = (to_average[land_no])->values_of_landscapes_[grid_point].
size();
1007 this->values_of_landscapes_[grid_point] = std::vector<double>(maximal_size_of_vector);
1010 std::clog <<
"We are considering the point : " << grid_point
1011 <<
" of the grid. In this point, there are at most : " << maximal_size_of_vector
1012 <<
" nonzero landscape functions \n";
1016 for (std::size_t land_no = 0; land_no != to_average.size(); ++land_no) {
1018 for (std::size_t i = 0; i != (to_average[land_no])->values_of_landscapes_[grid_point].size(); ++i) {
1020 this->values_of_landscapes_[grid_point][i] += (to_average[land_no])->values_of_landscapes_[grid_point][i];
1024 for (std::size_t i = 0; i != this->values_of_landscapes_[grid_point].size(); ++i) {
1025 this->values_of_landscapes_[grid_point][i] /=
static_cast<double>(to_average.size());
1038 if (power < std::numeric_limits<double>::max()) {
1070 void plot(
const char* filename, std::size_t from, std::size_t to)
const
1072 this->
plot(filename,
1073 std::numeric_limits<double>::max(),
1074 std::numeric_limits<double>::max(),
1075 std::numeric_limits<double>::max(),
1076 std::numeric_limits<double>::max(),
1085 void plot(
const char* filename,
1086 double min_x = std::numeric_limits<double>::max(),
1087 double max_x = std::numeric_limits<double>::max(),
1088 double min_y = std::numeric_limits<double>::max(),
1089 double max_y = std::numeric_limits<double>::max(),
1090 std::size_t from = std::numeric_limits<std::size_t>::max(),
1091 std::size_t to = std::numeric_limits<std::size_t>::max())
const;
1096 std::vector<std::vector<double> > values_of_landscapes_;
1097 std::size_t number_of_functions_for_vectorization_;
1098 std::size_t number_of_functions_for_projections_to_reals_;
1100 void _set_up_values_of_landscapes(
const std::vector<std::pair<double, double> >& p,
1103 std::size_t number_of_points,
1104 unsigned number_of_levels = std::numeric_limits<unsigned>::max());
1108inline void Persistence_landscape_on_grid::_set_up_values_of_landscapes(
1109 const std::vector<std::pair<double, double> >& p,
1112 std::size_t number_of_points,
1113 unsigned number_of_levels)
1116 std::clog <<
"Here is the procedure : set_up_values_of_landscapes. The parameters are : grid_min_ : " << grid_min
1117 <<
", grid_max_ : " << grid_max <<
", number_of_points_ : " << number_of_points
1118 <<
", number_of_levels: " << number_of_levels << std::endl;
1119 std::clog <<
"Here are the intervals at our disposal : \n";
1120 for (std::size_t i = 0; i != p.size(); ++i) {
1121 std::clog << p[i].first <<
" , " << p[i].second << std::endl;
1125 if ((grid_min == std::numeric_limits<double>::max()) || (grid_max == std::numeric_limits<double>::max())) {
1127 double min = std::numeric_limits<double>::max();
1128 double max = std::numeric_limits<double>::min();
1129 for (std::size_t i = 0; i != p.size(); ++i) {
1130 if (p[i].first < min) min = p[i].first;
1131 if (p[i].second > max) max = p[i].second;
1133 if (grid_min == std::numeric_limits<double>::max()) {
1144 this->values_of_landscapes_ = std::vector<std::vector<double> >(number_of_points + 1);
1146 this->grid_min_ = grid_min;
1147 this->grid_max_ = grid_max;
1149 if (grid_max <= grid_min) {
1150 throw std::invalid_argument(
1151 "Wrong parameters of grid_min and grid_max given to the procedure. The program will now terminate.");
1154 double dx = (grid_max - grid_min) /
static_cast<double>(number_of_points);
1156 for (std::size_t int_no = 0; int_no != p.size(); ++int_no) {
1157 std::size_t grid_interval_begin = (p[int_no].first - grid_min) / dx;
1158 std::size_t grid_interval_end = (p[int_no].second - grid_min) / dx;
1159 std::size_t grid_interval_midpoint = (std::size_t)(0.5 * (grid_interval_begin + grid_interval_end));
1162 std::clog <<
"Considering an interval : " << p[int_no].first <<
"," << p[int_no].second << std::endl;
1164 std::clog <<
"grid_interval_begin : " << grid_interval_begin << std::endl;
1165 std::clog <<
"grid_interval_end : " << grid_interval_end << std::endl;
1166 std::clog <<
"grid_interval_midpoint : " << grid_interval_midpoint << std::endl;
1169 double landscape_value = dx;
1170 for (std::size_t i = grid_interval_begin + 1; i < grid_interval_midpoint; ++i) {
1172 std::clog <<
"Adding landscape value (going up) for a point : " << i <<
" equal : " << landscape_value
1175 if (number_of_levels != std::numeric_limits<unsigned>::max()) {
1179 if (this->values_of_landscapes_[i].
size() >= number_of_levels) {
1182 if (-landscape_value < this->values_of_landscapes_[i].front()) {
1184 std::pop_heap(this->values_of_landscapes_[i].begin(), this->values_of_landscapes_[i].end());
1185 this->values_of_landscapes_[i][this->values_of_landscapes_[i].size() - 1] = -landscape_value;
1186 std::push_heap(this->values_of_landscapes_[i].begin(), this->values_of_landscapes_[i].end());
1190 this->values_of_landscapes_[i].push_back(-landscape_value);
1191 if (this->values_of_landscapes_[i].
size() == number_of_levels - 1) {
1194 std::make_heap(this->values_of_landscapes_[i].begin(), this->values_of_landscapes_[i].end());
1199 this->values_of_landscapes_[i].push_back(landscape_value);
1201 landscape_value += dx;
1203 for (std::size_t i = grid_interval_midpoint; i <= grid_interval_end; ++i) {
1204 if (landscape_value > 0) {
1205 if (number_of_levels != std::numeric_limits<unsigned>::max()) {
1207 if (this->values_of_landscapes_[i].
size() >= number_of_levels) {
1210 if (-landscape_value < this->values_of_landscapes_[i].front()) {
1212 std::pop_heap(this->values_of_landscapes_[i].begin(), this->values_of_landscapes_[i].end());
1213 this->values_of_landscapes_[i][this->values_of_landscapes_[i].size() - 1] = -landscape_value;
1214 std::push_heap(this->values_of_landscapes_[i].begin(), this->values_of_landscapes_[i].end());
1218 this->values_of_landscapes_[i].push_back(-landscape_value);
1219 if (this->values_of_landscapes_[i].
size() == number_of_levels - 1) {
1222 std::make_heap(this->values_of_landscapes_[i].begin(), this->values_of_landscapes_[i].end());
1226 this->values_of_landscapes_[i].push_back(landscape_value);
1230 std::clog <<
"Adding landscape value (going down) for a point : " << i <<
" equal : " << landscape_value
1234 landscape_value -= dx;
1238 if (number_of_levels != std::numeric_limits<unsigned>::max()) {
1242 for (std::size_t pt = 0; pt != this->values_of_landscapes_.size(); ++pt) {
1243 for (std::size_t j = 0; j != this->values_of_landscapes_[pt].size(); ++j) {
1244 this->values_of_landscapes_[pt][j] *= -1;
1250 for (std::size_t pt = 0; pt != this->values_of_landscapes_.size(); ++pt) {
1251 std::sort(this->values_of_landscapes_[pt].begin(), this->values_of_landscapes_[pt].end(), std::greater<double>());
1262 std::cerr <<
"The file : " << filename <<
" do not exist. The program will now terminate \n";
1264 throw std::invalid_argument(
"The persistence landscape file do not exist.");
1267 std::size_t number_of_points_in_the_grid = 0;
1268 in >> this->grid_min_ >> this->grid_max_ >> number_of_points_in_the_grid;
1270 std::vector<std::vector<double> > v(number_of_points_in_the_grid);
1272 std::getline(in, line);
1274 for (std::size_t i = 0; i != number_of_points_in_the_grid; ++i) {
1276 std::getline(in, line);
1277 std::istringstream stream(line);
1278 while (stream >> number) {
1279 v[i].push_back(number);
1282 this->values_of_landscapes_ = v;
1292 out << grid_min_ << std::endl << grid_max_ << std::endl << this->values_of_landscapes_.size() << std::endl;
1295 for (std::size_t i = 0; i != this->values_of_landscapes_.size(); ++i) {
1296 for (std::size_t j = 0; j != this->values_of_landscapes_[i].size(); ++j) {
1297 out << this->values_of_landscapes_[i][j] <<
" ";
1311 std::size_t to_)
const
1316 std::ostringstream gnuplot_script;
1317 gnuplot_script << filename <<
"_GnuplotScript";
1318 out.open(gnuplot_script.str().c_str());
1320 if (min_x == max_x) {
1322 out <<
"set xrange [" << this->grid_min_ <<
" : " << this->grid_max_ <<
"]" << std::endl;
1323 out <<
"set yrange [" << min_max.first <<
" : " << min_max.second <<
"]" << std::endl;
1325 out <<
"set xrange [" << min_x <<
" : " << max_x <<
"]" << std::endl;
1326 out <<
"set yrange [" << min_y <<
" : " << max_y <<
"]" << std::endl;
1330 double dx = (this->grid_max_ - this->grid_min_) /
static_cast<double>(this->values_of_landscapes_.size() - 1);
1332 std::size_t from = 0;
1333 if (from_ != std::numeric_limits<std::size_t>::max()) {
1341 if (to_ != std::numeric_limits<std::size_t>::max()) {
1348 for (std::size_t lambda = from; lambda != to; ++lambda) {
1349 out <<
" '-' using 1:2 notitle with lp";
1350 if (lambda + 1 != to) {
1356 for (std::size_t lambda = from; lambda != to; ++lambda) {
1357 double point = this->grid_min_;
1358 for (std::size_t i = 0; i != this->values_of_landscapes_.size(); ++i) {
1360 if (this->values_of_landscapes_[i].
size() > lambda) {
1361 value = this->values_of_landscapes_[i][lambda];
1363 out << point <<
" " << value << std::endl;
1366 out <<
"EOF" << std::endl;
1369 std::clog <<
"To visualize, install gnuplot and type the command: gnuplot -persist -e \"load \'"
1370 << gnuplot_script.str().c_str() <<
"\'\"" << std::endl;
1378 result.values_of_landscapes_ = std::vector<std::vector<double> >(this->values_of_landscapes_.size());
1379 result.grid_min_ = this->grid_min_;
1380 result.grid_max_ = this->grid_max_;
1382 for (std::size_t grid_point = 0; grid_point != this->values_of_landscapes_.size(); ++grid_point) {
1383 result.values_of_landscapes_[grid_point] = std::vector<double>(this->values_of_landscapes_[grid_point].
size());
1384 for (std::size_t i = 0; i != this->values_of_landscapes_[grid_point].size(); ++i) {
1385 result.values_of_landscapes_[grid_point][i] = x * this->values_of_landscapes_[grid_point][i];
A class implementing persistence landscapes by approximating them on a collection of grid points.
Definition Persistence_landscape_on_grid.h:63
std::size_t size() const
Definition Persistence_landscape_on_grid.h:738
void print_to_file(const char *filename) const
Definition Persistence_landscape_on_grid.h:1286
bool operator!=(const Persistence_landscape_on_grid &rhs) const
Definition Persistence_landscape_on_grid.h:595
friend Persistence_landscape_on_grid operator-(const Persistence_landscape_on_grid &first, const Persistence_landscape_on_grid &second)
Definition Persistence_landscape_on_grid.h:495
Persistence_landscape_on_grid(const char *filename, double grid_min, double grid_max, std::size_t number_of_points, std::uint16_t dimension=std::numeric_limits< std::uint16_t >::max())
Definition Persistence_landscape_on_grid.h:130
double compute_integral_of_landscape() const
Definition Persistence_landscape_on_grid.h:213
Persistence_landscape_on_grid(const std::vector< std::pair< double, double > > &p, double grid_min, double grid_max, std::size_t number_of_points)
Definition Persistence_landscape_on_grid.h:78
std::vector< std::vector< double > > output_for_visualization() const
Definition Persistence_landscape_on_grid.h:1060
void plot(const char *filename, std::size_t from, std::size_t to) const
Definition Persistence_landscape_on_grid.h:1070
void load_landscape_from_file(const char *filename)
Definition Persistence_landscape_on_grid.h:1255
std::size_t number_of_vectorize_functions() const
Definition Persistence_landscape_on_grid.h:968
Persistence_landscape_on_grid operator-=(const Persistence_landscape_on_grid &rhs)
Definition Persistence_landscape_on_grid.h:529
Persistence_landscape_on_grid(const char *filename, std::size_t number_of_points, unsigned number_of_levels_of_landscape, std::uint16_t dimension=std::numeric_limits< std::uint16_t >::max())
Definition Persistence_landscape_on_grid.h:152
std::vector< double > vectorize(int number_of_function) const
Definition Persistence_landscape_on_grid.h:947
std::pair< double, double > get_x_range(std::size_t level=0) const
Definition Persistence_landscape_on_grid.h:642
std::pair< double, double > compute_minimum_maximum() const
Definition Persistence_landscape_on_grid.h:618
std::size_t number_of_projections_to_R() const
Definition Persistence_landscape_on_grid.h:941
std::size_t number_of_nonzero_levels() const
Definition Persistence_landscape_on_grid.h:656
Persistence_landscape_on_grid(const char *filename, std::size_t number_of_points, std::uint16_t dimension=std::numeric_limits< std::uint16_t >::max())
Definition Persistence_landscape_on_grid.h:180
friend Persistence_landscape_on_grid operation_on_pair_of_landscapes_on_grid(const Persistence_landscape_on_grid &land1, const Persistence_landscape_on_grid &land2)
Definition Persistence_landscape_on_grid.h:427
double compute_integral_of_landscape(double p, std::size_t level) const
Definition Persistence_landscape_on_grid.h:280
Persistence_landscape_on_grid operator+=(const Persistence_landscape_on_grid &rhs)
Definition Persistence_landscape_on_grid.h:520
double compute_scalar_product(const Persistence_landscape_on_grid &second)
Definition Persistence_landscape_on_grid.h:1050
double operator()(unsigned int level, double x) const
Definition Persistence_landscape_on_grid.h:683
Persistence_landscape_on_grid operator/=(double x)
Definition Persistence_landscape_on_grid.h:548
friend Persistence_landscape_on_grid operator*(double con, const Persistence_landscape_on_grid &first)
Definition Persistence_landscape_on_grid.h:512
double compute_integral_of_landscape(std::size_t level) const
Definition Persistence_landscape_on_grid.h:226
friend Persistence_landscape_on_grid operator+(const Persistence_landscape_on_grid &first, const Persistence_landscape_on_grid &second)
Definition Persistence_landscape_on_grid.h:486
Persistence_landscape_on_grid(const char *filename, double grid_min, double grid_max, std::size_t number_of_points, unsigned number_of_levels_of_landscape, std::uint16_t dimension=std::numeric_limits< std::uint16_t >::max())
Definition Persistence_landscape_on_grid.h:107
friend double compute_max_norm_distance_of_landscapes(const Persistence_landscape_on_grid &first, const Persistence_landscape_on_grid &second)
Definition Persistence_landscape_on_grid.h:688
double find_max(unsigned lambda) const
Definition Persistence_landscape_on_grid.h:743
double distance(const Persistence_landscape_on_grid &second, double power=1) const
Definition Persistence_landscape_on_grid.h:1036
friend double compute_inner_product(const Persistence_landscape_on_grid &l1, const Persistence_landscape_on_grid &l2)
Definition Persistence_landscape_on_grid.h:757
friend Persistence_landscape_on_grid operator*(const Persistence_landscape_on_grid &first, double con)
Definition Persistence_landscape_on_grid.h:504
friend Persistence_landscape_on_grid subtract_two_landscapes(const Persistence_landscape_on_grid &land1, const Persistence_landscape_on_grid &land2)
Definition Persistence_landscape_on_grid.h:477
double compute_maximum() const
Definition Persistence_landscape_on_grid.h:600
void compute_average(const std::vector< Persistence_landscape_on_grid * > &to_average)
Definition Persistence_landscape_on_grid.h:974
double compute_value_at_a_given_point(unsigned level, double x) const
Definition Persistence_landscape_on_grid.h:364
Persistence_landscape_on_grid(const std::vector< std::pair< double, double > > &p, double grid_min, double grid_max, std::size_t number_of_points, unsigned number_of_levels_of_landscape)
Definition Persistence_landscape_on_grid.h:90
bool operator==(const Persistence_landscape_on_grid &rhs) const
Definition Persistence_landscape_on_grid.h:558
double project_to_R(int number_of_function) const
Definition Persistence_landscape_on_grid.h:932
double compute_integral_of_landscape(double p) const
Definition Persistence_landscape_on_grid.h:266
void abs()
Definition Persistence_landscape_on_grid.h:726
friend std::ostream & operator<<(std::ostream &out, const Persistence_landscape_on_grid &land)
Definition Persistence_landscape_on_grid.h:344
double compute_norm_of_landscape(double i) const
Definition Persistence_landscape_on_grid.h:668
std::pair< double, double > get_y_range(std::size_t level=0) const
Definition Persistence_landscape_on_grid.h:651
friend double compute_inner_product(const Persistence_landscape_on_grid &l1, const Persistence_landscape_on_grid &l2, std::size_t level)
Definition Persistence_landscape_on_grid.h:773
friend double compute_distance_of_landscapes_on_grid(const Persistence_landscape_on_grid &first, const Persistence_landscape_on_grid &second, double p)
Computations of distance between two landscapes on a grid. p is the parameter of the procedure.
Definition Persistence_landscape_on_grid.h:872
Persistence_landscape_on_grid operator*=(double x)
Definition Persistence_landscape_on_grid.h:539
Persistence_landscape_on_grid()
Definition Persistence_landscape_on_grid.h:68
friend Persistence_landscape_on_grid add_two_landscapes(const Persistence_landscape_on_grid &land1, const Persistence_landscape_on_grid &land2)
Definition Persistence_landscape_on_grid.h:468
This file contain an implementation of some common procedures used in the Persistence_representations...
std::pair< double, double > compute_parameters_of_a_line(const std::pair< double, double > &p1, const std::pair< double, double > &p2)
Definition common_persistence_representations.h:121
bool almost_equal(double a, double b)
Definition common_persistence_representations.h:65
std::vector< std::pair< double, double > > read_persistence_intervals_in_one_dimension_from_file(std::string const &filename, int dimension=-1, double what_to_substitute_for_infinite_bar=-1)
Definition read_persistence_from_file.h:44
Gudhi namespace.
Definition SimplicialComplexForAlpha.h:14