11 #ifndef PERSISTENCE_LANDSCAPE_H_ 12 #define PERSISTENCE_LANDSCAPE_H_ 15 #include <gudhi/read_persistence_from_file.h> 16 #include <gudhi/common_persistence_representations.h> 31 namespace Persistence_representations {
35 template <
typename operation>
71 size_t number_of_levels = std::numeric_limits<size_t>::max());
79 size_t number_of_levels = std::numeric_limits<size_t>::max());
122 template <
typename operation>
131 return operation_on_pair_of_landscapes<std::plus<double> >(land1, land2);
139 return operation_on_pair_of_landscapes<std::minus<double> >(land1, land2);
160 return first.multiply_lanscape_by_real_number_not_overwrite(con);
167 return first.multiply_lanscape_by_real_number_not_overwrite(con);
199 if (x == 0)
throw(
"In operator /=, division by 0. Program terminated.");
200 *
this = *
this * (1 / x);
219 if (this->land.
size()) {
220 maxValue = -std::numeric_limits<int>::max();
221 for (
size_t i = 0; i != this->land[0].
size(); ++i) {
222 if (this->land[0][i].second > maxValue) maxValue = this->land[0][i].second;
231 double compute_minimum()
const {
233 if (this->land.
size()) {
234 minValue = std::numeric_limits<int>::max();
235 for (
size_t i = 0; i != this->land[0].
size(); ++i) {
236 if (this->land[0][i].second < minValue) minValue = this->land[0][i].second;
245 double compute_norm_of_landscape(
double i) {
247 if (i < std::numeric_limits<double>::max()) {
289 double find_max(
unsigned lambda)
const;
320 std::vector<double>
vectorize(
int number_of_function)
const {
322 std::vector<double> v;
323 if ((
size_t)number_of_function > this->land.
size()) {
326 v.reserve(this->land[number_of_function].
size());
327 for (
size_t i = 0; i != this->land[number_of_function].
size(); ++i) {
328 v.push_back(this->land[number_of_function][i].second);
346 std::cerr <<
"to_average.size() : " << to_average.size() << std::endl;
349 std::vector<Persistence_landscape*> nextLevelMerge(to_average.size());
350 for (
size_t i = 0; i != to_average.size(); ++i) {
351 nextLevelMerge[i] = to_average[i];
353 bool is_this_first_level =
true;
358 while (nextLevelMerge.size() != 1) {
360 std::cerr <<
"nextLevelMerge.size() : " << nextLevelMerge.size() << std::endl;
362 std::vector<Persistence_landscape*> nextNextLevelMerge;
363 nextNextLevelMerge.reserve(to_average.size());
364 for (
size_t i = 0; i < nextLevelMerge.size(); i = i + 2) {
366 std::cerr <<
"i : " << i << std::endl;
369 if (i + 1 != nextLevelMerge.size()) {
370 (*l) = (*nextLevelMerge[i]) + (*nextLevelMerge[i + 1]);
372 (*l) = *nextLevelMerge[i];
374 nextNextLevelMerge.push_back(l);
377 std::cerr <<
"After this iteration \n";
381 if (!is_this_first_level) {
383 for (
size_t i = 0; i != nextLevelMerge.size(); ++i) {
384 delete nextLevelMerge[i];
387 is_this_first_level =
false;
388 nextLevelMerge.swap(nextNextLevelMerge);
390 (*this) = (*nextLevelMerge[0]);
391 (*this) *= 1 /
static_cast<double>(to_average.size());
401 if (power < std::numeric_limits<double>::max()) {
423 std::pair<double, double> result;
424 if (level < this->land.
size()) {
426 double minn = this->compute_minimum();
427 result = std::make_pair(minn, maxx);
429 result = std::make_pair(0, 0);
435 void plot(
const char* filename,
double xRangeBegin = std::numeric_limits<double>::max(),
436 double xRangeEnd = std::numeric_limits<double>::max(),
437 double yRangeBegin = std::numeric_limits<double>::max(),
438 double yRangeEnd = std::numeric_limits<double>::max(),
int from = std::numeric_limits<int>::max(),
439 int to = std::numeric_limits<int>::max());
442 std::vector<std::vector<std::pair<double, double> > > land;
443 size_t number_of_functions_for_vectorization;
444 size_t number_of_functions_for_projections_to_reals;
446 void construct_persistence_landscape_from_barcode(
const std::vector<std::pair<double, double> >& p,
447 size_t number_of_levels = std::numeric_limits<size_t>::max());
449 void multiply_lanscape_by_real_number_overwrite(
double x);
453 void set_up_numbers_of_functions_for_vectorization_and_projections_to_reals() {
455 this->number_of_functions_for_vectorization = this->land.size();
456 this->number_of_functions_for_projections_to_reals = this->land.size();
461 std::vector<std::pair<double, double> > barcode;
462 if (dimension < std::numeric_limits<double>::max()) {
463 barcode = read_persistence_intervals_in_one_dimension_from_file(filename, dimension);
465 barcode = read_persistence_intervals_in_one_dimension_from_file(filename);
467 this->construct_persistence_landscape_from_barcode(barcode, number_of_levels);
468 this->set_up_numbers_of_functions_for_vectorization_and_projections_to_reals();
471 bool operatorEqualDbg =
false;
473 if (this->land.size() != rhs.land.size()) {
474 if (operatorEqualDbg) std::cerr <<
"1\n";
477 for (
size_t level = 0; level != this->land.size(); ++level) {
478 if (this->land[level].
size() != rhs.land[level].size()) {
479 if (operatorEqualDbg) std::cerr <<
"this->land[level].size() : " << this->land[level].size() <<
"\n";
480 if (operatorEqualDbg) std::cerr <<
"rhs.land[level].size() : " << rhs.land[level].size() <<
"\n";
481 if (operatorEqualDbg) std::cerr <<
"2\n";
484 for (
size_t i = 0; i != this->land[level].size(); ++i) {
485 if (!(almost_equal(this->land[level][i].first, rhs.land[level][i].first) &&
486 almost_equal(this->land[level][i].second, rhs.land[level][i].second))) {
487 if (operatorEqualDbg)
488 std::cerr <<
"this->land[level][i] : " << this->land[level][i].first <<
" " << this->land[level][i].second
490 if (operatorEqualDbg)
491 std::cerr <<
"rhs.land[level][i] : " << rhs.land[level][i].first <<
" " << rhs.land[level][i].second <<
"\n";
492 if (operatorEqualDbg) std::cerr <<
"3\n";
501 size_t number_of_levels) {
502 this->construct_persistence_landscape_from_barcode(p, number_of_levels);
503 this->set_up_numbers_of_functions_for_vectorization_and_projections_to_reals();
506 void Persistence_landscape::construct_persistence_landscape_from_barcode(
507 const std::vector<std::pair<double, double> >& p,
size_t number_of_levels) {
510 std::cerr <<
"Persistence_landscape::Persistence_landscape( const std::vector< std::pair< double , double > >& p )" 515 std::vector<std::pair<double, double> > bars;
516 bars.insert(bars.begin(), p.begin(), p.end());
517 std::sort(bars.begin(), bars.end(), compare_points_sorting);
520 std::cerr <<
"Bars : \n";
521 for (
size_t i = 0; i != bars.size(); ++i) {
522 std::cerr << bars[i].first <<
" " << bars[i].second <<
"\n";
527 std::vector<std::pair<double, double> > characteristicPoints(p.size());
528 for (
size_t i = 0; i != bars.size(); ++i) {
529 characteristicPoints[i] =
530 std::make_pair((bars[i].first + bars[i].second) / 2.0, (bars[i].second - bars[i].first) / 2.0);
533 size_t number_of_levels_in_the_landscape = 0;
534 while (!characteristicPoints.empty()) {
536 for (
size_t i = 0; i != characteristicPoints.size(); ++i) {
537 std::cout <<
"(" << characteristicPoints[i].first <<
" " << characteristicPoints[i].second <<
")\n";
542 std::vector<std::pair<double, double> > lambda_n;
543 lambda_n.push_back(std::make_pair(-std::numeric_limits<int>::max(), 0));
544 lambda_n.push_back(std::make_pair(minus_length(characteristicPoints[0]), 0));
545 lambda_n.push_back(characteristicPoints[0]);
548 std::cerr <<
"1 Adding to lambda_n : (" << -std::numeric_limits<int>::max() <<
" " << 0 <<
") , (" 549 << minus_length(characteristicPoints[0]) <<
" " << 0 <<
") , (" << characteristicPoints[0].first <<
" " 550 << characteristicPoints[0].second <<
") \n";
554 std::vector<std::pair<double, double> > newCharacteristicPoints;
555 while (i < characteristicPoints.size()) {
557 if ((minus_length(characteristicPoints[i]) >= minus_length(lambda_n[lambda_n.size() - 1])) &&
558 (birth_plus_deaths(characteristicPoints[i]) > birth_plus_deaths(lambda_n[lambda_n.size() - 1]))) {
559 if (minus_length(characteristicPoints[i]) < birth_plus_deaths(lambda_n[lambda_n.size() - 1])) {
560 std::pair<double, double> point = std::make_pair(
561 (minus_length(characteristicPoints[i]) + birth_plus_deaths(lambda_n[lambda_n.size() - 1])) / 2,
562 (birth_plus_deaths(lambda_n[lambda_n.size() - 1]) - minus_length(characteristicPoints[i])) / 2);
563 lambda_n.push_back(point);
565 std::cerr <<
"2 Adding to lambda_n : (" << point.first <<
" " << point.second <<
")\n";
569 std::cerr <<
"characteristicPoints[i+p] : " << characteristicPoints[i + p].first <<
" " 570 << characteristicPoints[i + p].second <<
"\n";
571 std::cerr <<
"point : " << point.first <<
" " << point.second <<
"\n";
575 while ((i + p < characteristicPoints.size()) &&
576 (almost_equal(minus_length(point), minus_length(characteristicPoints[i + p]))) &&
577 (birth_plus_deaths(point) <= birth_plus_deaths(characteristicPoints[i + p]))) {
578 newCharacteristicPoints.push_back(characteristicPoints[i + p]);
580 std::cerr <<
"3.5 Adding to newCharacteristicPoints : (" << characteristicPoints[i + p].first <<
" " 581 << characteristicPoints[i + p].second <<
")\n";
587 newCharacteristicPoints.push_back(point);
589 std::cerr <<
"4 Adding to newCharacteristicPoints : (" << point.first <<
" " << point.second <<
")\n";
592 while ((i + p < characteristicPoints.size()) &&
593 (minus_length(point) <= minus_length(characteristicPoints[i + p])) &&
594 (birth_plus_deaths(point) >= birth_plus_deaths(characteristicPoints[i + p]))) {
595 newCharacteristicPoints.push_back(characteristicPoints[i + p]);
597 std::cerr <<
"characteristicPoints[i+p] : " << characteristicPoints[i + p].first <<
" " 598 << characteristicPoints[i + p].second <<
"\n";
599 std::cerr <<
"point : " << point.first <<
" " << point.second <<
"\n";
600 std::cerr <<
"characteristicPoints[i+p] birth and death : " << minus_length(characteristicPoints[i + p])
601 <<
" , " << birth_plus_deaths(characteristicPoints[i + p]) <<
"\n";
602 std::cerr <<
"point birth and death : " << minus_length(point) <<
" , " << birth_plus_deaths(point)
605 std::cerr <<
"3 Adding to newCharacteristicPoints : (" << characteristicPoints[i + p].first <<
" " 606 << characteristicPoints[i + p].second <<
")\n";
613 lambda_n.push_back(std::make_pair(birth_plus_deaths(lambda_n[lambda_n.size() - 1]), 0));
614 lambda_n.push_back(std::make_pair(minus_length(characteristicPoints[i]), 0));
616 std::cerr <<
"5 Adding to lambda_n : (" << birth_plus_deaths(lambda_n[lambda_n.size() - 1]) <<
" " << 0
618 std::cerr <<
"5 Adding to lambda_n : (" << minus_length(characteristicPoints[i]) <<
" " << 0 <<
")\n";
621 lambda_n.push_back(characteristicPoints[i]);
623 std::cerr <<
"6 Adding to lambda_n : (" << characteristicPoints[i].first <<
" " 624 << characteristicPoints[i].second <<
")\n";
627 newCharacteristicPoints.push_back(characteristicPoints[i]);
629 std::cerr <<
"7 Adding to newCharacteristicPoints : (" << characteristicPoints[i].first <<
" " 630 << characteristicPoints[i].second <<
")\n";
635 lambda_n.push_back(std::make_pair(birth_plus_deaths(lambda_n[lambda_n.size() - 1]), 0));
636 lambda_n.push_back(std::make_pair(std::numeric_limits<int>::max(), 0));
638 characteristicPoints = newCharacteristicPoints;
640 lambda_n.erase(std::unique(lambda_n.begin(), lambda_n.end()), lambda_n.end());
641 this->land.push_back(lambda_n);
643 ++number_of_levels_in_the_landscape;
644 if (number_of_levels == number_of_levels_in_the_landscape) {
652 if (this->land.size() < lambda)
return 0;
653 double maximum = -std::numeric_limits<int>::max();
654 for (
size_t i = 0; i != this->land[lambda].size(); ++i) {
655 if (this->land[lambda][i].second > maximum) maximum = this->land[lambda][i].second;
662 for (
size_t i = 0; i != this->land.size(); ++i) {
663 for (
size_t nr = 2; nr != this->land[i].size() - 1; ++nr) {
665 result += 0.5 * (this->land[i][nr].first - this->land[i][nr - 1].first) *
666 (this->land[i][nr].second + this->land[i][nr - 1].second);
674 if (level >= this->land.size()) {
679 if (level < 0)
return 0;
681 for (
size_t nr = 2; nr != this->land[level].size() - 1; ++nr) {
683 result += 0.5 * (this->land[level][nr].first - this->land[level][nr - 1].first) *
684 (this->land[level][nr].second + this->land[level][nr - 1].second);
693 for (
size_t i = 0; i != this->land.size(); ++i) {
694 for (
size_t nr = 2; nr != this->land[i].size() - 1; ++nr) {
695 if (dbg) std::cout <<
"nr : " << nr <<
"\n";
698 std::pair<double, double> coef = compute_parameters_of_a_line(this->land[i][nr], this->land[i][nr - 1]);
699 double a = coef.first;
700 double b = coef.second;
703 std::cout <<
"(" << this->land[i][nr].first <<
"," << this->land[i][nr].second <<
") , " 704 << this->land[i][nr - 1].first <<
"," << this->land[i][nr].second <<
")" << std::endl;
705 if (this->land[i][nr].first == this->land[i][nr - 1].first)
continue;
707 result += 1 / (a * (p + 1)) *
708 (pow((a * this->land[i][nr].first + b), p + 1) - pow((a * this->land[i][nr - 1].first + b), p + 1));
710 result += (this->land[i][nr].first - this->land[i][nr - 1].first) * (pow(this->land[i][nr].second, p));
713 std::cout <<
"a : " << a <<
" , b : " << b << std::endl;
714 std::cout <<
"result : " << result << std::endl;
723 bool compute_value_at_a_given_pointDbg =
false;
725 if (level >= this->land.size())
return 0;
729 unsigned coordBegin = 1;
730 unsigned coordEnd = this->land[level].size() - 2;
732 if (compute_value_at_a_given_pointDbg) {
733 std::cerr <<
"Here \n";
734 std::cerr <<
"x : " << x <<
"\n";
735 std::cerr <<
"this->land[level][coordBegin].first : " << this->land[level][coordBegin].first <<
"\n";
736 std::cerr <<
"this->land[level][coordEnd].first : " << this->land[level][coordEnd].first <<
"\n";
740 if (x <= this->land[level][coordBegin].first)
return 0;
741 if (x >= this->land[level][coordEnd].first)
return 0;
743 if (compute_value_at_a_given_pointDbg) std::cerr <<
"Entering to the while loop \n";
745 while (coordBegin + 1 != coordEnd) {
746 if (compute_value_at_a_given_pointDbg) {
747 std::cerr <<
"coordBegin : " << coordBegin <<
"\n";
748 std::cerr <<
"coordEnd : " << coordEnd <<
"\n";
749 std::cerr <<
"this->land[level][coordBegin].first : " << this->land[level][coordBegin].first <<
"\n";
750 std::cerr <<
"this->land[level][coordEnd].first : " << this->land[level][coordEnd].first <<
"\n";
753 unsigned newCord = (unsigned)floor((coordEnd + coordBegin) / 2.0);
755 if (compute_value_at_a_given_pointDbg) {
756 std::cerr <<
"newCord : " << newCord <<
"\n";
757 std::cerr <<
"this->land[level][newCord].first : " << this->land[level][newCord].first <<
"\n";
761 if (this->land[level][newCord].first <= x) {
762 coordBegin = newCord;
763 if (this->land[level][newCord].first == x)
return this->land[level][newCord].second;
769 if (compute_value_at_a_given_pointDbg) {
770 std::cout <<
"x : " << x <<
" is between : " << this->land[level][coordBegin].first <<
" a " 771 << this->land[level][coordEnd].first <<
"\n";
772 std::cout <<
"the y coords are : " << this->land[level][coordBegin].second <<
" a " 773 << this->land[level][coordEnd].second <<
"\n";
774 std::cerr <<
"coordBegin : " << coordBegin <<
"\n";
775 std::cerr <<
"coordEnd : " << coordEnd <<
"\n";
778 return function_value(this->land[level][coordBegin], this->land[level][coordEnd], x);
782 for (
size_t level = 0; level != land.land.size(); ++level) {
783 out <<
"Lambda_" << level <<
":" << std::endl;
784 for (
size_t i = 0; i != land.land[level].size(); ++i) {
785 if (land.land[level][i].first == -std::numeric_limits<int>::max()) {
788 if (land.land[level][i].first == std::numeric_limits<int>::max()) {
791 out << land.land[level][i].first;
794 out <<
" , " << land.land[level][i].second << std::endl;
800 void Persistence_landscape::multiply_lanscape_by_real_number_overwrite(
double x) {
801 for (
size_t dim = 0; dim != this->land.size(); ++dim) {
802 for (
size_t i = 0; i != this->land[dim].size(); ++i) {
803 this->land[dim][i].second *= x;
811 for (
size_t level = 0; level != this->land.size(); ++level) {
813 std::cout <<
"level: " << level << std::endl;
815 std::vector<std::pair<double, double> > lambda_n;
816 lambda_n.push_back(std::make_pair(-std::numeric_limits<int>::max(), 0));
817 for (
size_t i = 1; i != this->land[level].size(); ++i) {
819 std::cout <<
"this->land[" << level <<
"][" << i <<
"] : " << this->land[level][i].first <<
" " 820 << this->land[level][i].second << std::endl;
824 if ((this->land[level][i - 1].second) * (this->land[level][i].second) < 0) {
826 find_zero_of_a_line_segment_between_those_two_points(this->land[level][i - 1], this->land[level][i]);
828 lambda_n.push_back(std::make_pair(zero, 0));
829 lambda_n.push_back(std::make_pair(this->land[level][i].first, fabs(this->land[level][i].second)));
831 std::cout <<
"Adding pair : (" << zero <<
",0)" << std::endl;
832 std::cout <<
"In the same step adding pair : (" << this->land[level][i].first <<
"," 833 << fabs(this->land[level][i].second) <<
") " << std::endl;
837 lambda_n.push_back(std::make_pair(this->land[level][i].first, fabs(this->land[level][i].second)));
839 std::cout <<
"Adding pair : (" << this->land[level][i].first <<
"," << fabs(this->land[level][i].second)
840 <<
") " << std::endl;
845 result.land.push_back(lambda_n);
852 for (
size_t level = 0; level != this->land.size(); ++level) {
854 std::cout <<
"level: " << level << std::endl;
856 std::vector<std::pair<double, double> > lambda_n;
857 lambda_n.push_back(std::make_pair(-std::numeric_limits<int>::max(), 0));
858 for (
size_t i = 1; i != this->land[level].size(); ++i) {
860 std::cout <<
"this->land[" << level <<
"][" << i <<
"] : " << this->land[level][i].first <<
" " 861 << this->land[level][i].second << std::endl;
865 if ((this->land[level][i - 1].second) * (this->land[level][i].second) < 0) {
867 find_zero_of_a_line_segment_between_those_two_points(this->land[level][i - 1], this->land[level][i]);
869 lambda_n.push_back(std::make_pair(zero, 0));
870 lambda_n.push_back(std::make_pair(this->land[level][i].first, fabs(this->land[level][i].second)));
872 std::cout <<
"Adding pair : (" << zero <<
",0)" << std::endl;
873 std::cout <<
"In the same step adding pair : (" << this->land[level][i].first <<
"," 874 << fabs(this->land[level][i].second) <<
") " << std::endl;
878 lambda_n.push_back(std::make_pair(this->land[level][i].first, fabs(this->land[level][i].second)));
880 std::cout <<
"Adding pair : (" << this->land[level][i].first <<
"," << fabs(this->land[level][i].second)
881 <<
") " << std::endl;
886 result->land.push_back(lambda_n);
891 Persistence_landscape Persistence_landscape::multiply_lanscape_by_real_number_not_overwrite(
double x)
const {
892 std::vector<std::vector<std::pair<double, double> > > result(this->land.size());
893 for (
size_t dim = 0; dim != this->land.size(); ++dim) {
894 std::vector<std::pair<double, double> > lambda_dim(this->land[dim].
size());
895 for (
size_t i = 0; i != this->land[dim].size(); ++i) {
896 lambda_dim[i] = std::make_pair(this->land[dim][i].first, x * this->land[dim][i].second);
898 result[dim] = lambda_dim;
903 res.land.swap(result);
909 write.open(filename);
910 for (
size_t dim = 0; dim != this->land.size(); ++dim) {
911 write <<
"#lambda_" << dim << std::endl;
912 for (
size_t i = 1; i != this->land[dim].size() - 1; ++i) {
913 write << this->land[dim][i].first <<
" " << this->land[dim][i].second << std::endl;
928 std::cerr <<
"The file : " << filename <<
" do not exist. The program will now terminate \n";
929 throw "The persistence landscape file do not exist. The program will now terminate \n";
933 std::vector<std::pair<double, double> > landscapeAtThisLevel;
935 bool isThisAFirsLine =
true;
938 if (!(line.length() == 0 || line[0] ==
'#')) {
939 std::stringstream lineSS;
944 landscapeAtThisLevel.push_back(std::make_pair(beginn, endd));
946 std::cerr <<
"Reading a point : " << beginn <<
" , " << endd << std::endl;
950 std::cout <<
"IGNORE LINE\n";
953 if (!isThisAFirsLine) {
954 landscapeAtThisLevel.push_back(std::make_pair(std::numeric_limits<int>::max(), 0));
955 this->land.push_back(landscapeAtThisLevel);
956 std::vector<std::pair<double, double> > newLevelOdLandscape;
957 landscapeAtThisLevel.swap(newLevelOdLandscape);
959 landscapeAtThisLevel.push_back(std::make_pair(-std::numeric_limits<int>::max(), 0));
960 isThisAFirsLine =
false;
963 if (landscapeAtThisLevel.size() > 1) {
966 landscapeAtThisLevel.push_back(std::make_pair(std::numeric_limits<int>::max(), 0));
967 this->land.push_back(landscapeAtThisLevel);
973 template <
typename T>
976 bool operation_on_pair_of_landscapesDBG =
false;
977 if (operation_on_pair_of_landscapesDBG) {
978 std::cout <<
"operation_on_pair_of_landscapes\n";
982 std::vector<std::vector<std::pair<double, double> > > land(std::max(land1.land.size(), land2.land.size()));
986 if (operation_on_pair_of_landscapesDBG) {
987 for (
size_t i = 0; i != std::min(land1.land.size(), land2.land.size()); ++i) {
988 std::cerr <<
"land1.land[" << i <<
"].size() : " << land1.land[i].size() << std::endl;
989 std::cerr <<
"land2.land[" << i <<
"].size() : " << land2.land[i].size() << std::endl;
994 for (
size_t i = 0; i != std::min(land1.land.size(), land2.land.size()); ++i) {
995 std::vector<std::pair<double, double> > lambda_n;
998 while ((p + 1 < land1.land[i].size()) && (q + 1 < land2.land[i].size())) {
999 if (operation_on_pair_of_landscapesDBG) {
1000 std::cerr <<
"p : " << p <<
"\n";
1001 std::cerr <<
"q : " << q <<
"\n";
1002 std::cerr <<
"land1.land.size() : " << land1.land.size() << std::endl;
1003 std::cerr <<
"land2.land.size() : " << land2.land.size() << std::endl;
1004 std::cerr <<
"land1.land[" << i <<
"].size() : " << land1.land[i].size() << std::endl;
1005 std::cerr <<
"land2.land[" << i <<
"].size() : " << land2.land[i].size() << std::endl;
1006 std::cout <<
"land1.land[i][p].first : " << land1.land[i][p].first <<
"\n";
1007 std::cout <<
"land2.land[i][q].first : " << land2.land[i][q].first <<
"\n";
1010 if (land1.land[i][p].first < land2.land[i][q].first) {
1011 if (operation_on_pair_of_landscapesDBG) {
1012 std::cout <<
"first \n";
1013 std::cout <<
" function_value(land2.land[i][q-1],land2.land[i][q],land1.land[i][p].first) : " 1014 << function_value(land2.land[i][q - 1], land2.land[i][q], land1.land[i][p].first) <<
"\n";
1017 std::make_pair(land1.land[i][p].first,
1018 oper(static_cast<double>(land1.land[i][p].second),
1019 function_value(land2.land[i][q - 1], land2.land[i][q], land1.land[i][p].first))));
1023 if (land1.land[i][p].first > land2.land[i][q].first) {
1024 if (operation_on_pair_of_landscapesDBG) {
1025 std::cout <<
"Second \n";
1026 std::cout <<
"function_value(" << land1.land[i][p - 1].first <<
" " << land1.land[i][p - 1].second <<
" ," 1027 << land1.land[i][p].first <<
" " << land1.land[i][p].second <<
", " << land2.land[i][q].first
1028 <<
" ) : " << function_value(land1.land[i][p - 1], land1.land[i][p - 1], land2.land[i][q].first)
1030 std::cout <<
"oper( " << function_value(land1.land[i][p], land1.land[i][p - 1], land2.land[i][q].first) <<
"," 1031 << land2.land[i][q].second <<
" : " 1032 << oper(land2.land[i][q].second,
1033 function_value(land1.land[i][p], land1.land[i][p - 1], land2.land[i][q].first))
1036 lambda_n.push_back(std::make_pair(
1037 land2.land[i][q].first, oper(function_value(land1.land[i][p], land1.land[i][p - 1], land2.land[i][q].first),
1038 land2.land[i][q].second)));
1042 if (land1.land[i][p].first == land2.land[i][q].first) {
1043 if (operation_on_pair_of_landscapesDBG) std::cout <<
"Third \n";
1045 std::make_pair(land2.land[i][q].first, oper(land1.land[i][p].second, land2.land[i][q].second)));
1049 if (operation_on_pair_of_landscapesDBG) {
1050 std::cout <<
"Next iteration \n";
1053 while ((p + 1 < land1.land[i].size()) && (q + 1 >= land2.land[i].size())) {
1054 if (operation_on_pair_of_landscapesDBG) {
1055 std::cout <<
"New point : " << land1.land[i][p].first
1056 <<
" oper(land1.land[i][p].second,0) : " << oper(land1.land[i][p].second, 0) << std::endl;
1058 lambda_n.push_back(std::make_pair(land1.land[i][p].first, oper(land1.land[i][p].second, 0)));
1061 while ((p + 1 >= land1.land[i].size()) && (q + 1 < land2.land[i].size())) {
1062 if (operation_on_pair_of_landscapesDBG) {
1063 std::cout <<
"New point : " << land2.land[i][q].first
1064 <<
" oper(0,land2.land[i][q].second) : " << oper(0, land2.land[i][q].second) << std::endl;
1066 lambda_n.push_back(std::make_pair(land2.land[i][q].first, oper(0, land2.land[i][q].second)));
1069 lambda_n.push_back(std::make_pair(std::numeric_limits<int>::max(), 0));
1072 result.land[i].swap(lambda_n);
1074 if (land1.land.size() > std::min(land1.land.size(), land2.land.size())) {
1075 if (operation_on_pair_of_landscapesDBG) {
1076 std::cout <<
"land1.land.size() > std::min( land1.land.size() , land2.land.size() )" << std::endl;
1078 for (
size_t i = std::min(land1.land.size(), land2.land.size()); i != std::max(land1.land.size(), land2.land.size());
1080 std::vector<std::pair<double, double> > lambda_n(land1.land[i]);
1081 for (
size_t nr = 0; nr != land1.land[i].size(); ++nr) {
1082 lambda_n[nr] = std::make_pair(land1.land[i][nr].first, oper(land1.land[i][nr].second, 0));
1086 result.land[i].swap(lambda_n);
1089 if (land2.land.size() > std::min(land1.land.size(), land2.land.size())) {
1090 if (operation_on_pair_of_landscapesDBG) {
1091 std::cout <<
"( land2.land.size() > std::min( land1.land.size() , land2.land.size() ) ) " << std::endl;
1093 for (
size_t i = std::min(land1.land.size(), land2.land.size()); i != std::max(land1.land.size(), land2.land.size());
1095 std::vector<std::pair<double, double> > lambda_n(land2.land[i]);
1096 for (
size_t nr = 0; nr != land2.land[i].size(); ++nr) {
1097 lambda_n[nr] = std::make_pair(land2.land[i][nr].first, oper(0, land2.land[i][nr].second));
1101 result.land[i].swap(lambda_n);
1104 if (operation_on_pair_of_landscapesDBG) {
1105 std::cout <<
"operation_on_pair_of_landscapes END\n";
1113 if (dbg) std::cerr <<
" compute_maximal_distance_non_symmetric \n";
1116 size_t minimalNumberOfLevels = std::min(pl1.land.size(), pl2.land.size());
1117 for (
size_t level = 0; level != minimalNumberOfLevels; ++level) {
1119 std::cerr <<
"Level : " << level << std::endl;
1120 std::cerr <<
"PL1 : \n";
1121 for (
size_t i = 0; i != pl1.land[level].size(); ++i) {
1122 std::cerr <<
"(" << pl1.land[level][i].first <<
"," << pl1.land[level][i].second <<
") \n";
1124 std::cerr <<
"PL2 : \n";
1125 for (
size_t i = 0; i != pl2.land[level].size(); ++i) {
1126 std::cerr <<
"(" << pl2.land[level][i].first <<
"," << pl2.land[level][i].second <<
") \n";
1133 for (
size_t i = 1; i != pl1.land[level].size() - 1; ++i) {
1135 if ((pl1.land[level][i].first >= pl2.land[level][p2Count].first) &&
1136 (pl1.land[level][i].first <= pl2.land[level][p2Count + 1].first))
1141 fabs(function_value(pl2.land[level][p2Count], pl2.land[level][p2Count + 1], pl1.land[level][i].first) -
1142 pl1.land[level][i].second);
1143 if (maxDist <= val) maxDist = val;
1146 std::cerr << pl1.land[level][i].first <<
"in [" << pl2.land[level][p2Count].first <<
"," 1147 << pl2.land[level][p2Count + 1].first <<
"] \n";
1148 std::cerr <<
"pl1[level][i].second : " << pl1.land[level][i].second << std::endl;
1149 std::cerr <<
"function_value( pl2[level][p2Count] , pl2[level][p2Count+1] , pl1[level][i].first ) : " 1150 << function_value(pl2.land[level][p2Count], pl2.land[level][p2Count + 1], pl1.land[level][i].first)
1152 std::cerr <<
"val : " << val << std::endl;
1158 if (dbg) std::cerr <<
"minimalNumberOfLevels : " << minimalNumberOfLevels << std::endl;
1160 if (minimalNumberOfLevels < pl1.land.size()) {
1161 for (
size_t level = minimalNumberOfLevels; level != pl1.land.size(); ++level) {
1162 for (
size_t i = 0; i != pl1.land[level].size(); ++i) {
1163 if (dbg) std::cerr <<
"pl1[level][i].second : " << pl1.land[level][i].second << std::endl;
1164 if (maxDist < pl1.land[level][i].second) maxDist = pl1.land[level][i].second;
1184 std::cerr <<
"Abs of difference ; " << lan << std::endl;
1188 if (p < std::numeric_limits<double>::max()) {
1192 if (dbg) std::cerr <<
"Power != 1, compute integral to the power p\n";
1195 if (dbg) std::cerr <<
"Power = 1, compute integral \n";
1199 return pow(result, 1.0 / p);
1202 if (dbg) std::cerr <<
"Power = infty, compute maximum \n";
1209 return std::max(compute_maximal_distance_non_symmetric(first, second),
1210 compute_maximal_distance_non_symmetric(second, first));
1213 bool comparePairsForMerging(std::pair<double, unsigned> first, std::pair<double, unsigned> second) {
1214 return (first.first < second.first);
1221 for (
size_t level = 0; level != std::min(l1.
size(), l2.
size()); ++level) {
1223 std::cerr <<
"Computing inner product for a level : " << level << std::endl;
1226 auto&& l1_land_level = l1.land[level];
1227 auto&& l2_land_level = l2.land[level];
1229 if (l1_land_level.size() * l2_land_level.size() == 0)
continue;
1232 double x1 = -std::numeric_limits<int>::max();
1234 if (l1_land_level[1].first < l2_land_level[1].first) {
1235 x2 = l1_land_level[1].first;
1237 x2 = l2_land_level[1].first;
1244 while ((l1It < l1_land_level.size() - 1) && (l2It < l2_land_level.size() - 1)) {
1249 if (l1_land_level[l1It + 1].first != l1_land_level[l1It].first) {
1250 a = (l1_land_level[l1It + 1].second - l1_land_level[l1It].second) /
1251 (l1_land_level[l1It + 1].first - l1_land_level[l1It].first);
1255 b = l1_land_level[l1It].second - a * l1_land_level[l1It].first;
1256 if (l2_land_level[l2It + 1].first != l2_land_level[l2It].first) {
1257 c = (l2_land_level[l2It + 1].second - l2_land_level[l2It].second) /
1258 (l2_land_level[l2It + 1].first - l2_land_level[l2It].first);
1262 d = l2_land_level[l2It].second - c * l2_land_level[l2It].first;
1264 double contributionFromThisPart = (a * c * x2 * x2 * x2 / 3 + (a * d + b * c) * x2 * x2 / 2 + b * d * x2) -
1265 (a * c * x1 * x1 * x1 / 3 + (a * d + b * c) * x1 * x1 / 2 + b * d * x1);
1267 result += contributionFromThisPart;
1270 std::cerr <<
"[l1_land_level[l1It].first,l1_land_level[l1It+1].first] : " << l1_land_level[l1It].first
1271 <<
" , " << l1_land_level[l1It + 1].first << std::endl;
1272 std::cerr <<
"[l2_land_level[l2It].first,l2_land_level[l2It+1].first] : " << l2_land_level[l2It].first
1273 <<
" , " << l2_land_level[l2It + 1].first << std::endl;
1274 std::cerr <<
"a : " << a <<
", b : " << b <<
" , c: " << c <<
", d : " << d << std::endl;
1275 std::cerr <<
"x1 : " << x1 <<
" , x2 : " << x2 << std::endl;
1276 std::cerr <<
"contributionFromThisPart : " << contributionFromThisPart << std::endl;
1277 std::cerr <<
"result : " << result << std::endl;
1288 if (x2 == l1_land_level[l1It + 1].first) {
1289 if (x2 == l2_land_level[l2It + 1].first) {
1293 std::cerr <<
"Incrementing both \n";
1297 std::cerr <<
"Incrementing first \n";
1305 std::cerr <<
"Incrementing second \n";
1309 if ( l1It + 1 >= l1_land_level.size() )
break;
1310 if ( l2It + 1 >= l2_land_level.size() )
break;
1314 if (l1_land_level[l1It + 1].first < l2_land_level[l2It + 1].first) {
1315 x2 = l1_land_level[l1It + 1].first;
1317 x2 = l2_land_level[l2It + 1].first;
1324 void Persistence_landscape::plot(
const char* filename,
double xRangeBegin,
double xRangeEnd,
double yRangeBegin,
1325 double yRangeEnd,
int from,
int to) {
1329 std::ostringstream gnuplot_script;
1330 gnuplot_script << filename <<
"_GnuplotScript";
1331 out.open(gnuplot_script.str().c_str());
1333 if ((xRangeBegin != std::numeric_limits<double>::max()) || (xRangeEnd != std::numeric_limits<double>::max()) ||
1334 (yRangeBegin != std::numeric_limits<double>::max()) || (yRangeEnd != std::numeric_limits<double>::max())) {
1335 out <<
"set xrange [" << xRangeBegin <<
" : " << xRangeEnd <<
"]" << std::endl;
1336 out <<
"set yrange [" << yRangeBegin <<
" : " << yRangeEnd <<
"]" << std::endl;
1339 if (from == std::numeric_limits<int>::max()) {
1342 if (to == std::numeric_limits<int>::max()) {
1343 to = this->land.size();
1347 for (
size_t lambda = std::min((
size_t)from, this->land.size()); lambda != std::min((
size_t)to, this->land.size());
1350 out <<
" '-' using 1:2 notitle with lp";
1351 if (lambda + 1 != std::min((
size_t)to, this->land.size())) {
1357 for (
size_t lambda = std::min((
size_t)from, this->land.size()); lambda != std::min((
size_t)to, this->land.size());
1359 for (
size_t i = 1; i != this->land[lambda].size() - 1; ++i) {
1360 out << this->land[lambda][i].first <<
" " << this->land[lambda][i].second << std::endl;
1362 out <<
"EOF" << std::endl;
1364 std::cout <<
"To visualize, install gnuplot and type the command: gnuplot -persist -e \"load \'" 1365 << gnuplot_script.str().c_str() <<
"\'\"" << std::endl;
1371 #endif // PERSISTENCE_LANDSCAPE_H_ std::pair< double, double > get_y_range(size_t level=0) const
Definition: Persistence_landscape.h:422
Persistence_landscape operator+=(const Persistence_landscape &rhs)
Definition: Persistence_landscape.h:173
friend std::ostream & operator<<(std::ostream &out, Persistence_landscape &land)
Definition: Persistence_landscape.h:781
bool operator==(const Persistence_landscape &rhs) const
Definition: Persistence_landscape.h:472
friend Persistence_landscape subtract_two_landscapes(const Persistence_landscape &land1, const Persistence_landscape &land2)
Definition: Persistence_landscape.h:137
size_t size() const
Definition: Persistence_landscape.h:284
double operator()(unsigned level, double x) const
Definition: Persistence_landscape.h:257
Persistence_landscape()
Definition: Persistence_landscape.h:65
void print_to_file(const char *filename) const
Definition: Persistence_landscape.h:907
double compute_integral_of_a_level_of_a_landscape(size_t level) const
Definition: Persistence_landscape.h:672
void load_landscape_from_file(const char *filename)
Definition: Persistence_landscape.h:919
size_t number_of_vectorize_functions() const
Definition: Persistence_landscape.h:336
Persistence_landscape abs()
Definition: Persistence_landscape.h:809
size_t number_of_projections_to_R() const
Definition: Persistence_landscape.h:314
double distance(const Persistence_landscape &second, double power=1) const
Definition: Persistence_landscape.h:400
Definition: SimplicialComplexForAlpha.h:14
A class implementing persistence landscapes data structures.
Definition: Persistence_landscape.h:60
void compute_average(const std::vector< Persistence_landscape *> &to_average)
Definition: Persistence_landscape.h:342
double find_max(unsigned lambda) const
Definition: Persistence_landscape.h:651
friend double compute_distance_of_landscapes(const Persistence_landscape &first, const Persistence_landscape &second, double p)
Definition: Persistence_landscape.h:1171
double compute_scalar_product(const Persistence_landscape &second) const
Definition: Persistence_landscape.h:413
double project_to_R(int number_of_function) const
Definition: Persistence_landscape.h:306
std::vector< double > vectorize(int number_of_function) const
Definition: Persistence_landscape.h:320
bool operator!=(const Persistence_landscape &rhs) const
Definition: Persistence_landscape.h:212
Persistence_landscape operator/=(double x)
Definition: Persistence_landscape.h:198
friend Persistence_landscape operator+(const Persistence_landscape &first, const Persistence_landscape &second)
Definition: Persistence_landscape.h:145
friend Persistence_landscape operator*(double con, const Persistence_landscape &first)
Definition: Persistence_landscape.h:166
double compute_integral_of_landscape() const
Definition: Persistence_landscape.h:660
Persistence_landscape operator*=(double x)
Definition: Persistence_landscape.h:190
friend double compute_inner_product(const Persistence_landscape &l1, const Persistence_landscape &l2)
Definition: Persistence_landscape.h:1217
Persistence_landscape operator-=(const Persistence_landscape &rhs)
Definition: Persistence_landscape.h:181
friend Persistence_landscape add_two_landscapes(const Persistence_landscape &land1, const Persistence_landscape &land2)
Definition: Persistence_landscape.h:129
double compute_value_at_a_given_point(unsigned level, double x) const
Definition: Persistence_landscape.h:722
double compute_maximum() const
Definition: Persistence_landscape.h:217
friend double compute_max_norm_distance_of_landscapes(const Persistence_landscape &first, const Persistence_landscape &second)
Definition: Persistence_landscape.h:1207
friend Persistence_landscape operator*(const Persistence_landscape &first, double con)
Definition: Persistence_landscape.h:159
friend Persistence_landscape operator-(const Persistence_landscape &first, const Persistence_landscape &second)
Definition: Persistence_landscape.h:152