Xped
Loading...
Searching...
No Matches
PlainInterface_Eigen_Array_impl.hpp
Go to the documentation of this file.
1#ifndef PLAIN_INTERFACE_EIGEN_IMPL_H_
2#define PLAIN_INTERFACE_EIGEN_IMPL_H_
3
4#include <Eigen/Core>
5#include <Eigen/SVD>
6#include <unsupported/Eigen/CXX11/Tensor>
7
8namespace Xped {
9
10struct PlainInterface : public MatrixInterface, public TensorInterface, public VectorInterface
11{
12 using Indextype = Eigen::Index;
23
30
38
39 template <std::size_t Rank, typename Derived, typename Scalar>
40 static void set_block_from_tensor(Eigen::MatrixBase<Derived>& M,
41 const Indextype& row,
42 const Indextype& col,
43 const Indextype& rows,
44 const Indextype& cols,
45 const Scalar& scale,
46 const TType<Scalar, Rank>& T)
47 {
48 M.block(row, col, rows, cols) = scale * Eigen::Map<cMType<Scalar>>(T.data(), rows, cols);
49 }
50
51 template <std::size_t Rank, typename Derived, typename Scalar>
52 static void add_to_block_from_tensor(Eigen::MatrixBase<Derived>& M,
53 const Indextype& row,
54 const Indextype& col,
55 const Indextype& rows,
56 const Indextype& cols,
57 const Scalar& scale,
58 const TType<Scalar, Rank>& T)
59 {
60 M.block(row, col, rows, cols) += scale * Eigen::Map<cMType<Scalar>>(T.data(), rows, cols);
61 }
62
63 template <std::size_t Rank, typename Derived, typename Scalar>
64 static void set_block_from_tensor(Eigen::MatrixBase<Derived>&& M,
65 const Indextype& row,
66 const Indextype& col,
67 const Indextype& rows,
68 const Indextype& cols,
69 const Scalar& scale,
70 const TType<Scalar, Rank>& T)
71 {
72 M.block(row, col, rows, cols) = scale * Eigen::Map<cMType<Scalar>>(T.data(), rows, cols);
73 }
74 template <std::size_t Rank, typename Derived, typename Scalar>
75 static void add_to_block_from_tensor(Eigen::MatrixBase<Derived>&& M,
76 const Indextype& row,
77 const Indextype& col,
78 const Indextype& rows,
79 const Indextype& cols,
80 const Scalar& scale,
81 const TType<Scalar, Rank>& T)
82 {
83 M.block(row, col, rows, cols) += scale * Eigen::Map<cMType<Scalar>>(T.data(), rows, cols);
84 }
85
86 template <std::size_t Rank, typename Derived>
87 static TType<typename Derived::Scalar, Rank> tensor_from_matrix_block(const Eigen::MatrixBase<Derived>& M,
88 const Indextype& row,
89 const Indextype& col,
90 const Indextype& rows,
91 const Indextype& cols,
92 const std::array<Indextype, Rank>& dims)
93 {
94 MType<typename Derived::Scalar> submatrix = M.block(row, col, rows, cols);
95 nda::dim<-9, -9, 1> first_dim;
96 first_dim.set_extent(dims[0]);
97 std::array<nda::dim<-9, -9, -9>, Rank - 1> shape_data;
98 for(std::size_t i = 1; i < Rank; i++) {
99 shape_data[i - 1].set_extent(dims[i]);
100 shape_data[i - 1].set_stride(std::accumulate(dims.begin(), dims.begin() + i, 1ul, std::multiplies<typename Derived::Scalar>()));
101 }
102 auto dims_tuple = std::tuple_cat(std::make_tuple(first_dim), TensorInterface::as_tuple(shape_data));
103
104 nda::dense_shape<Rank> block_shape(dims_tuple);
105
106 cMapTType<typename Derived::Scalar, Rank> tensorview(submatrix.data(), block_shape);
107
108 // cMapTType<Scalar, Rank> tensorview = cMap(submatrix.data(), dims);
109 return construct<typename Derived::Scalar, Rank>(tensorview);
110 }
111
112 template <typename Derived>
113 static void diagonal_head_matrix_to_vector(VType<typename Derived::Scalar>& V, const Eigen::MatrixBase<Derived>& M, const Indextype& n_elems)
114 {
115 V = M.diagonal().head(n_elems);
116 }
117
118 template <typename Derived>
119 static std::tuple<MType<typename Derived::Scalar>, VType<typename Derived::Scalar>, MType<typename Derived::Scalar>>
120 svd(const Eigen::MatrixBase<Derived>& M)
121 {
122#ifdef XPED_DONT_USE_BDCSVD
123 Eigen::JacobiSVD<MType<typename Derived::Scalar>> Jack; // standard SVD
124#else
125 Eigen::BDCSVD<MType<typename Derived::Scalar>> Jack; // "Divide and conquer" SVD (only available in Eigen)
126#endif
127
128 Jack.compute(M, Eigen::ComputeThinU | Eigen::ComputeThinV);
129 return std::make_tuple(Jack.matrixU(), Jack.singularValues(), Jack.matrixV().adjoint());
130 }
131
132 template <typename Scalar>
134 {
135 return V.matrix().asDiagonal();
136 }
137};
138
139} // namespace Xped
140
141#endif
Definition: bench.cpp:62
CTF::Matrix< Scalar > MType
Definition: MatrixInterface_Cyclops_impl.cpp:13
CTF::Vector< Scalar > VType
Definition: PlainInterface_Cyclops_impl.cpp:33
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 > 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 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 MType< typename ctf_traits< MT1 >::Scalar > difference(MT1 &&M1, MT2 &&M2)
Definition: MatrixInterface_Cyclops_impl.cpp:219
static MIndextype rows(const MType< Scalar > &M)
Definition: MatrixInterface_Cyclops_impl.cpp:84
static MType< Scalar > construct(const MIndextype &rows, const MIndextype &cols, CTF::World &world)
Definition: MatrixInterface_Cyclops_impl.cpp:26
static MType< typename ctf_traits< MT1 >::Scalar > add(MT1 &&M1, MT2 &&M2)
Definition: MatrixInterface_Cyclops_impl.cpp:210
static void scale(MType< Scalar > &M, const Scalar &val)
Definition: MatrixInterface_Cyclops_impl.cpp:228
CTF::Matrix< Scalar > MType
Definition: MatrixInterface_Cyclops_impl.hpp:40
static void set_block_from_tensor(Eigen::MatrixBase< Derived > &M, const Indextype &row, const Indextype &col, const Indextype &rows, const Indextype &cols, const Scalar &scale, const TType< Scalar, Rank > &T)
Definition: PlainInterface_Eigen_Array_impl.hpp:40
int Indextype
Definition: PlainInterface_Cyclops_impl.hpp:11
static std::tuple< MType< typename Derived::Scalar >, VType< typename Derived::Scalar >, MType< typename Derived::Scalar > > svd(const Eigen::MatrixBase< Derived > &M)
Definition: PlainInterface_Eigen_Array_impl.hpp:120
static void set_block_from_tensor(Eigen::MatrixBase< Derived > &&M, const Indextype &row, const Indextype &col, const Indextype &rows, const Indextype &cols, const Scalar &scale, const TType< Scalar, Rank > &T)
Definition: PlainInterface_Eigen_Array_impl.hpp:64
static void diagonal_head_matrix_to_vector(VType< typename Derived::Scalar > &V, const Eigen::MatrixBase< Derived > &M, const Indextype &n_elems)
Definition: PlainInterface_Eigen_Array_impl.hpp:113
static void add_to_block_from_tensor(Eigen::MatrixBase< Derived > &M, const Indextype &row, const Indextype &col, const Indextype &rows, const Indextype &cols, const Scalar &scale, const TType< Scalar, Rank > &T)
Definition: PlainInterface_Eigen_Array_impl.hpp:52
static void add_to_block_from_tensor(Eigen::MatrixBase< Derived > &&M, const Indextype &row, const Indextype &col, const Indextype &rows, const Indextype &cols, const Scalar &scale, const TType< Scalar, Rank > &T)
Definition: PlainInterface_Eigen_Array_impl.hpp:75
static void scale(MType< Scalar > &M, const Scalar &val)
Definition: MatrixInterface_Cyclops_impl.cpp:228
static MType< Scalar > vec_to_diagmat(const VType< Scalar > &V)
Definition: PlainInterface_Eigen_Array_impl.hpp:133
static TType< typename Derived::Scalar, Rank > tensor_from_matrix_block(const Eigen::MatrixBase< Derived > &M, const Indextype &row, const Indextype &col, const Indextype &rows, const Indextype &cols, const std::array< Indextype, Rank > &dims)
Definition: PlainInterface_Eigen_Array_impl.hpp:87
nda::dense_array< Scalar, Rank > TType
Definition: TensorInterface_Array_impl.hpp:44
static nda::internal::tuple_of_n< element_t, N > as_tuple(std::array< element_t, N > const &arr, std::index_sequence< Is... >)
Definition: TensorInterface_Array_impl.hpp:31
static void setZero(TType< Scalar, Rank > &T)
Definition: TensorInterface_Array_impl.hpp:95
static void setConstant(TType< Scalar, Rank > &T, const Scalar &val)
Definition: TensorInterface_Array_impl.hpp:107
static std::string print(const TType< Scalar, Rank > &T)
Definition: TensorInterface_Array_impl.hpp:389
static Scalar getVal(const TType< Scalar, Rank > &T, const std::array< Indextype, Rank > &index)
Definition: TensorInterface_Array_impl.hpp:119
static TType< Scalar, Rank > construct(const std::array< Indextype, Rank > &dims, mpi::XpedWorld &world=mpi::getUniverse())
Definition: TensorInterface_Array_impl.hpp:58
static void setRandom(TType< Scalar, Rank > &T)
Definition: TensorInterface_Array_impl.hpp:101
nda::const_dense_array_ref< Scalar, Rank > cMapTType
Definition: TensorInterface_Array_impl.hpp:51
static void setZero(VType< Scalar > &V)
Definition: VectorInterface_Cyclops_impl.cpp:34
static VType< Scalar > construct(const VIndextype &elems, CTF::World &world)
Definition: VectorInterface_Cyclops_impl.cpp:15
static VType< Scalar > construct_with_zero(const VIndextype &elems, CTF::World &world)
Definition: VectorInterface_Cyclops_impl.cpp:21
static void setRandom(VType< Scalar > &V)
Definition: VectorInterface_Cyclops_impl.cpp:40
static std::string print(const VType< Scalar > &V)
Definition: VectorInterface_Cyclops_impl.cpp:92
static void setConstant(VType< Scalar > &V, const Scalar &val)
Definition: VectorInterface_Cyclops_impl.cpp:46
static VType< Scalar > scale(VT1 &&V, const Scalar &val)
Definition: VectorInterface_Cyclops_impl.cpp:75
CTF::Vector< Scalar > VType
Definition: VectorInterface_Cyclops_impl.hpp:12