ArnoldiOp.h
Go to the documentation of this file.
1 // Copyright (C) 2018-2025 Yixuan Qiu <yixuan.qiu@cos.name>
2 //
3 // This Source Code Form is subject to the terms of the Mozilla
4 // Public License v. 2.0. If a copy of the MPL was not distributed
5 // with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
6 
7 #ifndef SPECTRA_ARNOLDI_OP_H
8 #define SPECTRA_ARNOLDI_OP_H
9 
10 #include <Eigen/Core>
11 #include <cmath> // std::sqrt
12 #include <complex> // std::real
13 
14 namespace Spectra {
15 
20 
26 
32 template <typename Scalar, typename OpType, typename BOpType>
33 class ArnoldiOp
34 {
35 private:
36  // The real part type of the matrix element
40 
41  const OpType& m_op;
42  const BOpType& m_Bop;
43  mutable Vector m_cache;
44 
45 public:
46  ArnoldiOp(const OpType& op, const BOpType& Bop) :
47  m_op(op), m_Bop(Bop), m_cache(op.rows())
48  {}
49 
50  // Move constructor
53  {
54  // We emulate the move constructor for Vector using Vector::swap()
55  m_cache.swap(other.m_cache);
56  }
57 
58  inline Index rows() const { return m_op.rows(); }
59 
60  // In generalized eigenvalue problem Ax=lambda*Bx, define the inner product to be <x, y> = (x^H)By.
61  // For regular eigenvalue problems, it is the usual inner product <x, y> = (x^H)y
62 
63  // Compute <x, y> = (x^H)By
64  // x and y are two vectors
65  template <typename Arg1, typename Arg2>
66  Scalar inner_product(const Arg1& x, const Arg2& y) const
67  {
68  m_Bop.perform_op(y.data(), m_cache.data());
69  return x.dot(m_cache);
70  }
71 
72  // Compute res = <X, y> = (X^H)By
73  // X is a matrix, y is a vector, res is a vector
74  template <typename Arg1, typename Arg2>
75  void adjoint_product(const Arg1& x, const Arg2& y, Eigen::Ref<Vector> res) const
76  {
77  m_Bop.perform_op(y.data(), m_cache.data());
78  res.noalias() = x.adjoint() * m_cache;
79  }
80 
81  // B-norm of a vector, ||x||_B = sqrt((x^H)Bx)
82  template <typename Arg>
83  RealScalar norm(const Arg& x) const
84  {
85  using std::sqrt;
86  using std::real;
87  return sqrt(real(inner_product<Arg, Arg>(x, x)));
88  }
89 
90  // The "A" operator to generate the Krylov subspace
91  inline void perform_op(const Scalar* x_in, Scalar* y_out) const
92  {
93  m_op.perform_op(x_in, y_out);
94  }
95 };
96 
103 {};
104 
110 template <typename Scalar, typename OpType>
111 class ArnoldiOp<Scalar, OpType, IdentityBOp>
112 {
113 private:
114  // The real part type of the matrix element
118 
119  const OpType& m_op;
120 
121 public:
122  ArnoldiOp(const OpType& op, const IdentityBOp& /*Bop*/) :
123  m_op(op)
124  {}
125 
126  inline Index rows() const { return m_op.rows(); }
127 
128  // Compute <x, y> = (x^H)y
129  // x and y are two vectors
130  template <typename Arg1, typename Arg2>
131  Scalar inner_product(const Arg1& x, const Arg2& y) const
132  {
133  return x.dot(y);
134  }
135 
136  // Compute res = <X, y> = (X^H)y
137  // X is a matrix, y is a vector, res is a vector
138  template <typename Arg1, typename Arg2>
139  void adjoint_product(const Arg1& x, const Arg2& y, Eigen::Ref<Vector> res) const
140  {
141  res.noalias() = x.adjoint() * y;
142  }
143 
144  // B-norm of a vector. For regular eigenvalue problems it is simply the L2 norm
145  template <typename Arg>
146  RealScalar norm(const Arg& x) const
147  {
148  return x.norm();
149  }
150 
151  // The "A" operator to generate the Krylov subspace
152  inline void perform_op(const Scalar* x_in, Scalar* y_out) const
153  {
154  m_op.perform_op(x_in, y_out);
155  }
156 };
157 
161 
162 } // namespace Spectra
163 
164 #endif // SPECTRA_ARNOLDI_OP_H
Eigen::PlainObjectBase::swap
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void swap(DenseBase< OtherDerived > &other)
Definition: PlainObjectBase.h:953
Spectra::ArnoldiOp< Scalar, OpType, IdentityBOp >::norm
RealScalar norm(const Arg &x) const
Definition: ArnoldiOp.h:146
x
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 x
Definition: gnuplot_common_settings.hh:12
real
float real
Definition: datatypes.h:10
Spectra::ArnoldiOp< Scalar, OpType, IdentityBOp >::ArnoldiOp
ArnoldiOp(const OpType &op, const IdentityBOp &)
Definition: ArnoldiOp.h:122
Spectra::ArnoldiOp< Scalar, OpType, IdentityBOp >::RealScalar
typename Eigen::NumTraits< Scalar >::Real RealScalar
Definition: ArnoldiOp.h:115
Spectra::ArnoldiOp< Scalar, OpType, IdentityBOp >::m_op
const OpType & m_op
Definition: ArnoldiOp.h:119
res
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
Spectra::ArnoldiOp::inner_product
Scalar inner_product(const Arg1 &x, const Arg2 &y) const
Definition: ArnoldiOp.h:66
Spectra::ArnoldiOp::ArnoldiOp
ArnoldiOp(const OpType &op, const BOpType &Bop)
Definition: ArnoldiOp.h:46
Spectra::ArnoldiOp::Index
Eigen::Index Index
Definition: ArnoldiOp.h:38
Spectra::ArnoldiOp::m_cache
Vector m_cache
Definition: ArnoldiOp.h:43
Spectra::IdentityBOp
Definition: ArnoldiOp.h:102
Spectra::ArnoldiOp::perform_op
void perform_op(const Scalar *x_in, Scalar *y_out) const
Definition: ArnoldiOp.h:91
Spectra::ArnoldiOp::m_Bop
const BOpType & m_Bop
Definition: ArnoldiOp.h:42
Spectra::ArnoldiOp::RealScalar
typename Eigen::NumTraits< Scalar >::Real RealScalar
Definition: ArnoldiOp.h:37
Spectra::ArnoldiOp< Scalar, OpType, IdentityBOp >::rows
Index rows() const
Definition: ArnoldiOp.h:126
Spectra::ArnoldiOp::m_op
const OpType & m_op
Definition: ArnoldiOp.h:41
Spectra::ArnoldiOp::rows
Index rows() const
Definition: ArnoldiOp.h:58
Spectra::ArnoldiOp
Definition: ArnoldiOp.h:33
Spectra::ArnoldiOp::norm
RealScalar norm(const Arg &x) const
Definition: ArnoldiOp.h:83
y
Scalar * y
Definition: level1_cplx_impl.h:124
Spectra::ArnoldiOp::ArnoldiOp
ArnoldiOp(ArnoldiOp &&other)
Definition: ArnoldiOp.h:51
Eigen::Ref
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:281
Spectra::ArnoldiOp< Scalar, OpType, IdentityBOp >::perform_op
void perform_op(const Scalar *x_in, Scalar *y_out) const
Definition: ArnoldiOp.h:152
Spectra::ArnoldiOp< Scalar, OpType, IdentityBOp >::Index
Eigen::Index Index
Definition: ArnoldiOp.h:116
Spectra
Definition: LOBPCGSolver.h:19
Eigen::PlainObjectBase::data
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Scalar * data() const
Definition: PlainObjectBase.h:247
Spectra::ArnoldiOp< Scalar, OpType, IdentityBOp >::inner_product
Scalar inner_product(const Arg1 &x, const Arg2 &y) const
Definition: ArnoldiOp.h:131
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 >
Spectra::ArnoldiOp::adjoint_product
void adjoint_product(const Arg1 &x, const Arg2 &y, Eigen::Ref< Vector > res) const
Definition: ArnoldiOp.h:75
ceres::sqrt
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition: jet.h:418
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
Eigen::GenericNumTraits::Real
T Real
Definition: NumTraits.h:164
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Spectra::ArnoldiOp< Scalar, OpType, IdentityBOp >::adjoint_product
void adjoint_product(const Arg1 &x, const Arg2 &y, Eigen::Ref< Vector > res) const
Definition: ArnoldiOp.h:139


gtsam
Author(s):
autogenerated on Sun Feb 16 2025 04:00:56