iterative-solver 0.0
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
22template <class R, class Q, class Z, class W, bool = is_one_of<Z, R, Q>::value&& is_one_of<W, R, Q>::value,
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> {
28 static Matrix<double> _(const CVecRef<R>& left, const CVecRef<Q>& right, array::ArrayHandler<Z, W>& handler) {
29 return handler.gemm_inner(left, right);
30 }
31};
32
33template <class R, class Q, class Z, class W>
34struct Overlap<R, Q, Z, W, true, false, false> {
35 static Matrix<double> _(const CVecRef<R>& left, const CVecRef<Q>& right, array::ArrayHandler<Z, W>& handler) {
36 auto mat = handler.gemm_inner(right, left);
37 auto m = Matrix<double>({left.size(), right.size()});
38 transpose_copy(m, mat);
39 return m;
40 }
41};
42
43template <class R, class Q, class Z, class W>
45} // namespace detail
46
48template <class R, class Q, class Z, class W>
49auto overlap(const CVecRef<R>& left, const CVecRef<Q>& right, array::ArrayHandler<Z, W>& handler)
50 -> std::enable_if_t<detail::Z_and_W_are_one_of_R_and_Q<R, Q, Z, W>, Matrix<double>> {
51 return detail::Overlap<R, Q, Z, W>::_(left, right, handler);
52}
53
55template <class R>
57 auto m = Matrix<double>({params.size(), params.size()});
58 for (size_t i = 0; i < m.rows(); ++i)
59 for (size_t j = 0; j <= i; ++j)
60 m(i, j) = m(j, i) = handler.dot(params[i], params[j]);
61 return m;
62}
63
64template <typename T>
66 assert(mat.rows() == mat.cols() && "must be a square matrix");
67 for (size_t i = 0; i < mat.rows(); ++i)
68 for (size_t j = 0; j < i; ++j)
69 mat(i, j) = mat(j, i) = 0.5 * (mat(i, j) + mat(j, i));
70}
71
73template <typename T>
74typename Matrix<T>::coord_type max_element_index(const std::list<size_t>& rows, const std::list<size_t>& cols,
75 const Matrix<T>& mat) {
76 auto max_el = std::numeric_limits<T>::lowest();
77 auto ind = typename Matrix<T>::coord_type{0, 0};
78 for (auto i : rows) {
79 for (auto j : cols) {
80 if (mat(i, j) > max_el) {
81 max_el = mat(i, j);
82 ind = {i, j};
83 }
84 }
85 }
86 return ind;
87}
88
102template <typename Slice>
103std::vector<size_t> eye_order(const Slice& mat) {
104 auto dim = mat.dimensions();
105 auto rows = std::list<size_t>{};
106 auto cols = std::list<size_t>{};
107 for (size_t i = 0; i < dim.first; ++i) {
108 rows.emplace_back(i);
109 cols.emplace_back(i);
110 }
111 auto order = std::vector<size_t>(dim.first);
112 size_t i, j;
113 while (!rows.empty() && !cols.empty()) {
114 std::tie(i, j) = max_element_index(rows, cols, mat);
115 order.at(j) = i;
116 auto it_row = std::find(begin(rows), end(rows), i);
117 auto it_col = std::find(begin(cols), end(cols), j);
118 rows.erase(it_row);
119 cols.erase(it_col);
120 }
121 return order;
122}
123
124} // namespace molpro::linalg::itsolv::subspace::util
125
126#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:162
virtual value_type dot(const AL &x, const AR &y)=0
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:28
std::pair< size_t, size_t > coord_type
Definition: Matrix.h:36
index_type rows() const
Definition: Matrix.h:165
index_type cols() const
Definition: Matrix.h:166
constexpr bool Z_and_W_are_one_of_R_and_Q
Definition: util.h:44
Definition: gram_schmidt.h:7
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< double > >
Calculates overlap matrix between left and right vectors.
Definition: util.h:49
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:74
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:103
void matrix_symmetrize(Matrix< T > &mat)
Definition: util.h:65
void transpose_copy(ML &&ml, const MR &mr)
Definition: Matrix.h:281
std::vector< std::reference_wrapper< const A > > CVecRef
Definition: wrap.h:14
static Matrix< double > _(const CVecRef< R > &left, const CVecRef< Q > &right, array::ArrayHandler< Z, W > &handler)
Definition: util.h:28
static Matrix< double > _(const CVecRef< R > &left, const CVecRef< Q > &right, array::ArrayHandler< Z, W > &handler)
Definition: util.h:35
Checks that type T1 is same as one of T2, Ts ...
Definition: util.h:13
static constexpr bool value
Definition: util.h:14