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;
40  Vector m_cache;
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
Eigen::Index Index
Definition: ArnoldiOp.h:35
SCALAR Scalar
Definition: bench_gemm.cpp:33
Scalar * y
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Definition: ArnoldiOp.h:103
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar * data() const
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
Scalar inner_product(const Arg1 &x, const Arg2 &y)
Definition: ArnoldiOp.h:55
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
void trans_product(const Arg1 &x, const Arg2 &y, Eigen::Ref< Vector > res) const
Definition: ArnoldiOp.h:125
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
void perform_op(const Scalar *x_in, Scalar *y_out)
Definition: ArnoldiOp.h:138
ArnoldiOp(OpType *op, BOpType *Bop)
Definition: ArnoldiOp.h:43
Index rows() const
Definition: ArnoldiOp.h:47
Scalar norm(const Arg &x)
Definition: ArnoldiOp.h:72
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:192
BOpType & m_Bop
Definition: ArnoldiOp.h:39
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Definition: ArnoldiOp.h:36
void trans_product(const Arg1 &x, const Arg2 &y, Eigen::Ref< Vector > res)
Definition: ArnoldiOp.h:64
void perform_op(const Scalar *x_in, Scalar *y_out)
Definition: ArnoldiOp.h:79
Scalar inner_product(const Arg1 &x, const Arg2 &y) const
Definition: ArnoldiOp.h:117
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


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:37