12 #include <Eigen/SparseCore> 13 #include <Eigen/Eigenvalues> 15 #include <Eigen/SparseCholesky> 17 #include "../SymGEigsSolver.h" 82 template <
typename Scalar =
long double>
111 SparseMatrix& true_BM,
bool has_true_BM =
false)
115 if (has_true_BM ==
false)
136 m_info = chol_MBM.
info();
137 return chol_MBM.info();
140 SparseComplexMatrix Upper_MBM = chol_MBM.matrixU().template cast<Complex>();
141 ComplexVector D_MBM_vec = chol_MBM.vectorD().template cast<Complex>();
143 D_MBM_vec = D_MBM_vec.cwiseSqrt();
145 for (
int i = 0;
i < D_MBM_vec.rows();
i++)
147 D_MBM_vec(
i) =
Complex(1.0, 0.0) / D_MBM_vec(
i);
150 SparseComplexMatrix D_MBM_mat(D_MBM_vec.asDiagonal());
152 SparseComplexMatrix U_inv(Upper_MBM.
rows(), Upper_MBM.
cols());
154 Upper_MBM.template triangularView<Eigen::Upper>().solveInPlace(U_inv);
156 SparseComplexMatrix right_product = U_inv * D_MBM_mat;
157 M = M * right_product.
real();
207 Matrix
D, Matrix
E, Matrix
F,
208 Matrix
G, Matrix
H, Matrix
I)
224 void sort_epairs(Vector& evalues, Matrix& evectors,
int SelectionRule)
226 std::function<bool(Scalar, Scalar)> cmp;
228 cmp = std::less<Scalar>{};
230 cmp = std::greater<Scalar>{};
232 std::map<Scalar, Vector, decltype(cmp)> epairs(cmp);
233 for (
int i = 0;
i < m_evectors.
cols(); ++
i)
234 epairs.insert(std::make_pair(evalues(
i), evectors.col(
i)));
237 for (
auto& epair : epairs)
239 evectors.col(i) = epair.second;
240 evalues(i) = epair.first;
248 SparseMatrix new_matrix(matrix.
cols(), matrix.
cols() -
int(colToRemove.size()));
250 std::vector<Eigen::Triplet<Scalar>> tripletList;
251 tripletList.reserve(matrix.
cols() -
int(colToRemove.size()));
253 for (
int iRow = 0; iRow < matrix.
cols(); iRow++)
255 if (std::find(colToRemove.begin(), colToRemove.end(), iRow) == colToRemove.end())
263 matrix = matrix * new_matrix;
269 int BlockSize =
m_nev;
272 for (
int iCol = 0; iCol <
m_nev; iCol++)
275 for (
int iRow = 0; iRow <
m_n; iRow++)
277 buffer = m_residuals.
coeff(iRow, iCol);
278 sum += buffer * buffer;
281 if (
sqrt(sum) < tolerance_L2)
284 columnsToDelete.push_back(iCol);
295 flag_with_constraints(false),
297 flag_with_preconditioner(false),
302 throw std::invalid_argument(
"Wrong size");
311 flag_with_constraints =
true;
317 throw std::invalid_argument(
"Wrong size");
324 m_preconditioner = preconditioner;
325 flag_with_preconditioner =
true;
332 int max_iter =
std::min(m_n, maxit);
334 SparseMatrix directions, AX, AR, BX, AD, ADD, DD, BDD, BD, XAD, RAD, DAD, XBD, RBD,
BR, sparse_eVecX, sparse_eVecR, sparse_eVecD, inverse_matrix;
335 Matrix XAR, RAR, XBR, gramA, gramB, eVecX, eVecR, eVecD;
336 std::vector<int> columnsToDelete;
338 if (flag_with_constraints)
358 m_info = eigs.
info();
366 sparse_eVecX = m_evectors.sparseView();
368 X = X * sparse_eVecX;
369 AX = AX * sparse_eVecX;
370 BX = BX * sparse_eVecX;
373 for (
int iter_num = 0; iter_num < max_iter; iter_num++)
375 m_residuals.
resize(m_n, m_nev);
389 if (columnsToDelete.size() > 0)
398 columnsToDelete.clear();
401 if (flag_with_preconditioner)
407 if (flag_with_constraints)
430 XAR =
Matrix(X.transpose() * AR);
431 RAR =
Matrix(m_residuals.transpose() * AR);
436 XAD = X.transpose() * AD;
437 RAD = m_residuals.transpose() * AD;
439 XBD = X.transpose() * BD;
440 RBD = m_residuals.transpose() * BD;
443 gramB =
stack_9_matricies(Matrix::Identity(m_nev, m_nev), XBR, XBD, XBR.transpose(), Matrix::Identity(BlockSize, BlockSize), RBD, XBD.
transpose(), RBD.
transpose(), Matrix::Identity(BlockSize, BlockSize));
448 gramB =
stack_4_matricies(Matrix::Identity(m_nev, m_nev), XBR, XBR.transpose(), Matrix::Identity(BlockSize, BlockSize));
457 geigs(&Aop, &Bop, m_nev,
std::min(10,
int(gramA.
rows()) - 1));
460 int nconv = geigs.compute();
465 m_evalues = geigs.eigenvalues();
466 m_evectors = geigs.eigenvectors();
472 m_info = geigs.info();
479 eVecX = m_evectors.block(0, 0, m_nev, m_nev);
480 eVecR = m_evectors.block(m_nev, 0, BlockSize, m_nev);
481 eVecD = m_evectors.block(m_nev + BlockSize, 0, BlockSize, m_nev);
483 sparse_eVecX = eVecX.sparseView();
484 sparse_eVecR = eVecR.sparseView();
485 sparse_eVecD = eVecD.sparseView();
487 DD = m_residuals * sparse_eVecR;
488 ADD = AR * sparse_eVecR;
489 BDD = BR * sparse_eVecR;
491 DD = DD + directions * sparse_eVecD;
492 ADD = ADD + AD * sparse_eVecD;
493 BDD = BDD + BD * sparse_eVecD;
497 eVecX = m_evectors.block(0, 0, m_nev, m_nev);
498 eVecR = m_evectors.block(m_nev, 0, BlockSize, m_nev);
500 sparse_eVecX = eVecX.sparseView();
501 sparse_eVecR = eVecR.sparseView();
503 DD = m_residuals * sparse_eVecR;
504 ADD = AR * sparse_eVecR;
505 BDD = BR * sparse_eVecR;
508 X = X * sparse_eVecX + DD;
509 AX = AX * sparse_eVecX + ADD;
510 BX = BX * sparse_eVecX + BDD;
519 m_residuals.
resize(m_n, m_nev);
544 return Matrix(m_residuals);
552 #endif // LOBPCG_SOLVER EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index cols() const
Eigen::Matrix< Complex, Eigen::Dynamic, 1 > ComplexVector
void setPreconditioner(const SparseMatrix &preconditioner)
Matrix< RealScalar, Dynamic, Dynamic > M
Scalar coeff(Index row, Index col) const
EigenvectorsType eigenvectors() const
Returns the eigenvectors of given matrix.
JacobiRotation< float > G
void resize(Index rows, Index cols)
Using Cholesky decomposition to solve generalized eigenvalues.
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rows() const
bool flag_with_constraints
void removeColumns(SparseMatrix &matrix, std::vector< int > &colToRemove)
bool flag_with_preconditioner
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
LOBPCGSolver(const SparseMatrix &A, const SparseMatrix X)
void setB(const SparseMatrix &B)
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics set mxtics default set mytics default set mx2tics default set my2tics default set xtics border mirror norotate autofreq set ytics border mirror norotate autofreq set ztics border nomirror norotate autofreq set nox2tics set noy2tics set timestamp bottom norotate set rrange[*:*] noreverse nowriteback set trange[*:*] noreverse nowriteback set urange[*:*] noreverse nowriteback set vrange[*:*] noreverse nowriteback set xlabel matrix size set x2label set timefmt d m y n H
A direct sparse LDLT Cholesky factorizations without square root.
Matrix stack_9_matricies(Matrix A, Matrix B, Matrix C, Matrix D, Matrix E, Matrix F, Matrix G, Matrix H, Matrix I)
ComputationInfo info() const
Computation was successful.
Select eigenvalues with smallest algebraic value. Only for symmetric eigen solvers.
std::complex< Scalar > Complex
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > Matrix
int orthogonalizeInPlace(SparseMatrix &M, SparseMatrix &B, SparseMatrix &true_BM, bool has_true_BM=false)
BearingRangeFactor< Pose, Point2 > BR
EIGEN_DEVICE_FUNC RealReturnType real() const
TransposeReturnType transpose()
Array< double, 1, 3 > e(1./3., 0.5, 2.)
const mpreal sum(const mpreal tab[], const unsigned long int n, int &status, mp_rnd_t mode=mpreal::get_default_rnd())
EIGEN_DEVICE_FUNC ColXpr col(Index i)
ComputationInfo info() const
Reports whether previous computation was successful.
void compute(int maxit=10, Scalar tol_div_n=1e-7)
Eigen::SparseMatrix< Scalar > SparseMatrix
Matrix< Scalar, Dynamic, Dynamic > C
A small structure to hold a non zero as a triplet (i,j,value).
void sort_epairs(Vector &evalues, Matrix &evectors, int SelectionRule)
void setFromTriplets(const InputIterators &begin, const InputIterators &end)
void applyConstraintsInPlace(SparseMatrix &X, SparseMatrix &Y, SparseMatrix &B)
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Eigen::SparseMatrix< Complex > SparseComplexMatrix
Eigen::Matrix< Complex, Eigen::Dynamic, Eigen::Dynamic > ComplexMatrix
SparseMatrix m_preconditioner
Computes eigenvalues and eigenvectors of general matrices.
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
void setConstraints(const SparseMatrix &Y)
const EigenvalueType & eigenvalues() const
Returns the eigenvalues of given matrix.
int checkConvergence_getBlocksize(SparseMatrix &m_residuals, Scalar tolerance_L2, std::vector< int > &columnsToDelete)
Matrix stack_4_matricies(Matrix A, Matrix B, Matrix C, Matrix D)