iterative-solver 0.0
ArrayHandlerSparse.h
1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_ARRAYHANDLERSPARSE_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_ARRAYHANDLERSPARSE_H
3#include <molpro/linalg/array/ArrayHandler.h>
4#include <molpro/linalg/array/util/gemm.h>
5#include <molpro/linalg/array/util/select.h>
6#include <molpro/linalg/array/util/select_max_dot.h>
7
10
11namespace molpro::linalg::array {
12
16template <typename AL, typename AR = AL>
17class ArrayHandlerSparse : public ArrayHandler<AL, AR> {
18public:
24
25 AL copy(const AR &source) override { return AL{source.begin(), source.end()}; };
26
27 void copy(AL &x, const AR &y) override {
28 using std::begin;
29 using std::end;
30 x.clear();
31 std::copy(begin(y), end(y), std::inserter(x, x.begin()));
32 };
33
34 void scal(value_type alpha, AL &x) override {
35 for (auto &el : x)
36 el.second *= alpha;
37 };
38
39 void fill(value_type alpha, AL &x) override {
40 for (auto &el : x)
41 el.second = alpha;
42 };
43
44 void axpy(value_type alpha, const AR &x, AL &y) override {
45 for (const auto &ix : x) {
46 auto iy = y.find(ix.first);
47 if (iy != y.end())
48 iy->second += alpha * ix.second;
49 }
50 };
51
52 value_type dot(const AL &x, const AR &y) override {
53 auto tot = value_type{};
54 for (const auto &ix : x) {
55 const auto iy = y.find(ix.first);
56 if (iy != y.end())
57 tot += iy->second * ix.second;
58 }
59 return tot;
60 };
61
62 void gemm_outer(const Matrix<value_type> alphas, const CVecRef<AR> &xx, const VecRef<AL> &yy) override {
63 gemm_outer_default(*this, alphas, xx, yy);
64 }
65
66 Matrix<value_type> gemm_inner(const CVecRef<AL> &xx, const CVecRef<AR> &yy) override {
67 return gemm_inner_default(*this, xx, yy);
68 }
69
70 std::map<size_t, value_type_abs> select_max_dot(size_t n, const AL &x, const AR &y) override {
71 if (n > x.size() || n > y.size())
72 error("ArrayHandlerSparse::select_max_dot() n is too large");
73 return util::select_max_dot_sparse<AL, AR, value_type, value_type_abs>(n, x, y);
74 }
75
76 std::map<size_t, value_type> select(size_t n, const AL &x, bool max = false, bool ignore_sign = false) override {
77 if (n > x.size())
78 error("ArrayHandlerSparse::select() n is too large");
79 return util::select_sparse<AL, value_type>(n, x, max, ignore_sign);
80 }
81
82 ProxyHandle lazy_handle() override { return this->lazy_handle(*this); };
83
84protected:
85 using ArrayHandler<AL, AR>::error;
86 using ArrayHandler<AL, AR>::lazy_handle;
87};
88
89} // namespace molpro::linalg::array
90
91#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_ARRAYHANDLERSPARSE_H
Array handler between two sparse arrays (e.g. std::map)
Definition: ArrayHandlerSparse.h:17
value_type dot(const AL &x, const AR &y) override
Definition: ArrayHandlerSparse.h:52
std::map< size_t, value_type > select(size_t n, const AL &x, bool max=false, bool ignore_sign=false) override
Select n indices with largest (or smallest) actual (or absolute) value.
Definition: ArrayHandlerSparse.h:76
void scal(value_type alpha, AL &x) override
Definition: ArrayHandlerSparse.h:34
void fill(value_type alpha, AL &x) override
Definition: ArrayHandlerSparse.h:39
void copy(AL &x, const AR &y) override
Definition: ArrayHandlerSparse.h:27
void gemm_outer(const Matrix< value_type > alphas, const CVecRef< AR > &xx, const VecRef< AL > &yy) override
Definition: ArrayHandlerSparse.h:62
void axpy(value_type alpha, const AR &x, AL &y) override
Definition: ArrayHandlerSparse.h:44
std::map< size_t, value_type_abs > select_max_dot(size_t n, const AL &x, const AR &y) override
Definition: ArrayHandlerSparse.h:70
ProxyHandle lazy_handle() override
Returns a lazy handle. Most implementations simply need to call the overload: return lazy_handle(*thi...
Definition: ArrayHandlerSparse.h:82
AL copy(const AR &source) override
Definition: ArrayHandlerSparse.h:25
Matrix< value_type > gemm_inner(const CVecRef< AL > &xx, const CVecRef< AR > &yy) override
Definition: ArrayHandlerSparse.h:66
Enhances various operations between pairs of arrays and allows dynamic code injection with uniform in...
Definition: ArrayHandler.h:162
decltype(value_type_L{} *value_type_R{}) value_type
Definition: ArrayHandler.h:181
virtual void error(const std::string &message)
Throws an error.
Definition: ArrayHandler.h:268
Matrix container that allows simple data access, slicing, copying and resizing without loosing data.
Definition: Matrix.h:28
auto begin(Span< T > &x)
Definition: Span.h:84
auto end(Span< T > &x)
Definition: Span.h:94
void gemm_outer_default(Handler &handler, const Matrix< typename Handler::value_type > alphas, const CVecRef< AR > &xx, const VecRef< AL > &yy)
Definition: gemm.h:258
Matrix< typename Handler::value_type > gemm_inner_default(Handler &handler, const CVecRef< AL > &xx, const CVecRef< AR > &yy)
Definition: gemm.h:268
Definition: ArrayHandler.h:22