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