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
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 += molpro::linalg::conjugate(static_cast<value_type>(ix.second)) *
58 static_cast<value_type>(iy->second);
59 }
60 return tot;
61 };
62
63 void gemm_outer(const Matrix<value_type> alphas, const CVecRef<AR> &xx, const VecRef<AL> &yy) override {
64 gemm_outer_default(*this, alphas, xx, yy);
65 }
66
67 Matrix<value_type> gemm_inner(const CVecRef<AL> &xx, const CVecRef<AR> &yy) override {
68 return gemm_inner_default(*this, xx, yy);
69 }
70
71 std::map<size_t, value_type_abs> select_max_dot(size_t n, const AL &x, const AR &y) override {
72 if (n > x.size() || n > y.size())
73 error("ArrayHandlerSparse::select_max_dot() n is too large");
74 return util::select_max_dot_sparse<AL, AR, value_type, value_type_abs>(n, x, y);
75 }
76
77 std::map<size_t, value_type> select(size_t n, const AL &x, bool max = false, bool ignore_sign = false) override {
78 if (n > x.size())
79 error("ArrayHandlerSparse::select() n is too large");
80 return util::select_sparse<AL, value_type>(n, x, max, ignore_sign);
81 }
82
83 ProxyHandle lazy_handle() override { return this->lazy_handle(*this); };
84
85protected:
86 using ArrayHandler<AL, AR>::error;
87 using ArrayHandler<AL, AR>::lazy_handle;
88};
89
90} // namespace molpro::linalg::array
91
92#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
The hermitian inner product <x|y>, i.e. conjugate-linear in x and linear in y.
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:77
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:63
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:71
ProxyHandle lazy_handle() override
Returns a lazy handle. Most implementations simply need to call the overload: return lazy_handle(*thi...
Definition: ArrayHandlerSparse.h:83
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:67
Enhances various operations between pairs of arrays and allows dynamic code injection with uniform in...
Definition: ArrayHandler.h:163
decltype(value_type_L{} *value_type_R{}) value_type
Definition: ArrayHandler.h:182
virtual void error(const std::string &message)
Throws an error.
Definition: ArrayHandler.h:276
Matrix container that allows simple data access, slicing, copying and resizing without loosing data.
Definition: Matrix.h:32
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:261
Matrix< typename Handler::value_type > gemm_inner_default(Handler &handler, const CVecRef< AL > &xx, const CVecRef< AR > &yy)
Definition: gemm.h:271
Definition: ArrayHandler.h:19
T conjugate(const T &x)
Complex conjugate, staying within the scalar type; the identity for a real type.
Definition: scalar_traits.h:47