iterative-solver 0.0
select_max_dot.h
1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_UTIL_SELECT_MAX_DOT_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_UTIL_SELECT_MAX_DOT_H
3#include <cmath>
4#include <cstdlib>
5#include <queue>
6
8
21template <class X, class Y, typename value_type, typename value_type_abs>
22auto select_max_dot(size_t n, const X& x, const Y& y) {
23 using std::abs;
24 using std::begin;
25 using std::end;
26 using std::greater;
27 using select_pair = std::pair<value_type_abs, size_t>; // value and index
28 auto selection = std::priority_queue<select_pair, std::vector<select_pair>, greater<select_pair>>();
29 auto ix = begin(x);
30 auto iy = begin(y);
31 for (size_t i = 0; i < n; ++i, ++ix, ++iy) {
32 selection.emplace(abs((*ix) * (*iy)), i);
33 }
34 for (size_t i = n; i < std::min(x.size(), y.size()); ++i, ++ix, ++iy) {
35 selection.emplace(abs((*ix) * (*iy)), i);
36 selection.pop();
37 }
38 auto selection_map = std::map<size_t, value_type_abs>();
39 auto m = selection.size();
40 for (size_t i = 0; i < m; ++i) {
41 selection_map.emplace(selection.top().second, selection.top().first);
42 selection.pop();
43 }
44 return selection_map;
45}
46
59template <class X, class Y, typename value_type, typename value_type_abs>
60auto select_max_dot_iter_sparse(size_t n, const X& x, const Y& y) {
61 using std::abs;
62 using std::begin;
63 using std::end;
64 using std::greater;
65 using select_pair = std::pair<value_type_abs, size_t>; // value and index
66 auto selection = std::priority_queue<select_pair, std::vector<select_pair>, greater<select_pair>>();
67 auto iy = begin(y);
68 for (size_t i = 0; i < n; ++i, ++iy) {
69 if (iy->first < x.size())
70 selection.emplace(abs(x[iy->first] * iy->second), iy->first);
71 }
72 for (auto jy = iy; jy != end(y); ++jy) {
73 if (jy->first < x.size()) {
74 selection.emplace(abs(x[jy->first] * jy->second), jy->first);
75 selection.pop();
76 }
77 }
78 auto selection_map = std::map<size_t, value_type_abs>();
79 auto m = selection.size();
80 for (size_t i = 0; i < m; ++i) {
81 selection_map.emplace(selection.top().second, selection.top().first);
82 selection.pop();
83 }
84 return selection_map;
85}
86
99template <class X, class Y, typename value_type, typename value_type_abs>
100auto select_max_dot_sparse(size_t n, const X& x, const Y& y) {
101 using std::abs;
102 using std::begin;
103 using std::end;
104 using std::greater;
105 using select_pair = std::pair<value_type_abs, size_t>; // value and index
106 auto selection = std::priority_queue<select_pair, std::vector<select_pair>, greater<select_pair>>();
107 auto ix = begin(x);
108 auto iy = begin(y);
109 while (selection.size() < n && ix != end(x)) {
110 iy = y.find(ix->first);
111 if (iy != y.end())
112 selection.emplace(abs(ix->second * iy->second), ix->first);
113 ++ix;
114 }
115 while (ix != end(x)) {
116 iy = y.find(ix->first);
117 if (iy != y.end()) {
118 selection.emplace(abs(ix->second * iy->second), ix->first);
119 selection.pop();
120 }
121 ++ix;
122 }
123 auto selection_map = std::map<size_t, value_type_abs>();
124 auto m = selection.size();
125 for (size_t i = 0; i < m; ++i) {
126 selection_map.emplace(selection.top().second, selection.top().first);
127 selection.pop();
128 }
129 return selection_map;
130}
131} // namespace molpro::linalg::array::util
132
133#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_UTIL_SELECT_MAX_DOT_H
auto begin(Span< T > &x)
Definition: Span.h:84
auto end(Span< T > &x)
Definition: Span.h:94
Definition: ArrayHandler.h:23
auto select_max_dot(size_t n, const X &x, const Y &y)
Select n indices with largest by absolute value contributions to the dot product.
Definition: select_max_dot.h:22
auto select_max_dot_iter_sparse(size_t n, const X &x, const Y &y)
Select n indices with largest by absolute value contributions to the dot product.
Definition: select_max_dot.h:60
auto select_max_dot_sparse(size_t n, const X &x, const Y &y)
Select n indices with largest by absolute value contributions to the dot product.
Definition: select_max_dot.h:100