PETLS
PersistentSheafLaplacian.hpp
Go to the documentation of this file.
1 #ifndef PL_H
2 #define PL_H
3 
4 #include <vector>
5 
6 #include "../typedefs.hpp"
7 #include "../petls.hpp"
8 #include "../core/up_algorithms.hpp"
9 #include "../eigenvalues/eigs_algorithms.hpp"
11 #include "sheaf_simplex_tree.hpp"
12 #include <unsupported/Eigen/SparseExtra>
13 #include <Eigen/Dense>
14 
15 #include <chrono>
16 #include <numeric> //std::iota
17 #include <map>
18 #include <set>
19 
20 #include <cassert>
21 
22 namespace petls{
29 // template <typename storage = int>
31  using storage = float;
32  public:
33  /********************/
34  /* Member variables */
35  /********************/
36 
37  int top_dim;
38  std::vector<FilteredBoundaryMatrix<storage>> filtered_boundaries;
39  bool verbose;
40  bool use_flipped;
43  std::function<std::pair<spectra_vec,DenseMatrix_PL>(DenseMatrix_PL&)> eigenpairs_algorithm_func;
44  // std::function<void(FilteredBoundaryMatrix<storage>*, filtration_type, filtration_type, DenseMatrix_PL&)> up_algorithm_func;
46  //may need to be void* instead of FilteredBoundaryMatrix*
47 
48  /****************/
49  /* Constructors */
50  /****************/
52  filtered_boundaries = std::vector<FilteredBoundaryMatrix<storage>>();
53  top_dim = 0;
54  verbose = false;
55  use_flipped=false;
58  up_algorithm_func = petls::schur_algorithm<storage>;
59  filtered_boundaries.push_back(dummy_d0());
60 
61  std::vector<FilteredBoundaryMatrix<coefficient_type>> filtered_boundaries_temp = sst.apply_restriction_function();
62  for (auto fbm : filtered_boundaries_temp){
63  this->filtered_boundaries.push_back(fbm);
64  }
65  this->top_dim = this->filtered_boundaries.size();
66  for (int i = 0; i < (int) this->filtered_boundaries.size(); i++){
67  this->filtered_boundaries[i] = this->filtered_boundaries[i].transpose();
68  }
69  }
70 
71 
81  PersistentSheafLaplacian(std::vector<Eigen::SparseMatrix<storage>> boundaries,
82  std::vector<std::vector<filtration_type>> filtrations){
83  filtered_boundaries = std::vector<FilteredBoundaryMatrix<storage>>();
84  verbose = false;
85  use_flipped=false;
88  up_algorithm_func = petls::schur_algorithm<storage>;
89  this->set_boundaries_filtrations(boundaries, filtrations);
90  }
91 
92 
93  /****************** */
94  /* Empty destructor */
95  /****************** */
96 
98 
99  /***********************/
100  /* Getters and setters */
101  /***********************/
102 
103  void set_eigs_algorithm_func(std::function<spectra_vec(DenseMatrix_PL&)> _eigs_algorithm_func){
104  eigs_algorithm_func = _eigs_algorithm_func;
105  }
106 
107  void set_eigs_algorithm_func(std::string name){
108  if (name == "selfadjoint"){
110  } else if (name == "eigensolver"){
112  } else if (name == "bdcsvd"){
114  }
115  }
116 
117  void set_up_func(std::function<void(FilteredBoundaryMatrix<float>*, filtration_type, filtration_type, DenseMatrix_PL&)> _up_algorithm_func){
118  up_algorithm_func = _up_algorithm_func;
119  }
120 
130  void set_boundaries_filtrations(std::vector<Eigen::SparseMatrix<storage>> boundaries,
131  std::vector<std::vector<filtration_type>> filtrations){
132  // Primary setter function called by constructors
133  // Input:
134  // boundaries: vector of Eigen::SparseMatrix of type int.
135  // boundaries must be sorted in order of dimension
136  // filtrations: vector of vector of filtrations.
137  // filtrations[dim] is a list of all filtrations of
138  // simplices in dimension dim.
139  // filtrations must be sorted in order of dimension,
140  // filtrations[i] must be sorted in order of filtration
141  // Important Assumptions:
142  // 1) Boundary matrix has real coefficients stored as integers (but not mod 2!)
143  // 2) Boundary matrix dimensions agree with filtrations sizes
144  // 3) Length(filtrations) = Length(boundaries) + 1
147  // up_algorithm_func = petls::schur_algorithm;
148  filtered_boundaries.clear();
149  top_dim = boundaries.size();
150  filtered_boundaries.reserve(top_dim+1);
151 
152  filtered_boundaries.push_back(dummy_d0());
153 
154  // Stitch together boundary matrices with filtrations for domain and range
155  // Check for consistent sizing
156  for (unsigned long int dim = 1; dim <= (unsigned long int) top_dim; dim++){
157  if(boundaries[dim-1].cols() != (long int) filtrations[dim].size()){
158  std::cout << "boundaries[" << dim -1 << "].cols()=" << boundaries[dim-1].cols() << " != filtrations[" << dim << "].size()=" << filtrations[dim].size() << std::endl;
159  }
160  if (boundaries[dim-1].rows() != (long int) filtrations[dim-1].size()){
161  std::cout << "boundaries[" << dim -1 << "].rows()=" << boundaries[dim-1].rows() << " != filtrations[" << dim-1 << "].size()=" << filtrations[dim-1].size() << std::endl;
162  }
163  filtered_boundaries.push_back(FilteredBoundaryMatrix<storage>(boundaries[dim-1],filtrations[dim],filtrations[dim-1]));
164  }
165  }
166 
167 
172  void set_verbose(bool verbose){this->verbose = verbose;}
173 
178  void set_flipped(bool use_flipped){this->use_flipped = use_flipped;}
179 
180  /***********************************/
181  /* Primary Mathematical Operations */
182  /***********************************/
183 
184 
193  // Get the Persistent Laplacian Matrix in dimension dim from filtration a to filtration b
194  // Inputs: integer dimension, start filtration level, end filtration level
195  // Output (by reference to avoid a large copy): L of type Eigen::MatrixXf
196 
197  // L_0 = L_up
198  if (dim == 0){
199  this->profile.start_L_up();
200  up_algorithm_func(&filtered_boundaries[dim+1],a,b,L);
201  // up_Algorithm up_alg; // see up_algorithms.hpp
202  // up_alg(&filtered_boundaries[dim+1],a,b, L);
203 
204  // Time monitoring
205  this->profile.stop_L_up();
206  profile.durations_sum_up_down.push_back(0);
207  profile.durations_L_down.push_back(0);
208 
209  return;
210  } else if (dim == top_dim){
211  // L_{top_dim} = L_down
212  this->profile.start_L_down();
213  Eigen::SparseMatrix<storage> down(L.rows(),L.rows());
214  get_down(dim, a, down);
215  // Down produces a sparse matrix so we convert to dense
216  L = DenseMatrix_PL(down.template cast<coefficient_type>());
217 
218  // Time monitoring
219  this->profile.stop_L_down();
220  profile.durations_sum_up_down.push_back(0);
221  profile.durations_L_up.push_back(0);
222 
223  return;
224  } else if (dim > top_dim){
225  // L = 0
226  profile.durations_sum_up_down.push_back(0);
227  profile.durations_L_up.push_back(0);
228  profile.durations_L_down.push_back(0);
229  L.setZero(0,0);
230  return;
231  }
232  // Else L = L_up + L_down
233 
234  // Set L = L_up then add L_down later
235  this->profile.start_L_up();
236  get_up(dim, a, b, L);
237  this->profile.stop_L_up();
238 
239  this->profile.start_L_down();
240 
241  // Get L_down
242  Eigen::SparseMatrix<storage> down(L.rows(), L.rows());
243  get_down(dim,a, down);
244  this->profile.stop_L_down();
245 
246  if (L.size() == 0){// L_up is empty, use L = L_down
247  profile.durations_sum_up_down.push_back(0);
248  L = DenseMatrix_PL(down.template cast<coefficient_type>());
249  return;
250  } else if (down.size() == 0){// L_down is empty, use L = L_up (already done)
251  profile.durations_sum_up_down.push_back(0);
252  return;
253  }
254  assert (L.cols() == down.cols() && L.rows() == down.rows() && "up and down Laplacians must have same dimensions");
255 
256  // L = L_up + L_down,
257  // but L = L_up already, so just add L_down.
258  // https://libeigen.gitlab.io/docs/group__TutorialSparse.html
259  // this is faster than just returning up+down
260  this->profile.start_sum_up_down();
261  L+= down.template cast<coefficient_type>();
262  this->profile.stop_sum_up_down();
263 
264  return;
265  // L is passed by reference to avoid a copy
266  }
267 
280  DenseMatrix_PL L;
281  get_L(dim, a, b, L);
282  return L;
283  }
284 
291  Eigen::SparseMatrix<storage> B;
292  filtered_boundaries[top_dim].submatrix_at_filtration(a, B);
293  // The nonzero eigenvalues of (B * B^T) are the same as (B^T * B), and its possible BB^T is faster to compute the eigenvalues of.
294  L = (B*B.transpose()).template cast<coefficient_type>();
295  }
296 
307  SparseMatrix_PL L;
308  get_L_top_dim_flipped(a, L);
309  return L;
310  }
311  // void get_up_standard(int up_dim, filtration_type a, filtration_type b, DenseMatrix_PL &L_up);
312 
313 
323  up_algorithm_func(&filtered_boundaries[dim+1],a,b,L_up);
324  // up_Algorithm up_alg; // see up_algorithms.hpp
325  // up_alg(&filtered_boundaries[dim+1],a,b, L_up);
326  }
327 
328 
338  DenseMatrix_PL L_up;
339  get_up(dim, a, b, L_up);
340  return L_up;
341  }
342 
349  void get_down(int dim, filtration_type a, Eigen::SparseMatrix<storage> &L_down){
350  Eigen::SparseMatrix<storage> B;
351  filtered_boundaries[dim].submatrix_at_filtration(a, B);
352  L_down = B.transpose()*B;
353 
354  // Every test of the self adjoint view rank update has been slower for down Laplacian than just B.transpose()*B.
355  // L_down.selfadjointView<Eigen::Lower>().rankUpdate(B.transpose());
356  return;
357  }
358 
369  Eigen::SparseMatrix<storage> get_down(int dim, filtration_type a){
370  Eigen::SparseMatrix<storage> L_down;
371  get_down(dim, a, L_down);
372  return L_down;
373  }
374 
375  // Some build environments seem to struggle to compile with the call to inverse() only in the PSL
376  // so for now we will leave out the nonzero_spectra function.
377  // std::vector<spectra_type> nonzero_spectra(int dim, filtration_type a, filtration_type b, SparseMatrix_PL PH_basis, bool use_dummy_harmonic_basis){
378  // }
379 
380 
381  /**********************************************/
382  /* Driver functions to get PL and eigenvalues */
383  /**********************************************/
384 
392  std::vector<spectra_type> spectra(int dim, filtration_type a, filtration_type b){
393  spectra_vec eigenvalues;
394  this->profile.start_L();
395  // no 1-simplices, return vector of zeros
396  if (dim == 0 && filtered_boundaries.size() == 1){
397  int betti0 = filtered_boundaries[0].index_of_filtration(true, a)+1;
398  // for (int i = 0; i < betti0; i++){
399  // eigenvalues.push_back(0.0);
400  // }
401  this->profile.stop_L();
402  profile.L_rows.push_back(betti0);
403  return std::vector<spectra_type>(betti0,0.0);
404  }
405 
406  // Get number of rows in Laplacian
407  int L_rows;
408  if (dim == 0){
409  L_rows = filtered_boundaries[1].index_of_filtration(false,a)+1;
410  } else {
411  L_rows = filtered_boundaries[dim].index_of_filtration(true,a) + 1;
412  }
413  profile.L_rows.push_back(L_rows);
414 
415  // Top dimension might use the "flipped" technique
416  // This must be called from the spectra function so
417  // that the eigenvalues vector can be 0-padded correctly
418  if (dim == top_dim && use_flipped){
419  int L_rows_flipped = filtered_boundaries[dim-1].index_of_filtration(true,a) + 1;
420  int zero_pad_length = L_rows - L_rows_flipped;
421 
422  // Only use flipped version if it will be smaller
423  if (L_rows_flipped < L_rows){
424  this->profile.start_L_down();
425  SparseMatrix_PL L(L_rows_flipped, L_rows_flipped);
426  get_L_top_dim_flipped(a, L);
427  this->profile.stop_L_down();
428  this->profile.stop_L();
429 
430  // L=0 record trivial stats
431  if (L.size()==0){
432  eigenvalues.setZero(0);
433  profile.durations_eigs.push_back(0);
434  profile.durations_sum_up_down.push_back(0);
435  profile.durations_L_up.push_back(0);
436  return std::vector<spectra_type>();
437  }
438 
439  // Get eigenvalues of smaller L
440  this->profile.start_eigs();
442  // Eigen::SelfAdjointEigenSolver<SparseMatrix_spectra_PL> es(L, Eigen::EigenvaluesOnly);
443  // spectra_vec eigs = es.eigenvalues();
444  std::vector<spectra_type> std_eigs_flipped(eigs.data(), eigs.data() + eigs.size());
445  if (zero_pad_length <= 0){
446  std::cout << "zero_pag_length <= 0 (should not happen)"<< std::endl;
447  this->profile.stop_eigs();
448 
449  profile.durations_sum_up_down.push_back(0);
450  profile.durations_L_up.push_back(0);
451  return std_eigs_flipped;
452  }
453 
454  // Pad the eigenvalues with 0s
455  std::vector<spectra_type> zero_pad(zero_pad_length, 0.0);
456  std::move(std_eigs_flipped.begin(), std_eigs_flipped.end(), std::back_inserter(zero_pad));
457 
458  // Record timing
459  this->profile.stop_eigs();
460  profile.durations_sum_up_down.push_back(0);
461  profile.durations_L_up.push_back(0);
462  return zero_pad;
463  }
464  }
465 
466  // Get L
467  DenseMatrix_PL L(L_rows, L_rows);
468  get_L(dim,a,b, L);
469  this->profile.stop_L();
470 
471  // L = 0, recover trivial stats
472  if (L.size()==0){
473  eigenvalues.setZero(0);
474  profile.durations_eigs.push_back(0);
475  return std::vector<spectra_type>();
476  }
477 
478  // Compute Eigenvalues
479  this->profile.start_eigs();
480  // eigs_Algorithm es;
481  eigenvalues = eigs_algorithm_func(L);
482  // eigenvalues = es.eigenvalues(L);
483  this->profile.stop_eigs();
484  std::vector<spectra_type> std_eigenvalues(eigenvalues.data(), eigenvalues.data() + eigenvalues.size());
485  return std_eigenvalues;
486  }
487 
492  std::vector<std::tuple<int, filtration_type, filtration_type, std::vector<spectra_type>>> spectra(){
493  // Output:
494  // A vector of tuples (dim, a, b, eigenvalues), where the eigenvalues is a sorted vector of real numbers
495 
496 
497  // Create a vector listing all dimensions 0, 1, ..., top_dim
498  std::vector<int> dims(top_dim+1);
499  std::iota (std::begin(dims), std::end(dims), 0);
500 
501  // Get all filtration values that occur in the complex
502  std::vector<filtration_type> all_filtrations = get_all_filtrations();
503 
504  // Convert the vector of dimensions and filtration values into triples
505  // (dim, a, b) to get L_{dim}^{a,b}
506  std::vector<std::tuple<int, filtration_type, filtration_type>> requests = filtration_list_to_spectra_request(all_filtrations,dims);
507 
508  // Compute the PL matrices and eigenvalues
509  std::vector<std::tuple<int, filtration_type, filtration_type, std::vector<spectra_type>>> requested_spectra = spectra(requests);
510 
511  // Record results for the profiler, not necessary for the user outside of benchmarking
512  for (int i = 0; i < (int) requested_spectra.size(); i++){
513  std::tuple<int, filtration_type, filtration_type, std::vector<spectra_type>> current = requested_spectra[i];
514  profile.dims.push_back(std::get<0>(current));
515  profile.filtration_a.push_back(std::get<1>(current));
516  profile.filtration_b.push_back(std::get<2>(current));
517  std::pair<int,spectra_type> summary = eigenvalues_summarize(std::get<3>(current));
518  profile.bettis.push_back(summary.first);
519  profile.lambdas.push_back(summary.second);
520  }
521  // Output profiler to csv
522  if (filtered_boundaries.size() > 1)// dont report profile when no 1-simplices
523  profile.to_csv("./profile.csv");
524  return requested_spectra;
525  }
526 
532  std::vector<std::tuple<int, filtration_type, filtration_type, std::vector<spectra_type>>> spectra(std::vector<std::tuple<int,filtration_type,filtration_type>> spectra_request_list){
533  // Declare variables and vectors
534  int dim;
535  filtration_type a;
536  filtration_type b;
537  std::tuple<int,filtration_type,filtration_type> spectra_request;
538  std::vector<spectra_vec> spectra_list;
539  std::vector<std::tuple<int, filtration_type, filtration_type, std::vector<spectra_type>>> requested_spectra;
540  requested_spectra.reserve(spectra_request_list.size());
541 
542  // Loop through list of tuples (dim, a, b)
543  for (unsigned long int i = 0; i < spectra_request_list.size(); i++){
544  // Unpack the tuples
545  spectra_request = spectra_request_list[i];
546  dim = std::get<0>(spectra_request);
547  a = std::get<1>(spectra_request);
548  b = std::get<2>(spectra_request);
549 
550  // Get eigenvalues and re-pack as (dim, a, b, eigenvalues)
551  this->profile.start_all();
552  requested_spectra.push_back(std::make_tuple(dim, a, b, spectra(dim, a, b)));
553  this->profile.stop_all();
554  if (verbose)
555  std::cout << "duration spectra for dim=" << dim << ", a= " << a << ", b=" << b << ": " << this->profile.all.duration << std::endl;
556  }
557  return requested_spectra;
558  }
559 
560  /***************************************************************/
561  /* Driver functions to get PL and eigenvalues and eigenvectors */
562  /***************************************************************/
563 
564 
572  std::pair<std::vector<spectra_type>,DenseMatrix_PL> eigenpairs(int dim, filtration_type a, filtration_type b){
573  spectra_vec eigenvalues;
574  DenseMatrix_PL eigenvectors;
575  this->profile.start_L();
576  // no 1-simplices, return all eigenvalues zero
577  // each eigenvector is a unit vector in only one component, so the matrix of all eigenvectors is just the identity matrux
578  if (dim == 0 && filtered_boundaries.size() == 1){
579  int betti0 = filtered_boundaries[0].index_of_filtration(true, a)+1;
580  // // for (int i = 0; i < betti0; i++){
581  // // eigenvalues.push_back(0.0);
582  // // }
583  this->profile.stop_L();
584  profile.L_rows.push_back(betti0);
585  std::vector<spectra_type> std_eigs = std::vector<spectra_type>(betti0,0.0);
586  eigenvectors.setIdentity(betti0,betti0);
587  return std::pair<std::vector<spectra_type>,DenseMatrix_PL>(std_eigs, eigenvectors);
588  }
589 
590  // Get number of rows in Laplacian
591  int L_rows;
592  if (dim == 0){
593  L_rows = filtered_boundaries[1].index_of_filtration(false,a)+1;
594  } else {
595  L_rows = filtered_boundaries[dim].index_of_filtration(true,a) + 1;
596  }
597  profile.L_rows.push_back(L_rows);
598 
599  // Get L
600  DenseMatrix_PL L(L_rows, L_rows);
601  get_L(dim,a,b, L);
602  this->profile.stop_L();
603 
604  // L = 0, recover trivial stats
605  if (L.size()==0){
606  eigenvalues.setZero(0);
607  profile.durations_eigs.push_back(0);
608  return std::pair<std::vector<spectra_type>,DenseMatrix_PL>(std::vector<spectra_type>(),DenseMatrix_PL());
609  }
610 
611  // Compute Eigenvalues
612  this->profile.start_eigs();
613  std::pair<spectra_vec,DenseMatrix_spectra_PL> eigenpairs = eigenpairs_algorithm_func(L);
614  // eigs_Algorithm es;
615  // std::pair<spectra_vec,DenseMatrix_spectra_PL> eigenpairs = es.eigenpairs(L);
616  eigenvalues = eigenpairs.first;
617  this->profile.stop_eigs();
618  std::vector<spectra_type> std_eigenvalues(eigenvalues.data(), eigenvalues.data() + eigenvalues.size());
619  return std::pair<std::vector<spectra_type>,DenseMatrix_PL>(std_eigenvalues,eigenpairs.second);
620  }
621 
626  std::vector<std::tuple<int, filtration_type, filtration_type, std::vector<spectra_type>,DenseMatrix_PL>> eigenpairs(){
627 
628  // Create a vector listing all dimensions 0, 1, ..., top_dim
629  std::vector<int> dims(top_dim+1);
630  std::iota (std::begin(dims), std::end(dims), 0);
631 
632  // Get all filtration values that occur in the complex
633  std::vector<filtration_type> all_filtrations = get_all_filtrations();
634 
635  // Convert the vector of dimensions and filtration values into triples
636  // (dim, a, b) to get L_{dim}^{a,b}
637  std::vector<std::tuple<int, filtration_type, filtration_type>> requests = filtration_list_to_spectra_request(all_filtrations,dims);
638 
639  // Compute the PL matrices and eigenvalues
640  std::vector<std::tuple<int, filtration_type, filtration_type, std::vector<spectra_type>,DenseMatrix_PL>> requested_spectra = eigenpairs(requests);
641 
642  // Record results for the profiler, not necessary for the user outside of benchmarking
643  for (int i = 0; i < (int) requested_spectra.size(); i++){
644  std::tuple<int, filtration_type, filtration_type, std::vector<spectra_type>,DenseMatrix_PL> current = requested_spectra[i];
645  profile.dims.push_back(std::get<0>(current));
646  profile.filtration_a.push_back(std::get<1>(current));
647  profile.filtration_b.push_back(std::get<2>(current));
648  std::pair<int,spectra_type> summary = eigenvalues_summarize(std::get<3>(current));
649  profile.bettis.push_back(summary.first);
650  profile.lambdas.push_back(summary.second);
651  }
652  // Output profiler to csv
653  if (filtered_boundaries.size() > 1)// dont report profile when no 1-simplices
654  profile.to_csv("./profile.csv");
655  return requested_spectra;
656  }
657 
663  std::vector<std::tuple<int, filtration_type, filtration_type, std::vector<spectra_type>, DenseMatrix_PL>> eigenpairs(std::vector<std::tuple<int,filtration_type,filtration_type>> spectra_request_list){
664  // Declare variables and vectors
665  int dim;
666  filtration_type a;
667  filtration_type b;
668  std::tuple<int,filtration_type,filtration_type> spectra_request;
669  std::vector<spectra_vec> spectra_list;
670  std::vector<std::tuple<int, filtration_type, filtration_type, std::vector<spectra_type>, DenseMatrix_PL>> requested_spectra;
671  requested_spectra.reserve(spectra_request_list.size());
672 
673  // Loop through list of tuples (dim, a, b)
674  for (unsigned long int i = 0; i < spectra_request_list.size(); i++){
675  // Unpack the tuples
676  spectra_request = spectra_request_list[i];
677  dim = std::get<0>(spectra_request);
678  a = std::get<1>(spectra_request);
679  b = std::get<2>(spectra_request);
680 
681  // Get eigenvalues and eigenvectors and re-pack as (dim, a, b, eigenvalues, eigenvectors)
682  this->profile.start_all();
683  std::pair<std::vector<spectra_type>,DenseMatrix_PL> eigenpairs = this->eigenpairs(dim, a, b);
684  std::vector<spectra_type> eigenvalues = eigenpairs.first;
685  DenseMatrix_PL eigenvectors = eigenpairs.second;
686  requested_spectra.push_back(std::make_tuple(dim, a, b, eigenvalues, eigenvectors));
687  this->profile.stop_all();
688  if (verbose)
689  std::cout << "duration spectra for dim=" << dim << ", a= " << a << ", b=" << b << ": " << this->profile.all.duration << std::endl;
690  }
691  return requested_spectra;
692  }
693 
694 
700  std::pair<int, spectra_type> eigenvalues_summarize(std::vector<spectra_type> eigenvalues){
701  // Input: vector of eigenvalues
702  // Output: betti number and least nonzero eigenvalue (tolerance 1e-4)
703  int current_betti = 0;
704  spectra_type tol = 1e-4;
705  for (int k = 0; k < (int) eigenvalues.size(); k++){
706  if (eigenvalues[k] > tol){ // reached a nonzero eigenvalue
707  return std::make_pair(current_betti, eigenvalues[k]);
708  }
709  current_betti++;
710  }
711  // if we reach the end of the loop, then there were no nonzero eigenvalues
712  if (current_betti == 0){
713  if ((int) eigenvalues.size() > 0){
714  return std::make_pair(0,eigenvalues[0]);
715  }
716  return std::make_pair(0,0.0);
717  }
718  // else never encountered a nonzero eigenvalue
719  return std::make_pair(current_betti,0.0);
720  }
721 
722  /**********************************/
723  /* Helpful input/output functions */
724  /**********************************/
725 
726 
734  void store_L(int dim, filtration_type a, filtration_type b, std::string filename){
735  int L_rows = filtered_boundaries[dim].index_of_filtration(true,a) + 1;
736  DenseMatrix_PL L(L_rows, L_rows);
737 
738  get_L(dim,a,b, L);
739  SparseMatrix_PL L_sparse = L.sparseView();
740  bool success = Eigen::saveMarket(L_sparse, filename); // conversion to sparse is probably extremely expensive, but not all versions of eigen have saveMarketDense
741  if (success)
742  std::cout << "saved matrix to file " << filename << std::endl;
743  else
744  std::cout << "failed saving matrix to file " << filename << std::endl;
745  return;
746  }
747 
752  for (int i = 0; i <= top_dim; i++){
753  filtered_boundaries[i].print();
754  filtered_boundaries[i].print_range_filtration();
755  filtered_boundaries[i].print_domain_filtration();
756  }
757  }
758 
769  void store_spectra(std::vector<std::tuple<int, filtration_type, filtration_type, std::vector<spectra_type>>> spectra, std::string out_prefix){
770  // Open the files
771  std::vector<std::ofstream> out_streams(top_dim+1);
772  for (int i = 0; i <= top_dim; i++){
773  out_streams[i] = std::ofstream("./" + out_prefix + "_spectra_" + std::to_string(i) + ".txt");
774  }
775 
776  // Write the eigenvalues (not assuming any ordering on the dimensions)
777  for (int i = 0; i < (int) spectra.size(); i++){
778  std::tuple<int, filtration_type, filtration_type, std::vector<spectra_type>> spectrum_record = spectra[i];
779  int dim = std::get<0>(spectrum_record);
780  std::vector<spectra_type> eigs = std::get<3>(spectrum_record);
781  for (int j = 0; j < (int) eigs.size(); j++){
782  out_streams[dim] << eigs[j] << " ";
783  }
784  out_streams[dim] << std::endl;
785  }
786  // Close the files
787  for (int i = 0; i <= top_dim; i++){
788  out_streams[i].close();
789  }
790  }
791 
792 
800  void store_spectra_summary(std::vector<std::tuple<int, filtration_type, filtration_type, std::vector<spectra_type>>> spectra, std::string out_prefix){
801  std::set<std::pair<filtration_type, filtration_type>> filtration_values_set;
802 
803  // get all unique (a,b) filtration value pairs
804  for (int i = 0 ; i < (int) spectra.size(); i++){
805  filtration_type a = std::get<1>(spectra[i]);
806  filtration_type b = std::get<2>(spectra[i]);
807  filtration_values_set.insert(
808  std::make_pair(a,b));
809  }
810  int num_filtrations = filtration_values_set.size();
811  std::vector<std::pair<filtration_type,filtration_type>> filtration_values_vec(filtration_values_set.begin(), filtration_values_set.end());
812 
813  // create a mapping for their index
814  // unordered_map may be much faster, but cannot hash a std::pair
815  std::map<std::pair<filtration_type,filtration_type>,int> filtration_index_map;
816  std::vector<std::vector<spectra_type>> output_lines(num_filtrations);
817  int items_per_line = 2 + 2*(top_dim+1);
818  for (int i = 0; i < num_filtrations; i++){
819  filtration_index_map[filtration_values_vec[i]] = i;
820 
821  std::vector<spectra_type> temp_line(items_per_line);
822  temp_line[0] = (spectra_type) filtration_values_vec[i].first;
823  temp_line[1] = (spectra_type) filtration_values_vec[i].second;
824  output_lines[i] = temp_line;
825  }
826 
827  for (int i = 0; i < (int) spectra.size(); i++){
828  // Unpack each tuple
829  std::tuple<int, filtration_type, filtration_type, std::vector<spectra_type>> spectrum_record = spectra[i];
830  int dim = std::get<0>(spectrum_record);
831  filtration_type a = std::get<1>(spectrum_record);
832  filtration_type b = std::get<2>(spectrum_record);
833 
834  // compute eigenvalue summary
835  std::pair<filtration_type, filtration_type> filtration_pair = std::make_pair(a,b);
836  std::vector<spectra_type> eigs = std::get<3>(spectrum_record);
837  std::pair<int, spectra_type> temp_pair = eigenvalues_summarize(eigs);
838  spectra_type betti = (spectra_type) temp_pair.first;
839  spectra_type lambda = (spectra_type) temp_pair.second;
840 
841  int line_index = filtration_index_map[filtration_pair];
842  // a, b, betti_0, betti_1, betti_2, lambda_0, lambda_1, lambda_2
843  // 0, 1, 2, 3, 4, 5, 6, 7
844  output_lines[line_index][2+dim] = betti;
845  output_lines[line_index][3+top_dim+dim] = lambda;
846  }
847 
848  // open file and write headers
849  std::ofstream outstream("./" + out_prefix + "_spectra_summary.txt");
850  outstream << "a\tb";
851  for (int i = 0; i <= top_dim; i++){
852  outstream <<"\tbetti_" << i;
853  }
854  for (int i = 0; i <= top_dim; i++){
855  outstream <<"\tlambda_" << i;
856  }
857  outstream << std::endl;
858 
859  // write the data to the file
860  for (int i = 0; i < (int) num_filtrations; i++){
861  outstream << output_lines[i][0];
862  for (int j = 1; j < items_per_line; j++){
863  outstream << "\t" << output_lines[i][j];
864  }
865  outstream << std::endl;
866  }
867  outstream.close();
868  }
869 
870 
877  std::vector<std::tuple<int, filtration_type, filtration_type>> filtration_list_to_spectra_request(std::vector<filtration_type> filtrations, std::vector<int> dims){
878  // Declare variables
879  filtration_type a;
880  filtration_type b;
881  int dim;
882  std::vector<std::tuple<int, filtration_type, filtration_type>> requests;
883  std::tuple<int, filtration_type, filtration_type> spectra_request;
884 
885  // create the pairs in order (dim=0, a=0, b=1), (dim=1, a=0, b=1), ..., (dim=top_dim, a=0, b=1), (dim=0, a=1, b=2), ...
886  for(unsigned long int filtration_index = 0; filtration_index < filtrations.size()-1; filtration_index++){
887  a = filtrations[filtration_index];
888  b = filtrations[filtration_index+1];
889  for (unsigned long int dim_index = 0; dim_index < dims.size(); dim_index++){
890  dim = dims[dim_index];
891  spectra_request = std::make_tuple(dim, a, b);
892  requests.push_back(spectra_request);
893  }
894  }
895  // add the [a,infinity) case as (dim=0, a, a), (dim=1, a, a), ...
896  a = filtrations[filtrations.size()-1];
897  b = a;
898  for (unsigned long int dim_index = 0; dim_index < dims.size(); dim_index++){
899  dim = dims[dim_index];
900  spectra_request = std::make_tuple(dim, a, b);
901  requests.push_back(spectra_request);
902  }
903  return requests;
904  }
905 
909  std::vector<filtration_type> get_all_filtrations(){
910  std::set<filtration_type> all_filtrations;
911 
912  //add zero-th dimensional
913  std::vector<filtration_type> c0 = filtered_boundaries[0].get_domain_filtrations();
914  for (unsigned long int i = 0; i < c0.size(); i++){
915  all_filtrations.insert(c0[i]);
916  }
917  //add other dimensions, which correspond to domain filtrations
918  std::vector<filtration_type> temp;
919  for (unsigned long int dim = 1; dim <= (unsigned long int) top_dim; dim++){
920  temp = filtered_boundaries[dim].get_domain_filtrations();
921  for (unsigned long int i = 0; i < temp.size(); i++){
922  all_filtrations.insert(temp[i]);
923  }
924  };
925  std::vector<filtration_type> all_filtrations_vector(all_filtrations.begin(), all_filtrations.end());
926  return all_filtrations_vector;
927  }
928 
929  protected:
930 
932  // Construct a placeholder matrix for d0 so that filtered_boundaries[dim] actually gives d_{dim}
933  Eigen::SparseMatrix<storage> dummy_d0_matrix;
934  std::vector<filtration_type> domain_filtrations = {0.0};
935  std::vector<filtration_type> range_filtrations = {0.0};
936  petls::FilteredBoundaryMatrix<storage> dummy_d0_fbm(dummy_d0_matrix,domain_filtrations,range_filtrations);
937  return dummy_d0_fbm;
938  }
939 };
940 
941 
942 }
943 #endif
Definition: FilteredBoundaryMatrix.hpp:15
Definition: PersistentSheafLaplacian.hpp:30
SparseMatrix_PL get_L_top_dim_flipped(filtration_type a)
Definition: PersistentSheafLaplacian.hpp:306
void set_flipped(bool use_flipped)
Definition: PersistentSheafLaplacian.hpp:178
std::vector< std::tuple< int, filtration_type, filtration_type > > filtration_list_to_spectra_request(std::vector< filtration_type > filtrations, std::vector< int > dims)
Definition: PersistentSheafLaplacian.hpp:877
void store_spectra(std::vector< std::tuple< int, filtration_type, filtration_type, std::vector< spectra_type >>> spectra, std::string out_prefix)
Definition: PersistentSheafLaplacian.hpp:769
petls::FilteredBoundaryMatrix< storage > dummy_d0()
Definition: PersistentSheafLaplacian.hpp:931
Eigen::SparseMatrix< storage > get_down(int dim, filtration_type a)
Definition: PersistentSheafLaplacian.hpp:369
void get_L(int dim, filtration_type a, filtration_type b, DenseMatrix_PL &L)
Definition: PersistentSheafLaplacian.hpp:192
void set_up_func(std::function< void(FilteredBoundaryMatrix< float > *, filtration_type, filtration_type, DenseMatrix_PL &)> _up_algorithm_func)
Definition: PersistentSheafLaplacian.hpp:117
std::function< std::pair< spectra_vec, DenseMatrix_PL >DenseMatrix_PL &)> eigenpairs_algorithm_func
Definition: PersistentSheafLaplacian.hpp:43
PersistentSheafLaplacian(std::vector< Eigen::SparseMatrix< storage >> boundaries, std::vector< std::vector< filtration_type >> filtrations)
Definition: PersistentSheafLaplacian.hpp:81
bool use_flipped
Compute the top-dimensional Laplacian's eigenvalues in spectra function via the eigenvalues of the sm...
Definition: PersistentSheafLaplacian.hpp:40
std::vector< std::tuple< int, filtration_type, filtration_type, std::vector< spectra_type >, DenseMatrix_PL > > eigenpairs()
Definition: PersistentSheafLaplacian.hpp:626
int top_dim
Top dimension of the complex.
Definition: PersistentSheafLaplacian.hpp:37
void get_up(int dim, filtration_type a, filtration_type b, DenseMatrix_PL &L_up)
Definition: PersistentSheafLaplacian.hpp:322
std::function< spectra_vec(DenseMatrix_PL &)> eigs_algorithm_func
Definition: PersistentSheafLaplacian.hpp:42
void get_down(int dim, filtration_type a, Eigen::SparseMatrix< storage > &L_down)
Definition: PersistentSheafLaplacian.hpp:349
std::vector< std::tuple< int, filtration_type, filtration_type, std::vector< spectra_type >, DenseMatrix_PL > > eigenpairs(std::vector< std::tuple< int, filtration_type, filtration_type >> spectra_request_list)
Definition: PersistentSheafLaplacian.hpp:663
std::pair< int, spectra_type > eigenvalues_summarize(std::vector< spectra_type > eigenvalues)
Definition: PersistentSheafLaplacian.hpp:700
void store_spectra_summary(std::vector< std::tuple< int, filtration_type, filtration_type, std::vector< spectra_type >>> spectra, std::string out_prefix)
Definition: PersistentSheafLaplacian.hpp:800
DenseMatrix_PL get_L(int dim, filtration_type a, filtration_type b)
Definition: PersistentSheafLaplacian.hpp:279
std::pair< std::vector< spectra_type >, DenseMatrix_PL > eigenpairs(int dim, filtration_type a, filtration_type b)
Definition: PersistentSheafLaplacian.hpp:572
std::vector< FilteredBoundaryMatrix< storage > > filtered_boundaries
Boundary matrix assuming real (or integer) coefficients.
Definition: PersistentSheafLaplacian.hpp:38
void set_eigs_algorithm_func(std::function< spectra_vec(DenseMatrix_PL &)> _eigs_algorithm_func)
Definition: PersistentSheafLaplacian.hpp:103
void get_L_top_dim_flipped(filtration_type a, SparseMatrix_PL &L)
Definition: PersistentSheafLaplacian.hpp:290
std::vector< std::tuple< int, filtration_type, filtration_type, std::vector< spectra_type > > > spectra()
Definition: PersistentSheafLaplacian.hpp:492
Profile profile
Profiler to track time usage of various steps.
Definition: PersistentSheafLaplacian.hpp:41
PersistentSheafLaplacian(sheaf_simplex_tree &sst)
Definition: PersistentSheafLaplacian.hpp:51
void print_boundaries()
Definition: PersistentSheafLaplacian.hpp:751
std::vector< std::tuple< int, filtration_type, filtration_type, std::vector< spectra_type > > > spectra(std::vector< std::tuple< int, filtration_type, filtration_type >> spectra_request_list)
Definition: PersistentSheafLaplacian.hpp:532
std::vector< spectra_type > spectra(int dim, filtration_type a, filtration_type b)
Definition: PersistentSheafLaplacian.hpp:392
void set_boundaries_filtrations(std::vector< Eigen::SparseMatrix< storage >> boundaries, std::vector< std::vector< filtration_type >> filtrations)
Definition: PersistentSheafLaplacian.hpp:130
bool verbose
Print progress if spectra() is called.
Definition: PersistentSheafLaplacian.hpp:39
~PersistentSheafLaplacian()
Definition: PersistentSheafLaplacian.hpp:97
void set_eigs_algorithm_func(std::string name)
Definition: PersistentSheafLaplacian.hpp:107
void store_L(int dim, filtration_type a, filtration_type b, std::string filename)
Definition: PersistentSheafLaplacian.hpp:734
std::vector< filtration_type > get_all_filtrations()
Definition: PersistentSheafLaplacian.hpp:909
DenseMatrix_PL get_up(int dim, filtration_type a, filtration_type b)
Definition: PersistentSheafLaplacian.hpp:337
void set_verbose(bool verbose)
Definition: PersistentSheafLaplacian.hpp:172
std::function< void(FilteredBoundaryMatrix< float > *, filtration_type, filtration_type, DenseMatrix_PL &)> up_algorithm_func
Definition: PersistentSheafLaplacian.hpp:45
Definition: sheaf_simplex_tree.hpp:21
std::vector< FilteredBoundaryMatrix< float > > apply_restriction_function()
Definition: sheaf_simplex_tree.hpp:54
Definition: Complex.cpp:26
spectra_vec BDCSVDEigen(DenseMatrix_PL &L)
Definition: eigs_algorithms.cpp:63
spectra_vec SelfAdjointEigenSparse(SparseMatrix_PL &L)
Definition: eigs_algorithms.cpp:32
spectra_vec EigensolverEigen(DenseMatrix_PL &L)
Definition: eigs_algorithms.cpp:55
spectra_vec SelfAdjointEigen(DenseMatrix_PL &L)
Definition: eigs_algorithms.cpp:14
std::pair< spectra_vec, DenseMatrix_spectra_PL > SelfAdjointEigenpairsEigen(DenseMatrix_PL &L)
Definition: eigs_algorithms.cpp:47
Definition: petls.hpp:38
void stop_sum_up_down()
Definition: petls.hpp:76
void start_sum_up_down()
Definition: petls.hpp:69
std::vector< int > L_rows
Definition: petls.hpp:56
std::vector< int > bettis
Definition: petls.hpp:57
void stop_all()
Definition: petls.hpp:71
void stop_L_up()
Definition: petls.hpp:74
void start_eigs()
Definition: petls.hpp:65
void start_L_down()
Definition: petls.hpp:68
std::vector< spectra_type > lambdas
Definition: petls.hpp:58
std::vector< int > dims
Definition: petls.hpp:40
void stop_L()
Definition: petls.hpp:73
std::vector< filtration_type > filtration_b
Definition: petls.hpp:42
std::vector< int > durations_L_down
Definition: petls.hpp:53
void stop_eigs()
Definition: petls.hpp:72
timer all
Definition: petls.hpp:43
void start_L_up()
Definition: petls.hpp:67
void start_L()
Definition: petls.hpp:66
std::vector< int > durations_eigs
Definition: petls.hpp:50
void to_csv(std::string filename)
Definition: petls.cpp:136
std::vector< filtration_type > filtration_a
Definition: petls.hpp:41
std::vector< int > durations_L_up
Definition: petls.hpp:52
void stop_L_down()
Definition: petls.hpp:75
void start_all()
Definition: petls.hpp:64
std::vector< int > durations_sum_up_down
Definition: petls.hpp:54
int duration
Definition: petls.hpp:27
Eigen::Matrix< coefficient_type, Eigen::Dynamic, Eigen::Dynamic > DenseMatrix_PL
Definition: typedefs.hpp:14
Eigen::Matrix< spectra_type, Eigen::Dynamic, 1 > spectra_vec
Definition: typedefs.hpp:17
Eigen::SparseMatrix< coefficient_type, Eigen::ColMajor > SparseMatrix_PL
Definition: typedefs.hpp:13
float spectra_type
Definition: typedefs.hpp:11
double filtration_type
Definition: typedefs.hpp:24