iterative-solver 0.0
 
Loading...
Searching...
No Matches
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
9namespace molpro::linalg::array {
12template <typename AL, typename AR, bool = has_mapped_type_v<AR>>
13class ArrayHandlerIterableSparse : public ArrayHandler<AL, AR> {};
14
18template <typename AL, typename AR>
19class ArrayHandlerIterableSparse<AL, AR, true> : public ArrayHandler<AL, AR> {
20public:
26
27 AL copy(const AR &source) override {
28 // static_assert(true, "General copy from sparse to dense is ill-defined");
29 AL result;
30 copy(result, source);
31 return result;
32 };
33
34 void copy(AL &x, const AR &y) override {
35 // static_assert(true, "General copy from sparse to dense is ill-defined");
36 std::fill(x.begin(), x.end(), 0);
37 for (const auto &s : y)
38 *(x.begin() + s.first) = s.second;
39 }
40
41 void scal(value_type alpha, AL &x) override { static_assert(true, "Use ArrayHandlerIterable for unary operations"); };
42
43 void fill(value_type alpha, AL &x) override { static_assert(true, "Use ArrayHandlerIterable for unary operations"); };
44
45 void axpy(value_type alpha, const AR &x, AL &y) override {
46 for (const auto &el_x : x)
47 if (el_x.first < y.size())
48 y[el_x.first] += alpha * el_x.second;
49 };
50
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 += molpro::linalg::conjugate(static_cast<value_type>(x[el_y.first])) *
57 static_cast<value_type>(el_y.second);
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("ArrayHandlerIterableSparse::select_max_dot() n is too large");
72 return util::select_max_dot_iter_sparse<AL, AR, value_type, value_type_abs>(n, x, y);
73 }
74 std::map<size_t, value_type> select(size_t n, const AL &x, bool max = false, bool ignore_sign = false) override {
75 if (n > x.size())
76 error("ArrayHandlerIterableSparse::select() n is too large");
77 return util::select<AL, value_type>(n, x, max, ignore_sign);
78 }
79
80 ProxyHandle lazy_handle() override { return this->lazy_handle(*this); };
81
82protected:
83 using ArrayHandler<AL, AR>::error;
84 using ArrayHandler<AL, AR>::lazy_handle;
85 using ArrayHandler<AL, AR>::m_lazy_handles;
86};
87
88} // namespace molpro::linalg::array
89
90#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_ARRAYHANDLERITERABLESPARSE_H
void scal(value_type alpha, AL &x) override
Definition ArrayHandlerIterableSparse.h:41
void gemm_outer(const Matrix< value_type > alphas, const CVecRef< AR > &xx, const VecRef< AL > &yy) override
Definition ArrayHandlerIterableSparse.h:61
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:74
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:69
AL copy(const AR &source) override
Definition ArrayHandlerIterableSparse.h:27
void axpy(value_type alpha, const AR &x, AL &y) override
Definition ArrayHandlerIterableSparse.h:45
void copy(AL &x, const AR &y) override
Copy content of y into x.
Definition ArrayHandlerIterableSparse.h:34
void fill(value_type alpha, AL &x) override
Definition ArrayHandlerIterableSparse.h:43
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 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:80
Matrix< value_type > gemm_inner(const CVecRef< AL > &xx, const CVecRef< AR > &yy) override
Definition ArrayHandlerIterableSparse.h:65
Definition ArrayHandlerIterableSparse.h:13
A convenience wrapper around a pointer to the LazyHandle.
Definition ArrayHandler.h:392
Enhances various operations between pairs of arrays and allows dynamic code injection with uniform in...
Definition ArrayHandler.h:163
typename array::mapped_or_value_type_t< AR > value_type_R
Definition ArrayHandler.h:181
std::vector< std::weak_ptr< LazyHandle > > m_lazy_handles
keeps track of all created lazy handles
Definition ArrayHandler.h:424
typename array::mapped_or_value_type_t< AL > value_type_L
Definition ArrayHandler.h:180
decltype(value_type_L{} *value_type_R{}) value_type
Definition ArrayHandler.h:182
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...
decltype(check_abs< value_type >()) value_type_abs
Definition ArrayHandler.h:183
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
void gemm_outer_default(Handler &handler, const Matrix< typename Handler::value_type > alphas, const CVecRef< AR > &xx, const VecRef< AL > &yy)
Definition gemm.h:288
Matrix< typename Handler::value_type > gemm_inner_default(Handler &handler, const CVecRef< AL > &xx, const CVecRef< AR > &yy)
Definition gemm.h:298
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