NULAPACK
NUmerical Linear Algebra PACKage
Loading...
Searching...
No Matches
dgejsv.f
Go to the documentation of this file.
1C ====================================================================
2C N U L A P A C K
3C U U L A P A C K
4C L L L A P A C K
5C A A A A P A C K
6C P P P P P A C K
7C A A A A A A C K
8C C C C C C C C K
9C K K K K K K K K
10C
11C This file is part of NULAPACK - NUmerical Linear Algebra PACKage
12C
13C Copyright (C) 2025 Saud Zahir
14C
15C NULAPACK is free software: you can redistribute it and/or modify
16C it under the terms of the GNU General Public License as published by
17C the Free Software Foundation, either version 3 of the License, or
18C (at your option) any later version.
19C
20C NULAPACK is distributed in the hope that it will be useful,
21C but WITHOUT ANY WARRANTY; without even the implied warranty of
22C MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23C GNU General Public License for more details.
24C
25C You should have received a copy of the GNU General Public License
26C along with NULAPACK. If not, see <https://www.gnu.org/licenses/>.
27C
28C ====================================================================
29C DGEJSV - Jacobi Solver for A * X = B
30C ====================================================================
31C Description:
32C ------------------------------------------------------------------
33C Iterative Jacobi solver for solving linear systems of
34C equations A * X = B, where A is a square N x N matrix in
35C row-major flat array format. Double precision version.
36C
37C On input: X contains initial guess
38C On output: X contains solution
39C
40C Convergence is based on maximum absolute difference per iteration.
41C ====================================================================
42C Arguments:
43C ------------------------------------------------------------------
44C N : INTEGER -> size of the matrix (N x N)
45C A(*) : DOUBLE PRECISION -> flat array, row-major matrix A
46C B(N) : DOUBLE PRECISION -> right-hand side vector
47C X(N) : DOUBLE PRECISION -> input: initial guess, output: solution
48C MAX_ITER : INTEGER -> max number of iterations
49C TOL : DOUBLE PRECISION -> convergence tolerance
50C OMEGA : DOUBLE PRECISION -> relaxation coefficient
51C INFO : INTEGER -> return code:
52C 0 = success
53C >0 = did not converge
54C <0 = illegal or zero diagonal
55C ====================================================================
56 SUBROUTINE dgejsv(N, A, B, X, MAX_ITER, TOL, OMEGA, INFO)
57
58C I m p l i c i t T y p e s
59C ------------------------------------------------------------------
60 IMPLICIT NONE
61
62C D u m m y A r g u m e n t s
63C ------------------------------------------------------------------
64 INTEGER :: N, MAX_ITER, INFO
65 DOUBLE PRECISION :: A(*), B(N), X(N), TOL, OMEGA
66
67C L o c a l V a r i a b l e s
68C ------------------------------------------------------------------
69 INTEGER :: I, J, K, INDEX
70 DOUBLE PRECISION :: X_NEW(N)
71 DOUBLE PRECISION :: S, DIFF, MAX_DIFF
72
73C I n i t i a l S t a t u s
74C ------------------------------------------------------------------
75 info = 1 ! Default: did not converge
76
77C M a i n I t e r a t i o n L o o p
78C ------------------------------------------------------------------
79 DO k = 1, max_iter
80 DO i = 1, n
81 s = 0.0d0
82
83C Compute sum: S = sum_{j=1}^{N, j!=i} A(i,j) * X(j)
84 DO j = 1, n
85 IF (j .NE. i) THEN
86 index = (i - 1) * n + j
87 s = s + a(index) * x(j)
88 END IF
89 END DO
90
91C Check diagonal element A(i,i)
92 index = (i - 1) * n + i
93 IF (a(index) .EQ. 0.0d0) THEN
94 info = -i
95 RETURN
96 END IF
97
98C Jacobi update with relaxation
99 x_new(i) = (b(i) - s) / a(index)
100 x_new(i) = x(i) + omega * (x_new(i) - x(i))
101 END DO
102
103C C o n v e r g e n c e C h e c k
104C ----------------------------------------------------------------
105 max_diff = 0.0d0
106 DO i = 1, n
107 diff = abs(x_new(i) - x(i))
108 IF (diff .GT. max_diff) max_diff = diff
109 x(i) = x_new(i)
110 END DO
111
112 IF (max_diff .LT. tol) THEN
113 info = 0 ! Success
114 RETURN
115 END IF
116 END DO
117
118C N o n - C o n v e r g e n c e E x i t
119C ------------------------------------------------------------------
120 RETURN
121 END
subroutine dgejsv(n, a, b, x, max_iter, tol, omega, info)
Definition dgejsv.f:57