iterative-solver 0.0
ArrayHandlerIterableSparse.h
1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_ARRAYHANDLERITERABLESPARSE_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_ARRAYHANDLERITERABLESPARSE_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#include <numeric>
8
11
12namespace molpro::linalg::array {
13template <typename AL, typename AR, bool = has_mapped_type_v<AR>>
14class ArrayHandlerIterableSparse : public ArrayHandler<AL, AR> {};
15
19template <typename AL, typename AR>
20class ArrayHandlerIterableSparse<AL, AR, true> : public ArrayHandler<AL, AR> {
21public:
27
28 AL copy(const AR &source) override {
29 // static_assert(true, "General copy from sparse to dense is ill-defined");
30 AL result;
31 copy(result, source);
32 return result;
33 };
34
35 void copy(AL &x, const AR &y) override {
36 // static_assert(true, "General copy from sparse to dense is ill-defined");
37 std::fill(x.begin(), x.end(), 0);
38 for (const auto &s : y)
39 *(x.begin() + s.first) = s.second;
40 }
41
42 void scal(value_type alpha, AL &x) override { static_assert(true, "Use ArrayHandlerIterable for unary operations"); };
43
44 void fill(value_type alpha, AL &x) override { static_assert(true, "Use ArrayHandlerIterable for unary operations"); };
45
46 void axpy(value_type alpha, const AR &x, AL &y) override {
47 for (const auto &el_x : x)
48 if (el_x.first < y.size())
49 y[el_x.first] += alpha * el_x.second;
50 };
51
52 value_type dot(const AL &x, const AR &y) override {
53 value_type tot = 0;
54 for (const auto &el_y : y)
55 if (el_y.first < x.size())
56 tot += x[el_y.first] * el_y.second;
57 return tot;
58 };
59
60 void gemm_outer(const Matrix<value_type> alphas, const CVecRef<AR> &xx, const VecRef<AL> &yy) override {
61 gemm_outer_default(*this, alphas, xx, yy);
62 }
63
64 Matrix<value_type> gemm_inner(const CVecRef<AL> &xx, const CVecRef<AR> &yy) override {
65 return gemm_inner_default(*this, xx, yy);
66 }
67
68 std::map<size_t, value_type_abs> select_max_dot(size_t n, const AL &x, const AR &y) override {
69 if (n > x.size() || n > y.size())
70 error("ArrayHandlerIterableSparse::select_max_dot() n is too large");
71 return util::select_max_dot_iter_sparse<AL, AR, value_type, value_type_abs>(n, x, y);
72 }
73 std::map<size_t, value_type> select(size_t n, const AL &x, bool max = false, bool ignore_sign = false) override {
74 if (n > x.size())
75 error("ArrayHandlerIterableSparse::select() n is too large");
76 return util::select<AL, value_type>(n, x, max, ignore_sign);
77 }
78
79 ProxyHandle lazy_handle() override { return this->lazy_handle(*this); };
80
81protected:
82 using ArrayHandler<AL, AR>::error;
83 using ArrayHandler<AL, AR>::lazy_handle;
84 using ArrayHandler<AL, AR>::m_lazy_handles;
85};
86
87} // namespace molpro::linalg::array
88
89#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_ARRAYHANDLERITERABLESPARSE_H
void scal(value_type alpha, AL &x) override
Definition: ArrayHandlerIterableSparse.h:42
void gemm_outer(const Matrix< value_type > alphas, const CVecRef< AR > &xx, const VecRef< AL > &yy) override
Definition: ArrayHandlerIterableSparse.h:60
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: ArrayHandlerIterableSparse.h:73
std::map< size_t, value_type_abs > select_max_dot(size_t n, const AL &x, const AR &y) override
Select n indices with largest by absolute value contributions to the dot product.
Definition: ArrayHandlerIterableSparse.h:68
AL copy(const AR &source) override
Definition: ArrayHandlerIterableSparse.h:28
void axpy(value_type alpha, const AR &x, AL &y) override
Definition: ArrayHandlerIterableSparse.h:46
void copy(AL &x, const AR &y) override
Copy content of y into x.
Definition: ArrayHandlerIterableSparse.h:35
void fill(value_type alpha, AL &x) override
Definition: ArrayHandlerIterableSparse.h:44
value_type dot(const AL &x, const AR &y) override
Definition: ArrayHandlerIterableSparse.h:52
ProxyHandle lazy_handle() override
Returns a lazy handle. Most implementations simply need to call the overload: return lazy_handle(*thi...
Definition: ArrayHandlerIterableSparse.h:79
Matrix< value_type > gemm_inner(const CVecRef< AL > &xx, const CVecRef< AR > &yy) override
Definition: ArrayHandlerIterableSparse.h:64
Definition: ArrayHandlerIterableSparse.h:14
Enhances various operations between pairs of arrays and allows dynamic code injection with uniform in...
Definition: ArrayHandler.h:162
std::vector< std::weak_ptr< LazyHandle > > m_lazy_handles
keeps track of all created lazy handles
Definition: ArrayHandler.h:416
decltype(value_type_L{} *value_type_R{}) value_type
Definition: ArrayHandler.h:181
virtual AL copy(const AR &source)=0
virtual ProxyHandle lazy_handle()=0
Returns a lazy handle. Most implementations simply need to call the overload: return lazy_handle(*thi...
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
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