ArnoldiOp.h
Go to the documentation of this file.
1 // Copyright (C) 2018-2019 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 ARNOLDI_OP_H
8 #define ARNOLDI_OP_H
9 
10 #include <Eigen/Core>
11 #include <cmath> // std::sqrt
12 
13 namespace Spectra {
14 
19 
25 
31 template <typename Scalar, typename OpType, typename BOpType>
32 class ArnoldiOp
33 {
34 private:
37 
38  OpType& m_op;
39  BOpType& m_Bop;
41 
42 public:
43  ArnoldiOp(OpType* op, BOpType* Bop) :
44  m_op(*op), m_Bop(*Bop), m_cache(op->rows())
45  {}
46 
47  inline Index rows() const { return m_op.rows(); }
48 
49  // In generalized eigenvalue problem Ax=lambda*Bx, define the inner product to be <x, y> = x'By.
50  // For regular eigenvalue problems, it is the usual inner product <x, y> = x'y
51 
52  // Compute <x, y> = x'By
53  // x and y are two vectors
54  template <typename Arg1, typename Arg2>
55  Scalar inner_product(const Arg1& x, const Arg2& y)
56  {
57  m_Bop.mat_prod(y.data(), m_cache.data());
58  return x.dot(m_cache);
59  }
60 
61  // Compute res = <X, y> = X'By
62  // X is a matrix, y is a vector, res is a vector
63  template <typename Arg1, typename Arg2>
64  void trans_product(const Arg1& x, const Arg2& y, Eigen::Ref<Vector> res)
65  {
66  m_Bop.mat_prod(y.data(), m_cache.data());
67  res.noalias() = x.transpose() * m_cache;
68  }
69 
70  // B-norm of a vector, ||x||_B = sqrt(x'Bx)
71  template <typename Arg>
72  Scalar norm(const Arg& x)
73  {
74  using std::sqrt;
75  return sqrt(inner_product<Arg, Arg>(x, x));
76  }
77 
78  // The "A" operator to generate the Krylov subspace
79  inline void perform_op(const Scalar* x_in, Scalar* y_out)
80  {
81  m_op.perform_op(x_in, y_out);
82  }
83 };
84 
91 {};
92 
98 template <typename Scalar, typename OpType>
99 class ArnoldiOp<Scalar, OpType, IdentityBOp>
100 {
101 private:
104 
105  OpType& m_op;
106 
107 public:
109  m_op(*op)
110  {}
111 
112  inline Index rows() const { return m_op.rows(); }
113 
114  // Compute <x, y> = x'y
115  // x and y are two vectors
116  template <typename Arg1, typename Arg2>
117  Scalar inner_product(const Arg1& x, const Arg2& y) const
118  {
119  return x.dot(y);
120  }
121 
122  // Compute res = <X, y> = X'y
123  // X is a matrix, y is a vector, res is a vector
124  template <typename Arg1, typename Arg2>
125  void trans_product(const Arg1& x, const Arg2& y, Eigen::Ref<Vector> res) const
126  {
127  res.noalias() = x.transpose() * y;
128  }
129 
130  // B-norm of a vector. For regular eigenvalue problems it is simply the L2 norm
131  template <typename Arg>
132  Scalar norm(const Arg& x)
133  {
134  return x.norm();
135  }
136 
137  // The "A" operator to generate the Krylov subspace
138  inline void perform_op(const Scalar* x_in, Scalar* y_out)
139  {
140  m_op.perform_op(x_in, y_out);
141  }
142 };
143 
147 
148 } // namespace Spectra
149 
150 #endif // ARNOLDI_OP_H
Spectra::ArnoldiOp::m_Bop
BOpType & m_Bop
Definition: ArnoldiOp.h:39
Spectra::ArnoldiOp::norm
Scalar norm(const Arg &x)
Definition: ArnoldiOp.h:72
Spectra::ArnoldiOp< Scalar, OpType, IdentityBOp >
Definition: ArnoldiOp.h:99
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
Spectra::ArnoldiOp::perform_op
void perform_op(const Scalar *x_in, Scalar *y_out)
Definition: ArnoldiOp.h:79
res
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
Spectra::ArnoldiOp::Vector
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Definition: ArnoldiOp.h:36
Spectra::ArnoldiOp::m_cache
Vector m_cache
Definition: ArnoldiOp.h:40
Spectra::ArnoldiOp< Scalar, OpType, IdentityBOp >::trans_product
void trans_product(const Arg1 &x, const Arg2 &y, Eigen::Ref< Vector > res) const
Definition: ArnoldiOp.h:125
Spectra::IdentityBOp
Definition: ArnoldiOp.h:90
Spectra::ArnoldiOp::ArnoldiOp
ArnoldiOp(OpType *op, BOpType *Bop)
Definition: ArnoldiOp.h:43
Spectra::ArnoldiOp::Index
Eigen::Index Index
Definition: ArnoldiOp.h:35
Spectra::ArnoldiOp::trans_product
void trans_product(const Arg1 &x, const Arg2 &y, Eigen::Ref< Vector > res)
Definition: ArnoldiOp.h:64
Spectra::ArnoldiOp< Scalar, OpType, IdentityBOp >::rows
Index rows() const
Definition: ArnoldiOp.h:112
Spectra::ArnoldiOp::inner_product
Scalar inner_product(const Arg1 &x, const Arg2 &y)
Definition: ArnoldiOp.h:55
Spectra::ArnoldiOp< Scalar, OpType, IdentityBOp >::Index
Eigen::Index Index
Definition: ArnoldiOp.h:102
Spectra::ArnoldiOp::rows
Index rows() const
Definition: ArnoldiOp.h:47
Spectra::ArnoldiOp
Definition: ArnoldiOp.h:32
Spectra::ArnoldiOp::m_op
OpType & m_op
Definition: ArnoldiOp.h:38
y
Scalar * y
Definition: level1_cplx_impl.h:124
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)
Definition: ArnoldiOp.h:138
Spectra::ArnoldiOp< Scalar, OpType, IdentityBOp >::m_op
OpType & m_op
Definition: ArnoldiOp.h:105
Spectra
Definition: LOBPCGSolver.h:19
Spectra::ArnoldiOp< Scalar, OpType, IdentityBOp >::norm
Scalar norm(const Arg &x)
Definition: ArnoldiOp.h:132
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:117
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 >
ceres::sqrt
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition: jet.h:418
Spectra::ArnoldiOp< Scalar, OpType, IdentityBOp >::Vector
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Definition: ArnoldiOp.h:103
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


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:11:06