Xped
Loading...
Searching...
No Matches
MatrixInterface_Eigen_impl.hpp
Go to the documentation of this file.
1#ifndef MATRIX_INTERFACE_EIGEN_IMPL_H_
2#define MATRIX_INTERFACE_EIGEN_IMPL_H_
3
4#include <random>
5
6#include "Eigen/Core"
7#include "Eigen/Dense"
8#include <unsupported/Eigen/MatrixFunctions>
9
10#include "Xped/Util/Mpi.hpp"
11
12namespace Xped {
13
14struct MatrixInterface
15{
16 // typedefs
17 template <typename Scalar>
18 using MType = Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>;
19 template <typename Scalar>
20 using cMType = const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>;
21
22 template <typename Scalar>
23 using MapMType = Eigen::Map<MType<Scalar>>;
24 template <typename Scalar>
25 using cMapMType = Eigen::Map<cMType<Scalar>>;
26
27 typedef Eigen::Index MIndextype;
28 // constructors
29 template <typename Scalar>
31
32 template <typename Scalar>
34
35 template <typename Scalar>
36 static void resize(MType<Scalar>& M, const MIndextype& new_rows, const MIndextype& new_cols);
37
38 // initialization
39 template <typename Derived>
40 static void setZero(Eigen::MatrixBase<Derived>& M);
41
42 template <typename Derived>
43 static void setZero(Eigen::MatrixBase<Derived>&& M);
44
45 template <typename Derived>
46 static void setRandom(Eigen::MatrixBase<Derived>& M, std::mt19937& engine);
47
48 template <typename Derived>
49 static void setRandom(Eigen::MatrixBase<Derived>&& M, std::mt19937& engine);
50
51 template <typename Derived>
52 static void setIdentity(Eigen::MatrixBase<Derived>& M);
53
54 template <typename Derived>
55 static void setIdentity(Eigen::MatrixBase<Derived>&& M);
56
57 template <typename Derived>
58 static void setConstant(Eigen::MatrixBase<Derived>& M, const typename Derived::Scalar& val);
59
60 template <typename Derived>
61 static void setConstant(Eigen::MatrixBase<Derived>&& M, const typename Derived::Scalar& val);
62
63 template <typename Derived>
64 static void setVal(Eigen::DenseBase<Derived>& M, const MIndextype& row, const MIndextype& col, typename Derived::Scalar val);
65
66 template <typename Scalar>
67 static MType<Scalar> Identity(const MIndextype& rows, const MIndextype& cols, const mpi::XpedWorld&);
68
69 // shape
70 template <typename Derived>
71 static MIndextype rows(const Eigen::DenseBase<Derived>& M);
72
73 template <typename Derived>
74 static MIndextype cols(const Eigen::DenseBase<Derived>& M);
75
76 template <typename Derived>
77 static typename Derived::Scalar getVal(const Eigen::DenseBase<Derived>& M, const MIndextype& row, const MIndextype& col);
78
79 // raw data
80 template <typename Scalar>
81 static const Scalar* get_raw_data(const Eigen::Matrix<Scalar, -1, -1>& M)
82 {
83 return M.data();
84 }
85
86 template <typename Scalar>
87 static Scalar* get_raw_data(Eigen::Matrix<Scalar, -1, -1>& M)
88 {
89 return M.data();
90 }
91
92 // reduction
93 template <typename Derived>
94 static typename Derived::Scalar trace(const Eigen::MatrixBase<Derived>& M);
95
96 template <typename Derived>
97 static typename Derived::RealScalar maxNorm(const Eigen::MatrixBase<Derived>& M);
98
99 template <typename Derived>
100 static typename Derived::RealScalar maxCoeff(const Eigen::MatrixBase<Derived>& M, MIndextype& maxrow, MIndextype& maxcol);
101
102 // artithmetic
103 template <typename DerivedL, typename DerivedR>
105 const Eigen::MatrixBase<DerivedR>& M2);
106
107 template <typename DerivedL, typename DerivedR>
109 const Eigen::MatrixBase<DerivedR>& M2);
110
111 template <typename Scalar, typename MatrixExpr1, typename MatrixExpr2, typename MatrixExpr3, typename MatrixExprRes>
112 static void optimal_prod(const Scalar& scale, const MatrixExpr1& M1, const MatrixExpr2& M2, const MatrixExpr3& M3, MatrixExprRes& Mres);
113
114 template <typename Scalar, typename MatrixExpr1, typename MatrixExpr2, typename MatrixExpr3, typename MatrixExprRes>
115 static void optimal_prod_add(const Scalar& scale, const MatrixExpr1& M1, const MatrixExpr2& M2, const MatrixExpr3& M3, MatrixExprRes& Mres);
116
117 template <typename DerivedL, typename DerivedR>
118 // static const Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<typename Derived::Scalar, typename Derived::Scalar>,
119 // const Eigen::PlainObjectBase<Derived>,
120 // const Eigen::PlainObjectBase<Derived>>
121 static auto add(const Eigen::MatrixBase<DerivedL>& M1, const Eigen::MatrixBase<DerivedR>& M2)
122 {
123 return (M1 + M2);
124 }
125
126 template <typename DerivedL, typename DerivedR>
127 // static const Eigen::CwiseBinaryOp<Eigen::internal::scalar_difference_op<typename Derived::Scalar, typename Derived::Scalar>,
128 // const Eigen::PlainObjectBase<Derived>,
129 // const Eigen::PlainObjectBase<Derived>>
130 static auto difference(const Eigen::MatrixBase<DerivedL>& M1, const Eigen::MatrixBase<DerivedR>& M2)
131 {
132 return (M1 - M2);
133 }
134
135 template <typename Derived>
136 static void scale(Eigen::MatrixBase<Derived>& M, const typename Derived::Scalar& val);
137
138 template <typename Scalar, typename Derived>
139 static auto diagUnaryFunc(const Eigen::MatrixBase<Derived>& M, const std::function<Scalar(Scalar)>& func)
140 {
141 return M.diagonal().unaryExpr(func).asDiagonal();
142 }
143
144 template <typename Scalar, typename Derived>
145 static auto unaryFunc(const Eigen::MatrixBase<Derived>& M, const std::function<Scalar(typename Derived::Scalar)>& func)
146 {
147 return M.unaryExpr(func);
148 }
149
150 template <typename Scalar, typename Derived, typename OtherDerived>
151 static auto diagBinaryFunc(const Eigen::MatrixBase<Derived>& M_left,
152 const Eigen::MatrixBase<OtherDerived>& M_right,
153 const std::function<Scalar(Scalar, Scalar)>& func)
154 {
155 return M_left.diagonal().binaryExpr(M_right.diagonal(), func).asDiagonal();
156 }
157
158 template <typename Scalar, typename Derived, typename OtherDerived>
159 static auto binaryFunc(const Eigen::MatrixBase<Derived>& M_left,
160 const Eigen::MatrixBase<OtherDerived>& M_right,
161 const std::function<Scalar(Scalar, Scalar)>& func)
162 {
163 return M_left.binaryExpr(M_right, func);
164 }
165
166 template <typename Scalar, typename Derived>
167 static auto msqrt(const Eigen::MatrixBase<Derived>& M)
168 {
169 return M.sqrt();
170 }
171
172 template <typename Derived, typename Scalar>
173 static auto mexp(const Eigen::MatrixBase<Derived>& M, Scalar factor)
174 {
175 return (factor * M).exp();
176 }
177
178 template <typename Derived>
179 static auto adjoint(const Eigen::MatrixBase<Derived>& M)
180 {
181 return M.adjoint();
182 }
183
184 // block
185 template <typename Derived>
186 static auto
187 block(const Eigen::MatrixBase<Derived>& M, const MIndextype& row_off, const MIndextype& col_off, const MIndextype& rows, const MIndextype& cols)
188 {
189 return M.block(row_off, col_off, rows, cols);
190 }
191
192 template <typename Derived>
193 static auto
194 block(Eigen::MatrixBase<Derived>&& M, const MIndextype& row_off, const MIndextype& col_off, const MIndextype& rows, const MIndextype& cols)
195 {
196 return M.block(row_off, col_off, rows, cols);
197 }
198
199 template <typename Derived>
200 static auto
201 block(Eigen::MatrixBase<Derived>& M, const MIndextype& row_off, const MIndextype& col_off, const MIndextype& rows, const MIndextype& cols)
202 {
203 return M.block(row_off, col_off, rows, cols);
204 }
205
206 template <typename Derived>
207 static void add_to_block(Eigen::MatrixBase<Derived>& M1,
208 const MIndextype& row_off,
209 const MIndextype& col_off,
210 const MIndextype& rows,
211 const MIndextype& cols,
212 const Eigen::MatrixBase<Derived>& M2);
213
214 template <typename Derived>
215 static void set_block(Eigen::MatrixBase<Derived>& M1,
216 const MIndextype& row_off,
217 const MIndextype& col_off,
218 const MIndextype& rows,
219 const MIndextype& cols,
220 const Eigen::MatrixBase<Derived>& M2);
221
222 template <typename Derived>
223 static void add_to_block(Eigen::MatrixBase<Derived>&& M1,
224 const MIndextype& row_off,
225 const MIndextype& col_off,
226 const MIndextype& rows,
227 const MIndextype& cols,
228 const Eigen::MatrixBase<Derived>& M2);
229
230 template <typename Derived>
231 static void set_block(Eigen::MatrixBase<Derived>&& M1,
232 const MIndextype& row_off,
233 const MIndextype& col_off,
234 const MIndextype& rows,
235 const MIndextype& cols,
236 const Eigen::MatrixBase<Derived>& M2);
237
238 template <typename Derived>
239 static std::pair<MType<typename Derived::Scalar>, MType<typename Derived::Scalar>> eigh(const Eigen::MatrixBase<Derived>& M);
240
241 template <typename Derived>
242 static std::pair<MType<typename Derived::Scalar>, MType<typename Derived::Scalar>> qr(const Eigen::MatrixBase<Derived>& M);
243
244 template <typename Derived>
245 static std::string print(const Eigen::DenseBase<Derived>& M);
246};
247
248} // namespace Xped
249#ifndef XPED_COMPILED_LIB
251#endif
252
253#endif
Definition: bench.cpp:62
int MIndextype
Definition: MatrixInterface_Cyclops_impl.cpp:22
CTF::Matrix< Scalar > MType
Definition: MatrixInterface_Cyclops_impl.cpp:13
CTF::Matrix< Scalar > MapMType
Definition: MatrixInterface_Cyclops_impl.hpp:45
static auto unaryFunc(const Eigen::MatrixBase< Derived > &M, const std::function< Scalar(typename Derived::Scalar)> &func)
Definition: MatrixInterface_Eigen_impl.hpp:145
static void set_block(Eigen::MatrixBase< Derived > &M1, const MIndextype &row_off, const MIndextype &col_off, const MIndextype &rows, const MIndextype &cols, const Eigen::MatrixBase< Derived > &M2)
Definition: MatrixInterface_Eigen_impl.cpp:217
static auto binaryFunc(const Eigen::MatrixBase< Derived > &M_left, const Eigen::MatrixBase< OtherDerived > &M_right, const std::function< Scalar(Scalar, Scalar)> &func)
Definition: MatrixInterface_Eigen_impl.hpp:159
static auto block(const Eigen::MatrixBase< Derived > &M, const MIndextype &row_off, const MIndextype &col_off, const MIndextype &rows, const MIndextype &cols)
Definition: MatrixInterface_Eigen_impl.hpp:187
static void setZero(MType< Scalar > &M)
Definition: MatrixInterface_Cyclops_impl.cpp:47
static void setConstant(MType< Scalar > &M, const Scalar &val)
Definition: MatrixInterface_Cyclops_impl.cpp:65
static MType< Scalar > Identity(const MIndextype &rows, const MIndextype &cols, CTF::World &world)
Definition: MatrixInterface_Cyclops_impl.cpp:71
static MType< Scalar > construct_with_zero(const MIndextype &rows, const MIndextype &cols, CTF::World &world)
Definition: MatrixInterface_Cyclops_impl.cpp:32
static void print(MT &&M)
Definition: MatrixInterface_Cyclops_impl.cpp:343
static auto adjoint(const Eigen::MatrixBase< Derived > &M)
Definition: MatrixInterface_Eigen_impl.hpp:179
static ctf_traits< MT >::Scalar maxNorm(MT &&M)
Definition: MatrixInterface_Cyclops_impl.cpp:104
static auto block(Eigen::MatrixBase< Derived > &&M, const MIndextype &row_off, const MIndextype &col_off, const MIndextype &rows, const MIndextype &cols)
Definition: MatrixInterface_Eigen_impl.hpp:194
static auto add(const Eigen::MatrixBase< DerivedL > &M1, const Eigen::MatrixBase< DerivedR > &M2)
Definition: MatrixInterface_Eigen_impl.hpp:121
static Scalar getVal(const MType< Scalar > &M, const MIndextype &row, const MIndextype &col)
Definition: MatrixInterface_Cyclops_impl.cpp:136
static void setRandom(MType< Scalar > &M)
Definition: MatrixInterface_Cyclops_impl.cpp:53
static MIndextype cols(const MType< Scalar > &M)
Definition: MatrixInterface_Cyclops_impl.cpp:90
static void resize(MType< Scalar > &M, const MIndextype &new_rows, const MIndextype &new_cols)
static MIndextype rows(const MType< Scalar > &M)
Definition: MatrixInterface_Cyclops_impl.cpp:84
static void optimal_prod(const Scalar &scale, MatrixExpr1 &&M1, MatrixExpr2 &&M2, MatrixExpr3 &&M3, MatrixExprRes &Mres)
Definition: MatrixInterface_Cyclops_impl.cpp:176
static auto difference(const Eigen::MatrixBase< DerivedL > &M1, const Eigen::MatrixBase< DerivedR > &M2)
Definition: MatrixInterface_Eigen_impl.hpp:130
static auto mexp(const Eigen::MatrixBase< Derived > &M, Scalar factor)
Definition: MatrixInterface_Eigen_impl.hpp:173
static MType< Scalar > construct(const MIndextype &rows, const MIndextype &cols, CTF::World &world)
Definition: MatrixInterface_Cyclops_impl.cpp:26
static std::pair< MType< typename ctf_traits< MT >::Scalar >, MType< typename ctf_traits< MT >::Scalar > > qr(MT &&M)
Definition: MatrixInterface_Cyclops_impl.cpp:316
Eigen::Index MIndextype
Definition: MatrixInterface_Eigen_impl.hpp:27
static auto msqrt(const Eigen::MatrixBase< Derived > &M)
Definition: MatrixInterface_Eigen_impl.hpp:167
const CTF::Matrix< Scalar > cMapMType
Definition: MatrixInterface_Cyclops_impl.hpp:47
static void optimal_prod_add(const Scalar &scale, MatrixExpr1 &&M1, MatrixExpr2 &&M2, MatrixExpr3 &&M3, MatrixExprRes &Mres)
Definition: MatrixInterface_Cyclops_impl.cpp:193
static MType< typename ctf_traits< MT1 >::Scalar > prod(MT1 &&M1, MT2 &&M2)
Definition: MatrixInterface_Cyclops_impl.cpp:167
static MType< typename ctf_traits< MT1 >::Scalar > kronecker_prod(MT1 &&M1, MT2 &&M2)
Definition: MatrixInterface_Cyclops_impl.cpp:149
static void add_to_block(MType< Scalar > &M1, const MIndextype &row_off, const MIndextype &col_off, const MIndextype &rows, const MIndextype &cols, const MType< Scalar > &M2)
Definition: MatrixInterface_Cyclops_impl.cpp:324
static void setIdentity(MType< Scalar > &M)
Definition: MatrixInterface_Cyclops_impl.cpp:59
const CTF::Matrix< Scalar > cMType
Definition: MatrixInterface_Cyclops_impl.hpp:42
int MIndextype
Definition: MatrixInterface_Cyclops_impl.hpp:49
static std::pair< MType< typename ctf_traits< MT >::Scalar >, MType< typename ctf_traits< MT >::Scalar > > eigh(MT &&M)
Definition: MatrixInterface_Cyclops_impl.cpp:308
static auto diagBinaryFunc(const Eigen::MatrixBase< Derived > &M_left, const Eigen::MatrixBase< OtherDerived > &M_right, const std::function< Scalar(Scalar, Scalar)> &func)
Definition: MatrixInterface_Eigen_impl.hpp:151
static const Scalar * get_raw_data(const Eigen::Matrix< Scalar, -1, -1 > &M)
Definition: MatrixInterface_Eigen_impl.hpp:81
static auto block(Eigen::MatrixBase< Derived > &M, const MIndextype &row_off, const MIndextype &col_off, const MIndextype &rows, const MIndextype &cols)
Definition: MatrixInterface_Eigen_impl.hpp:201
static ctf_traits< MT >::Scalar trace(MT &&M)
Definition: MatrixInterface_Cyclops_impl.cpp:97
static void setVal(MType< Scalar > &M, const MIndextype row, const MIndextype col, const Scalar &val)
static void scale(MType< Scalar > &M, const Scalar &val)
Definition: MatrixInterface_Cyclops_impl.cpp:228
static Scalar * get_raw_data(Eigen::Matrix< Scalar, -1, -1 > &M)
Definition: MatrixInterface_Eigen_impl.hpp:87
CTF::Matrix< Scalar > MType
Definition: MatrixInterface_Cyclops_impl.hpp:40
static auto diagUnaryFunc(const Eigen::MatrixBase< Derived > &M, const std::function< Scalar(Scalar)> &func)
Definition: MatrixInterface_Eigen_impl.hpp:139
static ctf_traits< MT >::Scalar maxCoeff(MT &&M, MIndextype &maxrow, MIndextype &maxcol)
Definition: MatrixInterface_Cyclops_impl.cpp:111
Definition: Mpi.hpp:34