6 #include "../typedefs.hpp"
7 #include "../petls.hpp"
8 #include "../core/up_algorithms.hpp"
9 #include "../eigenvalues/eigs_algorithms.hpp"
12 #include <unsupported/Eigen/SparseExtra>
13 #include <Eigen/Dense>
31 using storage = float;
62 for (
auto fbm : filtered_boundaries_temp){
63 this->filtered_boundaries.push_back(fbm);
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();
82 std::vector<std::vector<filtration_type>> filtrations){
108 if (name ==
"selfadjoint"){
110 }
else if (name ==
"eigensolver"){
112 }
else if (name ==
"bdcsvd"){
131 std::vector<std::vector<filtration_type>> filtrations){
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;
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;
213 Eigen::SparseMatrix<storage> down(L.rows(),L.rows());
242 Eigen::SparseMatrix<storage> down(L.rows(), L.rows());
250 }
else if (down.size() == 0){
254 assert (L.cols() == down.cols() && L.rows() == down.rows() &&
"up and down Laplacians must have same dimensions");
261 L+= down.template cast<coefficient_type>();
291 Eigen::SparseMatrix<storage> B;
294 L = (B*B.transpose()).
template cast<coefficient_type>();
350 Eigen::SparseMatrix<storage> B;
352 L_down = B.transpose()*B;
370 Eigen::SparseMatrix<storage> L_down;
403 return std::vector<spectra_type>(betti0,0.0);
420 int zero_pad_length = L_rows - L_rows_flipped;
423 if (L_rows_flipped < L_rows){
432 eigenvalues.setZero(0);
436 return std::vector<spectra_type>();
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;
451 return std_eigs_flipped;
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));
473 eigenvalues.setZero(0);
475 return std::vector<spectra_type>();
484 std::vector<spectra_type> std_eigenvalues(eigenvalues.data(), eigenvalues.data() + eigenvalues.size());
485 return std_eigenvalues;
492 std::vector<std::tuple<int, filtration_type, filtration_type, std::vector<spectra_type>>>
spectra(){
498 std::vector<int> dims(
top_dim+1);
499 std::iota (std::begin(dims), std::end(dims), 0);
509 std::vector<std::tuple<int, filtration_type, filtration_type, std::vector<spectra_type>>> requested_spectra =
spectra(requests);
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];
524 return requested_spectra;
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){
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());
543 for (
unsigned long int i = 0; i < spectra_request_list.size(); i++){
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);
552 requested_spectra.push_back(std::make_tuple(dim, a, b,
spectra(dim, a, b)));
555 std::cout <<
"duration spectra for dim=" << dim <<
", a= " << a <<
", b=" << b <<
": " << this->profile.
all.
duration << std::endl;
557 return requested_spectra;
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);
606 eigenvalues.setZero(0);
618 std::vector<spectra_type> std_eigenvalues(eigenvalues.data(), eigenvalues.data() + eigenvalues.size());
629 std::vector<int> dims(
top_dim+1);
630 std::iota (std::begin(dims), std::end(dims), 0);
640 std::vector<std::tuple<int, filtration_type, filtration_type, std::vector<spectra_type>,
DenseMatrix_PL>> requested_spectra =
eigenpairs(requests);
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];
655 return requested_spectra;
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){
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());
674 for (
unsigned long int i = 0; i < spectra_request_list.size(); i++){
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);
684 std::vector<spectra_type> eigenvalues =
eigenpairs.first;
686 requested_spectra.push_back(std::make_tuple(dim, a, b, eigenvalues, eigenvectors));
689 std::cout <<
"duration spectra for dim=" << dim <<
", a= " << a <<
", b=" << b <<
": " << this->profile.
all.
duration << std::endl;
691 return requested_spectra;
703 int current_betti = 0;
705 for (
int k = 0; k < (int) eigenvalues.size(); k++){
706 if (eigenvalues[k] > tol){
707 return std::make_pair(current_betti, eigenvalues[k]);
712 if (current_betti == 0){
713 if ((
int) eigenvalues.size() > 0){
714 return std::make_pair(0,eigenvalues[0]);
716 return std::make_pair(0,0.0);
719 return std::make_pair(current_betti,0.0);
740 bool success = Eigen::saveMarket(L_sparse, filename);
742 std::cout <<
"saved matrix to file " << filename << std::endl;
744 std::cout <<
"failed saving matrix to file " << filename << std::endl;
752 for (
int i = 0; i <=
top_dim; i++){
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");
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] <<
" ";
784 out_streams[dim] << std::endl;
787 for (
int i = 0; i <=
top_dim; i++){
788 out_streams[i].close();
801 std::set<std::pair<filtration_type, filtration_type>> filtration_values_set;
804 for (
int i = 0 ; i < (int)
spectra.size(); i++){
807 filtration_values_set.insert(
808 std::make_pair(a,b));
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());
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;
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;
827 for (
int i = 0; i < (int)
spectra.size(); i++){
829 std::tuple<int, filtration_type, filtration_type, std::vector<spectra_type>> spectrum_record =
spectra[i];
830 int dim = std::get<0>(spectrum_record);
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);
841 int line_index = filtration_index_map[filtration_pair];
844 output_lines[line_index][2+dim] = betti;
845 output_lines[line_index][3+
top_dim+dim] = lambda;
849 std::ofstream outstream(
"./" + out_prefix +
"_spectra_summary.txt");
851 for (
int i = 0; i <=
top_dim; i++){
852 outstream <<
"\tbetti_" << i;
854 for (
int i = 0; i <=
top_dim; i++){
855 outstream <<
"\tlambda_" << i;
857 outstream << std::endl;
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];
865 outstream << std::endl;
882 std::vector<std::tuple<int, filtration_type, filtration_type>> requests;
883 std::tuple<int, filtration_type, filtration_type> spectra_request;
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);
896 a = filtrations[filtrations.size()-1];
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);
910 std::set<filtration_type> all_filtrations;
914 for (
unsigned long int i = 0; i < c0.size(); i++){
915 all_filtrations.insert(c0[i]);
918 std::vector<filtration_type> temp;
919 for (
unsigned long int dim = 1; dim <= (
unsigned long int)
top_dim; dim++){
921 for (
unsigned long int i = 0; i < temp.size(); i++){
922 all_filtrations.insert(temp[i]);
925 std::vector<filtration_type> all_filtrations_vector(all_filtrations.begin(), all_filtrations.end());
926 return all_filtrations_vector;
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};
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
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