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 }
32 real sigma = 0.0;
33 for (idx j = 0; j < n; ++j) {
34 if (j != i) {
35 sigma += A(i, j) * x[j];
36 }
37 }
38 x_new[i] = (b[i] - sigma) / A(i, i);
39 }
40
41 for (idx i = 0; i < n; ++i) {
42 x[i] = x_new[i];
43 }
44
45 // Residual ||b - Ax||
46 real res = 0.0;
47#ifdef NUMERICS_HAS_OMP
48 #pragma omp parallel for reduction(+ : res) \
49 schedule(static) if (backend == Backend::omp)
50#endif
51 for (idx i = 0; i < n; ++i) {
52 real ri = b[i];
53 for (idx j = 0; j < n; ++j) {
54 ri -= A(i, j) * x[j];
55 }
56 res += ri * ri;
57 }
58 result.residual = std::sqrt(res);
59 result.iterations = iter + 1;
60
61 if (result.residual < tol) {
62 result.converged = true;
63 break;
64 }
65 }
66 return result;
67}
68
69} // 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