iterative-solver 0.0
 
Loading...
Searching...
No Matches
helper.h
1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ITERATIVESOLVER_HELPER_H_
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ITERATIVESOLVER_HELPER_H_
3#include <complex>
4#include <cstddef>
5#include <limits>
6#include <list>
7#include <molpro/iostream.h>
8#include <molpro/linalg/array/Span.h>
9#include <molpro/linalg/scalar_traits.h>
10#include <span>
11#include <type_traits>
12#include <vector>
13
15
16// Scalar traits shared with molpro::linalg::array, re-exported here for backwards compatibility
24
26template <typename T>
27struct SVD {
28 using value_type = T;
30 std::vector<value_type> u;
31 std::vector<value_type> v;
32};
33
41int eigensolver_lapacke_dsyev(std::span<const double> matrix, std::span<double> eigenvectors,
42 std::span<double> eigenvalues, const size_t dimension);
43
45std::list<SVD<double>> eigensolver_lapacke_dsyev(size_t dimension, std::span<const double> matrix);
46
47template <typename value_type>
48size_t get_rank(std::span<const value_type> eigenvalues, value_type threshold);
49
50template <typename value_type>
52
54template <typename value_type>
55size_t get_rank(const std::vector<value_type>& eigenvalues, value_type threshold) {
56 return get_rank<value_type>(std::span<const value_type>{eigenvalues.data(), eigenvalues.size()}, threshold);
57}
58
68template <typename value_type, typename std::enable_if_t<!is_complex<value_type>{}, std::nullptr_t> = nullptr>
69std::list<SVD<value_type>> svd_system(size_t nrows, size_t ncols, const array::Span<value_type>& m,
71 bool reduce_to_rank = false);
72template <typename value_type, typename std::enable_if_t<is_complex<value_type>{}, int> = 0>
73std::list<SVD<value_type>> svd_system(size_t nrows, size_t ncols, const array::Span<value_type>& m,
75 bool reduce_to_rank = false);
76
77template <typename value_type>
78void printMatrix(const std::vector<value_type>&, size_t rows, size_t cols, std::string title = "",
79 std::ostream& s = molpro::cout);
80
81template <typename value_type, typename std::enable_if_t<is_complex<value_type>{}, int> = 0>
82void eigenproblem(std::vector<value_type>& eigenvectors, std::vector<value_type>& eigenvalues,
83 const std::vector<value_type>& matrix, const std::vector<value_type>& metric, size_t dimension,
85 std::vector<std::pair<std::size_t, value_type>>* imag_eval_parts = nullptr,
86 size_t nroots = std::numeric_limits<size_t>::max());
87
94template <typename value_type, typename std::enable_if_t<!is_complex<value_type>{}, std::nullptr_t> = nullptr>
95void eigenproblem(std::vector<value_type>& eigenvectors, std::vector<value_type>& eigenvalues,
96 const std::vector<value_type>& matrix, const std::vector<value_type>& metric, size_t dimension,
98 std::vector<std::pair<std::size_t, value_type>>* imag_eval_parts = nullptr,
99 size_t nroots = std::numeric_limits<size_t>::max());
100
101template <typename value_type, typename std::enable_if_t<is_complex<value_type>{}, int> = 0>
102void solve_LinearEquations(std::vector<value_type>& solution, std::vector<value_type>& eigenvalues,
103 const std::vector<value_type>& matrix, const std::vector<value_type>& metric,
104 const std::vector<value_type>& rhs, size_t dimension, size_t nroot,
106 int verbosity);
107
108template <typename value_type, typename std::enable_if_t<!is_complex<value_type>{}, std::nullptr_t> = nullptr>
109void solve_LinearEquations(std::vector<value_type>& solution, std::vector<value_type>& eigenvalues,
110 const std::vector<value_type>& matrix, const std::vector<value_type>& metric,
111 const std::vector<value_type>& rhs, size_t dimension, size_t nroot,
113 int verbosity);
114
115template <typename value_type, typename std::enable_if_t<is_complex<value_type>{}, int> = 0>
116void solve_DIIS(std::vector<value_type>& solution, const std::vector<value_type>& matrix, size_t dimension,
117 real_type_t<value_type> svdThreshold, int verbosity = 0);
118template <typename value_type, typename std::enable_if_t<!is_complex<value_type>{}, std::nullptr_t> = nullptr>
119void solve_DIIS(std::vector<value_type>& solution, const std::vector<value_type>& matrix, size_t dimension,
120 real_type_t<value_type> svdThreshold, int verbosity = 0);
121
122/*
123 * Explicit instantiation of double type
124 */
125
126extern template void printMatrix<double>(const std::vector<double>&, size_t rows, size_t cols, std::string title,
127 std::ostream& s);
128
129extern template size_t get_rank<double>(std::span<const double> eigenvalues, double threshold);
130
131extern template std::list<SVD<double>> svd_system(size_t nrows, size_t ncols, const array::Span<double>& m,
132 double threshold, bool hermitian, bool reduce_to_rank);
133
134extern template void eigenproblem<double>(std::vector<double>& eigenvectors, std::vector<double>& eigenvalues,
135 const std::vector<double>& matrix, const std::vector<double>& metric,
136 const size_t dimension, bool hermitian, double svdThreshold, int verbosity,
137 std::vector<std::pair<std::size_t, double>> *imag_eval_parts, size_t nroots);
138
139extern template void solve_LinearEquations<double>(std::vector<double>& solution, std::vector<double>& eigenvalues,
140 const std::vector<double>& matrix, const std::vector<double>& metric,
141 const std::vector<double>& rhs, size_t dimension, size_t nroot,
142 double augmented_hessian, double svdThreshold, int verbosity);
143
144extern template void solve_DIIS<double>(std::vector<double>& solution, const std::vector<double>& matrix,
145 const size_t dimension, double svdThreshold, int verbosity);
146
147/*
148 * Explicit instantiation of long double type
149 *
150 * The dense kernels fall back on Eigen for this precision, as they do for any other scalar type for
151 * which LAPACK provides no kernel.
152 */
153
154extern template void printMatrix<long double>(const std::vector<long double>&, size_t rows, size_t cols,
155 std::string title, std::ostream& s);
156
157extern template size_t get_rank<long double>(std::span<const long double> eigenvalues, long double threshold);
158
159extern template std::list<SVD<long double>> svd_system(size_t nrows, size_t ncols, const array::Span<long double>& m,
160 long double threshold, bool hermitian, bool reduce_to_rank);
161
162extern template void eigenproblem<long double>(std::vector<long double>& eigenvectors,
163 std::vector<long double>& eigenvalues,
164 const std::vector<long double>& matrix,
165 const std::vector<long double>& metric, const size_t dimension,
166 bool hermitian, long double svdThreshold, int verbosity,
167 std::vector<std::pair<std::size_t, long double>> *imag_eval_parts, size_t nroots);
168
170 std::vector<long double>& solution, std::vector<long double>& eigenvalues, const std::vector<long double>& matrix,
171 const std::vector<long double>& metric, const std::vector<long double>& rhs, size_t dimension, size_t nroot,
172 long double augmented_hessian, long double svdThreshold, int verbosity);
173
174extern template void solve_DIIS<long double>(std::vector<long double>& solution, const std::vector<long double>& matrix,
175 const size_t dimension, long double svdThreshold, int verbosity);
176
177/*
178 * Explicit instantiation of std::complex<double> type
179 */
180extern template void printMatrix<std::complex<double>>(const std::vector<std::complex<double>>&, size_t rows,
181 size_t cols, std::string title, std::ostream& s);
182
183extern template std::list<SVD<std::complex<double>>> svd_system(size_t nrows, size_t ncols,
184 const array::Span<std::complex<double>>& m,
185 double threshold, bool hermitian, bool reduce_to_rank);
186
188 std::vector<std::complex<double>>& eigenvectors, std::vector<std::complex<double>>& eigenvalues,
189 const std::vector<std::complex<double>>& matrix, const std::vector<std::complex<double>>& metric,
190 const size_t dimension, bool hermitian, double svdThreshold, int verbosity,
191 std::vector<std::pair<std::size_t, std::complex<double>>>* imag_eval_parts, size_t nroots);
192
194 std::vector<std::complex<double>>& solution, std::vector<std::complex<double>>& eigenvalues,
195 const std::vector<std::complex<double>>& matrix, const std::vector<std::complex<double>>& metric,
196 const std::vector<std::complex<double>>& rhs, size_t dimension, size_t nroot, double augmented_hessian,
197 double svdThreshold, int verbosity);
198
199extern template void solve_DIIS<std::complex<double>>(std::vector<std::complex<double>>& solution,
200 const std::vector<std::complex<double>>& matrix,
201 const size_t dimension, double svdThreshold, int verbosity);
202} // namespace molpro::linalg::itsolv
203#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_ITERATIVESOLVER_HELPER_H_
Non-owning container taking a pointer to the data buffer and its size and exposing routines for itera...
Definition Span.h:31
4-parameter interpolation of a 1-dimensional function given two points for which function values and ...
Definition helper.h:14
template void eigenproblem< double >(std::vector< double > &eigenvectors, std::vector< double > &eigenvalues, const std::vector< double > &matrix, const std::vector< double > &metric, const size_t dimension, bool hermitian, double svdThreshold, int verbosity, std::vector< std::pair< std::size_t, double > > *imag_eval_parts, size_t nroots)
template void printMatrix< double >(const std::vector< double > &, size_t rows, size_t cols, std::string title, std::ostream &s)
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
void solve_LinearEquations(std::vector< value_type > &solution, std::vector< value_type > &eigenvalues, const std::vector< value_type > &matrix, const std::vector< value_type > &metric, const std::vector< value_type > &rhs, size_t dimension, size_t nroot, real_type_t< value_type > augmented_hessian, real_type_t< value_type > svdThreshold, int verbosity)
Definition helper-implementation.h:844
template void solve_DIIS< double >(std::vector< double > &solution, const std::vector< double > &matrix, const size_t dimension, double svdThreshold, int verbosity)
int eigensolver_lapacke_dsyev(std::span< const double > matrix, std::span< double > eigenvectors, std::span< double > eigenvalues, const size_t dimension)
Eigen-decomposition of a real symmetric matrix in double precision.
Definition helper-implementation.h:153
template size_t get_rank< value_type >(std::span< const value_type > eigenvalues, value_type threshold)
void solve_DIIS(std::vector< value_type > &solution, const std::vector< value_type > &matrix, size_t dimension, real_type_t< value_type > svdThreshold, int verbosity=0)
Definition helper-implementation.h:930
template size_t get_rank< long double >(std::span< const long double > eigenvalues, long double threshold)
template void solve_LinearEquations< double >(std::vector< double > &solution, std::vector< double > &eigenvalues, const std::vector< double > &matrix, const std::vector< double > &metric, const std::vector< double > &rhs, size_t dimension, size_t nroot, double augmented_hessian, double svdThreshold, int verbosity)
template void printMatrix< long double >(const std::vector< long double > &, size_t rows, size_t cols, std::string title, std::ostream &s)
std::list< SVD< value_type > > svd_system(size_t nrows, size_t ncols, const array::Span< value_type > &m, real_type_t< value_type > threshold, bool hermitian=false, bool reduce_to_rank=false)
Performs singular value decomposition and returns SVD objects for singular values less than threshold...
Definition helper-implementation.h:268
template void solve_DIIS< long double >(std::vector< long double > &solution, const std::vector< long double > &matrix, const size_t dimension, long double svdThreshold, int verbosity)
template void solve_LinearEquations< long double >(std::vector< long double > &solution, std::vector< long double > &eigenvalues, const std::vector< long double > &matrix, const std::vector< long double > &metric, const std::vector< long double > &rhs, size_t dimension, size_t nroot, long double augmented_hessian, long double svdThreshold, int verbosity)
template void eigenproblem< long double >(std::vector< long double > &eigenvectors, std::vector< long double > &eigenvalues, const std::vector< long double > &matrix, const std::vector< long double > &metric, const size_t dimension, bool hermitian, long double svdThreshold, int verbosity, std::vector< std::pair< std::size_t, long double > > *imag_eval_parts, size_t nroots)
size_t get_rank(std::span< const value_type > eigenvalues, value_type threshold)
Definition helper-implementation.h:171
void eigenproblem(std::vector< value_type > &eigenvectors, std::vector< value_type > &eigenvalues, const std::vector< value_type > &matrix, const std::vector< value_type > &metric, size_t dimension, bool hermitian, real_type_t< value_type > svdThreshold, int verbosity, std::vector< std::pair< std::size_t, value_type > > *imag_eval_parts=nullptr, size_t nroots=std::numeric_limits< size_t >::max())
Definition helper-implementation.h:431
void printMatrix(const std::vector< value_type > &, size_t rows, size_t cols, std::string title="", std::ostream &s=molpro::cout)
Definition helper-implementation.h:280
template size_t get_rank< double >(std::span< const double > eigenvalues, double threshold)
real_type_t< T > real_part(const T &x)
The real part of a scalar; the value itself for a real type.
Definition scalar_traits.h:58
T conjugate(const T &x)
Complex conjugate, staying within the scalar type; the identity for a real type.
Definition scalar_traits.h:47
typename real_type< T >::type real_type_t
The real type underlying T, i.e. T itself for a real type and U for std::complex<U>.
Definition scalar_traits.h:43
real_type_t< T > imaginary_part(const T &x)
The imaginary part of a scalar; zero for a real type.
Definition scalar_traits.h:68
real_type_t< value_type > precision_scaled(double tolerance_for_double)
Rescale a tolerance that was calibrated for IEEE double precision to the working precision.
Definition scalar_traits.h:85
Definition scalar_traits.h:19
Stores a singular value and corresponding left and right singular vectors.
Definition helper.h:27
std::vector< value_type > v
right singular vector
Definition helper.h:31
T value_type
Definition helper.h:28
value_type value
Definition helper.h:29
std::vector< value_type > u
left singular vector
Definition helper.h:30
The real type underlying a (possibly complex) scalar type.
Definition scalar_traits.h:26