PETLS
Rips.hpp
Go to the documentation of this file.
1 #ifndef PL_R_H
2 #define PL_R_H
3 
4 #include "../core/Complex.hpp"
5 #include "../core/up_algorithms.hpp"
6 #include "../eigenvalues/eigs_algorithms.hpp"
7 #include "../petls.hpp"
8 #include "../../include/Ripser_modified/ripser_modified.h"
9 
10 
11 namespace petls{
12  class Rips : public Complex {
13  public:
14 
15  // Lower_Distance_Matrix file format
16  Rips(const char* filename, int max_dim) : petls::Complex() { // call default constructor of parent class
17  std::vector<std::vector<std::tuple<int64_t,int64_t,int>>> boundaries_triples;
18  std::vector<std::vector<filtration_type>> filtrations;
19  float threshold = std::numeric_limits<value_t>::max();
20  rips_distance_matrix_file(filename, (long long int) max_dim, threshold, boundaries_triples, filtrations);
21  std::vector<SparseMatrixInt> boundaries(boundaries_triples.size());
22  boundaries_eigen(boundaries_triples, boundaries, filtrations);
23  this->set_boundaries_filtrations(boundaries, filtrations);
24 
25  }
26 
27  // Lower_Distance_Matrix file format
28  Rips(const char* filename, int max_dim, float threshold) : petls::Complex() { // call default constructor of parent class
29  std::vector<std::vector<std::tuple<int64_t,int64_t,int>>> boundaries_triples;
30  std::vector<std::vector<filtration_type>> filtrations;
31  rips_distance_matrix_file(filename, (long long int) max_dim, threshold, boundaries_triples, filtrations);
32 
33  std::vector<SparseMatrixInt> boundaries(boundaries_triples.size());
34  boundaries_eigen(boundaries_triples, boundaries, filtrations);
35  this->set_boundaries_filtrations(boundaries, filtrations);
36 
37  }
38 
39  // Eigen DenseMatrix with distances (correlations) in lower triangular part
40  Rips(DenseMatrix_PL distances, int max_dim) : petls::Complex() { // call default constructor of parent class
41  std::vector<std::vector<std::tuple<int64_t,int64_t,int>>> boundaries_triples;
42  std::vector<std::vector<filtration_type>> filtrations;
43  int num_rows = distances.rows();
44  std::vector<value_t> distances_vec;
45  distances_vec.reserve(num_rows*(num_rows-1)/2);
46  for (int i = 1; i < num_rows; i++){
47  for (int j = 0; j < i; j++){
48  distances_vec.push_back(distances(i,j));
49  }
50  }
51  compressed_lower_distance_matrix ripser_mat(std::move(distances_vec));
52 
53  basic_rips(ripser_mat,max_dim, std::numeric_limits<value_t>::infinity(), boundaries_triples, filtrations);
54 
55  std::vector<SparseMatrixInt> boundaries(boundaries_triples.size());
56  boundaries_eigen(boundaries_triples, boundaries, filtrations);
57 
58  this->set_boundaries_filtrations(boundaries, filtrations);
59  }
60 
61  // Eigen DenseMatrix with distances (correlations) in lower triangular part
62  Rips(DenseMatrix_PL distances, int max_dim, float threshold) : petls::Complex() { // call default constructor of parent class
63  std::vector<std::vector<std::tuple<int64_t,int64_t,int>>> boundaries_triples;
64  std::vector<std::vector<filtration_type>> filtrations;
65  int num_rows = distances.rows();
66  std::vector<value_t> distances_vec;
67  distances_vec.reserve(num_rows*(num_rows-1)/2);
68  for (int i = 1; i < num_rows; i++){
69  for (int j = 0; j < i; j++){
70  distances_vec.push_back(distances(i,j));
71  }
72  }
73  compressed_lower_distance_matrix ripser_mat(std::move(distances_vec));
74 
75  basic_rips(ripser_mat,max_dim, threshold, boundaries_triples, filtrations);
76 
77  std::vector<SparseMatrixInt> boundaries(boundaries_triples.size());
78  boundaries_eigen(boundaries_triples, boundaries, filtrations);
79 
80  this->set_boundaries_filtrations(boundaries, filtrations);
81  }
82 
83 
84  // Point cloud format
85  Rips(std::vector<std::vector<double>> points, int max_dim){
86  std::vector<std::vector<std::tuple<int64_t,int64_t,int>>> boundaries_triples;
87  std::vector<std::vector<filtration_type>> filtrations;
88  float threshold = std::numeric_limits<value_t>::max();
89  rips_point_cloud(points, max_dim, threshold, boundaries_triples, filtrations);
90 
91  std::vector<SparseMatrixInt> boundaries(boundaries_triples.size());
92  boundaries_eigen(boundaries_triples, boundaries, filtrations);
93 
94  this->set_boundaries_filtrations(boundaries, filtrations);
95 
96  }
97 
98  Rips(std::vector<std::vector<double>> points, int max_dim, float threshold){
99  std::vector<std::vector<std::tuple<int64_t,int64_t,int>>> boundaries_triples;
100  std::vector<std::vector<filtration_type>> filtrations;
101  rips_point_cloud(points, max_dim, threshold, boundaries_triples, filtrations);
102 
103  std::vector<SparseMatrixInt> boundaries(boundaries_triples.size());
104  boundaries_eigen(boundaries_triples, boundaries, filtrations);
105  this->set_boundaries_filtrations(boundaries, filtrations);
106 
107  }
108 
109  // These static methods are to unambiguously wrap with Pybind11
110  // NumPy arrays can get converted to Eigen::Matrix or std::vector<std::vector< >>
111  static Rips from_distances(DenseMatrix_PL distances, int max_dim){
112  return Rips(distances, max_dim);
113  }
114 
115  static Rips from_distances(DenseMatrix_PL distances, int max_dim, float threshold){
116  return Rips(distances, max_dim, threshold);
117  }
118 
119  static Rips FromPoints(std::vector<std::vector<double>> points, int max_dim){
120  return Rips(points, max_dim);
121  }
122  static Rips FromPoints(std::vector<std::vector<double>> points, int max_dim, float threshold){
123  return Rips(points, max_dim, threshold);
124  }
125 
126 
127  private:
128  void boundaries_eigen(std::vector<std::vector<std::tuple<int64_t,int64_t,int>>> &boundaries_triples, std::vector<SparseMatrixInt> &boundaries, std::vector<std::vector<value_t>>& filtrations){
129  for (int i = 0; i < (int) boundaries_triples.size(); i++){
130 
131  int dim = i + 1;
132  std::vector<Eigen::Triplet<int>> tripletList;
133  std::vector<std::tuple<int64_t, int64_t, int>> boundary_triples = boundaries_triples[i]; // is this an unnecessary copy?
134  // convert to eigen triple format and use the mapping
135  for (int j = 0; j < (int) boundary_triples.size(); j++){
136  int row = std::get<0>(boundary_triples[j]);
137  int col = std::get<1>(boundary_triples[j]);
138  int coeff = std::get<2>(boundary_triples[j]);
139  tripletList.push_back(Eigen::Triplet<int>(row, col, coeff));
140  }
141  SparseMatrixInt boundary(filtrations[dim-1].size(), filtrations[dim].size());
142 
143  boundary.setFromTriplets(tripletList.begin(), tripletList.end());
144  boundaries[i]=boundary;
145  }
146  }
147  };
148 }
149 
150 #endif
Definition: Complex.hpp:30
void set_boundaries_filtrations(std::vector< Eigen::SparseMatrix< storage >> boundaries, std::vector< std::vector< filtration_type >> filtrations)
Definition: Complex.cpp:120
Definition: Rips.hpp:12
static Rips FromPoints(std::vector< std::vector< double >> points, int max_dim)
Definition: Rips.hpp:119
static Rips from_distances(DenseMatrix_PL distances, int max_dim)
Definition: Rips.hpp:111
static Rips FromPoints(std::vector< std::vector< double >> points, int max_dim, float threshold)
Definition: Rips.hpp:122
Rips(const char *filename, int max_dim)
Definition: Rips.hpp:16
Rips(std::vector< std::vector< double >> points, int max_dim, float threshold)
Definition: Rips.hpp:98
Rips(std::vector< std::vector< double >> points, int max_dim)
Definition: Rips.hpp:85
static Rips from_distances(DenseMatrix_PL distances, int max_dim, float threshold)
Definition: Rips.hpp:115
Rips(const char *filename, int max_dim, float threshold)
Definition: Rips.hpp:28
Rips(DenseMatrix_PL distances, int max_dim, float threshold)
Definition: Rips.hpp:62
Rips(DenseMatrix_PL distances, int max_dim)
Definition: Rips.hpp:40
Definition: Complex.cpp:26
Eigen::Matrix< coefficient_type, Eigen::Dynamic, Eigen::Dynamic > DenseMatrix_PL
Definition: typedefs.hpp:14
Eigen::SparseMatrix< int, Eigen::ColMajor > SparseMatrixInt
Definition: typedefs.hpp:19