NULAPACK
NUmerical Linear Algebra PACKage
Loading...
Searching...
No Matches
dgegssv.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 DGEGSSV - Gauss-Seidel Solver for A * X = B
30C ====================================================================
31C Description:
32C ------------------------------------------------------------------
33C Iterative Gauss-Seidel 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 STATUS : INTEGER -> return code:
52C 0 = success
53C >0 = did not converge
54C <0 = illegal or zero diagonal
55C ====================================================================
56 SUBROUTINE dgegssv(N, A, B, X, MAX_ITER, TOL, OMEGA, STATUS)
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, STATUS
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 :: S1, S2, DIFF, MAX_DIFF
72
73C I n i t i a l S t a t u s
74C ------------------------------------------------------------------
75 status = 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 s1 = 0.0d0
82 s2 = 0.0d0
83
84C Compute sum: S1 = sum_{j=1}^{i-1} A(i,j) * X_NEW(j)
85 DO j = 1, i - 1
86 index = (i - 1) * n + j
87 s1 = s1 + a(index) * x_new(j)
88 END DO
89
90C Compute sum: S2 = sum_{j=i+1}^{N} A(i,j) * X(j)
91 DO j = i + 1, n
92 index = (i - 1) * n + j
93 s2 = s2 + a(index) * x(j)
94 END DO
95
96C Check diagonal element A(i,i)
97 index = (i - 1) * n + i
98 IF (a(index) .EQ. 0.0d0) THEN
99 status = -i
100 RETURN
101 END IF
102
103C Update X_NEW(i) with relaxation
104 x_new(i) = (b(i) - s1 - s2) / a(index)
105 x_new(i) = x(i) + omega * (x_new(i) - x(i))
106 END DO
107
108C C o n v e r g e n c e C h e c k
109C ----------------------------------------------------------------
110 max_diff = 0.0d0
111 DO i = 1, n
112 diff = abs(x_new(i) - x(i))
113 IF (diff .GT. max_diff) max_diff = diff
114 x(i) = x_new(i)
115 END DO
116
117 IF (max_diff .LT. tol) THEN
118 status = 0 ! Success
119 RETURN
120 END IF
121 END DO
122
123C N o n - C o n v e r g e n c e E x i t
124C ------------------------------------------------------------------
125 RETURN
126 END
subroutine dgegssv(n, a, b, x, max_iter, tol, omega, status)
Definition dgegssv.f:57