numerics 0.1.0
Loading...
Searching...
No Matches
jacobi.cpp
Go to the documentation of this file.
2#include <cmath>
3#include <stdexcept>
4
5namespace num {
6
8 const Vector& b,
9 Vector& x,
10 real tol,
11 idx max_iter,
12 Backend backend) {
13 constexpr real zero_diag_tol = 1e-15;
14 idx n = b.size();
15 if (A.rows() != n || A.cols() != n || x.size() != n) {
16 throw std::invalid_argument("Dimension mismatch in Jacobi solver");
17 }
18
19 Vector x_new(n);
20 SolverResult result{0, 0.0, false};
21
22 for (idx iter = 0; iter < max_iter; ++iter) {
23 // Compute all updates from the previous iterate simultaneously
24#ifdef NUMERICS_HAS_OMP
25 #pragma omp parallel for schedule(static) if (backend == Backend::omp)
26#endif
27 for (idx i = 0; i < n; ++i) {
28 if (std::abs(A(i, i)) < zero_diag_tol)
29 throw std::runtime_error("Zero diagonal in Jacobi solver at row "
30 + std::to_string(i));
31 real sigma = 0.0;
32 for (idx j = 0; j < n; ++j)
33 if (j != i)
34 sigma += A(i, j) * x[j];
35 x_new[i] = (b[i] - sigma) / A(i, i);
36 }
37
38 for (idx i = 0; i < n; ++i)
39 x[i] = x_new[i];
40
41 // Residual ||b - Ax||
42 real res = 0.0;
43#ifdef NUMERICS_HAS_OMP
44 #pragma omp parallel for reduction(+ : res) \
45 schedule(static) if (backend == Backend::omp)
46#endif
47 for (idx i = 0; i < n; ++i) {
48 real ri = b[i];
49 for (idx j = 0; j < n; ++j)
50 ri -= A(i, j) * x[j];
51 res += ri * ri;
52 }
53 result.residual = std::sqrt(res);
54 result.iterations = iter + 1;
55
56 if (result.residual < tol) {
57 result.converged = true;
58 break;
59 }
60 }
61 return result;
62}
63
64} // namespace num
constexpr idx rows() const noexcept
Definition matrix.hpp:87
constexpr idx cols() const noexcept
Definition matrix.hpp:88
constexpr idx size() const noexcept
Definition vector.hpp:83
Jacobi iterative solver.
double real
Definition types.hpp:10
Backend
Definition policy.hpp:7
std::size_t idx
Definition types.hpp:11
constexpr real e
Definition math.hpp:44
SolverResult jacobi(const Matrix &A, const Vector &b, Vector &x, real tol=1e-10, idx max_iter=1000, Backend backend=default_backend)
Jacobi iterative solver for Ax = b.
Definition jacobi.cpp:7