| 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): Marc Glisse |
| 4 |
* |
| 5 |
* Copyright (C) 2022 Inria |
| 6 |
* |
| 7 |
* Modification(s): |
| 8 |
* - YYYY/MM Author: Description of the modification |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <gudhi/Bitmap_cubical_complex.h> |
| 12 |
#include <gudhi/Simplex_tree.h> |
| 13 |
#include <gudhi/Persistent_cohomology.h> |
| 14 |
#include <gudhi/Persistence_on_a_line.h> |
| 15 |
#include <gudhi/Clock.h> |
| 16 |
|
| 17 |
#include <vector> |
| 18 |
#include <cstdlib> |
| 19 |
#include <random> |
| 20 |
#include <algorithm> |
| 21 |
|
| 22 |
std::random_device rd; |
| 23 |
std::mt19937 gen(rd()); |
| 24 |
|
| 25 |
float get_random_f() |
| 26 |
{ |
| 27 |
// values essentially unique |
| 28 |
std::uniform_real_distribution<float> dist(0., 1.); |
| 29 |
return dist(gen); |
| 30 |
} |
| 31 |
float get_random_i() |
| 32 |
{ |
| 33 |
// lots of repetitions in the values |
| 34 |
std::uniform_int_distribution<int> dist(0, 7); |
| 35 |
return dist(gen); |
| 36 |
} |
| 37 |
|
| 38 |
// $ prog [n_points [n_repeat]] |
| 39 |
int main(int argc, char* argv[]) { |
| 40 |
|
| 41 |
using Cubical = Gudhi::cubical_complex::Bitmap_cubical_complex<Gudhi::cubical_complex::Bitmap_cubical_complex_base<float>>; |
| 42 |
using ST = Gudhi::Simplex_tree<Gudhi::Simplex_tree_options_fast_persistence>; |
| 43 |
using Field_Zp = Gudhi::persistent_cohomology::Field_Zp; |
| 44 |
|
| 45 |
int N = 1000000; |
| 46 |
int repeat = 1; |
| 47 |
if (argc >= 2) N = atoi(argv[1]); |
| 48 |
if (argc >= 3) repeat = atoi(argv[2]); |
| 49 |
|
| 50 |
for (int rep = 0; rep < repeat; ++rep) { |
| 51 |
for (bool b : { false, true }) { |
| 52 |
std::vector<float> data(N); |
| 53 |
if (b) { |
| 54 |
std::clog << "Many repeated values\n"; |
| 55 |
std::generate(data.begin(), data.end(), get_random_i); |
| 56 |
} else { |
| 57 |
std::clog << "Mostly unique values\n"; |
| 58 |
std::generate(data.begin(), data.end(), get_random_f); |
| 59 |
} |
| 60 |
auto convert_result = [](auto& cplx, auto& pers){ |
| 61 |
std::vector<std::pair<float,float>> res; |
| 62 |
for (auto p: pers.get_persistent_pairs()){ |
| 63 |
res.emplace_back(cplx.filtration(std::get<0>(p)), cplx.filtration(std::get<1>(p))); |
| 64 |
} |
| 65 |
return res; |
| 66 |
}; |
| 67 |
|
| 68 |
Gudhi::Clock clock_cub1("Cubical complex from top cells"); |
| 69 |
Cubical cub1({{ (unsigned)data.size() }}, data, true); |
| 70 |
Gudhi::persistent_cohomology::Persistent_cohomology<Cubical, Field_Zp> pcub1(cub1); |
| 71 |
pcub1.init_coefficients(2); |
| 72 |
pcub1.compute_persistent_cohomology(); |
| 73 |
clock_cub1.print(); |
| 74 |
auto res_cub1 = convert_result(cub1, pcub1); |
| 75 |
std::sort(res_cub1.begin(), res_cub1.end()); |
| 76 |
|
| 77 |
Gudhi::Clock clock_cub2("Cubical complex from vertices"); |
| 78 |
Cubical cub2({{ (unsigned)data.size() }}, data, false); |
| 79 |
Gudhi::persistent_cohomology::Persistent_cohomology<Cubical, Field_Zp> pcub2(cub2); |
| 80 |
pcub2.init_coefficients(2); |
| 81 |
pcub2.compute_persistent_cohomology(); |
| 82 |
clock_cub2.print(); |
| 83 |
auto res_cub2 = convert_result(cub2, pcub2); |
| 84 |
std::sort(res_cub2.begin(), res_cub2.end()); |
| 85 |
|
| 86 |
Gudhi::Clock clock_st("Simplex tree"); |
| 87 |
ST st; |
| 88 |
for (int i=0; i < (int)data.size()-1; ++i) { |
| 89 |
st.insert_simplex({i}, data[i]); |
| 90 |
st.insert_simplex({i, i+1}, -std::numeric_limits<float>::infinity()); |
| 91 |
} |
| 92 |
st.insert_simplex({(int)data.size()-1}, data.back()); |
| 93 |
st.make_filtration_non_decreasing(); |
| 94 |
Gudhi::persistent_cohomology::Persistent_cohomology<ST, Field_Zp> pst(st); |
| 95 |
pst.init_coefficients(2); |
| 96 |
pst.compute_persistent_cohomology(); |
| 97 |
clock_st.print(); |
| 98 |
auto res_st = convert_result(st, pst); |
| 99 |
std::sort(res_st.begin(), res_st.end()); |
| 100 |
|
| 101 |
Gudhi::Clock clock_line("Special case for function on a line"); |
| 102 |
std::vector<std::pair<float,float>> res_line; |
| 103 |
Gudhi::persistent_cohomology::compute_persistence_of_function_on_line(data, |
| 104 |
[&res_line](float a, float b){ res_line.emplace_back(a, b); }); |
| 105 |
clock_line.print(); |
| 106 |
std::sort(res_line.begin(), res_line.end()); |
| 107 |
|
| 108 |
if (res_cub2 != res_cub1) std::exit(-1); |
| 109 |
if (res_st != res_cub1) std::exit(-2); |
| 110 |
if (res_line != res_cub1) std::exit(-3); |
| 111 |
} |
| 112 |
} |
| 113 |
} |