NULAPACK
NUmerical Linear Algebra PACKage
Loading...
Searching...
No Matches
cgejsv.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 CGEJSV - 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. Single precision complex 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(*) : COMPLEX -> flat array, row-major matrix A
46C B(N) : COMPLEX -> right-hand side vector
47C X(N) : COMPLEX -> input: initial guess, output: solution
48C MAX_ITER : INTEGER -> max number of iterations
49C TOL : REAL -> convergence tolerance
50C OMEGA : REAL -> relaxation coefficient
51C INFO : INTEGER -> return code:
52C 0 = success
53C >0 = did not converge
54C <0 = illegal or zero diagonal
55C ====================================================================
56 SUBROUTINE cgejsv(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 COMPLEX :: A(*), B(N), X(N)
66 REAL :: 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 COMPLEX :: X_NEW(N)
72 COMPLEX :: S
73 REAL :: 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 s = (0.0,0.0)
84
85C Compute sum: S = sum_{j=1}^{N, j!=i} A(i,j) * X(j)
86 DO j = 1, n
87 IF (j .NE. i) THEN
88 index = (i - 1) * n + j
89 s = s + a(index) * x(j)
90 END IF
91 END DO
92
93C Check diagonal element A(i,i)
94 index = (i - 1) * n + i
95 IF (a(index) .EQ. (0.0,0.0)) THEN
96 info = -i
97 RETURN
98 END IF
99
100C Jacobi update with relaxation
101 x_new(i) = (b(i) - s) / a(index)
102 x_new(i) = x(i) + omega * (x_new(i) - x(i))
103 END DO
104
105C C o n v e r g e n c e C h e c k
106C ----------------------------------------------------------------
107 max_diff = 0.0
108 DO i = 1, n
109 diff = abs(x_new(i) - x(i))
110 IF (diff .GT. max_diff) max_diff = diff
111 x(i) = x_new(i)
112 END DO
113
114 IF (max_diff .LT. tol) THEN
115 info = 0 ! Success
116 RETURN
117 END IF
118 END DO
119
120C N o n - C o n v e r g e n c e E x i t
121C ------------------------------------------------------------------
122 RETURN
123 END
subroutine cgejsv(n, a, b, x, max_iter, tol, omega, info)
Definition cgejsv.f:57