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>
17 using Simplex_tree = Gudhi::Simplex_tree<Gudhi::Simplex_tree_options_default>;
23 std::unordered_map<Simplex_tree::Simplex_key, std::vector<float>>
extra_data;
26 std::unordered_map<Simplex_tree::Simplex_key, std::vector<float>> _extra_data,
27 std::function<
float(Simplex_tree::Simplex_handle, Simplex_tree::Simplex_handle,
sheaf_simplex_tree&)> _restriction){
32 int coface_index(Simplex_tree::Simplex_handle simplex, Simplex_tree::Simplex_handle coface){
34 Simplex_tree::Simplex_vertex_range vertex_range_simplex = this->st.simplex_vertex_range(simplex);
35 Simplex_tree::Simplex_vertex_range vertex_range_coface = this->st.simplex_vertex_range(coface);
38 std::vector<Simplex_tree::Vertex_handle> vec_simplex(vertex_range_simplex.begin(), vertex_range_simplex.end());
39 std::vector<Simplex_tree::Vertex_handle> vec_coface(vertex_range_coface.begin(), vertex_range_coface.end());
46 for (
int i = 0; i < (int) vec_simplex.size(); i++){
47 if (vec_simplex[i] != vec_coface[i])
50 return vec_coface.size() - 1;
55 int complex_dim = this->st.dimension();
56 std::vector<size_t> dims = this->st.num_simplices_by_dimension();
60 std::vector<std::vector<std::tuple<int64_t,int64_t,float>>> boundaries_triples;
61 std::vector<std::vector<std::pair<int,double>>> filtrations;
63 std::vector<SparseMatrixFloat> coboundary_matrices;
64 std::vector<std::vector<double>> reindexed_filtrations;
67 std::vector<FilteredBoundaryMatrix<float>> coboundaries;
70 for (
int dim = 0; dim < (int) complex_dim; dim++){
71 filtrations.push_back({});
72 boundaries_triples.push_back({});
74 filtrations.push_back({});
78 for (
auto sh :
st.filtration_simplex_range()){
79 int dim =
st.dimension(sh);
80 filtrations[dim].push_back(std::make_pair<int, float>(
st.key(sh),
st.filtration(sh)));
81 if (dim == complex_dim){
86 for (Simplex_tree::Simplex_handle c :
st.cofaces_simplex_range(sh, 1)){
88 float sign = (float) std::pow(-1.0, (
double) (this->
coface_index(sh, c) % 2));
94 boundaries_triples[dim].push_back(std::tuple<int64_t,int64_t,float>(
st.key(c),
st.key(sh),coeff));
97 reindex_boundaries_map(boundaries_triples,coboundary_matrices,filtrations, reindexed_filtrations);
100 for (
int dim = 0; dim < (int) complex_dim; dim++){
102 std::vector<double> filt_domain = reindexed_filtrations[dim];
103 std::vector<double> filt_range = reindexed_filtrations[dim+1];
105 coboundaries.push_back(fbm);
110 void reindex_boundaries_map(std::vector<std::vector<std::tuple<int64_t,int64_t,float>>> &boundaries_triples,std::vector<SparseMatrixFloat> &reindexed_boundaries,
111 std::vector<std::vector<std::pair<int,double>>> &filtrations, std::vector<std::vector<double>> &reindexed_filtrations){
112 std::vector<std::set<int64_t>> indices_of_actual_simplices_set(boundaries_triples.size()+1);
113 std::vector<std::vector<int>> indices_of_actual_simplices_vector(boundaries_triples.size()+1);
116 for (
int i = 0; i < (int) boundaries_triples.size(); i++){
119 std::vector<std::tuple<int64_t, int64_t, float>> boundary_triples = boundaries_triples[i];
123 for (
int j = 0; j < (int) boundary_triples.size(); j++){
124 int64_t row = std::get<0>(boundary_triples[j]);
125 int64_t col = std::get<1>(boundary_triples[j]);
127 indices_of_actual_simplices_set[dim-1].insert(col);
128 indices_of_actual_simplices_set[dim].insert(row);
133 std::vector<std::unordered_map<int64_t,int64_t>> index_maps(indices_of_actual_simplices_set.size());
134 for (
int i = 0; i < (int) indices_of_actual_simplices_set.size(); i++){
135 std::vector<int64_t> temp_indices;
136 temp_indices.assign(indices_of_actual_simplices_set[i].begin(), indices_of_actual_simplices_set[i].end());
138 std::unordered_map<int64_t, int64_t> temp_map(temp_indices.size());
139 for (
int j = 0; j < (int) temp_indices.size(); j++){
140 temp_map[temp_indices[j]] = j;
142 index_maps[i] = temp_map;
146 for (
int i = 0; i < (int) boundaries_triples.size(); i++){
148 std::vector<Eigen::Triplet<float>> tripletList;
149 std::vector<std::tuple<int64_t, int64_t, float>> boundary_triples = boundaries_triples[i];
151 for (
int j = 0; j < (int) boundary_triples.size(); j++){
152 int col = index_maps[dim-1][std::get<1>(boundary_triples[j])];
153 int row = index_maps[dim][std::get<0>(boundary_triples[j])];
154 float coeff = std::get<2>(boundary_triples[j]);
155 tripletList.push_back(Eigen::Triplet<float>(row, col, coeff));
160 boundary.setFromTriplets(tripletList.begin(), tripletList.end());
161 reindexed_boundaries.push_back(boundary);
164 for (
int dim = 0; dim < (int) filtrations.size(); dim++){
165 std::vector<double> temp(filtrations[dim].size(),0.0);
167 for (
int i = 0; i < (int) filtrations[dim].size(); i++){
168 temp[index_maps[dim][std::get<0>(filtrations[dim][i])]] = std::get<1>(filtrations[dim][i]);
170 reindexed_filtrations.push_back(temp);
177 std::function<
float(Simplex_tree::Simplex_handle, Simplex_tree::Simplex_handle,
sheaf_simplex_tree&)> restriction){
178 using Rips_complex = Gudhi::rips_complex::Rips_complex<Filtration_value>;
189 Rips_complex rips(points,max_length,Gudhi::Euclidean_distance());
196 std::unordered_map<PersistentLaplacians::Simplex_tree::Simplex_key, std::vector<float>> extra_data;
202 rips.create_complex(sst.
st, dim_max);
208 for (Simplex_tree::Vertex_handle v : sst.
st.complex_vertex_range()){
209 Simplex_tree::Simplex_handle as_sh = sst.
st.find({v});
210 sst.
st.assign_key(as_sh, counter);
211 sst.
extra_data[sst.
st.key(as_sh)] = {points[counter][0],
217 for (Simplex_tree::Simplex_handle sh : sst.
st.filtration_simplex_range()){
218 if (sst.
st.dimension(sh) == 0)
220 sst.
st.assign_key(sh, counter++);
Definition: FilteredBoundaryMatrix.hpp:16
Definition: sheaf_simplex_tree.hpp:20
Simplex_tree st
Definition: sheaf_simplex_tree.hpp:22
std::function< float(Simplex_tree::Simplex_handle, Simplex_tree::Simplex_handle, sheaf_simplex_tree &)> restriction
Definition: sheaf_simplex_tree.hpp:24
std::vector< FilteredBoundaryMatrix< float > > apply_restriction_function()
Definition: sheaf_simplex_tree.hpp:53
int coface_index(Simplex_tree::Simplex_handle simplex, Simplex_tree::Simplex_handle coface)
Definition: sheaf_simplex_tree.hpp:32
std::unordered_map< Simplex_tree::Simplex_key, std::vector< float > > extra_data
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:25
Definition: PersistentLaplacians.cpp:7
Simplex_tree::Filtration_value Filtration_value
Definition: sheaf_simplex_tree.hpp:18
PersistentLaplacians::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:176
std::vector< std::vector< Filtration_value > > Distance_matrix
Definition: sheaf_simplex_tree.hpp:19
Gudhi::Simplex_tree< Gudhi::Simplex_tree_options_default > Simplex_tree
Definition: sheaf_simplex_tree.hpp:17
Eigen::SparseMatrix< float, Eigen::ColMajor > SparseMatrixFloat
Definition: typedefs.hpp:31