iterative-solver 0.0
 
Loading...
Searching...
No Matches
util.h
1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_UTIL_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_UTIL_H
3#include <molpro/Profiler.h>
4#include <molpro/linalg/itsolv/ArrayHandlers.h>
5#include <molpro/linalg/itsolv/subspace/Matrix.h>
6#include <molpro/linalg/itsolv/wrap.h>
7
8#include <cmath>
9#include <ios>
10#include <locale>
11#include <map>
12#include <ostream>
13#include <string>
14#include <type_traits>
15#include <vector>
16
18
20template <class R>
21auto norm(array::ArrayHandler<R, R>& handler, const std::type_identity_t<R>& x) ->
23 return std::sqrt(std::abs(handler.dot(x, x)));
24}
25
34template <typename value_type, typename value_type_abs>
35void remove_null_vectors(subspace::Matrix<value_type>& lin_trans, std::vector<value_type_abs>& norm, const size_t start,
36 const size_t end, const value_type_abs norm_thresh) {
37 for (size_t i = start, j = start; i < end; ++i) {
38 if (norm[j] < norm_thresh) {
39 norm.erase(begin(norm) + j);
40 lin_trans.remove_row(j);
41 } else
42 ++j;
43 }
44}
45
50template <class ForwardIt, class EndIterator, typename Int>
52 bool result = true;
53 for (auto it = it_start; it != it_end && result; ++it, ++value_start)
54 result = (*it) == value_start;
55 return result;
56}
57
59template <class R, class Q>
61 auto q = handler.copy(param);
62 handler.fill(0, q);
63 return q;
64}
65
81template <class R, class Q, class P, typename value_type>
82void construct_solutions(const VecRef<R>& params, const std::vector<int>& roots,
83 const subspace::Matrix<value_type>& solutions, const CVecRef<P>& pparams,
84 const CVecRef<Q>& qparams, const CVecRef<Q>& dparams, size_t oP, size_t oQ, size_t oD,
87 auto prof = molpro::Profiler::single();
88 prof->start("itsolv::util::construct_solutions");
89 assert(params.size() >= roots.size());
90 const auto nroots = roots.size();
91 for (size_t i = 0; i < nroots; ++i)
92 handler_rr.fill(0, params.at(i));
93 // coefficient matrices with the subspace vectors at the given indices in rows and roots in columns, as required by
94 // gemm_outer
95 auto coefficients = [&](const std::vector<size_t>& indices) {
96 auto coeff = subspace::Matrix<value_type>({indices.size(), nroots});
97 for (size_t i = 0; i < nroots; ++i)
98 for (size_t j = 0; j < indices.size(); ++j)
99 coeff(j, i) = solutions(roots[i], indices[j]);
100 return coeff;
101 };
102 auto append_indices = [](std::vector<size_t>& indices, size_t offset, size_t n) {
103 for (size_t j = 0; j < n; ++j)
104 indices.push_back(offset + j);
105 };
106 if (nroots > 0) {
107 const auto wparams = VecRef<R>(params.begin(), params.begin() + nroots);
108 auto p_indices = std::vector<size_t>{};
109 append_indices(p_indices, oP, pparams.size());
110 handler_rp.gemm_outer(coefficients(p_indices), pparams, wparams);
111 // Q and D in one call, so that each solution is passed over only once
112 auto qd_indices = std::vector<size_t>{};
113 append_indices(qd_indices, oQ, qparams.size());
114 append_indices(qd_indices, oD, dparams.size());
115 auto qdparams = qparams;
116 qdparams.insert(qdparams.end(), dparams.begin(), dparams.end());
117 handler_rq.gemm_outer(coefficients(qd_indices), qdparams, wparams);
118 }
119 prof->stop();
120}
121
127template <class Container>
128void delete_parameters(std::vector<int> indices, Container& params) {
129 std::sort(std::begin(indices), std::end(indices), std::greater<int>());
130 for (auto i : indices)
131 params.erase(begin(params) + i);
132}
133
136public:
137 std::string toupper(std::string in) const;
138 std::string tolower(std::string in) const;
139 bool tobool(const std::string& in) const;
140 static void crop_space(std::string& path);
141 static std::map<std::string, std::string> parse_keyval_string(std::string s);
142
143private:
144 const std::ctype<char>& facet = std::use_facet<std::ctype<char>>(std::locale());
145};
146
153std::size_t tosize(const std::string& key, const std::string& value);
154
157public:
158 explicit StreamFormatGuard(std::ostream& stream)
159 : m_stream(stream), m_flags(stream.flags()), m_precision(stream.precision()) {}
161 m_stream.flags(m_flags);
162 m_stream.precision(m_precision);
163 }
166
167private:
168 std::ostream& m_stream;
169 std::ios_base::fmtflags m_flags;
170 std::streamsize m_precision;
171};
172
173} // namespace molpro::linalg::itsolv::util
174#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_UTIL_H
Enhances various operations between pairs of arrays and allows dynamic code injection with uniform in...
Definition ArrayHandler.h:163
virtual AL copy(const AR &source)=0
virtual void fill(value_type alpha, AL &x)=0
decltype(check_abs< value_type >()) value_type_abs
Definition ArrayHandler.h:183
Matrix container that allows simple data access, slicing, copying and resizing without loosing data.
Definition Matrix.h:32
Restores the format flags and precision of a stream when it goes out of scope.
Definition util.h:156
StreamFormatGuard(std::ostream &stream)
Definition util.h:158
StreamFormatGuard(const StreamFormatGuard &)=delete
StreamFormatGuard & operator=(const StreamFormatGuard &)=delete
Wraps some useful string manipulation functions.
Definition util.h:135
static std::map< std::string, std::string > parse_keyval_string(std::string s)
Definition util.cpp:54
bool tobool(const std::string &in) const
Definition util.cpp:19
std::string tolower(std::string in) const
Definition util.cpp:14
std::string toupper(std::string in) const
Definition util.cpp:9
static void crop_space(std::string &path)
Definition util.cpp:33
static std::shared_ptr< Profiler > single()
Definition ArrayHandlers.h:8
void remove_null_vectors(subspace::Matrix< value_type > &lin_trans, std::vector< value_type_abs > &norm, const size_t start, const size_t end, const value_type_abs norm_thresh)
Remove vectors considered null from the linear transformation matrix.
Definition util.h:35
Q construct_zeroed_copy(const R &param, array::ArrayHandler< Q, R > &handler)
Copy R parameter to Q and zero it. This ensures same size and distribution of the copy.
Definition util.h:60
bool is_iota(ForwardIt it_start, EndIterator it_end, Int value_start)
Returns true if iterator range contains integers starting with value_start and sequentially increment...
Definition util.h:51
void construct_solutions(const VecRef< R > &params, const std::vector< int > &roots, const subspace::Matrix< value_type > &solutions, const CVecRef< P > &pparams, const CVecRef< Q > &qparams, const CVecRef< Q > &dparams, size_t oP, size_t oQ, size_t oD, array::ArrayHandler< R, R > &handler_rr, array::ArrayHandler< R, P > &handler_rp, array::ArrayHandler< R, Q > &handler_rq)
Construct solutions.
Definition util.h:82
auto norm(array::ArrayHandler< R, R > &handler, const std::type_identity_t< R > &x) -> typename array::ArrayHandler< R, R >::value_type_abs
The length of x, sqrt(|<x|x>|), which is real also for complex values.
Definition util.h:21
void delete_parameters(std::vector< int > indices, Container &params)
Removes parameters.
Definition util.h:128
std::size_t tosize(const std::string &key, const std::string &value)
Converts the value of an option to a size.
Definition util.cpp:43
std::unique_ptr< LinearEigensystem< R, Q, P > > create_LinearEigensystem(const LinearEigensystemOptions &options, const std::shared_ptr< ArrayHandlers< R, Q, P > > &handlers=std::make_shared< molpro::linalg::itsolv::ArrayHandlers< R, Q, P > >())
Definition SolverFactory.h:111
std::vector< std::reference_wrapper< const A > > CVecRef
Definition wrap.h:14
std::vector< std::reference_wrapper< A > > VecRef
Definition wrap.h:11