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
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>
51size_t get_rank(std::list<SVD<value_type>> svd_system, real_type_t<value_type> threshold);
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,
70 real_type_t<value_type> threshold, bool hermitian = false,
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,
74 real_type_t<value_type> threshold, bool hermitian = false,
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,
84 bool hermitian, real_type_t<value_type> svdThreshold, int verbosity,
85 std::vector<std::pair<std::size_t, value_type>>* imag_eval_parts = nullptr);
86
87template <typename value_type, typename std::enable_if_t<!is_complex<value_type>{}, std::nullptr_t> = nullptr>
88void eigenproblem(std::vector<value_type>& eigenvectors, std::vector<value_type>& eigenvalues,
89 const std::vector<value_type>& matrix, const std::vector<value_type>& metric, size_t dimension,
90 bool hermitian, real_type_t<value_type> svdThreshold, int verbosity,
91 std::vector<std::pair<std::size_t, value_type>> *imag_eval_parts = nullptr);
92
93template <typename value_type, typename std::enable_if_t<is_complex<value_type>{}, int> = 0>
94void solve_LinearEquations(std::vector<value_type>& solution, std::vector<value_type>& eigenvalues,
95 const std::vector<value_type>& matrix, const std::vector<value_type>& metric,
96 const std::vector<value_type>& rhs, size_t dimension, size_t nroot,
97 real_type_t<value_type> augmented_hessian, real_type_t<value_type> svdThreshold,
98 int verbosity);
99
100template <typename value_type, typename std::enable_if_t<!is_complex<value_type>{}, std::nullptr_t> = nullptr>
101void solve_LinearEquations(std::vector<value_type>& solution, std::vector<value_type>& eigenvalues,
102 const std::vector<value_type>& matrix, const std::vector<value_type>& metric,
103 const std::vector<value_type>& rhs, size_t dimension, size_t nroot,
104 real_type_t<value_type> augmented_hessian, real_type_t<value_type> svdThreshold,
105 int verbosity);
106
107template <typename value_type, typename std::enable_if_t<is_complex<value_type>{}, int> = 0>
108void solve_DIIS(std::vector<value_type>& solution, const std::vector<value_type>& matrix, size_t dimension,
109 real_type_t<value_type> svdThreshold, int verbosity = 0);
110template <typename value_type, typename std::enable_if_t<!is_complex<value_type>{}, std::nullptr_t> = nullptr>
111void solve_DIIS(std::vector<value_type>& solution, const std::vector<value_type>& matrix, size_t dimension,
112 real_type_t<value_type> svdThreshold, int verbosity = 0);
113
114/*
115 * Explicit instantiation of double type
116 */
117
118extern template void printMatrix<double>(const std::vector<double>&, size_t rows, size_t cols, std::string title,
119 std::ostream& s);
120
121extern template size_t get_rank<double>(std::span<const double> eigenvalues, double threshold);
122
123extern template std::list<SVD<double>> svd_system(size_t nrows, size_t ncols, const array::Span<double>& m,
124 double threshold, bool hermitian, bool reduce_to_rank);
125
126extern template void eigenproblem<double>(std::vector<double>& eigenvectors, std::vector<double>& eigenvalues,
127 const std::vector<double>& matrix, const std::vector<double>& metric,
128 const size_t dimension, bool hermitian, double svdThreshold, int verbosity,
129 std::vector<std::pair<std::size_t, double>> *imag_eval_parts);
130
131extern template void solve_LinearEquations<double>(std::vector<double>& solution, std::vector<double>& eigenvalues,
132 const std::vector<double>& matrix, const std::vector<double>& metric,
133 const std::vector<double>& rhs, size_t dimension, size_t nroot,
134 double augmented_hessian, double svdThreshold, int verbosity);
135
136extern template void solve_DIIS<double>(std::vector<double>& solution, const std::vector<double>& matrix,
137 const size_t dimension, double svdThreshold, int verbosity);
138
139/*
140 * Explicit instantiation of long double type
141 *
142 * The dense kernels fall back on Eigen for this precision, as they do for any other scalar type for
143 * which LAPACK provides no kernel.
144 */
145
146extern template void printMatrix<long double>(const std::vector<long double>&, size_t rows, size_t cols,
147 std::string title, std::ostream& s);
148
149extern template size_t get_rank<long double>(std::span<const long double> eigenvalues, long double threshold);
150
151extern template std::list<SVD<long double>> svd_system(size_t nrows, size_t ncols, const array::Span<long double>& m,
152 long double threshold, bool hermitian, bool reduce_to_rank);
153
154extern template void eigenproblem<long double>(std::vector<long double>& eigenvectors,
155 std::vector<long double>& eigenvalues,
156 const std::vector<long double>& matrix,
157 const std::vector<long double>& metric, const size_t dimension,
158 bool hermitian, long double svdThreshold, int verbosity,
159 std::vector<std::pair<std::size_t, long double>> *imag_eval_parts);
160
162 std::vector<long double>& solution, std::vector<long double>& eigenvalues, const std::vector<long double>& matrix,
163 const std::vector<long double>& metric, const std::vector<long double>& rhs, size_t dimension, size_t nroot,
164 long double augmented_hessian, long double svdThreshold, int verbosity);
165
166extern template void solve_DIIS<long double>(std::vector<long double>& solution, const std::vector<long double>& matrix,
167 const size_t dimension, long double svdThreshold, int verbosity);
168
169/*
170 * Explicit instantiation of std::complex<double> type
171 */
172extern template void printMatrix<std::complex<double>>(const std::vector<std::complex<double>>&, size_t rows,
173 size_t cols, std::string title, std::ostream& s);
174
175extern template std::list<SVD<std::complex<double>>> svd_system(size_t nrows, size_t ncols,
176 const array::Span<std::complex<double>>& m,
177 double threshold, bool hermitian, bool reduce_to_rank);
178
179extern template void eigenproblem<std::complex<double>>(
180 std::vector<std::complex<double>>& eigenvectors, std::vector<std::complex<double>>& eigenvalues,
181 const std::vector<std::complex<double>>& matrix, const std::vector<std::complex<double>>& metric,
182 const size_t dimension, bool hermitian, double svdThreshold, int verbosity,
183 std::vector<std::pair<std::size_t, std::complex<double>>>* imag_eval_parts);
184
185extern template void solve_LinearEquations<std::complex<double>>(
186 std::vector<std::complex<double>>& solution, std::vector<std::complex<double>>& eigenvalues,
187 const std::vector<std::complex<double>>& matrix, const std::vector<std::complex<double>>& metric,
188 const std::vector<std::complex<double>>& rhs, size_t dimension, size_t nroot, double augmented_hessian,
189 double svdThreshold, int verbosity);
190
191extern template void solve_DIIS<std::complex<double>>(std::vector<std::complex<double>>& solution,
192 const std::vector<std::complex<double>>& matrix,
193 const size_t dimension, double svdThreshold, int verbosity);
194} // namespace molpro::linalg::itsolv
195#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)
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)
Definition: helper-implementation.h:377
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:727
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:813
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:262
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:274
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