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
8namespace molpro::linalg::array {
11
15template <typename AL, typename AR = AL>
16class ArrayHandlerSparse : public ArrayHandler<AL, AR> {
17public:
23
24 AL copy(const AR &source) override { return AL{source.begin(), source.end()}; };
25
26 void copy(AL &x, const AR &y) override {
27 using std::begin;
28 using std::end;
29 x.clear();
30 std::copy(begin(y), end(y), std::inserter(x, x.begin()));
31 };
32
33 void scal(value_type alpha, AL &x) override {
34 for (auto &el : x)
35 el.second *= alpha;
36 };
37
38 void fill(value_type alpha, AL &x) override {
39 for (auto &el : x)
40 el.second = alpha;
41 };
42
43 void axpy(value_type alpha, const AR &x, AL &y) override {
44 for (const auto &ix : x) {
45 auto iy = y.find(ix.first);
46 if (iy != y.end())
47 iy->second += alpha * ix.second;
48 }
49 };
50
51 value_type dot(const AL &x, const AR &y) override {
52 auto tot = value_type{};
53 for (const auto &ix : x) {
54 const auto iy = y.find(ix.first);
55 if (iy != y.end())
56 tot += iy->second * ix.second;
57 }
58 return tot;
59 };
60
61 void gemm_outer(const Matrix<value_type> alphas, const CVecRef<AR> &xx, const VecRef<AL> &yy) override {
62 gemm_outer_default(*this, alphas, xx, yy);
63 }
64
65 Matrix<value_type> gemm_inner(const CVecRef<AL> &xx, const CVecRef<AR> &yy) override {
66 return gemm_inner_default(*this, xx, yy);
67 }
68
69 std::map<size_t, value_type_abs> select_max_dot(size_t n, const AL &x, const AR &y) override {
70 if (n > x.size() || n > y.size())
71 error("ArrayHandlerSparse::select_max_dot() n is too large");
72 return util::select_max_dot_sparse<AL, AR, value_type, value_type_abs>(n, x, y);
73 }
74
75 std::map<size_t, value_type> select(size_t n, const AL &x, bool max = false, bool ignore_sign = false) override {
76 if (n > x.size())
77 error("ArrayHandlerSparse::select() n is too large");
78 return util::select_sparse<AL, value_type>(n, x, max, ignore_sign);
79 }
80
81 ProxyHandle lazy_handle() override { return this->lazy_handle(*this); };
82
83protected:
84 using ArrayHandler<AL, AR>::error;
85 using ArrayHandler<AL, AR>::lazy_handle;
86};
87
88} // namespace molpro::linalg::array
89
90#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_ARRAYHANDLERSPARSE_H
Array handler between two sparse arrays (e.g. std::map)
Definition: ArrayHandlerSparse.h:16
value_type dot(const AL &x, const AR &y) override
Definition: ArrayHandlerSparse.h:51
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:75
void scal(value_type alpha, AL &x) override
Definition: ArrayHandlerSparse.h:33
void fill(value_type alpha, AL &x) override
Definition: ArrayHandlerSparse.h:38
void copy(AL &x, const AR &y) override
Definition: ArrayHandlerSparse.h:26
void gemm_outer(const Matrix< value_type > alphas, const CVecRef< AR > &xx, const VecRef< AL > &yy) override
Definition: ArrayHandlerSparse.h:61
void axpy(value_type alpha, const AR &x, AL &y) override
Definition: ArrayHandlerSparse.h:43
std::map< size_t, value_type_abs > select_max_dot(size_t n, const AL &x, const AR &y) override
Definition: ArrayHandlerSparse.h:69
ProxyHandle lazy_handle() override
Returns a lazy handle. Most implementations simply need to call the overload: return lazy_handle(*thi...
Definition: ArrayHandlerSparse.h:81
AL copy(const AR &source) override
Definition: ArrayHandlerSparse.h:24
Matrix< value_type > gemm_inner(const CVecRef< AL > &xx, const CVecRef< AR > &yy) override
Definition: ArrayHandlerSparse.h:65
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:30
auto begin(Span< T > &x)
Definition: Span.h:87
auto end(Span< T > &x)
Definition: Span.h:97
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:18