24 #ifndef PERSISTENCE_LANDSCAPE_ON_GRID_H_ 25 #define PERSISTENCE_LANDSCAPE_ON_GRID_H_ 28 #include <gudhi/read_persistence_from_file.h> 29 #include <gudhi/common_persistence_representations.h> 45 namespace Persistence_representations {
49 template <
typename operation>
78 this->set_up_numbers_of_functions_for_vectorization_and_projections_to_reals();
79 this->grid_min = this->grid_max = 0;
86 size_t number_of_points_);
93 size_t number_of_points_,
unsigned number_of_levels_of_landscape);
104 unsigned number_of_levels_of_landscape,
105 uint16_t dimension_ = std::numeric_limits<uint16_t>::max());
115 uint16_t dimension_ = std::numeric_limits<uint16_t>::max());
125 uint16_t dimension = std::numeric_limits<uint16_t>::max());
137 uint16_t dimension = std::numeric_limits<uint16_t>::max());
156 for (
size_t i = 0; i != maximal_level; ++i) {
168 double dx = (this->grid_max - this->grid_min) / static_cast<double>(this->values_of_landscapes.size() - 1);
171 std::cerr <<
"this->grid_max : " << this->grid_max << std::endl;
172 std::cerr <<
"this->grid_min : " << this->grid_min << std::endl;
173 std::cerr <<
"this->values_of_landscapes.size() : " << this->values_of_landscapes.size() << std::endl;
177 double previous_x = this->grid_min - dx;
178 double previous_y = 0;
179 for (
size_t i = 0; i != this->values_of_landscapes.size(); ++i) {
180 double current_x = previous_x + dx;
181 double current_y = 0;
182 if (this->values_of_landscapes[i].
size() > level) current_y = this->values_of_landscapes[i][level];
185 std::cerr <<
"this->values_of_landscapes[i].size() : " << this->values_of_landscapes[i].size()
186 <<
" , level : " << level << std::endl;
187 if (this->values_of_landscapes[i].
size() > level)
188 std::cerr <<
"this->values_of_landscapes[i][level] : " << this->values_of_landscapes[i][level] << std::endl;
189 std::cerr <<
"previous_y : " << previous_y << std::endl;
190 std::cerr <<
"current_y : " << current_y << std::endl;
191 std::cerr <<
"dx : " << dx << std::endl;
192 std::cerr <<
"0.5*dx*( previous_y + current_y ); " << 0.5 * dx * (previous_y + current_y) << std::endl;
195 result += 0.5 * dx * (previous_y + current_y);
196 previous_x = current_x;
197 previous_y = current_y;
209 for (
size_t i = 0; i != maximal_level; ++i) {
223 double dx = (this->grid_max - this->grid_min) / static_cast<double>(this->values_of_landscapes.size() - 1);
224 double previous_x = this->grid_min;
225 double previous_y = 0;
226 if (this->values_of_landscapes[0].
size() > level) previous_y = this->values_of_landscapes[0][level];
229 std::cerr <<
"dx : " << dx << std::endl;
230 std::cerr <<
"previous_x : " << previous_x << std::endl;
231 std::cerr <<
"previous_y : " << previous_y << std::endl;
232 std::cerr <<
"power : " << p << std::endl;
236 for (
size_t i = 0; i != this->values_of_landscapes.size(); ++i) {
237 double current_x = previous_x + dx;
238 double current_y = 0;
239 if (this->values_of_landscapes[i].
size() > level) current_y = this->values_of_landscapes[i][level];
241 if (dbg) std::cerr <<
"current_y : " << current_y << std::endl;
243 if (current_y == previous_y)
continue;
245 std::pair<double, double> coef =
246 compute_parameters_of_a_line(std::make_pair(previous_x, previous_y), std::make_pair(current_x, current_y));
247 double a = coef.first;
248 double b = coef.second;
251 std::cerr <<
"A line passing through points : (" << previous_x <<
"," << previous_y <<
") and (" << current_x
252 <<
"," << current_y <<
") is : " << a <<
"x+" << b << std::endl;
257 double value_to_add = 0;
259 value_to_add = 1 / (a * (p + 1)) * (pow((a * current_x + b), p + 1) - pow((a * previous_x + b), p + 1));
261 value_to_add = (current_x - previous_x) * (pow(b, p));
263 result += value_to_add;
265 std::cerr <<
"Increasing result by : " << value_to_add << std::endl;
266 std::cerr <<
"result : " << result << std::endl;
269 previous_x = current_x;
270 previous_y = current_y;
272 if (dbg) std::cerr <<
"The total result is : " << result << std::endl;
282 double dx = (land.grid_max - land.grid_min) / static_cast<double>(land.values_of_landscapes.size() - 1);
283 double x = land.grid_min;
284 for (
size_t i = 0; i != land.values_of_landscapes.size(); ++i) {
286 for (
size_t j = 0; j != land.values_of_landscapes[i].size(); ++j) {
287 out << land.values_of_landscapes[i][j] <<
" ";
295 template <
typename oper>
306 if ((x < this->grid_min) || (x > this->grid_max))
return 0;
309 double dx = (this->grid_max - this->grid_min) / static_cast<double>(this->values_of_landscapes.size() - 1);
310 size_t position = size_t((x - this->grid_min) / dx);
313 std::cerr <<
"This is a procedure compute_value_at_a_given_point \n";
314 std::cerr <<
"level : " << level << std::endl;
315 std::cerr <<
"x : " << x << std::endl;
316 std::cerr <<
"position : " << position << std::endl;
319 if (almost_equal(position * dx + this->grid_min, x)) {
320 if (this->values_of_landscapes[position].
size() < level) {
321 return this->values_of_landscapes[position][level];
327 std::pair<double, double> line;
328 if ((this->values_of_landscapes[position].
size() > level) &&
329 (this->values_of_landscapes[position + 1].
size() > level)) {
330 line = compute_parameters_of_a_line(
331 std::make_pair(position * dx + this->grid_min, this->values_of_landscapes[position][level]),
332 std::make_pair((position + 1) * dx + this->grid_min, this->values_of_landscapes[position + 1][level]));
334 if ((this->values_of_landscapes[position].
size() > level) ||
335 (this->values_of_landscapes[position + 1].
size() > level)) {
336 if ((this->values_of_landscapes[position].
size() > level)) {
337 line = compute_parameters_of_a_line(
338 std::make_pair(position * dx + this->grid_min, this->values_of_landscapes[position][level]),
339 std::make_pair((position + 1) * dx + this->grid_min, 0));
341 line = compute_parameters_of_a_line(
342 std::make_pair(position * dx + this->grid_min, 0),
343 std::make_pair((position + 1) * dx + this->grid_min, this->values_of_landscapes[position + 1][level]));
350 return line.first * x + line.second;
359 return operation_on_pair_of_landscapes_on_grid<std::plus<double> >(land1, land2);
367 return operation_on_pair_of_landscapes_on_grid<std::minus<double> >(land1, land2);
390 return first.multiply_lanscape_by_real_number_not_overwrite(con);
397 return first.multiply_lanscape_by_real_number_not_overwrite(con);
402 if (land1.values_of_landscapes.size() != land2.values_of_landscapes.size())
return false;
403 if (land1.grid_min != land2.grid_min)
return false;
404 if (land1.grid_max != land2.grid_max)
return false;
437 if (x == 0)
throw(
"In operator /=, division by 0. Program terminated.");
438 *
this = *
this * (1 / x);
447 if (this->values_of_landscapes.size() != rhs.values_of_landscapes.size()) {
448 if (dbg) std::cerr <<
"values_of_landscapes of incompatible sizes\n";
451 if (!almost_equal(this->grid_min, rhs.grid_min)) {
452 if (dbg) std::cerr <<
"grid_min not equal\n";
455 if (!almost_equal(this->grid_max, rhs.grid_max)) {
456 if (dbg) std::cerr <<
"grid_max not equal\n";
459 for (
size_t i = 0; i != this->values_of_landscapes.size(); ++i) {
460 for (
size_t aa = 0; aa != this->values_of_landscapes[i].size(); ++aa) {
461 if (!almost_equal(this->values_of_landscapes[i][aa], rhs.values_of_landscapes[i][aa])) {
463 std::cerr <<
"Problem in the position : " << i <<
" of values_of_landscapes. \n";
464 std::cerr << this->values_of_landscapes[i][aa] <<
" " << rhs.values_of_landscapes[i][aa] << std::endl;
484 double max_value = -std::numeric_limits<double>::max();
485 for (
size_t i = 0; i != this->values_of_landscapes.size(); ++i) {
486 if (this->values_of_landscapes[i].
size()) {
487 if (this->values_of_landscapes[i][0] > max_value) max_value = this->values_of_landscapes[i][0];
488 if (this->values_of_landscapes[i][this->values_of_landscapes[i].
size() - 1] > max_value)
489 max_value = this->values_of_landscapes[i][this->values_of_landscapes[i].
size() - 1];
501 double max_value = -std::numeric_limits<double>::max();
502 double min_value = 0;
503 for (
size_t i = 0; i != this->values_of_landscapes.size(); ++i) {
504 if (this->values_of_landscapes[i].
size()) {
505 if (this->values_of_landscapes[i][0] > max_value) max_value = this->values_of_landscapes[i][0];
506 if (this->values_of_landscapes[i][this->values_of_landscapes[i].
size() - 1] > max_value)
507 max_value = this->values_of_landscapes[i][this->values_of_landscapes[i].
size() - 1];
509 if (this->values_of_landscapes[i][0] < min_value) min_value = this->values_of_landscapes[i][0];
510 if (this->values_of_landscapes[i][this->values_of_landscapes[i].
size() - 1] < min_value)
511 min_value = this->values_of_landscapes[i][this->values_of_landscapes[i].
size() - 1];
514 return std::make_pair(min_value, max_value);
522 return std::make_pair(this->grid_min, this->grid_max);
536 for (
size_t i = 0; i != this->values_of_landscapes.size(); ++i) {
537 if (this->values_of_landscapes[i].
size() > result) result = this->values_of_landscapes[i].
size();
546 std::vector<std::pair<double, double> > p;
549 if (i < std::numeric_limits<double>::max()) {
574 for (
size_t i = 0; i != this->values_of_landscapes.size(); ++i) {
575 for (
size_t j = 0; j != this->values_of_landscapes[i].size(); ++j) {
576 this->values_of_landscapes[i][j] = std::abs(this->values_of_landscapes[i][j]);
590 double max_value = -std::numeric_limits<double>::max();
591 for (
size_t i = 0; i != this->values_of_landscapes.size(); ++i) {
592 if (this->values_of_landscapes[i].
size() > lambda) {
593 if (this->values_of_landscapes[i][lambda] > max_value) max_value = this->values_of_landscapes[i][lambda];
604 if (!check_if_defined_on_the_same_domain(l1, l2))
605 throw "Landscapes are not defined on the same grid, the program will now terminate";
608 for (
size_t i = 0; i != maximal_level; ++i) {
621 if (!check_if_defined_on_the_same_domain(l1, l2))
622 throw "Landscapes are not defined on the same grid, the program will now terminate";
625 double dx = (l1.grid_max - l1.grid_min) / static_cast<double>(l1.values_of_landscapes.size() - 1);
627 double previous_x = l1.grid_min - dx;
628 double previous_y_l1 = 0;
629 double previous_y_l2 = 0;
630 for (
size_t i = 0; i != l1.values_of_landscapes.size(); ++i) {
631 if (dbg) std::cerr <<
"i : " << i << std::endl;
633 double current_x = previous_x + dx;
634 double current_y_l1 = 0;
635 if (l1.values_of_landscapes[i].size() > level) current_y_l1 = l1.values_of_landscapes[i][level];
637 double current_y_l2 = 0;
638 if (l2.values_of_landscapes[i].size() > level) current_y_l2 = l2.values_of_landscapes[i][level];
641 std::cerr <<
"previous_x : " << previous_x << std::endl;
642 std::cerr <<
"previous_y_l1 : " << previous_y_l1 << std::endl;
643 std::cerr <<
"current_y_l1 : " << current_y_l1 << std::endl;
644 std::cerr <<
"previous_y_l2 : " << previous_y_l2 << std::endl;
645 std::cerr <<
"current_y_l2 : " << current_y_l2 << std::endl;
648 std::pair<double, double> l1_coords = compute_parameters_of_a_line(std::make_pair(previous_x, previous_y_l1),
649 std::make_pair(current_x, current_y_l1));
650 std::pair<double, double> l2_coords = compute_parameters_of_a_line(std::make_pair(previous_x, previous_y_l2),
651 std::make_pair(current_x, current_y_l2));
655 double a = l1_coords.first;
656 double b = l1_coords.second;
658 double c = l2_coords.first;
659 double d = l2_coords.second;
662 std::cerr <<
"Here are the formulas for a line: \n";
663 std::cerr <<
"a : " << a << std::endl;
664 std::cerr <<
"b : " << b << std::endl;
665 std::cerr <<
"c : " << c << std::endl;
666 std::cerr <<
"d : " << d << std::endl;
673 double added_value = (a * c / 3 * current_x * current_x * current_x +
674 (a * d + b * c) / 2 * current_x * current_x + b * d * current_x) -
675 (a * c / 3 * previous_x * previous_x * previous_x +
676 (a * d + b * c) / 2 * previous_x * previous_x + b * d * previous_x);
679 std::cerr <<
"Value of the integral on the left end i.e. : " << previous_x <<
" is : " 680 << a * c / 3 * previous_x * previous_x * previous_x + (a * d + b * c) / 2 * previous_x * previous_x +
683 std::cerr <<
"Value of the integral on the right end i.e. : " << current_x <<
" is " 684 << a * c / 3 * current_x * current_x * current_x + (a * d + b * c) / 2 * current_x * current_x +
689 result += added_value;
692 std::cerr <<
"added_value : " << added_value << std::endl;
693 std::cerr <<
"result : " << result << std::endl;
697 previous_x = current_x;
698 previous_y_l1 = current_y_l1;
699 previous_y_l2 = current_y_l2;
719 std::cerr <<
"first : " << first << std::endl;
720 std::cerr <<
"second : " << second << std::endl;
728 std::cerr <<
"Difference : " << lan << std::endl;
735 std::cerr <<
"Abs : " << lan << std::endl;
738 if (p < std::numeric_limits<double>::max()) {
743 std::cerr <<
"p : " << p << std::endl;
748 std::cerr <<
"integral : " << result << std::endl;
754 std::cerr <<
"integral, without power : " << result << std::endl;
759 return pow(result, 1.0 / p);
790 std::vector<double>
vectorize(
int number_of_function)
const {
792 if ((number_of_function < 0) || ((
size_t)number_of_function >= this->values_of_landscapes.size())) {
793 throw "Wrong number of function\n";
795 std::vector<double> v(this->values_of_landscapes.size());
796 for (
size_t i = 0; i != this->values_of_landscapes.size(); ++i) {
798 if (this->values_of_landscapes[i].
size() > (size_t)number_of_function) {
799 v[i] = this->values_of_landscapes[i][number_of_function];
819 this->values_of_landscapes.clear();
820 this->grid_min = this->grid_max = 0;
823 if (to_average.size() == 0)
return;
826 for (
size_t i = 0; i != to_average.size(); ++i) {
827 if (!check_if_defined_on_the_same_domain(*(to_average[0]), *(to_average[i])))
828 throw "Two grids are not compatible";
831 this->values_of_landscapes = std::vector<std::vector<double> >((to_average[0])->values_of_landscapes.size());
832 this->grid_min = (to_average[0])->grid_min;
833 this->grid_max = (to_average[0])->grid_max;
836 std::cerr <<
"Computations of average. The data from the current landscape have been cleared. We are ready to do " 837 "the computations. \n";
841 for (
size_t grid_point = 0; grid_point != (to_average[0])->values_of_landscapes.size(); ++grid_point) {
843 size_t maximal_size_of_vector = 0;
844 for (
size_t land_no = 0; land_no != to_average.size(); ++land_no) {
845 if ((to_average[land_no])->values_of_landscapes[grid_point].size() > maximal_size_of_vector)
846 maximal_size_of_vector = (to_average[land_no])->values_of_landscapes[grid_point].size();
848 this->values_of_landscapes[grid_point] = std::vector<double>(maximal_size_of_vector);
851 std::cerr <<
"We are considering the point : " << grid_point
852 <<
" of the grid. In this point, there are at most : " << maximal_size_of_vector
853 <<
" nonzero landscape functions \n";
857 for (
size_t land_no = 0; land_no != to_average.size(); ++land_no) {
859 for (
size_t i = 0; i != (to_average[land_no])->values_of_landscapes[grid_point].
size(); ++i) {
861 this->values_of_landscapes[grid_point][i] += (to_average[land_no])->values_of_landscapes[grid_point][i];
865 for (
size_t i = 0; i != this->values_of_landscapes[grid_point].size(); ++i) {
866 this->values_of_landscapes[grid_point][i] /=
static_cast<double>(to_average.size());
878 if (power < std::numeric_limits<double>::max()) {
910 void plot(
const char* filename,
size_t from_,
size_t to_)
const {
911 this->
plot(filename, std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
912 std::numeric_limits<double>::max(), std::numeric_limits<double>::max(), from_, to_);
919 void plot(
const char* filename,
double min_x = std::numeric_limits<double>::max(),
920 double max_x = std::numeric_limits<double>::max(),
double min_y = std::numeric_limits<double>::max(),
921 double max_y = std::numeric_limits<double>::max(),
size_t from_ = std::numeric_limits<size_t>::max(),
922 size_t to_ = std::numeric_limits<size_t>::max())
const;
927 std::vector<std::vector<double> > values_of_landscapes;
928 size_t number_of_functions_for_vectorization;
929 size_t number_of_functions_for_projections_to_reals;
931 void set_up_numbers_of_functions_for_vectorization_and_projections_to_reals() {
933 this->number_of_functions_for_vectorization = this->values_of_landscapes.size();
934 this->number_of_functions_for_projections_to_reals = this->values_of_landscapes.size();
936 void set_up_values_of_landscapes(
const std::vector<std::pair<double, double> >& p,
double grid_min_,
double grid_max_,
937 size_t number_of_points_,
938 unsigned number_of_levels = std::numeric_limits<unsigned>::max());
942 void Persistence_landscape_on_grid::set_up_values_of_landscapes(
const std::vector<std::pair<double, double> >& p,
943 double grid_min_,
double grid_max_,
944 size_t number_of_points_,
unsigned number_of_levels) {
947 std::cerr <<
"Here is the procedure : set_up_values_of_landscapes. The parameters are : grid_min_ : " << grid_min_
948 <<
", grid_max_ : " << grid_max_ <<
", number_of_points_ : " << number_of_points_
949 <<
", number_of_levels: " << number_of_levels << std::endl;
950 std::cerr <<
"Here are the intervals at our disposal : \n";
951 for (
size_t i = 0; i != p.size(); ++i) {
952 std::cerr << p[i].first <<
" , " << p[i].second << std::endl;
956 if ((grid_min_ == std::numeric_limits<double>::max()) || (grid_max_ == std::numeric_limits<double>::max())) {
958 double min = std::numeric_limits<double>::max();
959 double max = std::numeric_limits<double>::min();
960 for (
size_t i = 0; i != p.size(); ++i) {
961 if (p[i].first < min) min = p[i].first;
962 if (p[i].second > max) max = p[i].second;
964 if (grid_min_ == std::numeric_limits<double>::max()) {
975 this->values_of_landscapes = std::vector<std::vector<double> >(number_of_points_ + 1);
977 this->grid_min = grid_min_;
978 this->grid_max = grid_max_;
980 if (grid_max_ <= grid_min_) {
981 throw "Wrong parameters of grid_min and grid_max given to the procedure. The program will now terminate.\n";
984 double dx = (grid_max_ - grid_min_) / static_cast<double>(number_of_points_);
986 for (
size_t int_no = 0; int_no != p.size(); ++int_no) {
987 size_t grid_interval_begin = (p[int_no].first - grid_min_) / dx;
988 size_t grid_interval_end = (p[int_no].second - grid_min_) / dx;
989 size_t grid_interval_midpoint = (size_t)(0.5 * (grid_interval_begin + grid_interval_end));
992 std::cerr <<
"Considering an interval : " << p[int_no].first <<
"," << p[int_no].second << std::endl;
994 std::cerr <<
"grid_interval_begin : " << grid_interval_begin << std::endl;
995 std::cerr <<
"grid_interval_end : " << grid_interval_end << std::endl;
996 std::cerr <<
"grid_interval_midpoint : " << grid_interval_midpoint << std::endl;
999 double landscape_value = dx;
1000 for (
size_t i = grid_interval_begin + 1; i < grid_interval_midpoint; ++i) {
1002 std::cerr <<
"Adding landscape value (going up) for a point : " << i <<
" equal : " << landscape_value
1005 if (number_of_levels != std::numeric_limits<unsigned>::max()) {
1009 if (this->values_of_landscapes[i].
size() >= number_of_levels) {
1012 if (-landscape_value < this->values_of_landscapes[i].front()) {
1014 std::pop_heap(this->values_of_landscapes[i].begin(), this->values_of_landscapes[i].end());
1015 this->values_of_landscapes[i][this->values_of_landscapes[i].size() - 1] = -landscape_value;
1016 std::push_heap(this->values_of_landscapes[i].begin(), this->values_of_landscapes[i].end());
1020 this->values_of_landscapes[i].push_back(-landscape_value);
1021 if (this->values_of_landscapes[i].
size() == number_of_levels - 1) {
1024 std::make_heap(this->values_of_landscapes[i].begin(), this->values_of_landscapes[i].end());
1029 this->values_of_landscapes[i].push_back(landscape_value);
1031 landscape_value += dx;
1033 for (
size_t i = grid_interval_midpoint; i <= grid_interval_end; ++i) {
1034 if (landscape_value > 0) {
1035 if (number_of_levels != std::numeric_limits<unsigned>::max()) {
1037 if (this->values_of_landscapes[i].
size() >= number_of_levels) {
1040 if (-landscape_value < this->values_of_landscapes[i].front()) {
1042 std::pop_heap(this->values_of_landscapes[i].begin(), this->values_of_landscapes[i].end());
1043 this->values_of_landscapes[i][this->values_of_landscapes[i].size() - 1] = -landscape_value;
1044 std::push_heap(this->values_of_landscapes[i].begin(), this->values_of_landscapes[i].end());
1048 this->values_of_landscapes[i].push_back(-landscape_value);
1049 if (this->values_of_landscapes[i].
size() == number_of_levels - 1) {
1052 std::make_heap(this->values_of_landscapes[i].begin(), this->values_of_landscapes[i].end());
1056 this->values_of_landscapes[i].push_back(landscape_value);
1060 std::cerr <<
"Adding landscape value (going down) for a point : " << i <<
" equal : " << landscape_value
1064 landscape_value -= dx;
1068 if (number_of_levels != std::numeric_limits<unsigned>::max()) {
1072 for (
size_t pt = 0; pt != this->values_of_landscapes.size(); ++pt) {
1073 for (
size_t j = 0; j != this->values_of_landscapes[pt].size(); ++j) {
1074 this->values_of_landscapes[pt][j] *= -1;
1080 for (
size_t pt = 0; pt != this->values_of_landscapes.size(); ++pt) {
1081 std::sort(this->values_of_landscapes[pt].begin(), this->values_of_landscapes[pt].end(), std::greater<double>());
1086 double grid_min_,
double grid_max_,
1087 size_t number_of_points_) {
1088 this->set_up_values_of_landscapes(p, grid_min_, grid_max_, number_of_points_);
1092 double grid_min_,
double grid_max_,
1093 size_t number_of_points_,
1094 unsigned number_of_levels_of_landscape) {
1095 this->set_up_values_of_landscapes(p, grid_min_, grid_max_, number_of_points_, number_of_levels_of_landscape);
1099 size_t number_of_points_, uint16_t dimension) {
1100 std::vector<std::pair<double, double> > p;
1101 if (dimension == std::numeric_limits<uint16_t>::max()) {
1102 p = read_persistence_intervals_in_one_dimension_from_file(filename);
1104 p = read_persistence_intervals_in_one_dimension_from_file(filename, dimension);
1106 this->set_up_values_of_landscapes(p, grid_min_, grid_max_, number_of_points_);
1110 size_t number_of_points_,
1111 unsigned number_of_levels_of_landscape,
1112 uint16_t dimension) {
1113 std::vector<std::pair<double, double> > p;
1114 if (dimension == std::numeric_limits<uint16_t>::max()) {
1115 p = read_persistence_intervals_in_one_dimension_from_file(filename);
1117 p = read_persistence_intervals_in_one_dimension_from_file(filename, dimension);
1119 this->set_up_values_of_landscapes(p, grid_min_, grid_max_, number_of_points_, number_of_levels_of_landscape);
1123 uint16_t dimension) {
1124 std::vector<std::pair<double, double> > p;
1125 if (dimension == std::numeric_limits<uint16_t>::max()) {
1126 p = read_persistence_intervals_in_one_dimension_from_file(filename);
1128 p = read_persistence_intervals_in_one_dimension_from_file(filename, dimension);
1130 double grid_min_ = std::numeric_limits<double>::max();
1131 double grid_max_ = -std::numeric_limits<double>::max();
1132 for (
size_t i = 0; i != p.size(); ++i) {
1133 if (p[i].first < grid_min_) grid_min_ = p[i].first;
1134 if (p[i].second > grid_max_) grid_max_ = p[i].second;
1136 this->set_up_values_of_landscapes(p, grid_min_, grid_max_, number_of_points_);
1140 unsigned number_of_levels_of_landscape,
1141 uint16_t dimension) {
1142 std::vector<std::pair<double, double> > p;
1143 if (dimension == std::numeric_limits<uint16_t>::max()) {
1144 p = read_persistence_intervals_in_one_dimension_from_file(filename);
1146 p = read_persistence_intervals_in_one_dimension_from_file(filename, dimension);
1148 double grid_min_ = std::numeric_limits<double>::max();
1149 double grid_max_ = -std::numeric_limits<double>::max();
1150 for (
size_t i = 0; i != p.size(); ++i) {
1151 if (p[i].first < grid_min_) grid_min_ = p[i].first;
1152 if (p[i].second > grid_max_) grid_max_ = p[i].second;
1154 this->set_up_values_of_landscapes(p, grid_min_, grid_max_, number_of_points_, number_of_levels_of_landscape);
1162 std::cerr <<
"The file : " << filename <<
" do not exist. The program will now terminate \n";
1163 throw "The persistence landscape file do not exist. The program will now terminate \n";
1166 size_t number_of_points_in_the_grid = 0;
1167 in >> this->grid_min >> this->grid_max >> number_of_points_in_the_grid;
1169 std::vector<std::vector<double> > v(number_of_points_in_the_grid);
1171 std::getline(in, line);
1173 for (
size_t i = 0; i != number_of_points_in_the_grid; ++i) {
1175 std::vector<double> vv;
1176 std::getline(in, line);
1177 std::istringstream stream(line);
1178 while (stream >> number) {
1179 vv.push_back(number);
1183 this->values_of_landscapes = v;
1192 out << grid_min << std::endl << grid_max << std::endl << this->values_of_landscapes.size() << std::endl;
1195 for (
size_t i = 0; i != this->values_of_landscapes.size(); ++i) {
1196 for (
size_t j = 0; j != this->values_of_landscapes[i].size(); ++j) {
1197 out << this->values_of_landscapes[i][j] <<
" ";
1206 size_t from_,
size_t to_)
const {
1210 std::ostringstream gnuplot_script;
1211 gnuplot_script << filename <<
"_GnuplotScript";
1212 out.open(gnuplot_script.str().c_str());
1214 if (min_x == max_x) {
1216 out <<
"set xrange [" << this->grid_min <<
" : " << this->grid_max <<
"]" << std::endl;
1217 out <<
"set yrange [" << min_max.first <<
" : " << min_max.second <<
"]" << std::endl;
1219 out <<
"set xrange [" << min_x <<
" : " << max_x <<
"]" << std::endl;
1220 out <<
"set yrange [" << min_y <<
" : " << max_y <<
"]" << std::endl;
1224 double dx = (this->grid_max - this->grid_min) / static_cast<double>(this->values_of_landscapes.size() - 1);
1227 if (from_ != std::numeric_limits<size_t>::max()) {
1228 if (from_ < number_of_nonzero_levels) {
1235 if (to_ != std::numeric_limits<size_t>::max()) {
1236 if (to_ < number_of_nonzero_levels) {
1242 for (
size_t lambda = from; lambda != to; ++lambda) {
1243 out <<
" '-' using 1:2 notitle with lp";
1244 if (lambda + 1 != to) {
1250 for (
size_t lambda = from; lambda != to; ++lambda) {
1251 double point = this->grid_min;
1252 for (
size_t i = 0; i != this->values_of_landscapes.size(); ++i) {
1254 if (this->values_of_landscapes[i].
size() > lambda) {
1255 value = this->values_of_landscapes[i][lambda];
1257 out << point <<
" " << value << std::endl;
1260 out <<
"EOF" << std::endl;
1262 std::cout <<
"To visualize, install gnuplot and type the command: gnuplot -persist -e \"load \'" 1263 << gnuplot_script.str().c_str() <<
"\'\"" << std::endl;
1266 template <
typename T>
1270 if (!check_if_defined_on_the_same_domain(land1, land2))
throw "Two grids are not compatible";
1274 result.values_of_landscapes = std::vector<std::vector<double> >(land1.values_of_landscapes.size());
1275 result.grid_min = land1.grid_min;
1276 result.grid_max = land1.grid_max;
1279 for (
size_t grid_point = 0; grid_point != land1.values_of_landscapes.size(); ++grid_point) {
1280 result.values_of_landscapes[grid_point] = std::vector<double>(
1281 std::max(land1.values_of_landscapes[grid_point].size(), land2.values_of_landscapes[grid_point].size()));
1282 for (
size_t lambda = 0; lambda != std::max(land1.values_of_landscapes[grid_point].size(),
1283 land2.values_of_landscapes[grid_point].size());
1287 if (lambda < land1.values_of_landscapes[grid_point].size())
1288 value1 = land1.values_of_landscapes[grid_point][lambda];
1289 if (lambda < land2.values_of_landscapes[grid_point].size())
1290 value2 = land2.values_of_landscapes[grid_point][lambda];
1291 result.values_of_landscapes[grid_point][lambda] = oper(value1, value2);
1301 result.values_of_landscapes = std::vector<std::vector<double> >(this->values_of_landscapes.size());
1302 result.grid_min = this->grid_min;
1303 result.grid_max = this->grid_max;
1305 for (
size_t grid_point = 0; grid_point != this->values_of_landscapes.size(); ++grid_point) {
1306 result.values_of_landscapes[grid_point] = std::vector<double>(this->values_of_landscapes[grid_point].size());
1307 for (
size_t i = 0; i != this->values_of_landscapes[grid_point].size(); ++i) {
1308 result.values_of_landscapes[grid_point][i] = x * this->values_of_landscapes[grid_point][i];
1320 if (!check_if_defined_on_the_same_domain(first, second))
throw "Two grids are not compatible";
1322 for (
size_t i = 0; i != first.values_of_landscapes.size(); ++i) {
1323 for (
size_t j = 0; j != std::min(first.values_of_landscapes[i].size(), second.values_of_landscapes[i].size());
1325 if (result <
abs(first.values_of_landscapes[i][j] - second.values_of_landscapes[i][j])) {
1326 result =
abs(first.values_of_landscapes[i][j] - second.values_of_landscapes[i][j]);
1329 if (first.values_of_landscapes[i].size() ==
1330 std::min(first.values_of_landscapes[i].size(), second.values_of_landscapes[i].size())) {
1331 for (
size_t j = first.values_of_landscapes[i].size(); j != second.values_of_landscapes[i].size(); ++j) {
1332 if (result < second.values_of_landscapes[i][j]) result = second.values_of_landscapes[i][j];
1335 if (second.values_of_landscapes[i].size() ==
1336 std::min(first.values_of_landscapes[i].size(), second.values_of_landscapes[i].size())) {
1337 for (
size_t j = second.values_of_landscapes[i].size(); j != first.values_of_landscapes[i].size(); ++j) {
1338 if (result < first.values_of_landscapes[i][j]) result = first.values_of_landscapes[i][j];
1348 #endif // PERSISTENCE_LANDSCAPE_ON_GRID_H_ friend double compute_distance_of_landscapes_on_grid(const Persistence_landscape_on_grid &first, const Persistence_landscape_on_grid &second, double p)
Definition: Persistence_landscape_on_grid.h:712
std::pair< double, double > get_x_range(size_t level=0) const
Definition: Persistence_landscape_on_grid.h:521
bool operator!=(const Persistence_landscape_on_grid &rhs) const
Definition: Persistence_landscape_on_grid.h:476
double distance(const Persistence_landscape_on_grid &second, double power=1) const
Definition: Persistence_landscape_on_grid.h:877
friend Persistence_landscape_on_grid operator*(const Persistence_landscape_on_grid &first, double con)
Definition: Persistence_landscape_on_grid.h:389
std::vector< double > vectorize(int number_of_function) const
Definition: Persistence_landscape_on_grid.h:790
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:1315
void print_to_file(const char *filename) const
Definition: Persistence_landscape_on_grid.h:1187
void compute_average(const std::vector< Persistence_landscape_on_grid *> &to_average)
Definition: Persistence_landscape_on_grid.h:815
void plot(const char *filename, size_t from_, size_t to_) const
Definition: Persistence_landscape_on_grid.h:910
friend Persistence_landscape_on_grid operator*(double con, const Persistence_landscape_on_grid &first)
Definition: Persistence_landscape_on_grid.h:396
Definition: SimplicialComplexForAlpha.h:26
double compute_integral_of_landscape() const
Definition: Persistence_landscape_on_grid.h:153
double operator()(unsigned level, double x) const
Definition: Persistence_landscape_on_grid.h:559
void load_landscape_from_file(const char *filename)
Definition: Persistence_landscape_on_grid.h:1157
Persistence_landscape_on_grid operator-=(const Persistence_landscape_on_grid &rhs)
Definition: Persistence_landscape_on_grid.h:419
double compute_integral_of_landscape(double p) const
Definition: Persistence_landscape_on_grid.h:206
double compute_value_at_a_given_point(unsigned level, double x) const
Definition: Persistence_landscape_on_grid.h:304
Persistence_landscape_on_grid()
Definition: Persistence_landscape_on_grid.h:77
void abs()
Definition: Persistence_landscape_on_grid.h:573
size_t number_of_nonzero_levels() const
Definition: Persistence_landscape_on_grid.h:534
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:357
friend Persistence_landscape_on_grid operator-(const Persistence_landscape_on_grid &first, const Persistence_landscape_on_grid &second)
Definition: Persistence_landscape_on_grid.h:381
A class implementing persistence landscapes by approximating them on a collection of grid points...
Definition: Persistence_landscape_on_grid.h:72
Persistence_landscape_on_grid operator+=(const Persistence_landscape_on_grid &rhs)
Definition: Persistence_landscape_on_grid.h:411
friend double compute_inner_product(const Persistence_landscape_on_grid &l1, const Persistence_landscape_on_grid &l2)
Definition: Persistence_landscape_on_grid.h:602
size_t size() const
Definition: Persistence_landscape_on_grid.h:584
std::pair< double, double > compute_minimum_maximum() const
Definition: Persistence_landscape_on_grid.h:498
friend std::ostream & operator<<(std::ostream &out, const Persistence_landscape_on_grid &land)
Definition: Persistence_landscape_on_grid.h:281
friend double compute_inner_product(const Persistence_landscape_on_grid &l1, const Persistence_landscape_on_grid &l2, size_t level)
Definition: Persistence_landscape_on_grid.h:617
double compute_norm_of_landscape(double i) const
Definition: Persistence_landscape_on_grid.h:545
size_t number_of_vectorize_functions() const
Definition: Persistence_landscape_on_grid.h:809
friend Persistence_landscape_on_grid operator+(const Persistence_landscape_on_grid &first, const Persistence_landscape_on_grid &second)
Definition: Persistence_landscape_on_grid.h:373
std::pair< double, double > get_y_range(size_t level=0) const
Definition: Persistence_landscape_on_grid.h:529
std::vector< std::vector< double > > output_for_visualization() const
Definition: Persistence_landscape_on_grid.h:899
double compute_maximum() const
Definition: Persistence_landscape_on_grid.h:481
double compute_scalar_product(const Persistence_landscape_on_grid &second)
Definition: Persistence_landscape_on_grid.h:890
double project_to_R(int number_of_function) const
Definition: Persistence_landscape_on_grid.h:776
Persistence_landscape_on_grid operator/=(double x)
Definition: Persistence_landscape_on_grid.h:436
double compute_integral_of_landscape(double p, size_t level) const
Definition: Persistence_landscape_on_grid.h:219
size_t number_of_projections_to_R() const
Definition: Persistence_landscape_on_grid.h:784
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:365
double compute_integral_of_landscape(size_t level) const
Definition: Persistence_landscape_on_grid.h:165
Persistence_landscape_on_grid operator*=(double x)
Definition: Persistence_landscape_on_grid.h:428
double find_max(unsigned lambda) const
Definition: Persistence_landscape_on_grid.h:589
bool operator==(const Persistence_landscape_on_grid &rhs) const
Definition: Persistence_landscape_on_grid.h:445