| 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) 2015 Inria |
| 6 |
* |
| 7 |
* Modification(s): |
| 8 |
* - YYYY/MM Author: Description of the modification |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <gudhi/Simplex_tree.h> |
| 12 |
#include <gudhi/Persistent_cohomology.h> |
| 13 |
|
| 14 |
#include <iostream> |
| 15 |
#include <vector> |
| 16 |
#include <cstdint> // for std::uint8_t |
| 17 |
|
| 18 |
/* We could perfectly well use the default Simplex_tree<> (which uses |
| 19 |
* Simplex_tree_options_default), the following simply demonstrates |
| 20 |
* how to save on storage by not storing a filtration value. */ |
| 21 |
|
| 22 |
struct MyOptions : Gudhi::Simplex_tree_options_minimal { |
| 23 |
// Implicitly use 0 as filtration value for all simplices - inherited from Simplex_tree_options_minimal |
| 24 |
// static const bool store_filtration = false; |
| 25 |
// However the persistence algorithm needs this |
| 26 |
static const bool store_key = true; |
| 27 |
// I have few vertices |
| 28 |
typedef short Vertex_handle; |
| 29 |
// Maximum number of simplices to compute persistence is 2^8 - 1 = 255. One is reserved for null_key |
| 30 |
typedef std::uint8_t Simplex_key; |
| 31 |
}; |
| 32 |
|
| 33 |
using ST = Gudhi::Simplex_tree<MyOptions>; |
| 34 |
using Field_Zp = Gudhi::persistent_cohomology::Field_Zp; |
| 35 |
using Persistent_cohomology = Gudhi::persistent_cohomology::Persistent_cohomology<ST, Field_Zp>; |
| 36 |
|
| 37 |
int main() { |
| 38 |
ST st; |
| 39 |
|
| 40 |
/* Complex to build. */ |
| 41 |
/* 1 3 5 */ |
| 42 |
/* o---o---o */ |
| 43 |
/* / \ / */ |
| 44 |
/* o---o o */ |
| 45 |
/* 2 0 4 */ |
| 46 |
|
| 47 |
const short edge01[] = {0, 1}; |
| 48 |
const short edge02[] = {0, 2}; |
| 49 |
const short edge12[] = {1, 2}; |
| 50 |
const short edge03[] = {0, 3}; |
| 51 |
const short edge13[] = {1, 3}; |
| 52 |
const short edge35[] = {3, 5}; |
| 53 |
const short vertex4[] = {4}; |
| 54 |
st.insert_simplex_and_subfaces(edge01); |
| 55 |
st.insert_simplex_and_subfaces(edge02); |
| 56 |
st.insert_simplex_and_subfaces(edge12); |
| 57 |
st.insert_simplex_and_subfaces(edge03); |
| 58 |
st.insert_simplex(edge13); |
| 59 |
st.insert_simplex_and_subfaces(edge35); |
| 60 |
st.insert_simplex(vertex4); |
| 61 |
|
| 62 |
// Class for homology computation |
| 63 |
// By default, since the complex has dimension 1, only 0-dimensional homology would be computed. |
| 64 |
// Here we also want persistent homology to be computed for the maximal dimension in the complex (persistence_dim_max = true) |
| 65 |
Persistent_cohomology pcoh(st, true); |
| 66 |
|
| 67 |
// Initialize the coefficient field Z/2Z for homology |
| 68 |
pcoh.init_coefficients(2); |
| 69 |
|
| 70 |
// Compute the persistence diagram of the complex |
| 71 |
pcoh.compute_persistent_cohomology(); |
| 72 |
|
| 73 |
// Print the result. The format is, on each line: 2 dim 0 inf |
| 74 |
// where 2 represents the field, dim the dimension of the feature. |
| 75 |
// 2 0 0 inf |
| 76 |
// 2 0 0 inf |
| 77 |
// 2 1 0 inf |
| 78 |
// 2 1 0 inf |
| 79 |
// means that in Z/2Z-homology, the Betti numbers are b0=2 and b1=2. |
| 80 |
pcoh.output_diagram(); |
| 81 |
|
| 82 |
// Print the Betti numbers are b0=2 and b1=2. |
| 83 |
std::clog << std::endl; |
| 84 |
std::clog << "The Betti numbers are : "; |
| 85 |
for (int i = 0; i < 3; i++) |
| 86 |
std::clog << "b" << i << " = " << pcoh.betti_number(i) << " ; "; |
| 87 |
std::clog << std::endl; |
| 88 |
} |