iterative-solver 0.0
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
21
23template <typename T>
24struct SVD {
25 using value_type = T;
27 std::vector<value_type> u;
28 std::vector<value_type> v;
29};
30
38int eigensolver_lapacke_dsyev(std::span<const double> matrix, std::span<double> eigenvectors,
39 std::span<double> eigenvalues, const size_t dimension);
40
42std::list<SVD<double>> eigensolver_lapacke_dsyev(size_t dimension, std::span<const double> matrix);
43
44template <typename value_type>
45size_t get_rank(std::span<const value_type> eigenvalues, value_type threshold);
46
47template <typename value_type>
48size_t get_rank(std::list<SVD<value_type>> svd_system, value_type threshold);
49
51template <typename value_type>
52size_t get_rank(const std::vector<value_type>& eigenvalues, value_type threshold) {
53 return get_rank<value_type>(std::span<const value_type>{eigenvalues.data(), eigenvalues.size()}, threshold);
54}
55
65template <typename value_type, typename std::enable_if_t<!is_complex<value_type>{}, std::nullptr_t> = nullptr>
66std::list<SVD<value_type>> svd_system(size_t nrows, size_t ncols, const array::Span<value_type>& m,
67 real_type_t<value_type> threshold, bool hermitian = false,
68 bool reduce_to_rank = false);
69template <typename value_type, typename std::enable_if_t<is_complex<value_type>{}, int> = 0>
70std::list<SVD<value_type>> svd_system(size_t nrows, size_t ncols, const array::Span<value_type>& m,
71 real_type_t<value_type> threshold, bool hermitian = false,
72 bool reduce_to_rank = false);
73
74template <typename value_type>
75void printMatrix(const std::vector<value_type>&, size_t rows, size_t cols, std::string title = "",
76 std::ostream& s = molpro::cout);
77
78template <typename value_type, typename std::enable_if_t<is_complex<value_type>{}, int> = 0>
79void eigenproblem(std::vector<value_type>& eigenvectors, std::vector<value_type>& eigenvalues,
80 const std::vector<value_type>& matrix, const std::vector<value_type>& metric, size_t dimension,
81 bool hermitian, real_type_t<value_type> svdThreshold, int verbosity);
82
83template <typename value_type, typename std::enable_if_t<!is_complex<value_type>{}, std::nullptr_t> = nullptr>
84void eigenproblem(std::vector<value_type>& eigenvectors, std::vector<value_type>& eigenvalues,
85 const std::vector<value_type>& matrix, const std::vector<value_type>& metric, size_t dimension,
86 bool hermitian, real_type_t<value_type> svdThreshold, int verbosity,
87 std::vector<std::pair<std::size_t, value_type>> *imag_eval_parts = nullptr);
88
89template <typename value_type, typename std::enable_if_t<is_complex<value_type>{}, int> = 0>
90void solve_LinearEquations(std::vector<value_type>& solution, std::vector<value_type>& eigenvalues,
91 const std::vector<value_type>& matrix, const std::vector<value_type>& metric,
92 const std::vector<value_type>& rhs, size_t dimension, size_t nroot,
93 real_type_t<value_type> augmented_hessian, real_type_t<value_type> svdThreshold,
94 int verbosity);
95
96template <typename value_type, typename std::enable_if_t<!is_complex<value_type>{}, std::nullptr_t> = nullptr>
97void solve_LinearEquations(std::vector<value_type>& solution, std::vector<value_type>& eigenvalues,
98 const std::vector<value_type>& matrix, const std::vector<value_type>& metric,
99 const std::vector<value_type>& rhs, size_t dimension, size_t nroot,
100 real_type_t<value_type> augmented_hessian, real_type_t<value_type> svdThreshold,
101 int verbosity);
102
103template <typename value_type, typename std::enable_if_t<is_complex<value_type>{}, int> = 0>
104void solve_DIIS(std::vector<value_type>& solution, const std::vector<value_type>& matrix, size_t dimension,
105 real_type_t<value_type> svdThreshold, int verbosity = 0);
106template <typename value_type, typename std::enable_if_t<!is_complex<value_type>{}, std::nullptr_t> = nullptr>
107void solve_DIIS(std::vector<value_type>& solution, const std::vector<value_type>& matrix, size_t dimension,
108 real_type_t<value_type> svdThreshold, int verbosity = 0);
109
110/*
111 * Explicit instantiation of double type
112 */
113
114extern template void printMatrix<double>(const std::vector<double>&, size_t rows, size_t cols, std::string title,
115 std::ostream& s);
116
117extern template size_t get_rank<double>(std::span<const double> eigenvalues, double threshold);
118
119extern template std::list<SVD<double>> svd_system(size_t nrows, size_t ncols, const array::Span<double>& m,
120 double threshold, bool hermitian, bool reduce_to_rank);
121
122extern template void eigenproblem<double>(std::vector<double>& eigenvectors, std::vector<double>& eigenvalues,
123 const std::vector<double>& matrix, const std::vector<double>& metric,
124 const size_t dimension, bool hermitian, double svdThreshold, int verbosity,
125 std::vector<std::pair<std::size_t, double>> *imag_eval_parts);
126
127extern template void solve_LinearEquations<double>(std::vector<double>& solution, std::vector<double>& eigenvalues,
128 const std::vector<double>& matrix, const std::vector<double>& metric,
129 const std::vector<double>& rhs, size_t dimension, size_t nroot,
130 double augmented_hessian, double svdThreshold, int verbosity);
131
132extern template void solve_DIIS<double>(std::vector<double>& solution, const std::vector<double>& matrix,
133 const size_t dimension, double svdThreshold, int verbosity);
134
135/*
136 * Explicit instantiation of long double type
137 *
138 * LAPACK has no kernels for this precision, so these exercise the Eigen branch of the dispatch.
139 */
140
141extern template void printMatrix<long double>(const std::vector<long double>&, size_t rows, size_t cols,
142 std::string title, std::ostream& s);
143
144extern template size_t get_rank<long double>(std::span<const long double> eigenvalues, long double threshold);
145
146extern template std::list<SVD<long double>> svd_system(size_t nrows, size_t ncols, const array::Span<long double>& m,
147 long double threshold, bool hermitian, bool reduce_to_rank);
148
149extern template void eigenproblem<long double>(std::vector<long double>& eigenvectors,
150 std::vector<long double>& eigenvalues,
151 const std::vector<long double>& matrix,
152 const std::vector<long double>& metric, const size_t dimension,
153 bool hermitian, long double svdThreshold, int verbosity,
154 std::vector<std::pair<std::size_t, long double>> *imag_eval_parts);
155
157 std::vector<long double>& solution, std::vector<long double>& eigenvalues, const std::vector<long double>& matrix,
158 const std::vector<long double>& metric, const std::vector<long double>& rhs, size_t dimension, size_t nroot,
159 long double augmented_hessian, long double svdThreshold, int verbosity);
160
161extern template void solve_DIIS<long double>(std::vector<long double>& solution, const std::vector<long double>& matrix,
162 const size_t dimension, long double svdThreshold, int verbosity);
163
164/*
165 * Explicit instantiation of std::complex<double> type
166 */
167extern template void printMatrix<std::complex<double>>(const std::vector<std::complex<double>>&, size_t rows,
168 size_t cols, std::string title, std::ostream& s);
169
170extern template std::list<SVD<std::complex<double>>> svd_system(size_t nrows, size_t ncols,
171 const array::Span<std::complex<double>>& m,
172 double threshold, bool hermitian, bool reduce_to_rank);
173
174extern template void eigenproblem<std::complex<double>>(std::vector<std::complex<double>>& eigenvectors,
175 std::vector<std::complex<double>>& eigenvalues,
176 const std::vector<std::complex<double>>& matrix,
177 const std::vector<std::complex<double>>& metric,
178 const size_t dimension, bool hermitian, double svdThreshold,
179 int verbosity);
180
181extern template void solve_LinearEquations<std::complex<double>>(
182 std::vector<std::complex<double>>& solution, std::vector<std::complex<double>>& eigenvalues,
183 const std::vector<std::complex<double>>& matrix, const std::vector<std::complex<double>>& metric,
184 const std::vector<std::complex<double>>& rhs, size_t dimension, size_t nroot, double augmented_hessian,
185 double svdThreshold, int verbosity);
186
187extern template void solve_DIIS<std::complex<double>>(std::vector<std::complex<double>>& solution,
188 const std::vector<std::complex<double>>& matrix,
189 const size_t dimension, double svdThreshold, int verbosity);
190} // namespace molpro::linalg::itsolv
191#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< 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)
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)
template void printMatrix< double >(const std::vector< double > &, size_t rows, size_t cols, std::string title, std::ostream &s)
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:477
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)
Definition: helper-implementation.h:260
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:554
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:213
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)
size_t get_rank(std::span< const value_type > eigenvalues, value_type threshold)
Definition: helper-implementation.h:171
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:254
template size_t get_rank< double >(std::span< const double > eigenvalues, double threshold)
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< 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:54
Definition: scalar_traits.h:19
Stores a singular value and corresponding left and right singular vectors.
Definition: helper.h:24
std::vector< value_type > v
right singular vector
Definition: helper.h:28
T value_type
Definition: helper.h:25
value_type value
Definition: helper.h:26
std::vector< value_type > u
left singular vector
Definition: helper.h:27
The real type underlying a (possibly complex) scalar type.
Definition: scalar_traits.h:26