iterative-solver 0.0
 
Loading...
Searching...
No Matches
util.h
1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_SUBSPACE_UTIL_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_SUBSPACE_UTIL_H
3#include <limits>
4#include <molpro/linalg/itsolv/ArrayHandlers.h>
5#include <molpro/linalg/itsolv/subspace/Matrix.h>
6#include <molpro/linalg/itsolv/wrap.h>
7
9namespace detail {
10
12template <typename T1, typename T2, typename... Ts>
13struct is_one_of {
14 static constexpr bool value = std::is_same<T1, T2>::value || is_one_of<T1, Ts...>::value;
15};
16
17template <typename T1, typename T2>
18struct is_one_of<T1, T2> {
19 static constexpr bool value = std::is_same<T1, T2>::value;
20};
21
23 bool = std::is_same<R, Z>::value, bool = std::is_same<W, Q>::value>
24struct Overlap {};
25
26template <class R, class Q, class Z, class W>
27struct Overlap<R, Q, Z, W, true, true, true> {
30 return handler.gemm_inner(left, right);
31 }
32};
33
34template <class R, class Q, class Z, class W>
35struct Overlap<R, Q, Z, W, true, false, false> {
38 // gemm_inner(right, left)(i, j) = <right_i|left_j>, and <left_j|right_i> is its conjugate
39 auto mat = handler.gemm_inner(right, left);
40 auto m = Matrix<value_type>({left.size(), right.size()});
42 return m;
43 }
44};
45
46template <class R, class Q, class Z, class W>
48} // namespace detail
49
51template <class R, class Q, class Z, class W>
53 -> std::enable_if_t<detail::Z_and_W_are_one_of_R_and_Q<R, Q, Z, W>,
56}
57
59template <class R>
62 auto m = Matrix<typename array::ArrayHandler<R, R>::value_type>({params.size(), params.size()});
63 for (size_t i = 0; i < m.rows(); ++i)
64 for (size_t j = 0; j <= i; ++j) {
65 m(i, j) = handler.dot(params[i], params[j]);
66 m(j, i) = conjugate(m(i, j)); // the matrix is hermitian, which for a real element type is symmetric
67 }
68 return m;
69}
70
72template <typename T>
74 assert(mat.rows() == mat.cols() && "must be a square matrix");
75 for (size_t i = 0; i < mat.rows(); ++i)
76 for (size_t j = 0; j < i; ++j) {
77 mat(i, j) = T(0.5) * (mat(i, j) + conjugate(mat(j, i)));
78 mat(j, i) = conjugate(mat(i, j));
79 }
80}
81
83template <typename T>
84typename Matrix<T>::coord_type max_element_index(const std::list<size_t>& rows, const std::list<size_t>& cols,
85 const Matrix<T>& mat) {
86 auto max_el = std::numeric_limits<T>::lowest();
87 auto ind = typename Matrix<T>::coord_type{0, 0};
88 for (auto i : rows) {
89 for (auto j : cols) {
90 if (mat(i, j) > max_el) {
91 max_el = mat(i, j);
92 ind = {i, j};
93 }
94 }
95 }
96 return ind;
97}
98
112template <typename Slice>
113std::vector<size_t> eye_order(const Slice& mat) {
114 auto dim = mat.dimensions();
115 auto rows = std::list<size_t>{};
116 auto cols = std::list<size_t>{};
117 for (size_t i = 0; i < dim.first; ++i) {
118 rows.emplace_back(i);
119 cols.emplace_back(i);
120 }
121 auto order = std::vector<size_t>(dim.first);
122 size_t i, j;
123 while (!rows.empty() && !cols.empty()) {
124 std::tie(i, j) = max_element_index(rows, cols, mat);
125 order.at(j) = i;
126 auto it_row = std::find(begin(rows), end(rows), i);
127 auto it_col = std::find(begin(cols), end(cols), j);
128 rows.erase(it_row);
129 cols.erase(it_col);
130 }
131 return order;
132}
133
134} // namespace molpro::linalg::itsolv::subspace::util
135
136#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_SUBSPACE_UTIL_H
Enhances various operations between pairs of arrays and allows dynamic code injection with uniform in...
Definition ArrayHandler.h:163
virtual value_type dot(const AL &x, const AR &y)=0
The hermitian inner product <x|y>: conjugate-linear in x, linear in y.
decltype(value_type_L{} *value_type_R{}) value_type
Definition ArrayHandler.h:182
virtual Matrix< value_type > gemm_inner(const CVecRef< AL > &xx, const CVecRef< AR > &yy)=0
Matrix container that allows simple data access, slicing, copying and resizing without loosing data.
Definition Matrix.h:32
std::pair< size_t, size_t > coord_type
Definition Matrix.h:40
index_type rows() const
Definition Matrix.h:169
index_type cols() const
Definition Matrix.h:170
constexpr bool Z_and_W_are_one_of_R_and_Q
Definition util.h:47
Definition gram_schmidt.h:8
Matrix< T >::coord_type max_element_index(const std::list< size_t > &rows, const std::list< size_t > &cols, const Matrix< T > &mat)
Return maximum element in a matrix along specified rows and columns.
Definition util.h:84
std::vector< size_t > eye_order(const Slice &mat)
Returns order of rows in a matrix slice that brings it closest to identity.
Definition util.h:113
auto overlap(const CVecRef< R > &left, const CVecRef< Q > &right, array::ArrayHandler< Z, W > &handler) -> std::enable_if_t< detail::Z_and_W_are_one_of_R_and_Q< R, Q, Z, W >, Matrix< typename array::ArrayHandler< Z, W >::value_type > >
Calculates overlap matrix between left and right vectors.
Definition util.h:52
void matrix_symmetrize(Matrix< T > &mat)
Replaces a matrix by its hermitian part, which for a real element type is its symmetric part.
Definition util.h:73
void conjugate_transpose_copy(ML &&ml, const MR &mr)
Copies the conjugate transpose of mr into ml.
Definition Matrix.h:299
std::unique_ptr< LinearEigensystem< R, Q, P > > create_LinearEigensystem(const LinearEigensystemOptions &options, const std::shared_ptr< ArrayHandlers< R, Q, P > > &handlers=std::make_shared< molpro::linalg::itsolv::ArrayHandlers< R, Q, P > >())
Definition SolverFactory.h:111
std::vector< std::reference_wrapper< const A > > CVecRef
Definition wrap.h:14
T conjugate(const T &x)
Complex conjugate, staying within the scalar type; the identity for a real type.
Definition scalar_traits.h:47
typename array::ArrayHandler< Z, W >::value_type value_type
Definition util.h:28
static Matrix< value_type > _(const CVecRef< R > &left, const CVecRef< Q > &right, array::ArrayHandler< Z, W > &handler)
Definition util.h:29
typename array::ArrayHandler< Z, W >::value_type value_type
Definition util.h:36
static Matrix< value_type > _(const CVecRef< R > &left, const CVecRef< Q > &right, array::ArrayHandler< Z, W > &handler)
Definition util.h:37
Checks that type T1 is same as one of T2, Ts ...
Definition util.h:13
static constexpr bool value
Definition util.h:14