ampsci
High-precision calculations for one- and two-valence atomic systems
Solvers.hpp
1#pragma once
2#include "Matrix.hpp"
3#include "Vector.hpp"
4
5namespace LinAlg {
6
7/*!
8 @brief Solves the linear system Ax = b for x, given square matrix A and
9 vector b.
10 @details
11 Uses LU decomposition (GSL). A is taken by value and overwritten during
12 decomposition.
13
14 @tparam T Element type: `double` or `std::complex<double>` only.
15 @param Am Square matrix A (copied; overwritten internally).
16 @param b Right-hand side vector; must satisfy `b.size() == A.rows()`.
17 @returns Solution vector x.
18*/
19template <typename T>
20Vector<T> solve_Axeqb(Matrix<T> Am, const Vector<T> &b);
21
22/*!
23 @brief Solves Av = ev for all eigenvalues and eigenvectors of a real
24 symmetric or complex Hermitian matrix A.
25 @details
26 Uses LAPACK `dsyev` (real) or `zheev` (complex Hermitian).
27 Eigenvalues are returned in ascending order.
28 A is taken by value and overwritten during the computation.
29
30 @tparam T Element type: `double` (symmetric) or `std::complex<double>`
31 (Hermitian).
32 @param A Square symmetric/Hermitian matrix (taken by value; overwritten).
33 @returns Pair `{e, V}`, where:
34 - `e(i)` is the i-th eigenvalue (always real, ascending order),
35 - `V(i,j)` is the j-th element of the i-th eigenvector.
36*/
37template <typename T>
38std::pair<Vector<double>, Matrix<T>> symmhEigensystem(Matrix<T> A);
39
40/*!
41 @brief Solves Av = ev for the first `number` eigenvalues and eigenvectors
42 of a real symmetric matrix A.
43 @details
44 Uses LAPACK `dsyevx`. Only available for `T = double`.
45
46 @tparam T Element type: `double` only.
47 @param A Square real symmetric matrix (taken by value; overwritten).
48 @param number Number of lowest eigenvalues/eigenvectors to compute.
49 Must satisfy `number < A.rows()`.
50 @returns Pair `{e, V}`, where only the first `number` entries of `e` and
51 rows of `V` are valid.
52*/
53template <typename T>
54std::pair<Vector<double>, Matrix<T>> symmhEigensystem(Matrix<T> A, int number);
55
56/*!
57 @brief Solves Av = ev for all eigenvalues below a given threshold of a real
58 symmetric matrix A.
59 @details
60 Uses LAPACK `dsyevr`. Only available for `T = double`.
61
62 @tparam T Element type: `double` only.
63 @param A Square real symmetric matrix (taken by value; overwritten).
64 @param all_below Upper bound: only eigenvalues `all_below` are returned.
65 @returns Tuple `{N, e, V}`, where:
66 - `N` is the number of eigenvalues found,
67 - `e(i)` is the i-th eigenvalue (ascending),
68 - `V(i,j)` is the j-th element of the i-th eigenvector.
69 Only the first `N` entries of `e` and rows of `V` are valid.
70*/
71template <typename T>
72std::tuple<int, Vector<double>, Matrix<T>> symmhEigensystem(Matrix<T> A,
73 double all_below);
74
75/*!
76 @brief Solves the generalised eigensystem Av = eBv for a real symmetric or
77 complex Hermitian matrix pair A, B.
78 @details
79 Uses LAPACK `dsygv` (real) or `zhegv` (complex Hermitian).
80 B must be positive definite. Eigenvalues are returned in ascending order.
81 Both A and B are taken by value and overwritten during computation.
82
83 @tparam T Element type: `double` or `std::complex<double>`.
84 @param A Square symmetric/Hermitian matrix (taken by value; overwritten).
85 @param B Square symmetric/Hermitian positive-definite matrix (same size as
86 A; taken by value; overwritten).
87 @returns Pair `{e, V}`, where:
88 - `e(i)` is the i-th eigenvalue (always real, ascending order),
89 - `V(i,j)` is the j-th element of the i-th eigenvector.
90*/
91template <typename T>
92std::pair<Vector<double>, Matrix<T>> symmhEigensystem(Matrix<T> A, Matrix<T> B);
93
94/*!
95 @brief Solves Av = ev for all eigenvalues and right eigenvectors of a
96 general (non-symmetric) real matrix A.
97 @details
98 Uses GSL `gsl_eigen_nonsymmv`. A must be real (`T = double`); eigenvalues
99 and eigenvectors are always complex.
100
101 @tparam T Element type: `double` only.
102 @param A Square real matrix (taken by value; overwritten).
103 @param sort If `true`, eigenvalues (and corresponding eigenvectors) are
104 sorted by ascending absolute value.
105 @returns Pair `{e, V}`, where:
106 - `e(i)` is the i-th complex eigenvalue,
107 - `V(i,j)` is the j-th element of the i-th complex eigenvector.
108*/
109template <typename T>
110std::pair<Vector<std::complex<double>>, Matrix<std::complex<double>>>
111genEigensystem(Matrix<T> A, bool sort);
112
113//==============================================================================
114//==============================================================================
115} // namespace LinAlg
116
117#include "Solvers.ipp"
Linear algebra: matrices, vectors, views, and solvers.
Definition Matrix.hpp:54
std::pair< Vector< std::complex< double > >, Matrix< std::complex< double > > > genEigensystem(Matrix< T > A, bool sort)
Solves Av = ev for all eigenvalues and right eigenvectors of a general (non-symmetric) real matrix A.
Definition Solvers.ipp:319
Vector< T > solve_Axeqb(Matrix< T > Am, const Vector< T > &b)
Solves the linear system Ax = b for x, given square matrix A and vector b.
Definition Solvers.ipp:36
std::pair< Vector< double >, Matrix< T > > symmhEigensystem(Matrix< T > A)
Solves Av = ev for all eigenvalues and eigenvectors of a real symmetric or complex Hermitian matrix A...
Definition Solvers.ipp:70