Loading...
Searching...
No Matches
common_persistence_representations.h
Go to the documentation of this file.
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 * - 2025/06 Hannah Schreiber: Various small bug fixes (missing `inline`s, `DEBUG_TRACES`s etc.)
9 * - YYYY/MM Author: Description of the modification
10 */
11
17
18#ifndef COMMON_PERSISTENCE_REPRESENTATIONS_H_
19#define COMMON_PERSISTENCE_REPRESENTATIONS_H_
20
21#include <stdexcept> // std::invalid_argument
22#include <utility> // std::pair, std::make_pair
23#include <sstream> // std::ostringstream
24#include <cmath> // std::fabs
25#include <vector>
26
27#include <boost/math/constants/constants.hpp>
28
29namespace Gudhi {
30namespace Persistence_representations {
31
38using Persistence_diagram = std::vector<std::pair<double, double> >;
39
45inline constexpr double pi = boost::math::constants::pi<double>();
46
47// TODO: I don't think this is a good idea? Having modifiable global variables like that.
48// But the description of `almost_equal` shows that it is the intended use.
49// At the same time, it seems to be the only method using it. Can we not add it as an argument instead?
56inline double epsi = 0.000005;
57
65inline bool almost_equal(double a, double b)
66{
67 if (std::fabs(a - b) < epsi) return true;
68 return false;
69}
70
71// landscapes
77inline constexpr double minus_length(const std::pair<double, double>& a) { return a.first - a.second; }
78
84inline constexpr double birth_plus_deaths(const std::pair<double, double>& a) { return a.first + a.second; }
85
86// landscapes
94inline constexpr bool compare_points_sorting(const std::pair<double, double>& f, const std::pair<double, double>& s)
95{
96 if (f.first < s.first) {
97 return true;
98 } else { // f.first >= s.first
99 if (f.first > s.first) {
100 return false;
101 } else { // f.first == s.first
102 if (f.second > s.second) {
103 return true;
104 } else {
105 return false;
106 }
107 }
108 }
109}
110
111// TODO: the following methods are mostly used with `std::make_pair` as argument in `Persistence_landscape(_on_grid)".
112// If called often, it would be more effective to have a (double, double, double, double) version instead.
113
114// landscapes
121inline std::pair<double, double> compute_parameters_of_a_line(const std::pair<double, double>& p1,
122 const std::pair<double, double>& p2)
123{
124 const double a = (p2.second - p1.second) / (p2.first - p1.first);
125 const double b = p1.second - a * p1.first;
126 return std::make_pair(a, b);
127}
128
129// landscapes
134inline double find_zero_of_a_line_segment_between_those_two_points(const std::pair<double, double>& p1,
135 const std::pair<double, double>& p2)
136{
137 if (p1.first == p2.first) return p1.first;
138 if (p1.second * p2.second > 0) {
139 std::ostringstream errMessage;
140 errMessage << "In function find_zero_of_a_line_segment_between_those_two_points the arguments are: (" << p1.first
141 << "," << p1.second << ") and (" << p2.first << "," << p2.second
142 << "). There is no zero in line between those two points. Program terminated.";
143 throw std::invalid_argument(errMessage.str());
144 }
145 // we assume here, that x \in [ p1.first, p2.first ] and p1 and p2 are points between which we will put the line
146 // segment
147 const auto [a, b] = compute_parameters_of_a_line(p1, p2);
148 return -b / a;
149}
150
151// landscapes
158inline double function_value(const std::pair<double, double>& p1, const std::pair<double, double>& p2, double x)
159{
160 // we assume here, that x \in [ p1.first, p2.first ] and p1 and p2 are points between which we will put the line
161 // segment
162 const auto [a, b] = compute_parameters_of_a_line(p1, p2);
163 return (a * x + b);
164}
165
166} // namespace Persistence_representations
167} // namespace Gudhi
168
169#endif // COMMON_PERSISTENCE_REPRESENTATIONS_H_
double find_zero_of_a_line_segment_between_those_two_points(const std::pair< double, double > &p1, const std::pair< double, double > &p2)
Definition common_persistence_representations.h:134
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
constexpr double pi
Definition common_persistence_representations.h:45
std::vector< std::pair< double, double > > Persistence_diagram
Definition common_persistence_representations.h:38
double function_value(const std::pair< double, double > &p1, const std::pair< double, double > &p2, double x)
Definition common_persistence_representations.h:158
bool almost_equal(double a, double b)
Definition common_persistence_representations.h:65
double epsi
Definition common_persistence_representations.h:56
Gudhi namespace.
Definition SimplicialComplexForAlpha.h:14