7 #include <gudhi/Simplex_tree.h>
8 #include <gudhi/Rips_complex.h>
9 #include <gudhi/distance_functions.h>
11 #include "Eigen/SparseCore"
12 #include <unordered_map>
18 using Simplex_tree = Gudhi::Simplex_tree<Gudhi::Simplex_tree_options_default>;
24 std::unordered_map<Simplex_tree::Simplex_key, std::vector<float>>
extra_data;
27 std::unordered_map<Simplex_tree::Simplex_key, std::vector<float>> _extra_data,
28 std::function<
float(Simplex_tree::Simplex_handle, Simplex_tree::Simplex_handle,
sheaf_simplex_tree&)> _restriction){
33 int coface_index(Simplex_tree::Simplex_handle simplex, Simplex_tree::Simplex_handle coface){
35 Simplex_tree::Simplex_vertex_range vertex_range_simplex = this->st.simplex_vertex_range(simplex);
36 Simplex_tree::Simplex_vertex_range vertex_range_coface = this->st.simplex_vertex_range(coface);
39 std::vector<Simplex_tree::Vertex_handle> vec_simplex(vertex_range_simplex.begin(), vertex_range_simplex.end());
40 std::vector<Simplex_tree::Vertex_handle> vec_coface(vertex_range_coface.begin(), vertex_range_coface.end());
43 std::reverse(vec_simplex.begin(), vec_simplex.end());
44 std::reverse(vec_coface.begin(), vec_coface.end());
47 for (
int i = 0; i < (int) vec_simplex.size(); i++){
48 if (vec_simplex[i] != vec_coface[i])
51 return vec_coface.size() - 1;
56 int complex_dim = this->st.dimension();
57 std::vector<size_t> dims = this->st.num_simplices_by_dimension();
61 std::vector<std::vector<std::tuple<int64_t,int64_t,float>>> boundaries_triples;
62 std::vector<std::vector<std::pair<int,double>>> filtrations;
64 std::vector<SparseMatrixFloat> coboundary_matrices;
65 std::vector<std::vector<double>> reindexed_filtrations;
68 std::vector<FilteredBoundaryMatrix<float>> coboundaries;
71 for (
int dim = 0; dim < (int) complex_dim; dim++){
72 filtrations.push_back({});
73 boundaries_triples.push_back({});
75 filtrations.push_back({});
79 for (
auto sh :
st.filtration_simplex_range()){
80 int dim =
st.dimension(sh);
81 filtrations[dim].push_back(std::make_pair<int, float>(
st.key(sh),
st.filtration(sh)));
82 if (dim == complex_dim){
87 for (Simplex_tree::Simplex_handle c :
st.cofaces_simplex_range(sh, 1)){
89 float sign = (float) std::pow(-1.0, (
double) (this->
coface_index(sh, c) % 2));
95 boundaries_triples[dim].push_back(std::tuple<int64_t,int64_t,float>(
st.key(c),
st.key(sh),coeff));
98 reindex_boundaries_map(boundaries_triples,coboundary_matrices,filtrations, reindexed_filtrations);
101 for (
int dim = 0; dim < (int) complex_dim; dim++){
103 std::vector<double> filt_domain = reindexed_filtrations[dim];
104 std::vector<double> filt_range = reindexed_filtrations[dim+1];
106 coboundaries.push_back(fbm);
111 void reindex_boundaries_map(std::vector<std::vector<std::tuple<int64_t,int64_t,float>>> &boundaries_triples,std::vector<SparseMatrixFloat> &reindexed_boundaries,
112 std::vector<std::vector<std::pair<int,double>>> &filtrations, std::vector<std::vector<double>> &reindexed_filtrations){
113 std::vector<std::set<int64_t>> indices_of_actual_simplices_set(boundaries_triples.size()+1);
114 std::vector<std::vector<int>> indices_of_actual_simplices_vector(boundaries_triples.size()+1);
116 std::vector<std::unordered_map<int64_t,int>> index_maps(filtrations.size());
118 for (
int i = 0; i < (int) filtrations.size(); i++){
120 std::vector<std::pair<int,double>> filtration_pairs_dim = filtrations[i];
123 std::sort(filtration_pairs_dim.begin(), filtration_pairs_dim.end(), [](
auto &left,
auto &right){
124 return (left.first < right.first) || (left.first == right.first && left.second < right.second);
128 std::unordered_map<int64_t, int> temp_map(filtration_pairs_dim.size());
129 std::vector<double> filtrations_dim;
130 for (
int j = 0; j < (int) filtration_pairs_dim.size(); j++){
131 temp_map[filtration_pairs_dim[j].first] = j;
132 filtrations_dim.push_back(filtration_pairs_dim[j].second);
134 reindexed_filtrations.push_back(filtrations_dim);
135 index_maps[i] = temp_map;
138 for (
int i = 0; i < (int) boundaries_triples.size(); i++){
140 std::vector<Eigen::Triplet<float>> tripletList;
141 std::vector<std::tuple<int64_t, int64_t, float>> boundary_triples = boundaries_triples[i];
142 for (
int j = 0; j < (int) boundary_triples.size(); j++){
143 int row = index_maps[dim][std::get<0>(boundary_triples[j])];
144 int col = index_maps[dim-1][std::get<1>(boundary_triples[j])];
145 float coeff = std::get<2>(boundary_triples[j]);
146 boundary_triples[j] = std::make_tuple(row, col, coeff);
147 tripletList.push_back(Eigen::Triplet<float>(row, col, coeff));
152 boundary.setFromTriplets(tripletList.begin(), tripletList.end());
153 reindexed_boundaries.push_back(boundary);
161 std::function<
float(Simplex_tree::Simplex_handle, Simplex_tree::Simplex_handle,
sheaf_simplex_tree&)> restriction){
162 using Rips_complex = Gudhi::rips_complex::Rips_complex<Filtration_value>;
173 Rips_complex rips(points,max_length,Gudhi::Euclidean_distance());
180 std::unordered_map<petls::Simplex_tree::Simplex_key, std::vector<float>> extra_data;
186 rips.create_complex(sst.
st, dim_max);
192 for (Simplex_tree::Vertex_handle v : sst.
st.complex_vertex_range()){
193 Simplex_tree::Simplex_handle as_sh = sst.
st.find({v});
194 sst.
st.assign_key(as_sh, counter);
195 sst.
extra_data[sst.
st.key(as_sh)] = {points[counter][0],
201 for (Simplex_tree::Simplex_handle sh : sst.
st.filtration_simplex_range()){
202 if (sst.
st.dimension(sh) == 0)
204 sst.
st.assign_key(sh, counter++);
Definition: FilteredBoundaryMatrix.hpp:15
Definition: sheaf_simplex_tree.hpp:21
int coface_index(Simplex_tree::Simplex_handle simplex, Simplex_tree::Simplex_handle coface)
Definition: sheaf_simplex_tree.hpp:33
Simplex_tree st
Definition: sheaf_simplex_tree.hpp:23
sheaf_simplex_tree(Simplex_tree _st, std::unordered_map< Simplex_tree::Simplex_key, std::vector< float >> _extra_data, std::function< float(Simplex_tree::Simplex_handle, Simplex_tree::Simplex_handle, sheaf_simplex_tree &)> _restriction)
Definition: sheaf_simplex_tree.hpp:26
std::unordered_map< Simplex_tree::Simplex_key, std::vector< float > > extra_data
Definition: sheaf_simplex_tree.hpp:24
std::function< float(Simplex_tree::Simplex_handle, Simplex_tree::Simplex_handle, sheaf_simplex_tree &)> restriction
Definition: sheaf_simplex_tree.hpp:25
std::vector< FilteredBoundaryMatrix< float > > apply_restriction_function()
Definition: sheaf_simplex_tree.hpp:54
Definition: Complex.cpp:26
Gudhi::Simplex_tree< Gudhi::Simplex_tree_options_default > Simplex_tree
Definition: sheaf_simplex_tree.hpp:18
std::vector< std::vector< Filtration_value > > Distance_matrix
Definition: sheaf_simplex_tree.hpp:20
Simplex_tree::Filtration_value Filtration_value
Definition: sheaf_simplex_tree.hpp:19
petls::sheaf_simplex_tree rips_sheaf_simplex_tree(std::vector< std::vector< float >> points, Filtration_value max_length, std::function< float(Simplex_tree::Simplex_handle, Simplex_tree::Simplex_handle, sheaf_simplex_tree &)> restriction)
Definition: sheaf_simplex_tree.hpp:160
Eigen::SparseMatrix< float, Eigen::ColMajor > SparseMatrixFloat
Definition: typedefs.hpp:21