numerics 0.1.0
Loading...
Searching...
No Matches
tridiag_complex.hpp
Go to the documentation of this file.
1/// @file factorization/tridiag_complex.hpp
2/// @brief Precomputed LU Thomas solver for constant-coefficient complex
3/// tridiagonal systems.
4///
5/// Solves the n x n system:
6///
7/// a*x[k-1] + b*x[k] + c*x[k+1] = d[k], k = 0 .. n-1
8///
9/// with Dirichlet boundary conditions x[-1] = x[n] = 0.
10///
11/// Because a, b, c are the same for every row the LU factorization depends only
12/// on the coefficients, not on d. Factor once, then call solve() for each
13/// new right-hand side in O(n).
14///
15/// Typical use: Crank-Nicolson kinetic sweeps in the TDSE solver, where
16/// a = c = -i*alpha and b = 1 + 2i*alpha for some real alpha.
17#pragma once
18
19#include "core/types.hpp"
20#include <complex>
21#include <vector>
22
23namespace num {
24
25/// @brief Constant-coefficient complex tridiagonal solver (precomputed LU).
26///
27/// @code
28/// num::ComplexTriDiag td;
29/// std::complex<double> a(0.0, -alpha);
30/// std::complex<double> b(1.0, 2.0*alpha);
31/// std::complex<double> c(0.0, -alpha);
32/// td.factor(n, a, b, c);
33///
34/// std::vector<std::complex<double>> rhs = ...;
35/// td.solve(rhs); // rhs is overwritten with the solution
36/// @endcode
38 using cplx = std::complex<double>;
39
40 std::vector<cplx> c_mod; ///< Modified super-diagonal (precomputed from LU)
41 std::vector<cplx> inv_b; ///< Inverse of modified main diagonal (precomputed)
42 int n = 0;
43 cplx a_coeff = {}; ///< Sub-diagonal value (constant across all rows)
44
45 /// @brief Factor the tridiagonal matrix.
46 ///
47 /// @param n_ System size
48 /// @param a_ Sub-diagonal coefficient (row k depends on x[k-1])
49 /// @param b_ Main-diagonal coefficient
50 /// @param c_ Super-diagonal coefficient (row k depends on x[k+1])
51 void factor(int n_, cplx a_, cplx b_, cplx c_);
52
53 /// @brief In-place Thomas solve.
54 ///
55 /// On entry d holds the right-hand side; on exit it holds the solution.
56 /// The size of d must equal n (set by the last factor() call).
57 void solve(std::vector<cplx> &d) const;
58};
59
60} // namespace num
Core type definitions.
std::complex< real > cplx
Definition types.hpp:12
Constant-coefficient complex tridiagonal solver (precomputed LU).
std::vector< cplx > inv_b
Inverse of modified main diagonal (precomputed)
void solve(std::vector< cplx > &d) const
In-place Thomas solve.
std::complex< double > cplx
cplx a_coeff
Sub-diagonal value (constant across all rows)
void factor(int n_, cplx a_, cplx b_, cplx c_)
Factor the tridiagonal matrix.
std::vector< cplx > c_mod
Modified super-diagonal (precomputed from LU)