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