| 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) 2023 Inria |
| 6 |
* |
| 7 |
* Modification(s): |
| 8 |
* - YYYY/MM Author: Description of the modification |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <gudhi/Clock.h> |
| 12 |
#include <gudhi/Bitmap_cubical_complex.h> |
| 13 |
#include <gudhi/Persistent_cohomology.h> |
| 14 |
#include <gudhi/Persistence_on_rectangle.h> |
| 15 |
|
| 16 |
#include <vector> |
| 17 |
#include <cstdlib> |
| 18 |
#include <random> |
| 19 |
#include <algorithm> |
| 20 |
#include <functional> |
| 21 |
#include <limits> |
| 22 |
|
| 23 |
// Set to true to test on a simple example with no finite interval. |
| 24 |
const bool monotone = false; |
| 25 |
|
| 26 |
int main() { |
| 27 |
std::vector<unsigned> sizes {1000, 999}; |
| 28 |
std::vector<double> data(sizes[0] * sizes[1]); |
| 29 |
if (monotone) { |
| 30 |
std::iota(data.begin(), data.end(), std::size_t(0)); |
| 31 |
} else { |
| 32 |
std::random_device rd; |
| 33 |
std::mt19937 gen(rd()); |
| 34 |
std::uniform_real_distribution<double> dist(0., 1.); |
| 35 |
std::generate(data.begin(), data.end(), std::bind(dist, gen)); |
| 36 |
} |
| 37 |
|
| 38 |
Gudhi::Clock clock; |
| 39 |
#ifndef ONLY_2D |
| 40 |
Gudhi::Clock clock_old; |
| 41 |
typedef Gudhi::cubical_complex::Bitmap_cubical_complex_base<double> Base; |
| 42 |
typedef Gudhi::cubical_complex::Bitmap_cubical_complex<Base> Cubical; |
| 43 |
Cubical complex_from_top_cells(sizes, data, true); |
| 44 |
std::clog << "Construction from top cells: " << clock; |
| 45 |
|
| 46 |
clock.begin(); |
| 47 |
complex_from_top_cells.initialize_filtration(); |
| 48 |
std::clog << "initialize_filtration: " << clock; |
| 49 |
|
| 50 |
clock.begin(); |
| 51 |
using Field_Zp = Gudhi::persistent_cohomology::Field_Zp; |
| 52 |
Gudhi::persistent_cohomology::Persistent_cohomology<Cubical, Field_Zp> pers(complex_from_top_cells); |
| 53 |
pers.init_coefficients(2); |
| 54 |
pers.compute_persistent_cohomology(); |
| 55 |
std::clog << "Compute persistent homology: " << clock; |
| 56 |
std::vector<std::pair<double, double>> res1; |
| 57 |
for (auto p: pers.get_persistent_pairs()){ |
| 58 |
res1.emplace_back(complex_from_top_cells.filtration(std::get<0>(p)), complex_from_top_cells.filtration(std::get<1>(p))); |
| 59 |
} |
| 60 |
std::clog << "Total old code: " << clock_old << std::endl; |
| 61 |
#endif |
| 62 |
|
| 63 |
clock.begin(); |
| 64 |
std::vector<std::pair<double, double>> res2; res2.reserve(data.size() / 2); |
| 65 |
auto out = [&res2](double b, double d) { if (b < d) res2.emplace_back(b, d); }; |
| 66 |
double global_min = Gudhi::cubical_complex::persistence_on_rectangle_from_top_cells(data.data(), sizes[1], sizes[0], out, out); |
| 67 |
res2.emplace_back(global_min, std::numeric_limits<double>::infinity()); |
| 68 |
std::clog << "Total new code: " << clock << std::endl; |
| 69 |
|
| 70 |
clock.begin(); |
| 71 |
std::vector<std::pair<double, double>> res3; res3.reserve(data.size() / 2); |
| 72 |
auto outi = [&res3, &data](double b, double d) { if (data[b] < data[d]) res3.emplace_back(data[b], data[d]); }; |
| 73 |
std::size_t gm = Gudhi::cubical_complex::persistence_on_rectangle_from_top_cells<true>(data.data(), sizes[1], sizes[0], outi, outi); |
| 74 |
res3.emplace_back(data[gm], std::numeric_limits<double>::infinity()); |
| 75 |
std::clog << "Total new code with index: " << clock << std::endl; |
| 76 |
|
| 77 |
#ifndef ONLY_2D |
| 78 |
std::sort(res1.begin(), res1.end()); |
| 79 |
std::sort(res2.begin(), res2.end()); |
| 80 |
std::sort(res3.begin(), res3.end()); |
| 81 |
if(res1 != res2) { |
| 82 |
std::cerr << "Bug 2!\n"; |
| 83 |
std::exit(-2); |
| 84 |
} |
| 85 |
if(res1 != res3) { |
| 86 |
std::cerr << "Bug 3!\n"; |
| 87 |
std::exit(-3); |
| 88 |
} |
| 89 |
#endif |
| 90 |
|
| 91 |
return 0; |
| 92 |
} |