NULAPACK
NUmerical Linear Algebra PACKage
Loading...
Searching...
No Matches
zgegssv.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 ZGEGSSV - 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. Complex 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 COMPLEX -> flat array, row-major matrix A
46C B(N) : DOUBLE COMPLEX -> right-hand side vector
47C X(N) : DOUBLE COMPLEX -> 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 zgegssv(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 COMPLEX :: A(*), B(N), X(N)
66 DOUBLE PRECISION :: TOL, OMEGA
67
68C L o c a l V a r i a b l e s
69C ------------------------------------------------------------------
70 INTEGER :: I, J, K, INDEX
71 DOUBLE COMPLEX :: X_NEW(N)
72 DOUBLE COMPLEX :: S1, S2
73 DOUBLE PRECISION :: DIFF, MAX_DIFF
74
75C I n i t i a l S t a t u s
76C ------------------------------------------------------------------
77 info = 1 ! Default: did not converge
78
79C M a i n I t e r a t i o n L o o p
80C ------------------------------------------------------------------
81 DO k = 1, max_iter
82 DO i = 1, n
83 s1 = (0.0d0, 0.0d0)
84 s2 = (0.0d0, 0.0d0)
85
86C Compute sum: S1 = sum_{j=1}^{i-1} A(i,j) * X_NEW(j)
87 DO j = 1, i - 1
88 index = (i - 1) * n + j
89 s1 = s1 + a(index) * x_new(j)
90 END DO
91
92C Compute sum: S2 = sum_{j=i+1}^{N} A(i,j) * X(j)
93 DO j = i + 1, n
94 index = (i - 1) * n + j
95 s2 = s2 + a(index) * x(j)
96 END DO
97
98C Check diagonal element A(i,i)
99 index = (i - 1) * n + i
100 IF (a(index) .EQ. (0.0d0, 0.0d0)) THEN
101 info = -i
102 RETURN
103 END IF
104
105C Update X_NEW(i) with relaxation
106 x_new(i) = (b(i) - s1 - s2) / a(index)
107 x_new(i) = x(i) + omega * (x_new(i) - x(i))
108 END DO
109
110C C o n v e r g e n c e C h e c k
111C ----------------------------------------------------------------
112 max_diff = 0.0d0
113 DO i = 1, n
114 diff = abs(x_new(i) - x(i))
115 IF (diff .GT. max_diff) max_diff = diff
116 x(i) = x_new(i)
117 END DO
118
119 IF (max_diff .LT. tol) THEN
120 info = 0 ! Success
121 RETURN
122 END IF
123 END DO
124
125C N o n - C o n v e r g e n c e E x i t
126C ------------------------------------------------------------------
127 RETURN
128 END
subroutine zgegssv(n, a, b, x, max_iter, tol, omega, info)
Definition zgegssv.f:57