eigs.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2023 Alec Jacobson <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #ifndef IGL_SPECTRA_EIGS_H
  9. #define IGL_SPECTRA_EIGS_H
  10. #include "../igl_inline.h"
  11. #include "../eigs.h"
  12. #include <Eigen/Core>
  13. #include <Eigen/Sparse>
  14. #include <Eigen/SparseLU>
  15. namespace igl
  16. {
  17. namespace spectra
  18. {
  19. /// Act like MATLAB's eigs function. Compute the first/last k eigen pairs of
  20. /// the generalized eigen value problem:
  21. ///
  22. /// A u = s B u
  23. ///
  24. /// Solutions are approximate and sorted.
  25. ///
  26. /// Ideally one should use ARPACK and the Eigen unsupported ARPACK module.
  27. /// This implementation does simple, naive power iterations.
  28. ///
  29. /// @param[in] A #A by #A symmetric matrix
  30. /// @param[in] B #A by #A symmetric positive-definite matrix
  31. /// @param[in] k number of eigen pairs to compute
  32. /// @param[in] type whether to extract from the high or low end
  33. /// @param[out] sU #A by k list of sorted eigen vectors (descending)
  34. /// @param[out] sS k list of sorted eigen values (descending)
  35. ///
  36. /// \bug only the 'sm' small magnitude eigen values are well supported
  37. ///
  38. template <
  39. typename EigsScalar,
  40. typename DerivedU,
  41. typename DerivedS,
  42. typename Solver = Eigen::SparseLU<Eigen::SparseMatrix<EigsScalar>> >
  43. IGL_INLINE bool eigs(
  44. const Eigen::SparseMatrix<EigsScalar> & A,
  45. const Eigen::SparseMatrix<EigsScalar> & B,
  46. const int k,
  47. const igl::EigsType type,
  48. Eigen::PlainObjectBase<DerivedU> & U,
  49. Eigen::PlainObjectBase<DerivedS> & S);
  50. /// \overload
  51. /// @param[in] sigma shift to apply to A, as in A ← A + sigma B
  52. template <
  53. typename EigsScalar,
  54. typename DerivedU,
  55. typename DerivedS,
  56. typename Solver = Eigen::SparseLU<Eigen::SparseMatrix<EigsScalar>> >
  57. IGL_INLINE bool eigs(
  58. const Eigen::SparseMatrix<EigsScalar> & A,
  59. const Eigen::SparseMatrix<EigsScalar> & B,
  60. const int k,
  61. const EigsScalar sigma,
  62. Eigen::PlainObjectBase<DerivedU> & U,
  63. Eigen::PlainObjectBase<DerivedS> & S);
  64. }
  65. }
  66. #ifndef IGL_STATIC_LIBRARY
  67. #include "eigs.cpp"
  68. #endif
  69. #endif