common_persistence_representations.h
1/* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2 * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3 * Author(s): Pawel Dlotko
4 *
5 * Copyright (C) 2016 Inria
6 *
7 * Modification(s):
8 * - YYYY/MM Author: Description of the modification
9 */
10
11#ifndef COMMON_PERSISTENCE_REPRESENTATIONS_H_
12#define COMMON_PERSISTENCE_REPRESENTATIONS_H_
13
14#include <utility>
15#include <string>
16#include <cmath>
17#include <boost/math/constants/constants.hpp>
18
19
20
21namespace Gudhi {
22namespace Persistence_representations {
23// this file contain an implementation of some common procedures used in Persistence_representations.
24
25static constexpr double pi = boost::math::constants::pi<double>();
26
27
31using Persistence_diagram = std::vector<std::pair<double, double> >;
32
33// double epsi = std::numeric_limits<double>::epsilon();
34double epsi = 0.000005;
35
41inline bool almost_equal(double a, double b) {
42 if (std::fabs(a - b) < epsi) return true;
43 return false;
44}
45
46// landscapes
50double minus_length(std::pair<double, double> a) { return a.first - a.second; }
51double birth_plus_deaths(std::pair<double, double> a) { return a.first + a.second; }
52
53// landscapes
57std::pair<double, double> compute_parameters_of_a_line(std::pair<double, double> p1, std::pair<double, double> p2) {
58 double a = (p2.second - p1.second) / (p2.first - p1.first);
59 double b = p1.second - a * p1.first;
60 return std::make_pair(a, b);
61}
62
63// landscapes
67double find_zero_of_a_line_segment_between_those_two_points(std::pair<double, double> p1,
68 std::pair<double, double> p2) {
69 if (p1.first == p2.first) return p1.first;
70 if (p1.second * p2.second > 0) {
71 std::ostringstream errMessage;
72 errMessage << "In function find_zero_of_a_line_segment_between_those_two_points the arguments are: (" << p1.first
73 << "," << p1.second << ") and (" << p2.first << "," << p2.second
74 << "). There is no zero in line between those two points. Program terminated.";
75 std::string errMessageStr = errMessage.str();
76 const char* err = errMessageStr.c_str();
77 throw(err);
78 }
79 // we assume here, that x \in [ p1.first, p2.first ] and p1 and p2 are points between which we will put the line
80 // segment
81 double a = (p2.second - p1.second) / (p2.first - p1.first);
82 double b = p1.second - a * p1.first;
83 return -b / a;
84}
85
86// landscapes
91bool compare_points_sorting(std::pair<double, double> f, std::pair<double, double> s) {
92 if (f.first < s.first) {
93 return true;
94 } else { // f.first >= s.first
95 if (f.first > s.first) {
96 return false;
97 } else { // f.first == s.first
98 if (f.second > s.second) {
99 return true;
100 } else {
101 return false;
102 }
103 }
104 }
105}
106
107// landscapes
112double function_value(std::pair<double, double> p1, std::pair<double, double> p2, double x) {
113 // we assume here, that x \in [ p1.first, p2.first ] and p1 and p2 are points between which we will put the line
114 // segment
115 double a = (p2.second - p1.second) / (p2.first - p1.first);
116 double b = p1.second - a * p1.first;
117 return (a * x + b);
118}
119
120} // namespace Persistence_representations
121} // namespace Gudhi
122
123#endif // COMMON_PERSISTENCE_REPRESENTATIONS_H_