PETLS
up_algorithms.hpp
Go to the documentation of this file.
1 #ifndef up_algs_H
2 #define up_algs_H
3 
4 #include "../typedefs.hpp"
6 // #include <Eigen/IterativeLinearSolvers>
7 // #include <unsupported/Eigen/SparseExtra>
8 #include <Eigen/Cholesky>
9 #include <Eigen/QR>
10 #include <chrono>
11 // #include "Eigen/Eigenvalues"
12 // #include "Eigen/SVD"
13 // #include "../petls.hpp"
14 namespace petls{
15 
16  // wrapper classes for algorithms to compute up Laplacian.
17  // just need to implement the following signature:
18  // void operator()(FilteredBoundaryMatrix<storage>* fbm, filtration_type a, filtration_type b, DenseMatrix_PL &L_up);
19 
20  template <typename storage>
22  // using storage = float;
23  int a_row_index = fbm->index_of_filtration(false,a);
24  int b_row_index = fbm->index_of_filtration(false,b);
25  int b_col_index = fbm->index_of_filtration(true,b);
26 
27  if (a_row_index == b_row_index){
28  Eigen::SparseMatrix<storage> B_pers(b_row_index, b_col_index);
29  fbm->submatrix_at_filtration(b, B_pers);
30  L_up = (B_pers*B_pers.transpose()).template cast<coefficient_type>();
31  return;
32  } else if (a_row_index == -1){ // no rows, return 0x0 empty matrix
33  L_up.setZero(0,0);
34  return;
35 
36  } else if (b_col_index == -1){ // no columns, return nxn 0-matrix for n = number of cells in C_{up_dim}^a
37  L_up.setZero(a_row_index+1,a_row_index+1);
38  return;
39 
40  }
41  // NOTE: doing the self adjoint lower triangular trick here is slower
42  // possibly because of the conversions between dense and sparse
43 
44  Eigen::SparseMatrix<storage> B_pers_int(b_row_index, b_col_index);
45  fbm->submatrix_at_filtration(b, B_pers_int);
46 
47 
48  Eigen::SparseMatrix<storage> L_up_b_int(B_pers_int.rows(), B_pers_int.rows());
49  // L_up_b_int = B_pers_int*B_pers_int.transpose();
50  L_up_b_int.template selfadjointView<Eigen::Lower>().rankUpdate(B_pers_int);
51  SparseMatrix_PL L_up_b(L_up_b_int.rows(), L_up_b_int.cols());
52  L_up_b = L_up_b_int.template cast<coefficient_type>();
53 
54  int a_rows = a_row_index + 1;
55  int b_rows = b_row_index + 1;
56 
57 
58 
59  // auto start_solve = std::chrono::high_resolution_clock::now();
60  // split L_up_b into 4 along those borders
61  // A B
62  // C D
63  // and compute A - B*D^{-1}*C
64  // by the structure of L, we know B = C^T
65  // and we want A - B * (things)
66  // Also compute D^{-1}*C by solving the sparse lower-triangular linear system
67 
68  SparseMatrix_PL A = L_up_b.topLeftCorner(a_rows, a_rows);
69  SparseMatrix_PL C = L_up_b.bottomLeftCorner(b_rows-a_rows, a_rows);
70  SparseMatrix_PL B = C.transpose();
71  SparseMatrix_PL D = L_up_b.bottomRightCorner(b_rows-a_rows, b_rows-a_rows);
72  // D.makeCompressed();
73  // Eigen::LeastSquaresConjugateGradient<SparseMatrix_PL> solver(D);
74  // Eigen::BiCGSTAB<SparseMatrix_PL> solver(D);
75  // solver.setMaxIterations(1000);
76 
77  // Eigen::SparseQR<SparseMatrix_PL, Eigen::COLAMDOrdering<int>> solver(D);
78  // Eigen::ConjugateGradient<SparseMatrix_PL, Eigen::Lower> solver(D);
79  // solver.setTolerance(1e-4)
80  // L_up = A - B * solver.solve(C);
81 
82  L_up = A - B * DenseMatrix_PL(D).ldlt().solve(DenseMatrix_PL(C));
83 
84  // The computed matrix is only correct in the lower triangular portion.
85  // Eigen::SelfAdjointEigensolver is okay with this, but to avoid confusion and issues with other eigensolvers,
86  // We symmetrize the matrix:
87  L_up = DenseMatrix_PL(L_up.selfadjointView<Eigen::Lower>());
88 
89 
90  // auto end_solve = std::chrono::high_resolution_clock::now();
91  // auto duration_solve = std::chrono::duration_cast<std::chrono::milliseconds>(end_solve - start_solve);
92  // std::cout << "duration solve (ms):" << duration_solve.count() << std::endl;
93 
94  return;
95  }
96 
97 
98  // inline void schur_algorithm(petls::FilteredBoundaryMatrix<int>* fbm, filtration_type a, filtration_type b, DenseMatrix_PL &L_up){
99  // using storage = int;
100  // int a_row_index = fbm->index_of_filtration(false,a);
101  // int b_row_index = fbm->index_of_filtration(false,b);
102  // int b_col_index = fbm->index_of_filtration(true,b);
103 
104  // if (a_row_index == b_row_index){
105  // Eigen::SparseMatrix<storage> B_pers(b_row_index, b_col_index);
106  // fbm->submatrix_at_filtration(b, B_pers);
107  // L_up = (B_pers*B_pers.transpose()).template cast<coefficient_type>();
108  // return;
109  // } else if (a_row_index == -1){ // no rows, return 0x0 empty matrix
110  // L_up.setZero(0,0);
111  // return;
112 
113  // } else if (b_col_index == -1){ // no columns, return nxn 0-matrix for n = number of cells in C_{up_dim}^a
114  // L_up.setZero(a_row_index+1,a_row_index+1);
115  // return;
116 
117  // }
118  // // NOTE: doing the self adjoint lower triangular trick here is slower
119  // // possibly because of the conversions between dense and sparse
120 
121  // Eigen::SparseMatrix<storage> B_pers_int(b_row_index, b_col_index);
122  // fbm->submatrix_at_filtration(b, B_pers_int);
123 
124 
125  // Eigen::SparseMatrix<storage> L_up_b_int(B_pers_int.rows(), B_pers_int.rows());
126  // // L_up_b_int = B_pers_int*B_pers_int.transpose();
127  // L_up_b_int.template selfadjointView<Eigen::Lower>().rankUpdate(B_pers_int);
128  // SparseMatrix_PL L_up_b(L_up_b_int.rows(), L_up_b_int.cols());
129  // L_up_b = L_up_b_int.template cast<coefficient_type>();
130 
131  // int a_rows = a_row_index + 1;
132  // int b_rows = b_row_index + 1;
133 
134 
135 
136  // // auto start_solve = std::chrono::high_resolution_clock::now();
137  // // split L_up_b into 4 along those borders
138  // // A B
139  // // C D
140  // // and compute A - B*D^{-1}*C
141  // // by the structure of L, we know B = C^T
142  // // and we want A - B * (things)
143  // // Also compute D^{-1}*C by solving the sparse lower-triangular linear system
144 
145  // SparseMatrix_PL A = L_up_b.topLeftCorner(a_rows, a_rows);
146  // SparseMatrix_PL C = L_up_b.bottomLeftCorner(b_rows-a_rows, a_rows);
147  // SparseMatrix_PL B = C.transpose();
148  // SparseMatrix_PL D = L_up_b.bottomRightCorner(b_rows-a_rows, b_rows-a_rows);
149  // // D.makeCompressed();
150  // // Eigen::LeastSquaresConjugateGradient<SparseMatrix_PL> solver(D);
151  // // Eigen::BiCGSTAB<SparseMatrix_PL> solver(D);
152  // // solver.setMaxIterations(1000);
153 
154  // // Eigen::SparseQR<SparseMatrix_PL, Eigen::COLAMDOrdering<int>> solver(D);
155  // // Eigen::ConjugateGradient<SparseMatrix_PL, Eigen::Lower> solver(D);
156  // // solver.setTolerance(1e-4)
157  // // L_up = A - B * solver.solve(C);
158 
159  // L_up = A - B * DenseMatrix_PL(D).ldlt().solve(DenseMatrix_PL(C));
160 
161  // // The computed matrix is only correct in the lower triangular portion.
162  // // Eigen::SelfAdjointEigensolver is okay with this, but to avoid confusion and issues with other eigensolvers,
163  // // We symmetrize the matrix:
164  // L_up = DenseMatrix_PL(L_up.selfadjointView<Eigen::Lower>());
165 
166 
167  // // auto end_solve = std::chrono::high_resolution_clock::now();
168  // // auto duration_solve = std::chrono::duration_cast<std::chrono::milliseconds>(end_solve - start_solve);
169  // // std::cout << "duration solve (ms):" << duration_solve.count() << std::endl;
170 
171  // return;
172  // }
173 
174 }
175 
176 #endif
Definition: FilteredBoundaryMatrix.hpp:15
void submatrix_at_filtration(filtration_type a, Eigen::SparseMatrix< FBMcoeff, Eigen::ColMajor > &M)
Definition: FilteredBoundaryMatrix.hpp:101
int index_of_filtration(bool use_domain_filtrations, filtration_type a)
Definition: FilteredBoundaryMatrix.hpp:78
Definition: Complex.cpp:26
void schur_algorithm(petls::FilteredBoundaryMatrix< storage > *fbm, filtration_type a, filtration_type b, DenseMatrix_PL &L_up)
Definition: up_algorithms.cpp:17
Eigen::Matrix< coefficient_type, Eigen::Dynamic, Eigen::Dynamic > DenseMatrix_PL
Definition: typedefs.hpp:14
Eigen::SparseMatrix< coefficient_type, Eigen::ColMajor > SparseMatrix_PL
Definition: typedefs.hpp:13
double filtration_type
Definition: typedefs.hpp:24